fix(native-chat): skip anchor scans under a wide selection and recover the reader's row after

A selection wider than a window keeps its rows drawn, so recording the anchor on
each scroll would scan them all; recording now waits, as it does under Find. The
reader's row is then taken from the virtualizer's offsets when recording resumes
or the pane hides, so showing it again does not return to an older row.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Brennan Benson
2026-11-12 04:07:43 -08:00
co-authored by Claude Opus 5.5
parent 1bce6e5556
commit 2f1a00fc32
4 changed files with 76 additions and 13 deletions
@@ -168,16 +168,18 @@ function Transcript({
mounted = true,
live = false,
visible = true,
find = false,
revealedDiff
}: {
blocks: NativeChatBlock[]
mounted?: boolean
live?: boolean
visible?: boolean
find?: boolean
revealedDiff?: NativeChatDiffReveal
}) {
const disclosures = useNativeChatDisclosures()
const windowFacts = useMemo(() => ({ isVisible: visible, findActive: false }), [visible])
const windowFacts = useMemo(() => ({ isVisible: visible, findActive: find }), [visible, find])
return (
<NativeChatDisclosureContext.Provider value={disclosures}>
<NativeChatToolRunWindowContext.Provider value={windowFacts}>
@@ -434,6 +436,25 @@ describe('NativeChatToolRun opened-run reader', () => {
expect(box.scrollTop).toBe(landed)
})
it('returns to where the reader scrolled with Find open after the pane hides and shows', async () => {
const { container, rerender } = render(<Transcript blocks={calls(1_000)} />)
await settle()
rerender(<Transcript blocks={calls(1_000)} find />)
await settle()
await scrollBox(container, 9_013)
const before = topRow(container)
expect(before.key).toBe('call:call-225')
// A hidden box loses its offset; only the remembered row brings the reader back.
pane.hidden = true
rerender(<Transcript blocks={calls(1_000)} find visible={false} />)
membersBox(container).scrollTop = 0
pane.hidden = false
rerender(<Transcript blocks={calls(1_000)} find />)
await settle()
expect(topRow(container)).toEqual(before)
})
it('does not keep a selection span drawn after it ended while the pane was hidden', async () => {
const { container, rerender } = render(<Transcript blocks={calls(1_000)} />)
await settle()
@@ -28,6 +28,7 @@ import {
} from '../../messages/disclosure/native-chat-disclosure-store'
import { NativeChatToolRunWindowContext } from './native-chat-tool-run-window-context'
import type { NativeChatToolRunRow } from './native-chat-tool-run-rows'
import type { NativeChatToolRunVirtualizer } from './use-native-chat-tool-run-window'
import { ROW_KEY_ATTRIBUTE, useNativeChatToolRunPins } from './use-native-chat-tool-run-pins'
import {
useNativeChatToolRunWindow,
@@ -47,6 +48,21 @@ export function revealNativeChatToolRunMember(member: HTMLElement): HTMLElement
return scroller
}
/** The reader's row from the window's own offsets, for when scrolling did not record it: reads no drawn row. */
function anchorAtScrollOffset(
virtualizer: NativeChatToolRunVirtualizer,
rows: readonly NativeChatToolRunRow[]
): VirtualizedScrollAnchor {
const scrollTop = virtualizer.scrollOffset ?? 0
const item = virtualizer.getVirtualItemForOffset(scrollTop)
const row = item && rows[item.index]
if (!item || !row) {
return null
}
const fallbackKeys = rows.slice(item.index + 1, item.index + 11).map((next) => next.key)
return { key: row.key, offset: Math.max(0, scrollTop - item.start), fallbackKeys, scrollTop }
}
const rowElementKey = (element: HTMLDivElement): string | null =>
element.getAttribute(ROW_KEY_ATTRIBUTE)
const rowKey = (row: NativeChatToolRunRow): string => row.key
@@ -200,6 +216,20 @@ export function NativeChatToolRunMembers({
onMeasure
})
// Recording scans the drawn rows, so it waits while every row or a wide selection is drawn.
const recordsOnScroll = !drawsEveryRow && !pins.spansSelection
// Set while nothing records the reader's moves: a later restore must start from where they are now.
const anchorStaleRef = useRef(false)
useLayoutEffect(() => {
if (!recordsOnScroll) {
anchorStaleRef.current = true
} else if (anchorStaleRef.current && isVisibleRef.current) {
anchorStaleRef.current = false
anchorRef.current = anchorAtScrollOffset(virtualizer, rows)
scrollOffsetRef.current = virtualizer.scrollOffset ?? 0
}
}, [recordsOnScroll, rows, virtualizer])
// A following reader belongs to the end, not to a row; a hidden pane has no rows to land on.
const shouldSkipRestore = useCallback(() => followsEndRef.current || !isVisibleRef.current, [])
useVirtualizedScrollAnchor({
@@ -213,8 +243,7 @@ export function NativeChatToolRunMembers({
scrollElementRef: scrollerRef,
scrollOffsetRef,
shouldSkipRestore,
// Drawing every row, a scroll would scan all of them for an anchor; leaving records it once on cleanup.
recordAnchorOnScroll: !drawsEveryRow,
recordAnchorOnScroll: recordsOnScroll,
// Restore only when rows arrive or the pane shows again: measurement churn is the virtualizer's
// own, and a reveal or the reader's scrolling must not be pulled back to an older row.
restoreSignal: `${rows.length}:${isVisible}`,
@@ -238,17 +267,21 @@ export function NativeChatToolRunMembers({
return
}
wasVisibleRef.current = isVisible
const anchor = anchorRef.current
if (!isVisible && anchor) {
anchorRef.current = {
key: anchor.key,
offset: anchor.offset,
fallbackKeys: anchor.fallbackKeys
if (!isVisible) {
const anchor = anchorStaleRef.current
? anchorAtScrollOffset(virtualizer, rows)
: anchorRef.current
if (anchor) {
anchorRef.current = {
key: anchor.key,
offset: anchor.offset,
fallbackKeys: anchor.fallbackKeys
}
}
} else if (isVisible && followsEndRef.current) {
} else if (followsEndRef.current) {
followRef.current = { observedTail: false }
}
}, [isVisible])
}, [isVisible, rows, virtualizer])
// Before paint: new work starts a follow only if the reader was at the end before it. Each visible
// pass re-pins; once the tail is really measured (now, on a later measurement, or when a hidden pane
@@ -1,4 +1,5 @@
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { VIRTUALIZED_LIST_MIN_ROWS } from '@/components/virtualized-list'
import { nativeChatTranscriptRange } from '../../viewport/virtualization/native-chat-pinned-rows'
import type { NativeChatToolRunRow } from './native-chat-tool-run-rows'
@@ -198,5 +199,7 @@ export function useNativeChatToolRunPins({
[scrollerRef]
)
return { rangeExtractor, retainInteractedRow, onFocusCapture, onBlurCapture }
// A selection wider than a window keeps that many rows drawn while the reader scrolls.
const spansSelection = span !== null && span[1] - span[0] >= VIRTUALIZED_LIST_MIN_ROWS
return { rangeExtractor, spansSelection, retainInteractedRow, onFocusCapture, onBlurCapture }
}
@@ -1,5 +1,10 @@
import { useCallback, useLayoutEffect, useMemo, useRef } from 'react'
import { elementScroll, measureElement, useVirtualizer } from '@tanstack/react-virtual'
import {
elementScroll,
measureElement,
useVirtualizer,
type Virtualizer
} from '@tanstack/react-virtual'
import { VIRTUALIZED_LIST_OVERSCAN } from '@/components/virtualized-list'
import type { ProgrammaticScrollMarks } from '@/hooks/programmatic-scroll-marks'
import type { NativeChatToolRunRow } from './native-chat-tool-run-rows'
@@ -8,6 +13,7 @@ import type { NativeChatToolRunRow } from './native-chat-tool-run-rows'
const ROW_ESTIMATE_PX = 26
const estimateRowSize = (): number => ROW_ESTIMATE_PX
export type NativeChatToolRunVirtualizer = Virtualizer<HTMLDivElement, HTMLDivElement>
export type NativeChatToolRunDrawnRow = { index: number; start: number; end: number }
/** The run's virtualizer, kept in its own hook: it reads a mutable library instance every render,