Skip to content

Commit 85ddda2

Browse files
committed
fix(web): release thread scroll anchors without disrupting manual navigation
1 parent eab987e commit 85ddda2

5 files changed

Lines changed: 173 additions & 17 deletions

File tree

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
scheduleEnvironmentReconnectWarning,
3535
startNewThreadForProject,
3636
shouldDockDraftHeroForSubmission,
37+
shouldReleaseTimelineAnchorForToolActivity,
3738
shouldShowBranchMismatchBanner,
3839
shouldWriteThreadErrorToCurrentServerThread,
3940
} from "./ChatView.logic";
@@ -77,6 +78,114 @@ describe("draft hero submission transition", () => {
7778
});
7879
});
7980

81+
describe("shouldReleaseTimelineAnchorForToolActivity", () => {
82+
const activeTurnId = TurnId.make("active-turn");
83+
const anchorMessageId = MessageId.make("anchored-message");
84+
const activeToolEntry = {
85+
id: "tool-entry",
86+
kind: "work" as const,
87+
createdAt: now,
88+
entry: {
89+
id: "active-tool",
90+
createdAt: now,
91+
turnId: activeTurnId,
92+
label: "Run command",
93+
tone: "tool" as const,
94+
command: "git status",
95+
},
96+
};
97+
98+
it("releases the send anchor for tool activity in the active turn", () => {
99+
expect(
100+
shouldReleaseTimelineAnchorForToolActivity({
101+
anchorMessageId,
102+
liveFollowEnabled: true,
103+
runningTurnId: activeTurnId,
104+
timelineEntries: [activeToolEntry],
105+
}),
106+
).toBe(true);
107+
});
108+
109+
it("keeps the anchor while the user reads history", () => {
110+
expect(
111+
shouldReleaseTimelineAnchorForToolActivity({
112+
anchorMessageId,
113+
liveFollowEnabled: false,
114+
runningTurnId: activeTurnId,
115+
timelineEntries: [activeToolEntry],
116+
}),
117+
).toBe(false);
118+
});
119+
120+
it("ignores tool activity from earlier turns", () => {
121+
expect(
122+
shouldReleaseTimelineAnchorForToolActivity({
123+
anchorMessageId,
124+
liveFollowEnabled: true,
125+
runningTurnId: activeTurnId,
126+
timelineEntries: [
127+
{
128+
...activeToolEntry,
129+
entry: {
130+
...activeToolEntry.entry,
131+
turnId: TurnId.make("previous-turn"),
132+
},
133+
},
134+
],
135+
}),
136+
).toBe(false);
137+
});
138+
139+
it("ignores thinking and error rows without tool activity", () => {
140+
expect(
141+
shouldReleaseTimelineAnchorForToolActivity({
142+
anchorMessageId,
143+
liveFollowEnabled: true,
144+
runningTurnId: activeTurnId,
145+
timelineEntries: [
146+
{
147+
...activeToolEntry,
148+
entry: {
149+
id: "thinking-entry",
150+
createdAt: now,
151+
turnId: activeTurnId,
152+
label: "Thinking",
153+
tone: "thinking",
154+
},
155+
},
156+
{
157+
...activeToolEntry,
158+
id: "error-entry",
159+
entry: {
160+
id: "error-entry",
161+
createdAt: now,
162+
turnId: activeTurnId,
163+
label: "Provider error",
164+
tone: "error",
165+
},
166+
},
167+
],
168+
}),
169+
).toBe(false);
170+
});
171+
172+
it("does nothing without an anchor or running turn", () => {
173+
const input = {
174+
anchorMessageId,
175+
liveFollowEnabled: true,
176+
runningTurnId: activeTurnId,
177+
timelineEntries: [activeToolEntry],
178+
};
179+
180+
expect(shouldReleaseTimelineAnchorForToolActivity({ ...input, anchorMessageId: null })).toBe(
181+
false,
182+
);
183+
expect(shouldReleaseTimelineAnchorForToolActivity({ ...input, runningTurnId: null })).toBe(
184+
false,
185+
);
186+
});
187+
});
188+
80189
describe("environment reconnect warning grace", () => {
81190
afterEach(() => vi.useRealTimers());
82191

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
type EnvironmentId,
33
isProviderDriverKind,
44
ProjectId,
5+
type MessageId,
56
type ModelSelection,
67
type ProviderDriverKind,
78
type ServerProvider,
@@ -22,6 +23,7 @@ import {
2223
} from "../lib/terminalContext";
2324
import type { DraftThreadEnvMode } from "../composerDraftStore";
2425
import type { ComposerSubmissionIntent } from "../composer-logic";
26+
import type { TimelineEntry } from "../session-logic";
2527

2628
export const LAST_INVOKED_SCRIPT_BY_PROJECT_KEY = "t3code:last-invoked-script-by-project";
2729
export const MAX_HIDDEN_MOUNTED_TERMINAL_THREADS = 10;
@@ -42,6 +44,31 @@ export function shouldDockDraftHeroForSubmission(input: {
4244
);
4345
}
4446

47+
export function shouldReleaseTimelineAnchorForToolActivity(input: {
48+
anchorMessageId: MessageId | null;
49+
liveFollowEnabled: boolean;
50+
runningTurnId: TurnId | null;
51+
timelineEntries: ReadonlyArray<TimelineEntry>;
52+
}): boolean {
53+
if (input.anchorMessageId === null || !input.liveFollowEnabled || input.runningTurnId === null) {
54+
return false;
55+
}
56+
57+
return input.timelineEntries.some((timelineEntry) => {
58+
if (timelineEntry.kind !== "work" || timelineEntry.entry.turnId !== input.runningTurnId) {
59+
return false;
60+
}
61+
62+
const entry = timelineEntry.entry;
63+
return (
64+
entry.tone === "tool" ||
65+
entry.itemType !== undefined ||
66+
entry.requestKind !== undefined ||
67+
(entry.command?.trim().length ?? 0) > 0
68+
);
69+
});
70+
}
71+
4572
export function resolveDraftHeroState(input: {
4673
isLocalDraftThread: boolean;
4774
hasTimelineEntries: boolean;

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

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ import {
328328
hasServerAcknowledgedLocalDispatch,
329329
isBranchMismatchDismissedForSession,
330330
shouldDockDraftHeroForSubmission,
331+
shouldReleaseTimelineAnchorForToolActivity,
331332
shouldShowBranchMismatchBanner,
332333
getStartedThreadModelChangeBlockReason,
333334
LAST_INVOKED_SCRIPT_BY_PROJECT_KEY,
@@ -1756,6 +1757,12 @@ function ChatViewContent(props: ChatViewProps) {
17561757
return openTerminalThreadKeys.filter((nextThreadKey) => existingThreadKeys.has(nextThreadKey));
17571758
}, [draftThreadKeys, openTerminalThreadKeys, serverThreadKeys]);
17581759
const activeLatestTurn = activeThread?.latestTurn ?? null;
1760+
const activeRunningTurnId =
1761+
activeThread?.session?.status === "running"
1762+
? activeThread.session.activeTurnId
1763+
: activeLatestTurn?.state === "running"
1764+
? activeLatestTurn.turnId
1765+
: null;
17591766
// Reading a finished thread clears the sidebar's Done badge. The visit is
17601767
// stamped at the turn's completion time — not now/updatedAt — so it clears
17611768
// exactly the completion the user is looking at: a wake or completion that
@@ -3895,6 +3902,8 @@ function ChatViewContent(props: ChatViewProps) {
38953902
liveFollowUserScrollGenerationRef.current = anchorUserScrollGenerationRef.current;
38963903
setTimelineLiveFollowEnabled(true);
38973904
pendingTimelineAnchorRef.current = null;
3905+
positionedTimelineAnchorRef.current = null;
3906+
settledTimelineAnchorRef.current = null;
38983907
activeTimelineAnchorIndexRef.current = null;
38993908
showScrollDebouncer.current.cancel();
39003909
setShowScrollToBottom(false);
@@ -3903,6 +3912,28 @@ function ChatViewContent(props: ChatViewProps) {
39033912
void legendListRef.current?.scrollToEnd?.({ animated });
39043913
});
39053914
}, []);
3915+
useLayoutEffect(() => {
3916+
if (timelineScrollModeRef.current !== "anchoring-new-turn") {
3917+
return;
3918+
}
3919+
3920+
if (
3921+
shouldReleaseTimelineAnchorForToolActivity({
3922+
anchorMessageId: timelineAnchorMessageId,
3923+
liveFollowEnabled: timelineLiveFollowEnabled,
3924+
runningTurnId: activeRunningTurnId,
3925+
timelineEntries,
3926+
})
3927+
) {
3928+
scrollToEnd();
3929+
}
3930+
}, [
3931+
activeRunningTurnId,
3932+
scrollToEnd,
3933+
timelineAnchorMessageId,
3934+
timelineEntries,
3935+
timelineLiveFollowEnabled,
3936+
]);
39063937
useEffect(() => {
39073938
let removeListeners: (() => void) | null = null;
39083939
let frame: number | null = null;
@@ -6640,11 +6671,7 @@ function ChatViewContent(props: ChatViewProps) {
66406671
listRef={legendListRef}
66416672
timelineEntries={timelineEntries}
66426673
latestTurn={activeLatestTurn}
6643-
runningTurnId={
6644-
activeThread.session?.status === "running"
6645-
? activeThread.session.activeTurnId
6646-
: null
6647-
}
6674+
runningTurnId={activeRunningTurnId}
66486675
turnDiffSummaryByAssistantMessageId={turnDiffSummaryByAssistantMessageId}
66496676
activeThreadEnvironmentId={activeThread.environmentId}
66506677
routeThreadKey={routeThreadKey}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ describe("MessagesTimeline", () => {
558558
expect(onAnchorReady).not.toHaveBeenCalled();
559559
});
560560

561-
it("does not reserve a blank viewport after the active turn starts tool work", () => {
561+
it("keeps reserved end space when tool work starts while reading history", () => {
562562
const turnId = TurnId.make("turn-with-active-tool");
563563
const firstEntry = buildUserTimelineEntry("Run the command.");
564564
const markup = renderToStaticMarkup(
@@ -574,6 +574,7 @@ describe("MessagesTimeline", () => {
574574
}}
575575
runningTurnId={turnId}
576576
anchorMessageId={firstEntry.message.id}
577+
liveFollowEnabled={false}
577578
timelineEntries={[
578579
firstEntry,
579580
{
@@ -596,8 +597,8 @@ describe("MessagesTimeline", () => {
596597
/>,
597598
);
598599

599-
expect(markup).not.toContain("data-anchor-index=");
600-
expect(markup).toContain('data-maintain-scroll-at-end="enabled"');
600+
expect(markup).toContain('data-anchor-index="0"');
601+
expect(markup).not.toContain('data-maintain-scroll-at-end="enabled"');
601602
});
602603

603604
it("hands end-following back to the list once the send anchor is released", () => {

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export const MessagesTimeline = memo(function MessagesTimeline({
271271
timestampFormat,
272272
workspaceRoot,
273273
skills = EMPTY_TIMELINE_SKILLS,
274-
anchorMessageId: requestedAnchorMessageId,
274+
anchorMessageId,
275275
onAnchorReady,
276276
contentInsetEndAdjustment,
277277
liveFollowEnabled,
@@ -281,14 +281,6 @@ export const MessagesTimeline = memo(function MessagesTimeline({
281281
topFadeEnabled = false,
282282
loadEarlier = null,
283283
}: MessagesTimelineProps) {
284-
const anchorMessageId = useMemo(
285-
() =>
286-
requestedAnchorMessageId !== null &&
287-
timelineEntries.some((entry) => entry.kind === "work" && workLogEntryIsToolLike(entry.entry))
288-
? null
289-
: requestedAnchorMessageId,
290-
[requestedAnchorMessageId, timelineEntries],
291-
);
292284
const [expandedTurnIds, setExpandedTurnIds] = useState<ReadonlySet<TurnId>>(new Set());
293285
const [expandedWorkGroupIds, setExpandedWorkGroupIds] = useState<ReadonlySet<string>>(new Set());
294286
const [disclosureToggleSettling, setDisclosureToggleSettling] = useState(false);

0 commit comments

Comments
 (0)