Skip to content

Commit 6a9f043

Browse files
fix(web): reopen a thread that was following its stream at the end
While a turn streams, the follow scroll trails new output, so scroll events report the thread as not at the end. The per-thread position cache saved that as a reading position, and returning to the thread restored it far above the latest message. Remember the thread at the end while live follow is latched and no anchored end space holds a first send near the top.
1 parent b4d3d51 commit 6a9f043

3 files changed

Lines changed: 109 additions & 2 deletions

File tree

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5887,6 +5887,10 @@ export default function ChatView(props: ChatViewProps) {
58875887
}
58885888
}
58895889
}, []);
5890+
const isTimelineLiveFollowLatched = useCallback(
5891+
() => liveFollowUserScrollGenerationRef.current === anchorUserScrollGenerationRef.current,
5892+
[],
5893+
);
58905894
const cancelTimelineLiveFollowForUserNavigationRef = useRef(
58915895
cancelTimelineLiveFollowForUserNavigation,
58925896
);
@@ -10701,6 +10705,7 @@ export default function ChatView(props: ChatViewProps) {
1070110705
onAnchorSizeChanged={onTimelineAnchorSizeChanged}
1070210706
contentInsetEndAdjustment={composerTimelineInset}
1070310707
liveFollowEnabled={!paintOnlyDisplayedTimeline && timelineLiveFollowEnabled}
10708+
isLiveFollowLatched={isTimelineLiveFollowLatched}
1070410709
onIsAtEndChange={onIsAtEndChange}
1070510710
onContentOverflowChange={setTimelineOverflows}
1070610711
onToolOutputCollapsedAtEnd={onToolOutputCollapsedAtEnd}

‎apps/web/src/components/chat/MessagesTimeline.test.tsx‎

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import {
1717
import { renderToStaticMarkup } from "react-dom/server";
1818
import { create, type ReactTestRenderer } from "react-test-renderer";
1919
import { shouldUseRestingComposerLayout } from "../composerFooterLayout";
20+
import { readTimelinePosition } from "./timelineScrollAnchoring";
2021
import { useComposerFocusState } from "./useComposerFocusState";
2122
import { beforeAll, beforeEach, describe, expect, it, vi } from "vite-plus/test";
22-
import type { LegendListRef } from "@legendapp/list/react";
23+
import { LegendList, type LegendListRef } from "@legendapp/list/react";
2324

2425
const activityTestState = vi.hoisted(() => ({
2526
expanded: false,
@@ -292,6 +293,7 @@ function buildProps() {
292293
onAnchorSizeChanged: () => {},
293294
contentInsetEndAdjustment: 0,
294295
liveFollowEnabled: true,
296+
isLiveFollowLatched: () => true,
295297
onIsAtEndChange: () => {},
296298
onManualNavigation: () => {},
297299
};
@@ -1108,6 +1110,92 @@ describe("MessagesTimeline", () => {
11081110
expect(markup).not.toContain("<a ");
11091111
});
11101112

1113+
it("remembers a live-following thread at the end while the follow scroll trails output", () => {
1114+
vi.stubGlobal("requestAnimationFrame", () => 0);
1115+
vi.stubGlobal("cancelAnimationFrame", () => {});
1116+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
1117+
const threadKey = "env-1:thread-following";
1118+
const entries = [
1119+
{
1120+
id: "entry-follow-work",
1121+
kind: "work" as const,
1122+
createdAt: MESSAGE_CREATED_AT,
1123+
entry: {
1124+
id: "work-follow",
1125+
createdAt: MESSAGE_CREATED_AT,
1126+
toolCallId: "call-follow",
1127+
label: "Run lint",
1128+
tone: "tool" as const,
1129+
itemType: "command_execution" as const,
1130+
command: "pnpm lint",
1131+
toolLifecycleStatus: "completed" as const,
1132+
},
1133+
},
1134+
];
1135+
let renderer!: ReactTestRenderer;
1136+
const list = () => renderer.root.findByType(LegendList);
1137+
let latched = true;
1138+
const props = { ...buildProps(), isLiveFollowLatched: () => latched };
1139+
// Streamed output has grown the list 500px past the viewport, and the
1140+
// follow scroll has not caught up yet.
1141+
props.listRef.current = {
1142+
getState: () => ({
1143+
data: list().props.data,
1144+
isAtEnd: false,
1145+
contentLength: 2000,
1146+
scroll: 700,
1147+
scrollLength: 800,
1148+
positionAtIndex: () => 0,
1149+
indexByKey: () => 0,
1150+
elementAtIndex: () => ({ getBoundingClientRect: () => ({ top: -700 }) }),
1151+
}),
1152+
getScrollableNode: () => ({ scrollTop: 700, getBoundingClientRect: () => ({ top: 0 }) }),
1153+
} as unknown as LegendListRef;
1154+
const renderTimeline = (
1155+
liveFollowEnabled: boolean,
1156+
anchorMessageId: MessageId | null = null,
1157+
) => (
1158+
<MessagesTimeline
1159+
{...props}
1160+
isWorking
1161+
liveFollowEnabled={liveFollowEnabled}
1162+
anchorMessageId={anchorMessageId}
1163+
routeThreadKey={threadKey}
1164+
timelineEntries={
1165+
anchorMessageId ? [buildUserTimelineEntry("First send"), ...entries] : entries
1166+
}
1167+
/>
1168+
);
1169+
try {
1170+
act(() => {
1171+
renderer = create(renderTimeline(true));
1172+
});
1173+
act(() => list().props.onScroll());
1174+
expect(readTimelinePosition(threadKey)?.atEnd).toBe(true);
1175+
1176+
// A gesture releases the latch before the next render turns follow off.
1177+
latched = false;
1178+
act(() => list().props.onScroll());
1179+
expect(readTimelinePosition(threadKey)).toMatchObject({ atEnd: false, scrollOffset: 700 });
1180+
latched = true;
1181+
act(() => list().props.onScroll());
1182+
expect(readTimelinePosition(threadKey)?.atEnd).toBe(true);
1183+
1184+
// Once the user scrolls away, the same offset is a reading position.
1185+
act(() => renderer.update(renderTimeline(false)));
1186+
act(() => list().props.onScroll());
1187+
expect(readTimelinePosition(threadKey)).toMatchObject({ atEnd: false, scrollOffset: 700 });
1188+
1189+
// A first send anchored near the top is not following the end either.
1190+
act(() => renderer.update(renderTimeline(true, MessageId.make("message-1"))));
1191+
act(() => list().props.onScroll());
1192+
expect(readTimelinePosition(threadKey)).toMatchObject({ atEnd: false, scrollOffset: 700 });
1193+
} finally {
1194+
act(() => renderer?.unmount());
1195+
vi.unstubAllGlobals();
1196+
}
1197+
});
1198+
11111199
it("keeps reserved end space when tool work starts while reading history", () => {
11121200
const runId = RunId.make("run-with-active-tool");
11131201
const firstEntry = buildUserTimelineEntry("Run the command.");

‎apps/web/src/components/chat/MessagesTimeline.tsx‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,11 @@ interface MessagesTimelineProps {
466466
* scroll-mode refs whenever the user drifts near the bottom.
467467
*/
468468
liveFollowEnabled: boolean;
469+
/**
470+
* Reads ChatView's live-follow latch. A navigation gesture releases it
471+
* synchronously, before `liveFollowEnabled` re-renders.
472+
*/
473+
isLiveFollowLatched: () => boolean;
469474
/**
470475
* Whether the real rows extend past the viewport above the composer.
471476
* Reported after scrolls, row size changes, and viewport resizes.
@@ -534,6 +539,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
534539
onIsAtEndChange,
535540
onContentOverflowChange,
536541
liveFollowEnabled,
542+
isLiveFollowLatched,
537543
onToolOutputCollapsedAtEnd,
538544
onManualNavigation,
539545
cancelPositionRestoreRef,
@@ -1023,7 +1029,12 @@ export const MessagesTimeline = memo(function MessagesTimeline({
10231029
// DOM geometry includes the header and the virtualizer's layout adjustment.
10241030
offsetWithinRow: element.getBoundingClientRect().top - row.getBoundingClientRect().top,
10251031
scrollOffset: element.scrollTop,
1026-
atEnd: isAtEnd,
1032+
// While live-following, a gap to the end is the follow scroll
1033+
// trailing streamed output, not a reading position to restore.
1034+
// Anchored end space holds the first send near the top instead of
1035+
// following, so its gap is a real position. The latch covers a
1036+
// gesture that released follow before this render caught up.
1037+
atEnd: isAtEnd || (liveFollowEnabled && isLiveFollowLatched() && !anchoredEndSpace),
10271038
disclosures: {
10281039
runs: paintedExpandedRunIds,
10291040
workGroups: paintedExpandedWorkGroupIds,
@@ -1082,6 +1093,9 @@ export const MessagesTimeline = memo(function MessagesTimeline({
10821093
workGroupViewState,
10831094
rows,
10841095
listIdentityKey,
1096+
liveFollowEnabled,
1097+
isLiveFollowLatched,
1098+
anchoredEndSpace,
10851099
restoringThreadPosition,
10861100
listRef,
10871101
minimapItems,

0 commit comments

Comments
 (0)