The best way to control Claude Code, Codex and more from any computer or phone
perf(client): streamed text no longer republishes thread queue and checkpoints (#27425)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 11, 2026 at 05:14 UTC
ad0b2a9d2f0e4ab6384c9ce601e7f2e755ab5117
2 files changed
+171
-2
packages/client-runtime/src/state/threadDetail.test.ts
+140
@@ -1,9 +1,16 @@
1
import {
2
+ CheckpointId,
3
+ CheckpointRef,
4
+ CheckpointScopeId,
5
EnvironmentId,
6
EventId,
7
MessageId,
8
+ NodeId,
9
RunId,
10
TurnItemId,
11
+ type OrchestrationV2Checkpoint,
12
+ type OrchestrationV2ConversationMessage,
13
+ type OrchestrationV2Run,
14
type OrchestrationV2ThreadProjection,
15
type OrchestrationV2TurnItem,
16
} from "@t3tools/contracts";
@@ -279,6 +286,139 @@ it("only compares replaced entries in a streamed reducer delta", () => {
286
expect(slotReads).toBeLessThanOrEqual(2);
287
});
288
289
+describe("derived thread state through a streamed message", () => {
290
+ const ref = { environmentId: EnvironmentId.make("environment-1"), threadId: v2ThreadId };
291
+ const runId = RunId.make("run-1");
292
+ const streamed: OrchestrationV2ConversationMessage = {
293
+ id: MessageId.make("message-assistant"),
294
+ threadId: v2ThreadId,
295
+ runId,
296
+ nodeId: null,
297
+ role: "assistant",
298
+ text: "Hel",
299
+ attachments: [],
300
+ streaming: true,
301
+ createdBy: "agent",
302
+ creationSource: "provider",
303
+ createdAt: v2Now,
304
+ updatedAt: v2Now,
305
+ };
306
+ const queuedMessage: OrchestrationV2ConversationMessage = {
307
+ ...streamed,
308
+ id: MessageId.make("message-queued"),
309
+ runId: RunId.make("run-2"),
310
+ role: "user",
311
+ text: "Then run the tests",
312
+ streaming: false,
313
+ createdBy: "user",
314
+ creationSource: "mobile",
315
+ };
316
+ const queuedRun: OrchestrationV2Run = {
317
+ id: RunId.make("run-2"),
318
+ threadId: v2ThreadId,
319
+ ordinal: 2,
320
+ providerInstanceId: v2Projection.thread.providerInstanceId,
321
+ modelSelection: v2Projection.thread.modelSelection,
322
+ providerThreadId: null,
323
+ userMessageId: queuedMessage.id,
324
+ rootNodeId: null,
325
+ activeAttemptId: null,
326
+ status: "queued",
327
+ requestedAt: v2Now,
328
+ startedAt: null,
329
+ completedAt: null,
330
+ checkpointId: null,
331
+ contextHandoffId: null,
332
+ };
333
+ const checkpoint = (ordinal: number): OrchestrationV2Checkpoint => ({
334
+ id: CheckpointId.make(`checkpoint-${ordinal}`),
335
+ threadId: v2ThreadId,
336
+ scopeId: CheckpointScopeId.make("scope"),
337
+ runId,
338
+ nodeId: NodeId.make("node"),
339
+ parentCheckpointId: null,
340
+ ordinalWithinScope: ordinal,
341
+ appRunOrdinal: ordinal,
342
+ ref: CheckpointRef.make(`refs/t3/checkpoint-${ordinal}`),
343
+ status: "ready",
344
+ files: [],
345
+ capturedAt: v2Now,
346
+ });
347
+ const initial: OrchestrationV2ThreadProjection = {
348
+ ...v2Projection,
349
+ runs: [queuedRun],
350
+ messages: [streamed, queuedMessage],
351
+ checkpoints: [checkpoint(1)],
352
+ };
353
+
354
+ function setup() {
355
+ const stateAtom = Atom.make(
356
+ AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(initial) }),
357
+ );
358
+ const details = createEnvironmentThreadDetailAtoms(() => stateAtom);
359
+ const queue = details.queueWorkflowAtom(ref);
360
+ const checkpoints = details.checkpointSummariesAtom(ref);
361
+ const registry = AtomRegistry.make();
362
+ registry.mount(queue);
363
+ registry.mount(checkpoints);
364
+ let projection = initial;
365
+ return {
366
+ queue: () => registry.get(queue),
367
+ checkpoints: () => registry.get(checkpoints),
368
+ apply: (event: Parameters<typeof applyOrchestrationV2ProjectionEvent>[1]) => {
369
+ projection = applyOrchestrationV2ProjectionEvent(projection, event)!;
370
+ registry.set(
371
+ stateAtom,
372
+ AsyncResult.success({ ...EMPTY_ENVIRONMENT_THREAD_STATE, data: Option.some(projection) }),
373
+ );
374
+ },
375
+ [Symbol.dispose]: () => registry.dispose(),
376
+ };
377
+ }
378
+
379
+ const messageUpdated = (message: OrchestrationV2ConversationMessage) => ({
380
+ id: EventId.make(`event-${message.id}-${message.text.length}`),
381
+ type: "message.updated" as const,
382
+ threadId: v2ThreadId,
383
+ occurredAt: later,
384
+ payload: message,
385
+ });
386
+
387
+ it("keeps the queue workflow through streamed text and follows a queued message edit", () => {
388
+ using thread = setup();
389
+ const first = thread.queue();
390
+ thread.apply(messageUpdated({ ...streamed, text: "Hello", updatedAt: later }));
391
+ thread.apply(messageUpdated({ ...streamed, text: "Hello there", updatedAt: later }));
392
+ expect(thread.queue()).toBe(first);
393
+
394
+ thread.apply(messageUpdated({ ...queuedMessage, text: "Then run lint", updatedAt: later }));
395
+ expect(thread.queue()?.queuedRuns.map((queued) => queued.text)).toEqual(["Then run lint"]);
396
+ });
397
+
398
+ it("keeps checkpoint summaries through streamed text and follows new assistant messages and checkpoints", () => {
399
+ using thread = setup();
400
+ const first = thread.checkpoints();
401
+ thread.apply(messageUpdated({ ...streamed, text: "Hello", updatedAt: later }));
402
+ thread.apply(messageUpdated({ ...streamed, text: "Hello there", updatedAt: later }));
403
+ expect(thread.checkpoints()).toBe(first);
404
+
405
+ const followUp = { ...streamed, id: MessageId.make("message-follow-up"), text: "Done" };
406
+ thread.apply(messageUpdated(followUp));
407
+ expect(thread.checkpoints().map((summary) => summary.assistantMessageId)).toEqual([
408
+ followUp.id,
409
+ ]);
410
+
411
+ thread.apply({
412
+ id: EventId.make("checkpoint-2"),
413
+ type: "checkpoint.captured",
414
+ threadId: v2ThreadId,
415
+ occurredAt: later,
416
+ payload: checkpoint(2),
417
+ });
418
+ expect(thread.checkpoints().map((summary) => summary.checkpointTurnCount)).toEqual([1, 2]);
419
+ });
420
+});
421
+
422
function replaceItem(projection: OrchestrationV2ThreadProjection, item: OrchestrationV2TurnItem) {
423
return applyOrchestrationV2ProjectionEvent(projection, {
424
id: EventId.make(`event-${item.id}`),
packages/client-runtime/src/state/threadDetail.ts
+31
-2
@@ -90,6 +90,26 @@ type AppThread = OrchestrationV2ThreadProjection["thread"];
90
const structureTurnItems = new WeakMap<TurnItem, ThreadStructureTurnItem>();
91
const structureVisibleTurnItems = new WeakMap<VisibleTurnItem, ThreadStructureVisibleTurnItem>();
92
93
+// Queued text, attachments and attribution all come from the message, so its identity covers them.
94
+function queueWorkflowStatesEqual(
95
+ left: ThreadQueueWorkflowState,
96
+ right: ThreadQueueWorkflowState,
97
+): boolean {
98
+ return (
99
+ left.activeRun === right.activeRun &&
100
+ left.isHeld === right.isHeld &&
101
+ left.canSendNow === right.canSendNow &&
102
+ left.canPromoteToSteer === right.canPromoteToSteer &&
103
+ left.promoteSkipsCompaction === right.promoteSkipsCompaction &&
104
+ left.queuedRuns.length === right.queuedRuns.length &&
105
+ left.queuedRuns.every(
106
+ (queued, index) =>
107
+ queued.run === right.queuedRuns[index]!.run &&
108
+ queued.message === right.queuedRuns[index]!.message,
109
+ )
110
+ );
111
+}
112
+
113
function toThreadStructureTurnItem(item: TurnItem): ThreadStructureTurnItem {
114
if (item.type !== "assistant_message" && item.type !== "reasoning") return item;
115
let structure = structureTurnItems.get(item);
@@ -397,7 +417,8 @@ export function createEnvironmentThreadDetailAtoms<E>(
417
projection.providerSessions !== previous.providerSessions ||
418
projection.providerTurns !== previous.providerTurns
419
) {
400
- value = deriveThreadQueueWorkflowState(projection);
420
+ const next = deriveThreadQueueWorkflowState(projection);
421
+ if (value === null || !queueWorkflowStatesEqual(value, next)) value = next;
422
const { thread, runs, messages, providerThreads, providerSessions, providerTurns } =
423
projection;
424
previous = { thread, runs, messages, providerThreads, providerSessions, providerTurns };
@@ -481,8 +502,16 @@ export function createEnvironmentThreadDetailAtoms<E>(
502
projection.messages !== previous?.messages
503
) {
504
const { checkpoints, messages } = projection;
505
+ const next = deriveThreadCheckpointSummaries({ checkpoints, messages });
506
+ // The same checkpoints give the same summaries; messages only pick each assistant message.
507
+ if (
508
+ checkpoints !== previous?.checkpoints ||
509
+ next.some(
510
+ (summary, index) => summary.assistantMessageId !== value[index]?.assistantMessageId,
511
+ )
512
+ )
513
+ value = next;
514
previous = { checkpoints, messages };
485
- value = deriveThreadCheckpointSummaries(previous);
515
}
516
return value;
517
}).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-checkpoint-summaries:${key}`));