The best way to control Claude Code, Codex and more from any computer or phone
perf(web): streamed assistant text re-renders only its own row (#27419)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 30, 2026 at 02:10 UTC
3cbc98241d33f202b78c1ebe7e5b8f35f5d6ac29
6 files changed
+127
-6
apps/web/src/components/chat/ThreadTimeline.tsx
+4
-3
@@ -2,7 +2,7 @@ import type { ScopedThreadRef } from "@t3tools/contracts";
2
import { type ComponentProps, useLayoutEffect } from "react";
3
import { useIncrementalTimelineEntries } from "../../hooks/useIncrementalTimelineEntries";
4
import type { TimelineEntriesInput, TimelineEntry } from "../../session-logic";
5
-import { useThreadVisibleTurnItems } from "../../state/entities";
5
+import { useTimelineVisibleTurnItems } from "../../state/entities";
6
import {
7
peekRememberedThreadTimeline,
8
rememberReadyThreadTimeline,
@@ -14,7 +14,8 @@ import { MessagesTimeline } from "./MessagesTimeline";
14
/**
15
* The timeline with the streamed text. ChatView reads the thread through its quiet
16
* atoms, which hold through text deltas; this reads the live turn items, so a
17
- * delta re-renders the timeline without re-rendering ChatView.
17
+ * reasoning delta re-renders the timeline without re-rendering ChatView. Streamed
18
+ * assistant text reaches only its own row, through useAssistantMessageText.
19
*/
20
export function ThreadTimeline({
21
threadDetailRef,
@@ -34,7 +35,7 @@ export function ThreadTimeline({
35
syncing: boolean;
36
detailLoading: boolean;
37
}) {
37
- const visibleTurnItems = useThreadVisibleTurnItems(threadDetailRef);
38
+ const visibleTurnItems = useTimelineVisibleTurnItems(threadDetailRef);
39
const serverEntries = useIncrementalTimelineEntries(activeThreadKey, {
40
...entriesInput,
41
visibleTurnItems,
apps/web/src/components/chat/TimelineAssistantRows.tsx
+8
-2
@@ -12,7 +12,7 @@ import { McpAppFrame } from "./McpAppFrame";
12
import { checkpointFileTotals } from "@t3tools/client-runtime/state/thread-checkpoints";
13
import { ChangedFilesCard } from "./ChangedFilesTree";
14
import { useFileContextMenuHandler } from "../../fileContextMenu";
15
-import { useProject, useThreadShell } from "../../state/entities";
15
+import { useAssistantMessageText, useProject, useThreadShell } from "../../state/entities";
16
import { MessageCopyButton } from "./MessageCopyButton";
17
import { AssistantCitationSource } from "./AssistantCitationSource";
18
import { resolveAssistantMessageCopyState } from "./MessagesTimeline.logic";
@@ -26,7 +26,13 @@ import { MESSAGE_HEADING_LEVEL, MessageAuthorHeading } from "./TimelineRowParts"
26
27
export function AssistantTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
28
const ctx = use(TimelineRowCtx);
29
- const messageText = row.message.text || (row.message.streaming ? "" : "(empty response)");
29
+ // The timeline holds a streaming message's row while only its text grows.
30
+ const liveText = useAssistantMessageText(
31
+ row.message.streaming ? ctx.threadRef : null,
32
+ row.projectedItem?.item.id,
33
+ );
34
+ const text = liveText ?? row.message.text;
35
+ const messageText = text || (row.message.streaming ? "" : "(empty response)");
36
37
return (
38
<>
apps/web/src/state/entities.ts
+26
@@ -65,6 +65,9 @@ const EMPTY_VISIBLE_TURN_ITEMS_ATOM = Atom.make(EMPTY_VISIBLE_TURN_ITEMS).pipe(
65
const EMPTY_CHECKPOINT_SUMMARIES_ATOM = Atom.make<ReadonlyArray<ThreadCheckpointSummary>>(
66
Object.freeze([]),
67
).pipe(Atom.withLabel("web-thread-checkpoint-summaries:empty"));
68
+const EMPTY_ASSISTANT_MESSAGE_TEXT_ATOM = Atom.make<string | null>(null).pipe(
69
+ Atom.withLabel("web-assistant-message-text:empty"),
70
+);
71
const EMPTY_LAST_VISITED_AT_ATOM = Atom.make<DateTime.Utc | null>(null).pipe(
72
Atom.withLabel("web-thread-last-visited:empty"),
73
);
@@ -212,6 +215,29 @@ export function useThreadVisibleTurnItems(
215
);
216
}
217
218
+/** Visible turn items held through streamed assistant text; see useAssistantMessageText. */
219
+export function useTimelineVisibleTurnItems(
220
+ ref: ScopedThreadRef | null,
221
+): ReadonlyArray<OrchestrationV2ProjectedTurnItem> {
222
+ return useAtomValue(
223
+ ref === null
224
+ ? EMPTY_VISIBLE_TURN_ITEMS_ATOM
225
+ : environmentThreadDetails.timelineVisibleTurnItemsAtom(ref),
226
+ );
227
+}
228
+
229
+/** One assistant message's live text, or null when the thread does not hold the item. */
230
+export function useAssistantMessageText(
231
+ ref: ScopedThreadRef | null,
232
+ itemId: string | undefined,
233
+): string | null {
234
+ return useAtomValue(
235
+ ref === null || itemId === undefined
236
+ ? EMPTY_ASSISTANT_MESSAGE_TEXT_ATOM
237
+ : environmentThreadDetails.assistantMessageTextAtom(ref, itemId),
238
+ );
239
+}
240
+
241
/** Visible turn items without their streamed text; see ThreadStructure. */
242
export function useQuietThreadVisibleTurnItems(
243
ref: ScopedThreadRef | null,
apps/web/test/appScenarios.renders.tsx
+3
-1
@@ -201,7 +201,9 @@ it("app-stream-assistant-turn", async () => {
201
.element(page.getByText(`word${STREAM_DELTAS - 1}`, { exact: false }))
202
.toBeVisible();
203
});
204
- // Only the streaming row changes; history rows must not re-render with it.
204
+ // Only the streaming row changes. The timeline renders for the new item and its layout, not
205
+ // per delta, and history rows not at all.
206
+ expect(counted.components.ConversationTimeline).toBeLessThanOrEqual(3);
207
expect(counted.components.UserTimelineRow).toBeUndefined();
208
expect(counted.components.AssistantTimelineRow).toBe(STREAM_DELTAS);
209
// Each delta adds a paragraph; finished blocks keep their render.
packages/client-runtime/src/state/threadDetail.test.ts
+36
@@ -197,6 +197,42 @@ describe("quietThreadAtom", () => {
197
});
198
});
199
200
+describe("timelineVisibleTurnItemsAtom", () => {
201
+ const ref = { environmentId: EnvironmentId.make("environment-1"), threadId: v2ThreadId };
202
+
203
+ it("holds through streamed assistant text while the message reads its own text", () => {
204
+ const stateAtom = Atom.make(
205
+ AsyncResult.success({
206
+ ...EMPTY_ENVIRONMENT_THREAD_STATE,
207
+ data: Option.some<OrchestrationV2ThreadProjection>(withItem(assistant)),
208
+ }),
209
+ );
210
+ const details = createEnvironmentThreadDetailAtoms(() => stateAtom);
211
+ const timelineAtom = details.timelineVisibleTurnItemsAtom(ref);
212
+ const textAtom = details.assistantMessageTextAtom(ref, assistant.id);
213
+ const registry = AtomRegistry.make();
214
+ registry.mount(timelineAtom);
215
+ registry.mount(textAtom);
216
+ const push = (item: OrchestrationV2TurnItem) =>
217
+ registry.set(
218
+ stateAtom,
219
+ AsyncResult.success({
220
+ ...EMPTY_ENVIRONMENT_THREAD_STATE,
221
+ data: Option.some(withItem(item, later)),
222
+ }),
223
+ );
224
+ const first = registry.get(timelineAtom);
225
+
226
+ push({ ...assistant, text: "Hello", updatedAt: later });
227
+ expect(registry.get(timelineAtom)).toBe(first);
228
+ expect(registry.get(textAtom)).toBe("Hello");
229
+
230
+ push({ ...assistant, text: "Hello", streaming: false, status: "completed" });
231
+ expect(registry.get(timelineAtom)[0]?.item).toMatchObject({ text: "Hello", streaming: false });
232
+ registry.dispose();
233
+ });
234
+});
235
+
236
it("only compares replaced entries in a streamed reducer delta", () => {
237
const size = 100;
238
let slotReads = 0;
packages/client-runtime/src/state/threadDetail.ts
+50
@@ -148,6 +148,26 @@ function sameVisibleTurnItemStructure(before: VisibleTurnItem, after: VisibleTur
148
);
149
}
150
151
+function isStreamingAssistantMessage(visibleItem: VisibleTurnItem): boolean {
152
+ return visibleItem.item.type === "assistant_message" && visibleItem.item.streaming;
153
+}
154
+
155
+/** Whether `next` only streams more text into assistant messages that were already streaming. */
156
+function sameApartFromStreamedAssistantText(
157
+ previous: OrchestrationV2ThreadProjection["visibleTurnItems"],
158
+ next: OrchestrationV2ThreadProjection["visibleTurnItems"],
159
+): boolean {
160
+ return sameProjectionArray(
161
+ previous,
162
+ next,
163
+ (before, after) =>
164
+ before === after ||
165
+ (isStreamingAssistantMessage(before) &&
166
+ isStreamingAssistantMessage(after) &&
167
+ sameVisibleTurnItemStructure(before, after)),
168
+ );
169
+}
170
+
171
/**
172
* Whether `previous` and `next` have the same ThreadStructure: they differ
173
* only by text streamed into existing assistant and reasoning items or by a
@@ -267,6 +287,32 @@ export function createEnvironmentThreadDetailAtoms<E>(
287
);
288
});
289
290
+ // Holds while only streamed assistant text moves, so the timeline re-renders on structure;
291
+ // each streaming message reads its text through assistantMessageTextAtom.
292
+ const timelineVisibleTurnItemsAtomFamily = Atom.family((key: string) => {
293
+ let previous = EMPTY_VISIBLE_TURN_ITEMS;
294
+ return Atom.make((get) => {
295
+ const next = get(visibleTurnItemsAtomFamily(key));
296
+ if (!sameApartFromStreamedAssistantText(previous, next)) previous = next;
297
+ return previous;
298
+ }).pipe(
299
+ Atom.setIdleTTL(0),
300
+ Atom.withLabel(`environment-thread-timeline-visible-turn-items:${key}`),
301
+ );
302
+ });
303
+
304
+ const assistantMessageTextAtomFamily = Atom.family((key: string) => {
305
+ const separator = key.lastIndexOf("\u0000");
306
+ const thread = key.slice(0, separator);
307
+ const itemId = key.slice(separator + 1);
308
+ return Atom.make((get) => {
309
+ const item = get(visibleTurnItemsAtomFamily(thread)).find(
310
+ (visibleItem) => visibleItem.item.id === itemId,
311
+ )?.item;
312
+ return item?.type === "assistant_message" ? item.text : null;
313
+ }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-assistant-message-text:${key}`));
314
+ });
315
+
316
const queueWorkflowAtomFamily = Atom.family((key: string) => {
317
let previous: Pick<
318
OrchestrationV2ThreadProjection,
@@ -472,6 +518,10 @@ export function createEnvironmentThreadDetailAtoms<E>(
518
visibleTurnItemsAtom: (ref: ScopedThreadRef) => visibleTurnItemsAtomFamily(threadKey(ref)),
519
quietVisibleTurnItemsAtom: (ref: ScopedThreadRef) =>
520
quietVisibleTurnItemsAtomFamily(threadKey(ref)),
521
+ timelineVisibleTurnItemsAtom: (ref: ScopedThreadRef) =>
522
+ timelineVisibleTurnItemsAtomFamily(threadKey(ref)),
523
+ assistantMessageTextAtom: (ref: ScopedThreadRef, itemId: string) =>
524
+ assistantMessageTextAtomFamily(`${threadKey(ref)}\u0000${itemId}`),
525
statusAtom: (ref: ScopedThreadRef) => statusAtomFamily(threadKey(ref)),
526
sequenceAtom: (ref: ScopedThreadRef) => sequenceAtomFamily(threadKey(ref)),
527
errorAtom: (ref: ScopedThreadRef) => errorAtomFamily(threadKey(ref)),