From cc88bb0eaedcff060986b9df5c9d468413f655c1 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Thu, 13 Aug 2026 18:31:41 -0700 Subject: [PATCH] fix(web): opening the sidebar no longer scrolls the thread away from the end Container width changes (sidebar/right-panel toggles) remeasure timeline rows progressively while maintainVisibleContentPosition holds the top visible row steady, walking the viewport outside LegendList's default end-maintenance window (0.1 viewports) after which it never re-pins. Worse, the send-time anchored end space stayed armed for the rest of the thread session, which disabled end maintenance entirely. - keep end maintenance active within a full viewport of the end - release the anchor once its end space collapses and the turn settles Co-Authored-By: Claude Fable 5 --- apps/web/src/components/ChatView.tsx | 188 ++++++++++++------ .../components/chat/MessagesTimeline.test.tsx | 9 +- .../src/components/chat/MessagesTimeline.tsx | 15 +- 3 files changed, 143 insertions(+), 69 deletions(-) diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index fdc7e7dee3..f8017fc229 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -2294,6 +2294,11 @@ function ChatViewContent(props: ChatViewProps) { threadError, }); const isWorking = phase === "running" || isSendBusy || isConnecting || isRevertingCheckpoint; + const activeTurnInProgress = isWorking || !latestTurnSettled; + const activeTurnInProgressRef = useRef(activeTurnInProgress); + useEffect(() => { + activeTurnInProgressRef.current = activeTurnInProgress; + }, [activeTurnInProgress]); const activeWorkStartedAt = deriveActiveWorkStartedAt( activeLatestTurn, activeThread?.session ?? null, @@ -3666,6 +3671,10 @@ function ChatViewContent(props: ChatViewProps) { const positionedTimelineAnchorRef = useRef(null); const settledTimelineAnchorRef = useRef(null); const activeTimelineAnchorIndexRef = useRef(null); + // True once the armed anchor's end space has shrunk to zero — the response + // outgrew the reserved blank space, so the anchored view is identical to + // ordinary end-following and the anchor can be released. + const timelineAnchorEndSpaceCollapsedRef = useRef(false); const anchorUserScrollGenerationRef = useRef(0); const liveFollowUserScrollGenerationRef = useRef(0); // Manual navigation stops live-follow without removing anchored end space. @@ -3861,75 +3870,125 @@ function ChatViewContent(props: ChatViewProps) { }; }, [activeThread?.id, composerOverlayHeight, timelineRealContentOverflowsViewport]); - const onTimelineAnchorReady = useCallback((messageId: MessageId, anchorIndex: number) => { - // Anchored-end space can be remeasured when the turn completes. Once the - // user has scrolled away (or returned to ordinary end-following), that - // remeasurement must not restart the send-time anchor positioning. - if (timelineScrollModeRef.current !== "anchoring-new-turn") { - return; + // A stale armed anchor keeps LegendList's maintainScrollAtEnd disabled for + // the rest of the thread session, so any layout resize (sidebar or right + // panel toggles) strands the viewport short of the live edge. Once the end + // space has collapsed, releasing the anchor is visually a no-op and hands + // ordinary end-follow back to LegendList. + const releaseCollapsedTimelineAnchor = useCallback(() => { + if (timelineScrollModeRef.current === "anchoring-new-turn") { + timelineScrollModeRef.current = "following-end"; } - if (pendingTimelineAnchorRef.current === messageId) { - pendingTimelineAnchorRef.current = null; - } - activeTimelineAnchorIndexRef.current = anchorIndex; - if (positionedTimelineAnchorRef.current === messageId) { - return; - } - positionedTimelineAnchorRef.current = messageId; + pendingTimelineAnchorRef.current = null; + positionedTimelineAnchorRef.current = null; settledTimelineAnchorRef.current = null; - const positionAnchor = (remainingAttempts: number) => { - requestAnimationFrame(() => { - if (positionedTimelineAnchorRef.current !== messageId) { - return; - } - const list = legendListRef.current; - if (!list) { - if (remainingAttempts > 0) { - positionAnchor(remainingAttempts - 1); - } - return; - } - void list - .scrollToIndex({ - index: anchorIndex, - animated: true, - viewPosition: 0, - viewOffset: CHAT_LIST_ANCHOR_OFFSET, - }) - .then(() => { - if (positionedTimelineAnchorRef.current !== messageId) { - return; - } - settledTimelineAnchorRef.current = messageId; - }); - }); - }; - requestAnimationFrame(() => positionAnchor(12)); + activeTimelineAnchorIndexRef.current = null; + setTimelineAnchor((current) => + current.messageId === null ? current : { ...current, messageId: null }, + ); }, []); - const onIsAtEndChange = useCallback((isAtEnd: boolean) => { - if ( - !isAtEnd && - liveFollowUserScrollGenerationRef.current === anchorUserScrollGenerationRef.current - ) { - showScrollDebouncer.current.cancel(); - setShowScrollToBottom(false); + // The anchored reveal effect below owns end-following while the turn is + // still streaming, so release only after the turn settles; a size-0 report + // often arrives mid-stream, the moment the response outgrows the space. + useEffect(() => { + if (activeTurnInProgress) { return; } - if (isAtEndRef.current === isAtEnd) return; - isAtEndRef.current = isAtEnd; - if (isAtEnd) { - timelineScrollModeRef.current = "following-end"; - liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current; - setTimelineLiveFollowEnabled(true); - showScrollDebouncer.current.cancel(); - setShowScrollToBottom(false); - } else { - timelineScrollModeRef.current = "free-scrolling"; - liveFollowUserScrollGenerationRef.current = null; - showScrollDebouncer.current.maybeExecute(); + if (timelineAnchorEndSpaceCollapsedRef.current) { + releaseCollapsedTimelineAnchor(); } - }, []); + }, [activeTurnInProgress, releaseCollapsedTimelineAnchor]); + + const onTimelineAnchorReady = useCallback( + (messageId: MessageId, anchorIndex: number, endSpaceSize: number) => { + timelineAnchorEndSpaceCollapsedRef.current = endSpaceSize === 0; + if ( + endSpaceSize === 0 && + settledTimelineAnchorRef.current === messageId && + !activeTurnInProgressRef.current + ) { + releaseCollapsedTimelineAnchor(); + return; + } + // Anchored-end space can be remeasured when the turn completes. Once the + // user has scrolled away (or returned to ordinary end-following), that + // remeasurement must not restart the send-time anchor positioning. + if (timelineScrollModeRef.current !== "anchoring-new-turn") { + return; + } + if (pendingTimelineAnchorRef.current === messageId) { + pendingTimelineAnchorRef.current = null; + } + activeTimelineAnchorIndexRef.current = anchorIndex; + if (positionedTimelineAnchorRef.current === messageId) { + return; + } + positionedTimelineAnchorRef.current = messageId; + settledTimelineAnchorRef.current = null; + const positionAnchor = (remainingAttempts: number) => { + requestAnimationFrame(() => { + if (positionedTimelineAnchorRef.current !== messageId) { + return; + } + const list = legendListRef.current; + if (!list) { + if (remainingAttempts > 0) { + positionAnchor(remainingAttempts - 1); + } + return; + } + void list + .scrollToIndex({ + index: anchorIndex, + animated: true, + viewPosition: 0, + viewOffset: CHAT_LIST_ANCHOR_OFFSET, + }) + .then(() => { + if (positionedTimelineAnchorRef.current !== messageId) { + return; + } + settledTimelineAnchorRef.current = messageId; + }); + }); + }; + requestAnimationFrame(() => positionAnchor(12)); + }, + [releaseCollapsedTimelineAnchor], + ); + + const onIsAtEndChange = useCallback( + (isAtEnd: boolean) => { + if ( + !isAtEnd && + liveFollowUserScrollGenerationRef.current === anchorUserScrollGenerationRef.current + ) { + showScrollDebouncer.current.cancel(); + setShowScrollToBottom(false); + return; + } + if (isAtEndRef.current === isAtEnd) return; + isAtEndRef.current = isAtEnd; + if (isAtEnd) { + timelineScrollModeRef.current = "following-end"; + liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current; + setTimelineLiveFollowEnabled(true); + showScrollDebouncer.current.cancel(); + setShowScrollToBottom(false); + // Covers anchors whose end space collapsed while the user was away from + // the end: no further onAnchorReady will fire, so release on return. + if (timelineAnchorEndSpaceCollapsedRef.current && !activeTurnInProgressRef.current) { + releaseCollapsedTimelineAnchor(); + } + } else { + timelineScrollModeRef.current = "free-scrolling"; + liveFollowUserScrollGenerationRef.current = null; + showScrollDebouncer.current.maybeExecute(); + } + }, + [releaseCollapsedTimelineAnchor], + ); // Anchored end space intentionally disables LegendList's normal end-follow so // the sent message can stay near the top. T3 only owns streaming adjustments @@ -3993,6 +4052,7 @@ function ChatViewContent(props: ChatViewProps) { positionedTimelineAnchorRef.current = null; settledTimelineAnchorRef.current = null; activeTimelineAnchorIndexRef.current = null; + timelineAnchorEndSpaceCollapsedRef.current = false; showScrollDebouncer.current.cancel(); setShowScrollToBottom(false); // activeThreadRef resets transitively with the active thread. @@ -5112,6 +5172,7 @@ function ChatViewContent(props: ChatViewProps) { setTimelineLiveFollowEnabled(true); pendingTimelineAnchorRef.current = messageIdForSend; activeTimelineAnchorIndexRef.current = null; + timelineAnchorEndSpaceCollapsedRef.current = false; showScrollDebouncer.current.cancel(); setShowScrollToBottom(false); setTimelineAnchor({ @@ -5557,6 +5618,7 @@ function ChatViewContent(props: ChatViewProps) { setTimelineLiveFollowEnabled(true); pendingTimelineAnchorRef.current = messageIdForSend; activeTimelineAnchorIndexRef.current = null; + timelineAnchorEndSpaceCollapsedRef.current = false; showScrollDebouncer.current.cancel(); setShowScrollToBottom(false); setTimelineAnchor({ @@ -6226,7 +6288,7 @@ function ChatViewContent(props: ChatViewProps) { key={activeThread.id} isWorking={isWorking} workingStepLabel={workingStepLabel} - activeTurnInProgress={isWorking || !latestTurnSettled} + activeTurnInProgress={activeTurnInProgress} activeTurnStartedAt={activeWorkStartedAt} listRef={legendListRef} timelineEntries={timelineEntries} diff --git a/apps/web/src/components/chat/MessagesTimeline.test.tsx b/apps/web/src/components/chat/MessagesTimeline.test.tsx index f22130906e..87dd10629c 100644 --- a/apps/web/src/components/chat/MessagesTimeline.test.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.test.tsx @@ -17,7 +17,7 @@ vi.mock("@legendapp/list/react", async () => { anchorIndex: number; anchorMaxSize?: number; anchorOffset?: number; - onReady?: (info: { anchorIndex: number }) => void; + onReady?: (info: { anchorIndex: number; size: number }) => void; }; contentInsetEndAdjustment?: number; className?: string; @@ -41,7 +41,10 @@ vi.mock("@legendapp/list/react", async () => { ref?: Ref; }) => { if (props.anchoredEndSpace) { - props.anchoredEndSpace.onReady?.({ anchorIndex: props.anchoredEndSpace.anchorIndex }); + props.anchoredEndSpace.onReady?.({ + anchorIndex: props.anchoredEndSpace.anchorIndex, + size: 120, + }); } return (
{ expect(markup).toContain('data-maintain-visible-content-position-size="true"'); expect(markup).toContain('data-maintain-visible-content-position-restore="true"'); expect(onAnchorReady).toHaveBeenCalledOnce(); - expect(onAnchorReady).toHaveBeenCalledWith(secondEntry.message.id, 1); + expect(onAnchorReady).toHaveBeenCalledWith(secondEntry.message.id, 1, 120); }); it("renders collapse controls for long user messages", () => { diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsx index 9cd1c78aa4..9c10f496be 100644 --- a/apps/web/src/components/chat/MessagesTimeline.tsx +++ b/apps/web/src/components/chat/MessagesTimeline.tsx @@ -196,6 +196,14 @@ const TIMELINE_MAINTAIN_SCROLL_AT_END = { layout: true, }, } as const; +// LegendList abandons end maintenance once the viewport drifts further from +// the end than this fraction of the viewport (library default 0.1). Container +// width changes (sidebar/right-panel toggles) remeasure rows progressively +// while maintainVisibleContentPosition holds the top visible row steady, which +// walks the end away in steps larger than that window and strands the timeline +// short of the live edge. One full viewport keeps maintenance active through a +// remeasure; ChatView's scroll-mode refs still gate when maintenance runs. +const TIMELINE_MAINTAIN_SCROLL_AT_END_THRESHOLD = 1; // --------------------------------------------------------------------------- // Props (public API) @@ -226,7 +234,7 @@ interface MessagesTimelineProps { workspaceRoot: string | undefined; skills?: ReadonlyArray>; anchorMessageId: MessageId | null; - onAnchorReady: (messageId: MessageId, anchorIndex: number) => void; + onAnchorReady: (messageId: MessageId, anchorIndex: number, endSpaceSize: number) => void; contentInsetEndAdjustment: number; /** * Whether the timeline should keep pinning to the live edge as content @@ -426,9 +434,9 @@ export const MessagesTimeline = memo(function MessagesTimeline({ const [minimapHasPersistentGutter, setMinimapHasPersistentGutter] = useState(false); const [minimapHitStripWidth, setMinimapHitStripWidth] = useState(0); const handleAnchorReady = useCallback( - (info: { anchorIndex: number | undefined }) => { + (info: { anchorIndex: number | undefined; size: number }) => { if (anchorMessageId !== null && info.anchorIndex !== undefined) { - onAnchorReady(anchorMessageId, info.anchorIndex); + onAnchorReady(anchorMessageId, info.anchorIndex, info.size); } }, [anchorMessageId, onAnchorReady], @@ -587,6 +595,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({ ? false : TIMELINE_MAINTAIN_SCROLL_AT_END } + maintainScrollAtEndThreshold={TIMELINE_MAINTAIN_SCROLL_AT_END_THRESHOLD} maintainVisibleContentPosition={maintainVisibleContentPosition} onScroll={handleScroll} className={cn(