Skip to content

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

Merged
chenmingwei23 merged 2 commits into
mainfrom
replace/18491
Oct 14, 2026
Merged

chenmingwei23 merged 2 commits into
mainfrom
replace/18491

Conversation

@iamwhatever

@iamwhatever iamwhatever commented Oct 13, 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.

Replaces #18491 by @Premshay (same-repo takeover after the fork went idle with a merge conflict; Premshay stays the commit author).

Fixes #19858

On Android Chrome (verified on a real device by the original author), text selection in the transcript fails three ways:

  1. 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.
  2. 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.
  3. 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

What changed (motivation → approach → change)

  • 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.

Conflict resolution against current main: MessageContextMenu.tsx and AssistantMessage.quote.test.tsx take main's side (see Not a goal); the ChatPage.tsx import and the docs/feature-map/README.md row are merged (main's row plus the new useSelectionInertOverlays.ts entry); ChatPage.dockClearance.test.tsx keeps main's two-host assertions and lets the dock root carry the new attribute. A second commit adds retainRange to the useVirtualChat mock in ChatPage.closedSessionProvider.test.tsx, which landed on main (#18303) after the original PR.

Pattern harvest

Not generalizable: the bugs come from how Android Chrome hit-tests selection handles over overlays and re-roots a selection whose anchor row a virtualizer unmounts; the fix is specific to the chat transcript's virtualizer and overlays, with no repo-wide code shape a lint rule could catch.

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.

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, 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, the retained-range wiring, and that only a settled selection is restored.
  • useVirtualChat.surfaceContract.test.tsx: retainRange added to the return surface.

Local runs on this branch:

  • The 3 own test files: 19/19 pass; with the source change reverted to main, all three files fail (2 cannot import, 4 contract tests fail).
  • tsc -p tsconfig.app.json clean.
  • src/test/ChatPage* + src/test/App*: 195 files, 2254 tests pass (after the mock fix).

Manual verification

By the original author, on Android Chrome against a live dashboard: 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). Not verified on iOS.

Screenshots / video

From #18491 (Android, 390×844, dark theme). The still shows a long-press selection with the bubble's action row visible:

Long-press selection on touch with the action row visible

touch-selection-18491.gif

touch-selection-18491.webm

