Skip to content

Commit 77eeaa7

Browse files
committed
fix(web): reasoning trace follows live stream to the bottom
The expanded reasoning trace (WorkLogDetails, max-h-96 overflow-auto) never pinned to the bottom as entry.detail grew, so live reasoning streamed out of view. WorkLogDetails now accepts followLive and pins the scroller to the end while content grows, until the reader scrolls away from the end (same hold behavior as the chat list). ReasoningTraceContent enables it while any entry is inProgress on the latest run. Fixes #16201
1 parent 54b6b66 commit 77eeaa7

3 files changed

Lines changed: 128 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4944,8 +4944,11 @@ function remarkThoughtPreview(fallback: string) {
49444944
function ReasoningTraceContent({ entries }: { entries: ReadonlyArray<TimelineWorkEntry> }) {
49454945
const ctx = use(TimelineRowCtx);
49464946
const { isWorking, latestRunId } = use(TimelineRowActivityCtx);
4947+
const streaming = isWorking && entries.some(
4948+
(entry) => entry.runId === latestRunId && entry.toolLifecycleStatus === "inProgress",
4949+
);
49474950
return (
4948-
<WorkLogDetails>
4951+
<WorkLogDetails followLive={streaming}>
49494952
{entries.map((entry) => (
49504953
<ChatMarkdown
49514954
key={entry.id}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { act } from "react";
2+
import { create, type ReactTestRenderer } from "react-test-renderer";
3+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4+
5+
import { WorkLogDetails } from "./WorkLog";
6+
7+
const VIEWPORT = 384;
8+
9+
// Stand-in for the measured scroll geometry; react-test-renderer has no layout.
10+
const mockEl = {
11+
scrollHeight: 1000,
12+
clientHeight: VIEWPORT,
13+
scrollTop: 0,
14+
};
15+
16+
let renderer: ReactTestRenderer;
17+
let followLive = true;
18+
19+
function scroller() {
20+
return renderer.root.findByType("div");
21+
}
22+
23+
function mount() {
24+
mockEl.scrollHeight = 1000;
25+
mockEl.scrollTop = 0;
26+
act(() => {
27+
renderer = create(
28+
<WorkLogDetails followLive={followLive}>
29+
<p>reasoning</p>
30+
</WorkLogDetails>,
31+
{ createNodeMock: () => mockEl },
32+
);
33+
});
34+
}
35+
36+
function streamMore() {
37+
mockEl.scrollHeight += 500;
38+
act(() => {
39+
renderer.update(
40+
<WorkLogDetails followLive={followLive}>
41+
<p>{"reasoning\n".repeat(100)}</p>
42+
</WorkLogDetails>,
43+
);
44+
});
45+
}
46+
47+
function userScrollsTo(top: number) {
48+
mockEl.scrollTop = top;
49+
act(() => {
50+
scroller().props.onScroll({ currentTarget: mockEl });
51+
});
52+
}
53+
54+
beforeEach(() => {
55+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
56+
followLive = true;
57+
});
58+
59+
afterEach(() => {
60+
act(() => renderer?.unmount());
61+
vi.unstubAllGlobals();
62+
});
63+
64+
describe("WorkLogDetails followLive", () => {
65+
it("pins the scroller to the end as live content grows", () => {
66+
mount();
67+
streamMore();
68+
expect(mockEl.scrollTop).toBe(mockEl.scrollHeight);
69+
streamMore();
70+
expect(mockEl.scrollTop).toBe(mockEl.scrollHeight);
71+
});
72+
73+
it("holds position once the reader scrolls away from the end", () => {
74+
mount();
75+
streamMore();
76+
expect(mockEl.scrollTop).toBe(mockEl.scrollHeight);
77+
78+
userScrollsTo(100);
79+
streamMore();
80+
expect(mockEl.scrollTop).toBe(100);
81+
});
82+
83+
it("re-pins after the reader scrolls back to the end", () => {
84+
mount();
85+
userScrollsTo(100);
86+
streamMore();
87+
expect(mockEl.scrollTop).toBe(100);
88+
89+
userScrollsTo(mockEl.scrollHeight - VIEWPORT);
90+
streamMore();
91+
expect(mockEl.scrollTop).toBe(mockEl.scrollHeight);
92+
});
93+
94+
it("does not move the scroller when followLive is off", () => {
95+
followLive = false;
96+
mount();
97+
streamMore();
98+
expect(mockEl.scrollTop).toBe(0);
99+
});
100+
});

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createContext, use, type ComponentProps, type ReactNode } from "react";
1+
import { createContext, use, useLayoutEffect, useRef, type ComponentProps, type ReactNode } from "react";
22
import { cn } from "../../lib/utils";
33

44
const GroupedRows = createContext(false);
@@ -109,15 +109,38 @@ export function WorkLogRow({
109109
);
110110
}
111111

112+
/** Within this many px of the end, the reader counts as following live content. */
113+
const FOLLOW_EDGE_PX = 4;
114+
112115
export function WorkLogDetails({
113116
children,
114117
kind = "text",
118+
followLive = false,
115119
}: {
116120
children: ReactNode;
117121
kind?: "text" | "panel" | "media";
122+
/** Pin the scroller to the end while live content grows, until the reader scrolls away. */
123+
followLive?: boolean;
118124
}) {
125+
const scrollerRef = useRef<HTMLDivElement | null>(null);
126+
const pinnedRef = useRef(true);
127+
128+
// Runs after every render so newly streamed content re-pins before paint.
129+
useLayoutEffect(() => {
130+
if (!followLive || !pinnedRef.current) return;
131+
const el = scrollerRef.current;
132+
if (!el) return;
133+
const end = el.scrollHeight - el.clientHeight;
134+
if (el.scrollTop < end) el.scrollTop = el.scrollHeight;
135+
});
136+
119137
return (
120138
<div
139+
ref={scrollerRef}
140+
onScroll={(event) => {
141+
const el = event.currentTarget;
142+
pinnedRef.current = el.scrollHeight - el.scrollTop - el.clientHeight <= FOLLOW_EDGE_PX;
143+
}}
121144
className={cn(
122145
"cursor-auto",
123146
kind === "text"

0 commit comments

Comments
 (0)