diff --git a/src/renderer/src/components/native-chat/tools/runs/NativeChatToolRunMembers.reader.test.tsx b/src/renderer/src/components/native-chat/tools/runs/NativeChatToolRunMembers.reader.test.tsx index 4a0df7321e0..16e6e06b569 100644 --- a/src/renderer/src/components/native-chat/tools/runs/NativeChatToolRunMembers.reader.test.tsx +++ b/src/renderer/src/components/native-chat/tools/runs/NativeChatToolRunMembers.reader.test.tsx @@ -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 ( @@ -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() + await settle() + rerender() + 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() + membersBox(container).scrollTop = 0 + pane.hidden = false + rerender() + 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() await settle() diff --git a/src/renderer/src/components/native-chat/tools/runs/NativeChatToolRunMembers.tsx b/src/renderer/src/components/native-chat/tools/runs/NativeChatToolRunMembers.tsx index 9df0292022c..eb5feaa204e 100644 --- a/src/renderer/src/components/native-chat/tools/runs/NativeChatToolRunMembers.tsx +++ b/src/renderer/src/components/native-chat/tools/runs/NativeChatToolRunMembers.tsx @@ -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 diff --git a/src/renderer/src/components/native-chat/tools/runs/use-native-chat-tool-run-pins.ts b/src/renderer/src/components/native-chat/tools/runs/use-native-chat-tool-run-pins.ts index 731cb051a08..cd373a02f36 100644 --- a/src/renderer/src/components/native-chat/tools/runs/use-native-chat-tool-run-pins.ts +++ b/src/renderer/src/components/native-chat/tools/runs/use-native-chat-tool-run-pins.ts @@ -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 } } diff --git a/src/renderer/src/components/native-chat/tools/runs/use-native-chat-tool-run-window.ts b/src/renderer/src/components/native-chat/tools/runs/use-native-chat-tool-run-window.ts index f44c1d9508a..a0466a1fef7 100644 --- a/src/renderer/src/components/native-chat/tools/runs/use-native-chat-tool-run-window.ts +++ b/src/renderer/src/components/native-chat/tools/runs/use-native-chat-tool-run-window.ts @@ -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 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,