Premshay and others added 2 commits October 13, 2026 17:50
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>
ChatPage now calls retainRange on selectionchange; this test landed on main (#18303) after the original PR, and its useVirtualChat mock lacked the new member.
@iamwhatever
iamwhatever requested review from a team and KiroCrewCaptain as code owners October 13, 2026 18:10
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Oct 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ✅ PASS

UX-level review of 2101439863385d7ccf9c3b7bb091aaf9dbcb79b4 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

UX-Verdict: PASS

Check on a real phone that the first tap on the composer or the header still works while a selection is held. iOS has not been tested.

[UX-REVIEWED] 2101439

Description read

[DESCRIPTION-READ] 968e91977c65afd4217103160f0c7e51986ffed02ca5f2365f52faaf9a996dca

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

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 2101439863385d7ccf9c3b7bb091aaf9dbcb79b4 — 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: PASS

Check on iOS Safari before merge. The inert overlays and the restore run on every touch device, but they were only verified on Android Chrome.

What this change ships

Inventory (6 items) — 6 justified

Intent: On a phone, keep a text selection in the chat transcript where the reader put it, instead of letting it jump to the title or the whole chat. This is a FIX (#19858).

  1. On touch, the chat header, composer dock and phone top bar ignore touches while a transcript selection is held, so a dragged handle stays in the transcript — justified
  2. A finger tap releases those overlays at once, so the first tap on the composer or header still reaches it — justified
  3. Rows holding a selection's endpoints stay mounted while the transcript scrolls (retainRange, one consumer in ChatPage.tsx) — justified
  4. A selection endpoint that drifts off its row (onto the title, or parked between rows) goes back to where it last sat — justified
  5. The retained rows are cleared when the session changes — justified
  6. A screenshot is committed under temp-screenshots/touch-selection-18491/ — justified

[FIRST-PRINCIPLES-REVIEWED] 2101439

Description read

[DESCRIPTION-READ] 90066edd4a70ce6506cf5b36397bb625911bceb3c78c6f69f9307f95f3026fd3

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

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — 🟡 CONCERNS

Design-level review of 2101439863385d7ccf9c3b7bb091aaf9dbcb79b4 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: CONCERNS

The retained range widens every window write, including far jumps, with no cap, and it runs on desktop too.

Watch

  • No limit on how many rows a held selection keeps mounted. The new setWindowRange wrapper returns { start: Math.min(next.start, start), end: Math.max(next.end, end) } for every owner. That includes the jump, minimap and search windows, and the wrapper is not limited to touch devices. If a selection is still held at row 10 and the reader jumps to the tail (jump-to-bottom, the turn minimap, a search hit, or a desktop click on a button that doesn't collapse the selection), the window becomes the union. Every row between the two then mounts and gets measured in one commit, and the virtualizer's anchoring for the target row runs against a window it never planned. The result could be a stall or a mis-anchored jump on long sessions. A revert undoes it, so this is a concern rather than a block.
    Clears when: the retained range only widens the window when the two are adjacent or overlapping, or a far jump releases it (or the union is capped). Alternatively, the PR adds a test showing that a far jump with a held selection mounts a bounded number of rows.
  • "Changing desktop selection" is listed under Not a goal, but desktop selection does change. The selectionchange retention/restore effect in ChatPage.tsx has no touch check. A desktop drag whose end moves onto the header or composer now gets put back by setBaseAndExtent.
    Clears when: the restore/retention is limited to touch devices like the overlays, or the description says the desktop behaviour changes and why that's acceptable.

[DESIGN-REVIEWED] 2101439

Description read

[DESCRIPTION-READ] a9dc5f1f16ccc582dd4c760876cc96e65894b9abb43e87b206563e99af92850a

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 13, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 2101439863385d7ccf9c3b7bb091aaf9dbcb79b4 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 2101439

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 2101439863385d7ccf9c3b7bb091aaf9dbcb79b4: <one-sentence reason>

@github-actions

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 2101439863385d7ccf9c3b7bb091aaf9dbcb79b4 — this comment is updated in place on each push.

Review details

No findings block the merge; one advisory finding.

FINDING — website/src/hooks/virtualizer/windowRange.ts:126 — when older history is prepended, retainedRangeRef is not shifted. The rows ChatPage tags with data-display-index (set from vi.index) move to old+net, but const retained = retainedRangeRef.current still holds the old indices. So shiftCompensation's rebase setWindowRange((r) => ({ start: clamp(r.start + shift), … })) gets merged back with those stale indices. That mounts rows that were never selected. The rows that do hold the selection lose their protection as soon as the reader scrolls further up than the stale end, because no selectionchange arrives to refresh the range. They unmount, and the browser re-roots the selection: the bug this change is meant to fix. → Fix: shift the retained range (and the row values in lastOnRows) by the prepend delta inside shiftCompensation's prepend rebase. Another option is to key the retained range by item key instead of display index. Either way the fix means editing shiftCompensation.ts, which this PR does not touch.

[OPUS-REVIEWED] 2101439

Verdict parsed from the review's SHA-scoped output markers for commit 2101439863385d7ccf9c3b7bb091aaf9dbcb79b4.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 2101439863385d7ccf9c3b7bb091aaf9dbcb79b4: <one-sentence reason>

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Oct 13, 2026
@bolichen97

Copy link
Copy Markdown
Contributor

@iamwhatever thanks. Before I approve: this PR commits a stray screenshot, temp-screenshots/touch-selection-18491/still-selection-action-row.png, at the repo root. Please drop it from the branch (attach it to the PR body instead if it is evidence). The code change itself looks fine.

Posted by an AI assistant at bolichen97's request.

@chenmingwei23
chenmingwei23 merged commit c44ac38 into main Oct 14, 2026
111 of 112 checks passed
@chenmingwei23
chenmingwei23 deleted the replace/18491 branch October 14, 2026 01:40
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Oct 14, 2026
@Pearcekieser Pearcekieser mentioned this pull request Oct 14, 2026
5 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chat: a touch text selection in the transcript escapes into overlays or jumps to the top

5 participants