Skip to content

fix(chat): keep a touch text selection where the reader put it - #18491

Closed
Premshay wants to merge 1 commit into
kirodotdev:mainfrom
Premshay:fix/touch-selection-transcript
Closed

Premshay wants to merge 1 commit into
kirodotdev:mainfrom
Premshay:fix/touch-selection-transcript

Conversation

@Premshay

@Premshay Premshay commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: On a phone, selecting text in a chat transcript keeps the selection where the reader put it.

On Android Chrome (verified on a real device), text selection in the transcript fails four ways:

  1. A long-press opens the message bubble's context menu over the selection the reader just started.
  2. Dragging a selection handle under the composer dock, the chat header, or the phone top bar (which hosts the session title) puts the endpoint in their text, and the selection grows to include everything in between, the title included.
  3. Scrolling with a selection unmounts the virtualized row that holds its start, and the browser re-roots the start at the top of the transcript.
  4. If the start row re-renders while it is scrolled away, the browser parks the start between rows. Each row mounted above it then walks it up, until the selection starts at "Load earlier messages" (about 5000 characters in the captured trace).

Why it matters

Copying a passage from a reply is a basic action on a phone, and in these cases it either fails or copies the whole conversation instead.

Not a goal

  • Changing desktop selection: the inert overlays and the context-menu opt-out are touch-only, and the retained range and restore only act on a non-collapsed transcript selection.
  • Fixing why a scrolled-away row re-renders: the restore makes the selection robust to it whatever the cause.

What changed (motivation → approach → change)

