From 0e0ebc7c9e4a9c7eaebbe1ebccd73fb9e8eb418e Mon Sep 17 00:00:00 2001 From: "paseo-bot[bot]" <266920839+paseo-bot[bot]@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:10:04 +0000 Subject: [PATCH] fix(chat): keep the reading position when older history becomes virtualized (#9664) * fix(chat): preserve scroll offset when enabling virtualization Reading older messages in a long web chat could jump to the first message when older rows moved into the virtualized history section. When the row virtualizer is enabled it scrolls to its remembered offset, which falls back to initialOffset (0) the first time. Start it from the scroll container's current position instead. Co-Authored-By: Claude Opus 5.5 * test(chat): pass mountTimeline options as an object Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: luke Co-authored-by: Claude Opus 5.5 Co-authored-by: paseo-bot[bot] --- .../strategy-web.browser.test.tsx | 78 +++++++++++++++++-- .../app/src/agent-stream/strategy-web.tsx | 2 + 2 files changed, 73 insertions(+), 7 deletions(-) diff --git a/packages/app/src/agent-stream/strategy-web.browser.test.tsx b/packages/app/src/agent-stream/strategy-web.browser.test.tsx index a9cc44a84..188e8d8ed 100644 --- a/packages/app/src/agent-stream/strategy-web.browser.test.tsx +++ b/packages/app/src/agent-stream/strategy-web.browser.test.tsx @@ -54,8 +54,16 @@ type OlderHistoryInput = Pick< "onNearHistoryStart" | "hasOlderHistory" | "olderHistoryProgressKey" >; -function mountTimeline(historyMounted: StreamItem[], olderHistory?: OlderHistoryInput) { - const strategy = createWebStreamStrategy({ isMobileBreakpoint: true }); +interface MountTimelineOptions { + olderHistory?: OlderHistoryInput; + isMobileBreakpoint?: boolean; +} + +function mountTimeline( + historyMounted: StreamItem[], + { olderHistory, isMobileBreakpoint = true }: MountTimelineOptions = {}, +) { + const strategy = createWebStreamStrategy({ isMobileBreakpoint }); const viewportRef = React.createRef(); const input: StreamRenderInput = { agentId: "agent", @@ -113,6 +121,13 @@ function mountTimeline(historyMounted: StreamItem[], olderHistory?: OlderHistory }, distanceFromEnd: () => scrollContainer.scrollHeight - scrollContainer.clientHeight - scrollContainer.scrollTop, + scrollTop: () => scrollContainer.scrollTop, + scrollUp: (distance: number) => + act(() => { + scrollContainer.dispatchEvent(new WheelEvent("wheel", { deltaY: -distance })); + scrollContainer.scrollTop -= distance; + scrollContainer.dispatchEvent(new Event("scroll")); + }), // Resize observers and the hold's settle run on animation frames. settle: () => act(async () => { @@ -124,6 +139,53 @@ function mountTimeline(historyMounted: StreamItem[], olderHistory?: OlderHistory } describe("web disclosure hold", () => { + it.each([{ isMobileBreakpoint: true }, { isMobileBreakpoint: false }])( + "keeps the reading position when mounted history becomes virtualized (compact: $isMobileBreakpoint)", + async (options) => { + const history = Array.from({ length: 80 }, (_, index) => row(index)); + const timeline = mountTimeline(history, options); + await timeline.settle(); + timeline.scrollUp(600); + await timeline.settle(); + const offset = timeline.scrollTop(); + const readingTop = timeline.rowTop("message-64"); + expect(offset).toBeGreaterThan(0); + + timeline.render({ + historyVirtualized: history.slice(0, 60), + historyMounted: history.slice(60), + }); + await timeline.settle(); + + expect(timeline.scrollTop()).toBe(offset); + expect(timeline.rowTop("message-64")).toBe(readingTop); + }, + ); + + it.each([{ isMobileBreakpoint: true }, { isMobileBreakpoint: false }])( + "keeps following output when mounted history becomes virtualized at the end (compact: $isMobileBreakpoint)", + async (options) => { + const history = Array.from({ length: 80 }, (_, index) => row(index)); + const timeline = mountTimeline(history, options); + await timeline.settle(); + + timeline.render({ + historyVirtualized: history.slice(0, 60), + historyMounted: history.slice(60), + }); + await timeline.settle(); + expect(timeline.distanceFromEnd()).toBeLessThanOrEqual(1); + + timeline.render({ + historyVirtualized: history.slice(0, 60), + historyMounted: history.slice(60), + liveHead: [row(80)], + }); + await timeline.settle(); + expect(timeline.distanceFromEnd()).toBeLessThanOrEqual(1); + }, + ); + it("keeps the trigger's top when an anchored toggle at the bottom opens rows below it, and stops following", async () => { const history = [1, 2, 3, 4, 5, 6].map((index) => row(index)); const timeline = mountTimeline(history); @@ -225,11 +287,13 @@ describe("web history start", () => { let olderHistoryLoads = 0; const history = [1, 2, 3, 4, 5, 6].map((index) => row(index)); const timeline = mountTimeline(history, { - hasOlderHistory: true, - olderHistoryProgressKey: "epoch:1", - onNearHistoryStart: async () => { - olderHistoryLoads += 1; - return true; + olderHistory: { + hasOlderHistory: true, + olderHistoryProgressKey: "epoch:1", + onNearHistoryStart: async () => { + olderHistoryLoads += 1; + return true; + }, }, }); await timeline.settle(); diff --git a/packages/app/src/agent-stream/strategy-web.tsx b/packages/app/src/agent-stream/strategy-web.tsx index 896b53b3f..465d98e8d 100644 --- a/packages/app/src/agent-stream/strategy-web.tsx +++ b/packages/app/src/agent-stream/strategy-web.tsx @@ -457,6 +457,8 @@ function WebStreamViewport(props: StreamRenderInput & { isMobileBreakpoint: bool const rowVirtualizer = useVirtualizer({ count: segments.historyVirtualized.length, enabled: shouldUseVirtualizer, + // History can become virtualized after the reader has already scrolled the mounted rows. + initialOffset: () => scrollContainerRef.current?.scrollTop ?? 0, initialRect: { width: scrollContainerRef.current?.clientWidth ?? 0, height: scrollContainerRef.current?.clientHeight ?? 0,