Compare commits

...
Author SHA1 Message Date
Theo BrowneandClaude Fable 5 88187c6e7e fix(web): release collapsed anchor from every quiescent point
Bugbot caught an interleaving where the size-0 report lands after the
turn settled with the positioning refs already nulled (user scrolled
away and back during the turn) — no release path fired. Consolidate the
release preconditions into one re-checkable helper invoked from anchor
reports, positioning settle, turn completion, and return-to-end.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-13 18:43:06 -07:00
Theo BrowneandClaude Fable 5 cc88bb0eae 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 <[email protected]>
2026-08-13 18:31:41 -07:00
3 changed files with 160 additions and 70 deletions

No files matched your search

+142 -64
View File
@@ -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<MessageId | null>(null);
const settledTimelineAnchorRef = useRef<MessageId | null>(null);
const activeTimelineAnchorIndexRef = useRef<number | null>(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<number | null>(0);
// Manual navigation stops live-follow without removing anchored end space.
@@ -3861,75 +3870,141 @@ 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);
return;
// Release preconditions, re-checked at every quiescent point: the anchored
// reveal effect below owns end-following while the turn is still streaming
// (a size-0 report often arrives mid-stream, the moment the response
// outgrows the space), and an in-flight anchored positioning must finish
// before the anchor state is torn down. Returns true when it released.
const maybeReleaseCollapsedTimelineAnchor = useCallback(() => {
if (!timelineAnchorEndSpaceCollapsedRef.current) {
return false;
}
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 (activeTurnInProgressRef.current) {
return false;
}
}, []);
const positioningInFlight =
pendingTimelineAnchorRef.current !== null ||
(positionedTimelineAnchorRef.current !== null &&
settledTimelineAnchorRef.current !== positionedTimelineAnchorRef.current);
if (positioningInFlight) {
return false;
}
releaseCollapsedTimelineAnchor();
return true;
}, [releaseCollapsedTimelineAnchor]);
const maybeReleaseCollapsedTimelineAnchorRef = useRef(maybeReleaseCollapsedTimelineAnchor);
useEffect(() => {
maybeReleaseCollapsedTimelineAnchorRef.current = maybeReleaseCollapsedTimelineAnchor;
}, [maybeReleaseCollapsedTimelineAnchor]);
useEffect(() => {
if (!activeTurnInProgress) {
maybeReleaseCollapsedTimelineAnchor();
}
}, [activeTurnInProgress, maybeReleaseCollapsedTimelineAnchor]);
const onTimelineAnchorReady = useCallback(
(messageId: MessageId, anchorIndex: number, endSpaceSize: number) => {
timelineAnchorEndSpaceCollapsedRef.current = endSpaceSize === 0;
if (maybeReleaseCollapsedTimelineAnchor()) {
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;
// The space may have collapsed while positioning was in flight.
maybeReleaseCollapsedTimelineAnchorRef.current();
});
});
};
requestAnimationFrame(() => positionAnchor(12));
},
[maybeReleaseCollapsedTimelineAnchor],
);
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.
maybeReleaseCollapsedTimelineAnchor();
} else {
timelineScrollModeRef.current = "free-scrolling";
liveFollowUserScrollGenerationRef.current = null;
showScrollDebouncer.current.maybeExecute();
}
},
[maybeReleaseCollapsedTimelineAnchor],
);
// 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 +4068,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 +5188,7 @@ function ChatViewContent(props: ChatViewProps) {
setTimelineLiveFollowEnabled(true);
pendingTimelineAnchorRef.current = messageIdForSend;
activeTimelineAnchorIndexRef.current = null;
timelineAnchorEndSpaceCollapsedRef.current = false;
showScrollDebouncer.current.cancel();
setShowScrollToBottom(false);
setTimelineAnchor({
@@ -5557,6 +5634,7 @@ function ChatViewContent(props: ChatViewProps) {
setTimelineLiveFollowEnabled(true);
pendingTimelineAnchorRef.current = messageIdForSend;
activeTimelineAnchorIndexRef.current = null;
timelineAnchorEndSpaceCollapsedRef.current = false;
showScrollDebouncer.current.cancel();
setShowScrollToBottom(false);
setTimelineAnchor({
@@ -6226,7 +6304,7 @@ function ChatViewContent(props: ChatViewProps) {
key={activeThread.id}
isWorking={isWorking}
workingStepLabel={workingStepLabel}
activeTurnInProgress={isWorking || !latestTurnSettled}
activeTurnInProgress={activeTurnInProgress}
activeTurnStartedAt={activeWorkStartedAt}
listRef={legendListRef}
timelineEntries={timelineEntries}
@@ -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<LegendListRef>;
}) => {
if (props.anchoredEndSpace) {
props.anchoredEndSpace.onReady?.({ anchorIndex: props.anchoredEndSpace.anchorIndex });
props.anchoredEndSpace.onReady?.({
anchorIndex: props.anchoredEndSpace.anchorIndex,
size: 120,
});
}
return (
<div
@@ -429,7 +432,7 @@ describe("MessagesTimeline", () => {
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", () => {
@@ -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<Pick<ServerProviderSkill, "name" | "displayName">>;
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(