Each change was checked against an on-device trace of the selection's endpoints (row, node, offset, selected text) captured while reproducing the bug.

  • Context menu (MessageContextMenu.tsx): ContextMenuTrigger is disabled on touch (useIsTouchDevice). Long-press is the platform's selection gesture; the same actions stay on the bubble's action row, which is always visible on touch: its shared class ICON_ACTION_ROW_CLS (utils/touchActions.ts) already carries [@media(hover:none)]:opacity-100, so the row needs no hover or tap to appear. Every frame of the recording shows it under each reply (Quote, Copy, More; Edit sits under the user's own bubble).
  • Overlays (useSelectionInertOverlays.ts, ChatPage.tsx, App.tsx): the chat header overlay, the composer dock, and the shell <header> carry data-selection-inert. While a transcript selection is held (either endpoint inside the scroller), touch devices set inert on them, so a dragged handle hit-tests the transcript beneath. user-select: none does not change where the caret lands; inert does. A capture-phase touchstart releases them at once: Android's selection handles dispatch no touch events, so a finger on the page is the reader reaching for something, and the first tap on the composer or header is never swallowed. The next handle move re-applies inert.
  • Retained range (windowRange.ts, types.ts, useVirtualChat.ts): new retainRange(range | null). useWindowState wraps its setter so every window write keeps the retained rows mounted, whichever owner moved the window. It is cleared on session change.
  • Selection sync + restore (utils/selectionRetention.ts, ChatPage.tsx): on selectionchange, both endpoints on rows set the retained span; an endpoint off any row keeps the last span; a collapsed or outside selection releases it. Each endpoint is also remembered as row index + character offset. When the start drifts off its row while the end is on one, or the end leaves the transcript, that endpoint is put back at its last position, by node if still mounted, else by character offset in the row's current text. A selection that has not yet sat on rows (a fresh long-press) is never rewritten. Rewriting one was found to cancel the first press or extend it to the whole chat.

Backwards compatibility

Compatible: retainRange is a new member on the useVirtualChat return; nothing existing changes shape. The touch-only behaviours do not run on desktop.

Removes nothing: the bubble context menu is unchanged on desktop, and on touch its actions stay on the bubble's action row.

Tests

  • selectionRetention.test.ts: row-span computation; keep vs release; restoring a start that landed on the title; restoring a start parked between rows after its row re-rendered (by character offset); a fresh long-press left alone; an end over the scroller padding left alone.
  • useSelectionInertOverlays.test.tsx: overlays inert while a transcript selection is held on touch (including when only the focus is in the transcript), released after; released at once by a finger touchstart and re-applied by the next selection change; untouched for an outside selection and on desktop.
  • ChatPage.overlaySelection.test.tsx: source contracts for the inert markers (header, dock, shell top bar), the retained-range wiring, and that only a settled selection is restored.
  • AssistantMessage.quote.test.tsx: the bubble context menu does not open on touch.
  • useVirtualChat.surfaceContract.test.tsx: retainRange added to the return surface and its stable-identity list.
  • useVirtualChat mocks in 10 ChatPage tests gain retainRange; two dock-root regexes allow the new attribute.

Local runs: tsc -b clean; ChatPage + App tests 2168/2168; virtualizer + selection tests 464/464.

Manual verification

On Android Chrome against a live dashboard, each case above was reproduced, traced, and confirmed fixed after the change: long-press selects without a menu; dragging a handle over the composer, the header, or the title stays in the transcript; long-pressing a word, scrolling it off screen, and extending the end handle keeps the selection starting at that word (copy confirms).

Screenshots / video

Long-press on touch: the pressed word is selected with the selection toolbar, no bubble context menu opens, and the bubble's action row (Fork, Copy, More) stays visible under the reply:

Long-press selection on touch with the action row visible

Recording (33 s, 390×844, dark theme):

touch-selection-18491.gif

touch-selection-18491.webm

It runs the real built SPA from this branch, with fixture data only: the transcript-harness (scripts/lib/transcript-harness.mjs) mocks /api/**. The viewport is phone-sized with hasTouch emulated, so (pointer: coarse) and (hover: none) match. A yellow dot marks the finger, and a caption states each result. The capture script also asserts each result in the page and aborts if one fails.

  1. Long-press a word: the word is selected, and message-context-menu never mounts. The bubble's action row stays visible under the reply throughout, which is where the menu's actions live on touch.
  2. Drag the end handle down over the composer dock: the selection stays in the transcript (681 chars).
  3. Drag it up over the chat header and the session title: the selection's end stays in the transcript.
  4. Scroll the start off screen and keep extending: the selection still starts at the pressed word (4373 chars), and is not re-rooted at "Load earlier messages".
  5. With text still selected, tap the composer once: the composer takes focus, and typing goes straight in. This covers the UX-review watch item; overlays now release inert on touchstart.

Simulated part: headless Chromium draws no native selection handles, so each handle drag is reproduced as the browser performs one. It hit-tests the handle's point with caretRangeFromPoint, then calls Selection.extend to it. That is the hit-test the inert overlays change. The long-press and the composer tap are real CDP touch events. The same flows were also verified by hand on Android Chrome against a live dashboard (see Manual verification).

Related Issues

N/A

Pattern harvest

Rule candidate: review-prompt
Pattern: a virtualized list that unmounts or re-renders rows must keep any rows holding a live DOM Selection or Range endpoint mounted (or restore the endpoint by logical position), or the browser silently re-roots it.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable): N/A
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributed under the project's Apache-2.0 license.

🤖 Generated with Claude Code

@Premshay
Premshay requested review from a team and KiroCrewCaptain as code owners October 9, 2026 14:35
@Premshay
Premshay requested a review from CrysisDeu October 9, 2026 14:35
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Oct 9, 2026
@Premshay
Premshay force-pushed the fix/touch-selection-transcript branch from 86b751c to 18c67cf Compare October 9, 2026 15:36
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — 🟡 CONCERNS

Premise-level review of b2b834400a170ecd70e640fcc62ecf0426023eb4 via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: CONCERNS

Two mechanisms fix the handle-over-chrome defect: the inert overlays, and the restore's focus-out branch, which the PR's own comments say the inert overlays already cover.

Not justified as shipped

  • Item 4 — duplicate of item 2 (useSelectionInertOverlays.ts): the restoreEndpointToTranscript doc gives its first case as "a touch handle dragged over the title or the composer lands in their text". But nextRetainedRange's doc says "The overlays are inert while a selection is held (useSelectionInertOverlays), so a handle no longer lands in the chrome."
    Subtraction: delete the anchorOnRow && !container.contains(focusNode) branch of restoreEndpointToTranscript. Keep only the anchor restore, which fixes defect 4 (re-rendered start row).
    Clears when: the branch is deleted, or an on-device trace shows the end leaving the transcript while the overlays are inert.
  • Item 6 — rides along: a screenshot added under temp-screenshots/, following the existing convention there.

What this change ships

Inventory (6 items) — 4 justified

Intent: on a phone, a text selection in a chat transcript stays where the reader put it. FIX.

  1. Long-press on a message bubble no longer opens its context menu on touch. The same actions stay on the always-visible action row and its More menu. — justified
  2. On touch, the header, composer dock and phone top bar become inert while a transcript selection is held, and a finger tap releases them. — justified
  3. Rows under a live selection stay mounted while you scroll (retainRange on useVirtualChat). — justified
  4. An endpoint that leaves its row is put back at its last row and character offset. — duplicate of item 2 for the focus-out case
  5. Feature-map row added for useSelectionInertOverlays.ts. — justified
  6. A screenshot PNG is committed to the repo. — rides along, harmless

[FIRST-PRINCIPLES-REVIEWED] b2b8344

Description read

[DESCRIPTION-READ] 08106240cf7a693a85d87fa36d0f4f74e4bb8dc4a6a06143221cdfc719286fd7

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — 🟡 CONCERNS

UX-level review of b2b834400a170ecd70e640fcc62ecf0426023eb4 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

UX-Verdict: CONCERNS

No first-time reader has looked at the touch long-press change; the screenshot and recording show it, but nobody has read them cold.

Watch

  • On touch, long-press no longer opens the bubble menu (<ContextMenuTrigger asChild disabled={isTouch}>), so its Copy link, Pin and Raw markdown items are only reachable through the action row's More button. The still shows that row under each reply, but not what More opens. This affects every phone user who learned long-press. Before merging, check on a phone that More lists Pin and Copy link, for both user and assistant bubbles.

[UX-REVIEWED] b2b8344

Description read

[DESCRIPTION-READ] 0a919817bd57bc53126b5eaffdf8f20a51feedc07698f08647ef1b98a7bdd566

That sha256 names everything this verdict read: the PR description, plus the 5 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — 🟡 CONCERNS

Design-level review of b2b834400a170ecd70e640fcc62ecf0426023eb4 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: CONCERNS

iOS Safari is never verified, yet the inert overlays, the touchstart release and the selection rewrite all run on every touch device.

Watch

  • iOS is assumed to behave like Android. The mechanism relies on Android's selection handles sending no touch events to the page ("Android's selection handles dispatch no touch events, so a finger on the page is the reader reaching for something"). It also rewrites the live selection with setBaseAndExtent when an endpoint leaves its row. Both are gated on useIsTouchDevice, so they also run on iOS WebKit. If iOS handle drags do fire touchstart, the overlays drop inert mid-drag and fix refactor: rename project KiroClaw to KiroCrew #2 silently does nothing there. If WebKit reports transient off-row endpoints, as the PR's own selectedRowRange comment says it does, the restore can pull the handle away from where the reader is dragging it. All of this can be reverted, but the description only covers Android.
    Clears when: a manual check on iOS Safari of the long-press, a handle drag over the composer and title, and scroll-and-extend is recorded in the PR, or the touch-only paths are limited to Android and the limit is stated.

Suggestions

  • Move the inline selectionchange retention effect out of ChatPage.tsx into a pages/chat/ hook next to useSelectionInertOverlays. Both listen to the same event, and ChatPage is being split up into controllers.

[DESIGN-REVIEWED] b2b8344

Description read

[DESCRIPTION-READ] db26da6960411f101c16a260b85931bd91987c4285b026fe8fa5d78ca8432894

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed b2b834400a170ecd70e640fcc62ecf0426023eb4 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] b2b8344

@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed b2b834400a170ecd70e640fcc62ecf0426023eb4 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings block this merge.

FINDING — website/src/pages/ChatPage.tsx:198 — the selection-retention effect has no isTouch gate, and on desktop a selection stays after the drag. So with text selected in an old row (say row 10 of a long session), each scroll-driven recomputeWindow goes through the new setWindowRange wrapper, whose { start: Math.min(next.start, start), end: Math.max(next.end, end) } keeps the window stretched from row 10 to wherever the reader is. Scrolling to the bottom therefore mounts every row in between, and they stay mounted until the selection is cleared. This undoes the virtualizer's windowing and the far-jump "replace" in mountIndex → Fix: gate the effect on useIsTouchDevice() the way useSelectionInertOverlays already is, or cap the retained span and drop it when a far jump replaces the window.

[OPUS-REVIEWED] b2b8344

@Premshay
Premshay force-pushed the fix/touch-selection-transcript branch from 18c67cf to 993c951 Compare October 9, 2026 16:42
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Oct 9, 2026
@Premshay

Premshay commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author
touch-selection-18491.webm

touch-selection-18491.gif

@Premshay
Premshay force-pushed the fix/touch-selection-transcript branch from 993c951 to 01c91c6 Compare October 9, 2026 17:05
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Oct 9, 2026
On a phone, selecting transcript text failed four ways:
- a long-press opened the bubble's context menu over the selection;
- a handle dragged under the composer dock, the chat header or the
  phone top bar (which hosts the session title) landed in their text,
  and the selection took everything in between;
- scrolling with a selection unmounted the row holding its start, and
  the browser re-rooted the start at the top of the transcript;
- a start row re-rendered while scrolled away left the start parked
  between rows, walked up to offset 0 as rows mounted above it.

Fix:
- disable the bubble context menu on touch (the action row keeps the
  same actions);
- make the header, the dock and the shell top bar inert while a
  transcript selection is held (touch only);
- add retainRange to the virtualizer: every window write keeps the
  selection's rows mounted;
- remember each endpoint as row + character offset, and put an endpoint
  that leaves its row back where it last sat. A fresh long-press, with
  no settled position yet, is never rewritten.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Premshay
Premshay force-pushed the fix/touch-selection-transcript branch from 01c91c6 to b2b8344 Compare October 9, 2026 17:22
@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention labels Oct 9, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR fixes four Android touch-selection failures in the chat transcript (long-press menu, handles landing in chrome, start re-rooted on unmount, start drifting after re-render), and the touch-side root causes are addressed: inert overlays, a retained virtual range, and an endpoint restore. I read the full production diff, the five AI lanes on head b2b8344, and the new tests. GPT and Design have no blocking findings, but the Opus finding is real and has no disposition. The new selection-retention effect at website/src/pages/ChatPage.tsx:3696-3719 is not gated on touch. The new setWindowRange wrapper in website/src/hooks/virtualizer/windowRange.ts:122-134 always unions the window with the retained span. So on desktop, a selection made in an old row (desktop selections stay after the mouse-up) keeps every row from that point to the current scroll position mounted, and defeats virtualization and the far-jump replace. That contradicts the body's "Not a goal: changing desktop selection" claim. Suggested fix: gate the effect on useIsTouchDevice() like useSelectionInertOverlays, or cap the retained span and drop it when a far jump replaces the window, and add a test that a desktop (non-touch) selection does not call retainRange. Please also answer the open Design watch (iOS Safari is not verified while every touch device runs the restore and inert paths), the UX watch (long-press Pin/Copy link now reachable only via More), and the First Principles subtraction of the focus-out restore branch. — crew-pr-reviewer

@github-actions github-actions Bot added merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: action required A blocking check or review needs attention and removed readiness: passed Eligible automated validation passed for the current revision labels Oct 9, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

Thank you @Premshay for this fix and the careful on-device traces. The PR went into a merge conflict with main, so we carried it forward as a same-repo PR: #19864. You stay the author of the main commit. One change: the long-press context-menu part (case 1) is dropped, because main already fixed it in #18340. The other three fixes and their tests are kept as you wrote them. Closing this one in favour of #19864; your branch is left as is.

@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Oct 13, 2026
chenmingwei23 pushed a commit that referenced this pull request Oct 14, 2026
* fix(chat): keep a touch text selection where the reader put it

On a phone, selecting transcript text failed three ways:
- a handle dragged under the composer dock, the chat header or the
  phone top bar (which hosts the session title) landed in their text,
  and the selection took everything in between;
- scrolling with a selection unmounted the row holding its start, and
  the browser re-rooted the start at the top of the transcript;
- a start row re-rendered while scrolled away left the start parked
  between rows, walked up to offset 0 as rows mounted above it.

(The fourth case from the original PR, a long-press opening the bubble
context menu, is already fixed on main by #18340 and is dropped here.)

Fix:
- make the header, the dock and the shell top bar inert while a
  transcript selection is held (touch only);
- add retainRange to the virtualizer: every window write keeps the
  selection's rows mounted;
- remember each endpoint as row + character offset, and put an endpoint
  that leaves its row back where it last sat. A fresh long-press, with
  no settled position yet, is never rewritten.

Replaces #18491.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: cixuuz <26782336+cixuuz@users.noreply.github.com>

* test(chat): give the closed-session provider mock retainRange

ChatPage now calls retainRange on selectionchange; this test landed on main (#18303) after the original PR, and its useVirtualChat mock lacked the new member.

---------

Co-authored-by: Premshay <28099628+Premshay@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: cixuuz <26782336+cixuuz@users.noreply.github.com>
@Premshay
Premshay deleted the fix/touch-selection-transcript branch November 13, 2026 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor) merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants