Skip to content

Commit 4d26a50

Browse files
fix(web): reopen a thread that was following its stream at the end
The timeline remembered each thread's position from raw scroll geometry. While a turn streams, new output sits below the viewport until the follow scroll catches up, so a thread that was following could be remembered as a reading position. Switching away and back then restored that offset, often thousands of pixels above the latest message. Remember the thread as at the end while live follow is active, matching how ChatView already treats that gap in onIsAtEndChange. Once the user scrolls away, follow is off and the real offset is saved as before. Fixes #13601 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent e3e7cc3 commit 4d26a50

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

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

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ import { act, createRef, useLayoutEffect, type ReactNode, type Ref } from "react
1010
import { renderToStaticMarkup } from "react-dom/server";
1111
import { create, type ReactTestRenderer } from "react-test-renderer";
1212
import { beforeAll, beforeEach, describe, expect, it, vi } from "vite-plus/test";
13-
import type { LegendListRef, MaintainScrollAtEndOptions } from "@legendapp/list/react";
13+
import {
14+
LegendList,
15+
type LegendListRef,
16+
type MaintainScrollAtEndOptions,
17+
} from "@legendapp/list/react";
1418
import { shouldUseRestingComposerLayout } from "../composerFooterLayout";
19+
import { readTimelinePosition } from "./timelineScrollAnchoring";
1520
import { useComposerFocusState } from "./useComposerFocusState";
1621

1722
vi.mock("@legendapp/list/react", async () => {
@@ -998,6 +1003,72 @@ describe("MessagesTimeline", () => {
9981003
}
9991004
});
10001005

1006+
it("remembers a live-following thread at the end while the follow scroll trails output", () => {
1007+
vi.stubGlobal("requestAnimationFrame", () => 0);
1008+
vi.stubGlobal("cancelAnimationFrame", () => {});
1009+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
1010+
const threadKey = "env-1:thread-following";
1011+
const entries = [
1012+
{
1013+
id: "entry-follow-work",
1014+
kind: "work" as const,
1015+
createdAt: MESSAGE_CREATED_AT,
1016+
entry: {
1017+
id: "work-follow",
1018+
createdAt: MESSAGE_CREATED_AT,
1019+
toolCallId: "call-follow",
1020+
label: "Run lint",
1021+
tone: "tool" as const,
1022+
itemType: "command_execution" as const,
1023+
command: "pnpm lint",
1024+
toolLifecycleStatus: "completed" as const,
1025+
},
1026+
},
1027+
];
1028+
let renderer!: ReactTestRenderer;
1029+
const list = () => renderer.root.findByType(LegendList);
1030+
const props = buildProps();
1031+
// Streamed output has grown the list 500px past the viewport, and the
1032+
// follow scroll has not caught up yet.
1033+
props.listRef.current = {
1034+
getState: () => ({
1035+
data: list().props.data,
1036+
isAtEnd: false,
1037+
contentLength: 2000,
1038+
scroll: 700,
1039+
scrollLength: 800,
1040+
positionAtIndex: () => 0,
1041+
indexByKey: () => 0,
1042+
elementAtIndex: () => ({ getBoundingClientRect: () => ({ top: -700 }) }),
1043+
}),
1044+
getScrollableNode: () => ({ scrollTop: 700, getBoundingClientRect: () => ({ top: 0 }) }),
1045+
} as unknown as LegendListRef;
1046+
const renderTimeline = (liveFollowEnabled: boolean) => (
1047+
<MessagesTimeline
1048+
{...props}
1049+
isWorking
1050+
liveFollowEnabled={liveFollowEnabled}
1051+
routeThreadKey={threadKey}
1052+
timelineEntries={entries}
1053+
/>
1054+
);
1055+
try {
1056+
act(() => {
1057+
renderer = create(renderTimeline(true));
1058+
});
1059+
act(() => list().props.onScroll());
1060+
expect(readTimelinePosition(threadKey)?.atEnd).toBe(true);
1061+
1062+
// Once the user scrolls away, the same offset is a reading position.
1063+
act(() => renderer.update(renderTimeline(false)));
1064+
act(() => list().props.onScroll());
1065+
expect(readTimelinePosition(threadKey)).toMatchObject({ atEnd: false, scrollOffset: 700 });
1066+
} finally {
1067+
act(() => renderer?.unmount());
1068+
vi.unstubAllGlobals();
1069+
}
1070+
});
1071+
10011072
it("keeps reserved end space when tool work starts while reading history", () => {
10021073
const turnId = TurnId.make("turn-with-active-tool");
10031074
const firstEntry = buildUserTimelineEntry("Run the command.");

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,9 @@ export const MessagesTimeline = memo(function MessagesTimeline({
10291029
// DOM geometry includes the header and the virtualizer's layout adjustment.
10301030
offsetWithinRow: element.getBoundingClientRect().top - row.getBoundingClientRect().top,
10311031
scrollOffset: element.scrollTop,
1032-
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+
atEnd: isAtEnd || liveFollowEnabled,
10331035
disclosures: {
10341036
turns: paintedExpandedTurnIds,
10351037
workGroups: paintedExpandedWorkGroupIds,
@@ -1087,6 +1089,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
10871089
workGroupViewState,
10881090
rows,
10891091
listIdentityKey,
1092+
liveFollowEnabled,
10901093
restoringThreadPosition,
10911094
listRef,
10921095
minimapItems,

0 commit comments

Comments
 (0)