@devries-cameron20 / t3code Mirror · read-only / commits / e19fe0837b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 18:15 UTC

perf(server): thread opens and live events send each message body once (#24647)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Oct 24, 2026 at 19:15 UTC e19fe0837b6eaba1c61a779dac335be281a93ded
16 files changed +943 -29
apps/server/src/orchestration-v2/ThreadStream.ts
+10 -4
@@ -4,6 +4,7 @@ import type {
4 } from "@t3tools/contracts";
5
6 import { omitLocalVisibleTurnItems } from "@t3tools/shared/orchestrationV2BoundedSnapshot";
7 +import { dedupeSnapshotBodies } from "@t3tools/shared/orchestrationV2DedupedBodies";
8
9 import {
10 buildBoundedThreadProjection,
@@ -42,13 +43,16 @@ type ThreadSnapshotStreamItem = Extract<
43 export function boundedSnapshotResponseFields(input: {
44 readonly bounded: BoundedProjectionResult;
45 readonly compactTurnItems: boolean;
46 + readonly dedupeBodies: boolean;
47 }) {
48 const { bounded } = input;
47 - const compactProjection = input.compactTurnItems
48 - ? omitLocalVisibleTurnItems(bounded.projection)
49 - : null;
49 + // Dedupe keeps shared items one object, which the compaction's identity check needs.
50 + const projection = input.dedupeBodies
51 + ? dedupeSnapshotBodies(bounded.projection)
52 + : bounded.projection;
53 + const compactProjection = input.compactTurnItems ? omitLocalVisibleTurnItems(projection) : null;
54 return {
51 - projection: compactProjection ?? bounded.projection,
55 + projection: compactProjection ?? projection,
56 historyCursor: bounded.historyCursor,
57 hasMoreHistory: bounded.hasMoreHistory,
58 latestLocalTurnOrdinal: bounded.latestLocalTurnOrdinal,
@@ -60,6 +64,7 @@ export function buildBoundedThreadStreamSnapshot(input: {
64 readonly snapshotSequence: number;
65 readonly projection: OrchestrationV2ThreadProjection;
66 readonly compactTurnItems?: boolean;
67 + readonly dedupeBodies?: boolean;
68 }): ThreadSnapshotStreamItem {
69 const bounded = buildBoundedThreadProjection({
70 snapshotSequence: input.snapshotSequence,
@@ -71,6 +76,7 @@ export function buildBoundedThreadStreamSnapshot(input: {
76 ...boundedSnapshotResponseFields({
77 bounded,
78 compactTurnItems: input.compactTurnItems === true,
79 + dedupeBodies: input.dedupeBodies === true,
80 }),
81 };
82 }
apps/server/src/orchestration-v2/WireProjection.test.ts
+75
@@ -9,7 +9,9 @@ import {
9 ProviderInstanceId,
10 ThreadId,
11 TurnItemId,
12 + type OrchestrationV2DomainEvent,
13 type OrchestrationV2ThreadProjection,
14 + type OrchestrationV2ThreadStreamItem,
15 OrchestrationV2TurnItem,
16 OrchestrationV2TurnItemJson,
17 } from "@t3tools/contracts";
@@ -17,7 +19,9 @@ import { describe, expect, it } from "@effect/vitest";
19 import * as DateTime from "effect/DateTime";
20 import * as Schema from "effect/Schema";
21
22 +import { coalesceLiveUpdatedEvents } from "./ThreadLiveEventCoalescer.ts";
23 import {
24 + makeLiveBodyDeduper,
25 projectTurnItemForWire,
26 projectTurnItemForDetail,
27 projectDomainEventForWire,
@@ -302,3 +306,74 @@ describe("orchestration V2 wire projection", () => {
306 expect(projectDomainEventForWire(projected)).toEqual(projected);
307 });
308 });
309 +
310 +describe("live body deduper", () => {
311 + const at = base.updatedAt;
312 + const messageId = MessageId.make("message-live");
313 + const event = (
314 + sequence: number,
315 + rest: Pick<OrchestrationV2DomainEvent, "type" | "payload">,
316 + ): Extract<OrchestrationV2ThreadStreamItem, { kind: "event" }> => ({
317 + kind: "event",
318 + sequence,
319 + event: {
320 + id: EventId.make(`event-${sequence}`),
321 + threadId: base.threadId,
322 + occurredAt: at,
323 + ...rest,
324 + } as OrchestrationV2DomainEvent,
325 + });
326 + const message = (sequence: number, text: string) =>
327 + event(sequence, {
328 + type: "message.updated",
329 + payload: {
330 + id: messageId,
331 + threadId: base.threadId,
332 + runId: null,
333 + nodeId: null,
334 + role: "assistant",
335 + text,
336 + attachments: [],
337 + streaming: true,
338 + createdBy: "agent",
339 + creationSource: "provider",
340 + createdAt: at,
341 + updatedAt: at,
342 + },
343 + });
344 + const reply = (sequence: number, text: string) =>
345 + event(sequence, {
346 + type: "turn-item.updated",
347 + payload: {
348 + ...base,
349 + status: "running",
350 + type: "assistant_message",
351 + messageId,
352 + text,
353 + streaming: true,
354 + },
355 + });
356 + const payloadOf = (item: OrchestrationV2ThreadStreamItem) =>
357 + item.kind === "event" ? item.event.payload : undefined;
358 +
359 + it("blanks a reply that repeats the message delivered before it, after coalescing", () => {
360 + const delivered = coalesceLiveUpdatedEvents([
361 + message(1, "a"),
362 + reply(2, "a"),
363 + message(3, "ab"),
364 + reply(4, "ab"),
365 + ]).map(makeLiveBodyDeduper());
366 +
367 + expect(
368 + delivered.map((item) => [item.kind === "event" && item.sequence, payloadOf(item)]),
369 + ).toEqual([
370 + [3, message(3, "ab").event.payload],
371 + [4, { ...reply(4, "ab").event.payload, text: "", textOmitted: true }],
372 + ]);
373 + });
374 +
375 + it("ships a reply whole when its message arrives after it", () => {
376 + const first = reply(1, "a");
377 + expect([first, message(2, "a")].map(makeLiveBodyDeduper())[0]).toBe(first);
378 + });
379 +});
apps/server/src/orchestration-v2/WireProjection.ts
+72
@@ -1,9 +1,19 @@
1 import type {
2 + CheckpointId,
3 + MessageId,
4 + NodeId,
5 + OrchestrationV2Checkpoint,
6 OrchestrationV2DomainEvent,
7 OrchestrationV2ContextHandoff,
8 + OrchestrationV2Subagent,
9 OrchestrationV2ThreadProjection,
10 + OrchestrationV2ThreadStreamItem,
11 OrchestrationV2TurnItem,
12 } from "@t3tools/contracts";
13 +import {
14 + dedupeSubagentBodies,
15 + dedupeTurnItemBodies,
16 +} from "@t3tools/shared/orchestrationV2DedupedBodies";
17 import {
18 compactDynamicToolOutput,
19 omitToolOutputImageData,
@@ -245,3 +255,65 @@ export function projectDomainEventForWire(
255 ? { ...event, payload: projectContextHandoffForWire(event.payload) }
256 : event;
257 }
258 +
259 +const LIVE_DEDUPE_MEMORY = 32;
260 +
261 +function remember<K, V>(memory: Map<K, V>, key: K, value: V): void {
262 + memory.delete(key);
263 + memory.set(key, value);
264 + if (memory.size > LIVE_DEDUPE_MEMORY) memory.delete(memory.keys().next().value as K);
265 +}
266 +
267 +/**
268 + * One per subscription, applied after coalescing so it only remembers records the client
269 + * received, in the snapshot or in events. A record event comes before the turn item that repeats
270 + * its body (adapters and the orchestrator emit them in that order), so the item can ship blank
271 + * and marked; so can a repeated subagent record. Emitting in the other order, or forgetting a
272 + * record, only costs bytes: the repeat ships whole.
273 + */
274 +export function makeLiveBodyDeduper(): (
275 + item: OrchestrationV2ThreadStreamItem,
276 +) => OrchestrationV2ThreadStreamItem {
277 + const messageText = new Map<MessageId, string>();
278 + const checkpointFiles = new Map<CheckpointId, OrchestrationV2Checkpoint["files"]>();
279 + const subagents = new Map<NodeId, Pick<OrchestrationV2Subagent, "prompt" | "result">>();
280 + const source = {
281 + messageText: (id: MessageId) => messageText.get(id),
282 + checkpointFiles: (id: CheckpointId) => checkpointFiles.get(id),
283 + subagent: (id: NodeId) => subagents.get(id),
284 + };
285 + const rememberSubagent = ({ id, prompt, result }: OrchestrationV2Subagent) =>
286 + remember(subagents, id, { prompt, result });
287 + return (item) => {
288 + if (item.kind === "snapshot") {
289 + // The client now holds exactly these records; their bodies are never blanked.
290 + const { messages, checkpoints, subagents: records } = item.projection;
291 + for (const message of messages) remember(messageText, message.id, message.text);
292 + for (const checkpoint of checkpoints)
293 + remember(checkpointFiles, checkpoint.id, checkpoint.files);
294 + for (const subagent of records) rememberSubagent(subagent);
295 + return item;
296 + }
297 + if (item.kind !== "event") return item;
298 + const { event } = item;
299 + switch (event.type) {
300 + case "message.updated":
301 + remember(messageText, event.payload.id, event.payload.text);
302 + return item;
303 + case "checkpoint.captured":
304 + remember(checkpointFiles, event.payload.id, event.payload.files);
305 + return item;
306 + case "subagent.updated": {
307 + const payload = dedupeSubagentBodies(event.payload, subagents.get(event.payload.id));
308 + rememberSubagent(event.payload);
309 + return payload === event.payload ? item : { ...item, event: { ...event, payload } };
310 + }
311 + case "turn-item.updated": {
312 + const payload = dedupeTurnItemBodies(event.payload, source);
313 + return payload === event.payload ? item : { ...item, event: { ...event, payload } };
314 + }
315 + default:
316 + return item;
317 + }
318 + };
319 +}
apps/server/src/orchestration-v2/boundedSnapshotTransport.test.ts
+227 -1
@@ -2,6 +2,9 @@ import { assert, describe, it } from "@effect/vitest";
2 import {
3 AuthOrchestrationReadScope,
4 AuthSessionId,
5 + CheckpointId,
6 + CheckpointRef,
7 + CheckpointScopeId,
8 EnvironmentAuthenticatedAuth,
9 EnvironmentAuthenticatedPrincipal,
10 EnvironmentHttpApi,
@@ -20,6 +23,8 @@ import {
23 ThreadId,
24 TurnItemId,
25 type OrchestrationV2DomainEvent,
26 + type OrchestrationV2StoredEvent,
27 + type OrchestrationV2Subagent,
28 type OrchestrationV2ThreadProjection,
29 type OrchestrationV2TurnItem,
30 } from "@t3tools/contracts";
@@ -59,8 +64,10 @@ const projectId = ProjectId.make("project:compact-transport");
64 const PARENT = ThreadId.make("thread:compact-transport:parent");
65 const FORK = ThreadId.make("thread:compact-transport:fork");
66 const LONG = ThreadId.make("thread:compact-transport:long");
67 +const BODIES = ThreadId.make("thread:compact-transport:bodies");
68 const parentRun = RunId.make("run:compact-transport:parent");
69 const longRun = RunId.make("run:compact-transport:long");
70 +const bodiesRun = RunId.make("run:compact-transport:bodies");
71
72 const store = Layer.mergeAll(
73 ProjectionStore.layer.pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
@@ -74,7 +81,8 @@ const management = Layer.unwrap(
81 getThreadSnapshot: (id) => projections.getThreadSnapshot(id).pipe(Effect.orDie),
82 getThreadSnapshotWindow: (id, options) =>
83 projections.getThreadSnapshotWindow(id, options).pipe(Effect.orDie),
77 - streamStoredEventsFrom: () => Stream.empty,
84 + streamStoredEventsFrom: (input) =>
85 + input?.threadId === BODIES ? Stream.fromIterable(bodiesLiveTail()) : Stream.empty,
86 });
87 }),
88 );
@@ -203,6 +211,157 @@ function itemUpdated(
211 };
212 }
213
214 +// A conversation turn whose records (message, checkpoint, subagent) repeat in its turn items.
215 +function bodiesEvents(at: DateTime.Utc): OrchestrationV2DomainEvent[] {
216 + const event = (key: string, rest: Pick<OrchestrationV2DomainEvent, "type" | "payload">) =>
217 + ({
218 + id: EventId.make(`event:${BODIES}:${key}`),
219 + threadId: BODIES,
220 + runId: bodiesRun,
221 + occurredAt: at,
222 + ...rest,
223 + }) as OrchestrationV2DomainEvent;
224 + const item = (key: string, ordinal: number, fields: object) =>
225 + event(`${key}-item`, {
226 + type: "turn-item.updated",
227 + payload: {
228 + id: TurnItemId.make(`turn-item:${BODIES}:${key}`),
229 + threadId: BODIES,
230 + runId: bodiesRun,
231 + nodeId: null,
232 + providerThreadId: null,
233 + providerTurnId: null,
234 + nativeItemRef: null,
235 + parentItemId: null,
236 + ordinal,
237 + status: "completed",
238 + title: null,
239 + startedAt: at,
240 + completedAt: at,
241 + updatedAt: at,
242 + ...fields,
243 + } as OrchestrationV2TurnItem,
244 + });
245 + const message = (key: string, role: "user" | "assistant", text: string) =>
246 + event(`${key}-message`, {
247 + type: "message.updated",
248 + payload: {
249 + id: MessageId.make(`message:${key === "prompt" ? bodiesRun : `${BODIES}:${key}`}`),
250 + threadId: BODIES,
251 + runId: bodiesRun,
252 + nodeId: null,
253 + role,
254 + text,
255 + attachments: [],
256 + streaming: false,
257 + createdBy: role === "user" ? "user" : "agent",
258 + creationSource: role === "user" ? "web" : "provider",
259 + createdAt: at,
260 + updatedAt: at,
261 + },
262 + });
263 + const files = [{ path: "src/index.ts", kind: "modified", additions: 4, deletions: 2 }];
264 + return [
265 + threadCreated(BODIES, at),
266 + runCreated(BODIES, bodiesRun, at),
267 + message("prompt", "user", "Please fix the build."),
268 + item("prompt", 1, {
269 + type: "user_message",
270 + messageId: MessageId.make(`message:${bodiesRun}`),
271 + inputIntent: "turn_start",
272 + text: "Please fix the build.",
273 + attachments: [],
274 + createdBy: "user",
275 + creationSource: "web",
276 + }),
277 + event("task-record", { type: "subagent.updated", payload: bodiesSubagent(at) }),
278 + item("task", 2, {
279 + type: "subagent",
280 + subagentId: bodiesSubagent(at).id,
281 + origin: "app_owned",
282 + driver: "codex",
283 + providerInstanceId: provider,
284 + childThreadId: null,
285 + prompt: bodiesSubagent(at).prompt,
286 + result: bodiesSubagent(at).result,
287 + }),
288 + message("reply", "assistant", "Fixed the import."),
289 + item("reply", 3, {
290 + type: "assistant_message",
291 + messageId: MessageId.make(`message:${BODIES}:reply`),
292 + text: "Fixed the import.",
293 + streaming: false,
294 + }),
295 + event("checkpoint-record", {
296 + type: "checkpoint.captured",
297 + payload: {
298 + id: CheckpointId.make(`checkpoint:${BODIES}`),
299 + threadId: BODIES,
300 + scopeId: CheckpointScopeId.make(`scope:${BODIES}`),
301 + runId: bodiesRun,
302 + nodeId: NodeId.make(`node:${bodiesRun}`),
303 + parentCheckpointId: null,
304 + ordinalWithinScope: 1,
305 + appRunOrdinal: 1,
306 + ref: CheckpointRef.make(`refs/t3/${BODIES}`),
307 + status: "ready",
308 + files,
309 + capturedAt: at,
310 + },
311 + }),
312 + item("checkpoint", 4, {
313 + type: "checkpoint",
314 + checkpointId: CheckpointId.make(`checkpoint:${BODIES}`),
315 + scopeId: CheckpointScopeId.make(`scope:${BODIES}`),
316 + files,
317 + }),
318 + ];
319 +}
320 +
321 +function bodiesSubagent(at: DateTime.Utc): OrchestrationV2Subagent {
322 + return {
323 + id: NodeId.make(`node:${BODIES}:task`),
324 + threadId: BODIES,
325 + runId: bodiesRun,
326 + parentNodeId: NodeId.make(`node:${bodiesRun}`),
327 + origin: "app_owned",
328 + createdBy: "agent",
329 + driver: ProviderDriverKind.make("codex"),
330 + providerInstanceId: provider,
331 + providerThreadId: null,
332 + childThreadId: null,
333 + nativeTaskRef: null,
334 + prompt: "Find the failing import.",
335 + title: null,
336 + model: null,
337 + status: "completed",
338 + result: "It was src/index.ts.",
339 + startedAt: at,
340 + completedAt: at,
341 + updatedAt: at,
342 + };
343 +}
344 +
345 +// Live after the snapshot: a reply pair, then a subagent update that repeats its prompt.
346 +function bodiesLiveTail(): OrchestrationV2StoredEvent[] {
347 + const at = DateTime.makeUnsafe("2026-10-01T00:00:00.000Z");
348 + const [reply, replyItem] = bodiesEvents(at).filter((event) =>
349 + String(event.id).includes(":reply-"),
350 + );
351 + return [
352 + reply!,
353 + replyItem!,
354 + {
355 + id: EventId.make(`event:${BODIES}:task-progress`),
356 + type: "subagent.updated",
357 + threadId: BODIES,
358 + runId: bodiesRun,
359 + occurredAt: at,
360 + payload: { ...bodiesSubagent(at), progress: "Checked the build." },
361 + } as OrchestrationV2DomainEvent,
362 + ].map((event, index) => ({ sequence: 1_000 + index, commandId: null, event }));
363 +}
364 +
365 const seed = Effect.gen(function* () {
366 const projections = yield* ProjectionStore.ProjectionStoreV2;
367 const at = yield* DateTime.now;
@@ -219,6 +378,7 @@ const seed = Effect.gen(function* () {
378 itemUpdated(LONG, longRun, `tool-${index}`, index + 2, at),
379 ),
380 itemUpdated(LONG, longRun, "result", 92, at, "run_interrupt_result"),
381 + ...bodiesEvents(at),
382 ];
383 for (const event of events) yield* projections.apply(event);
384 });
@@ -365,4 +525,70 @@ it.layer(TestLayer)("compact bounded snapshot transport", (it) => {
525 }),
526 );
527 });
528 +
529 + describe("deduped bodies", () => {
530 + const markers = ["textOmitted", "filesOmitted", "promptOmitted", "resultOmitted"];
531 +
532 + it.effect(
533 + "HTTP: opted-in snapshots blank repeated bodies and restore to the same snapshot",
534 + () =>
535 + withHttp(async (get) => {
536 + const base = `/api/orchestration/threads/${encodeURIComponent(BODIES)}/bounded`;
537 + const compactBody = await (await get(`${base}?compactTurnItems=1`)).text();
538 + const dedupedBody = await (await get(`${base}?compactTurnItems=1&dedupeBodies=1`)).text();
539 +
540 + for (const marker of markers) {
541 + assert.notInclude(compactBody, marker);
542 + assert.include(dedupedBody, marker);
543 + }
544 + assert.isBelow(dedupedBody.length, compactBody.length);
545 + assert.deepStrictEqual(
546 + boundedSnapshotProjection(decodeBounded(dedupedBody)),
547 + boundedSnapshotProjection(decodeBounded(compactBody)),
548 + );
549 + }),
550 + );
551 +
552 + it.effect(
553 + "WS: bounded and full snapshots restore, and the live tail sends each body once",
554 + () =>
555 + Effect.gen(function* () {
556 + const subscribe = (
557 + input: Partial<Parameters<typeof subscribeOrchestrationV2Thread>[0]>,
558 + ) =>
559 + subscribeOrchestrationV2Thread({ threadId: BODIES, ...input }).pipe(
560 + Effect.flatMap(Stream.runCollect),
561 + Effect.map((items) =>
562 + Array.from(items, (item) =>
563 + decodeStreamItem(JSON.parse(JSON.stringify(encodeStreamItem(item)))),
564 + ),
565 + ),
566 + );
567 + const bounded = { acceptBoundedSnapshot: true, acceptCompactTurnItems: true };
568 + for (const mode of [bounded, {}]) {
569 + const [plainSnapshot, ...plainTail] = yield* subscribe(mode);
570 + const [dedupedSnapshot, ...dedupedTail] = yield* subscribe({
571 + ...mode,
572 + acceptDedupedBodies: true,
573 + });
574 + if (plainSnapshot?.kind !== "snapshot" || dedupedSnapshot?.kind !== "snapshot") {
575 + throw new Error("No snapshot");
576 + }
577 + assert.deepStrictEqual(
578 + boundedSnapshotProjection(dedupedSnapshot),
579 + boundedSnapshotProjection(plainSnapshot),
580 + );
581 + const payloads = (tail: typeof plainTail) =>
582 + tail.map((item) => (item.kind === "event" ? item.event.payload : null));
583 + const [reply, replyItem, progress] = payloads(plainTail);
584 + assert.deepStrictEqual(payloads(dedupedTail), [
585 + reply,
586 + { ...replyItem, text: "", textOmitted: true },
587 + // The snapshot delivered this subagent's record, so its unchanged bodies repeat.
588 + { ...progress, prompt: "", promptOmitted: true, result: null, resultOmitted: true },
589 + ]);
590 + }
591 + }),
592 + );
593 + });
594 });
apps/server/src/orchestration-v2/http.ts
+1
@@ -229,6 +229,7 @@ export const layer = HttpApiBuilder.group(
229 ...boundedSnapshotResponseFields({
230 bounded,
231 compactTurnItems: args.query.compactTurnItems === "1",
232 + dedupeBodies: args.query.dedupeBodies === "1",
233 }),
234 };
235 }),
apps/server/src/ws.ts
+27 -11
@@ -63,6 +63,7 @@ import {
63 OrchestrationV2ThreadLaunchError,
64 type OrchestrationProjectShell,
65 type OrchestrationV2ShellSnapshot,
66 + type OrchestrationV2ThreadStreamItem,
67 type ApplicationStoredEvent,
68 type ProjectEntriesFailure,
69 type ProjectFileFailure,
@@ -157,9 +158,11 @@ import {
158 THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
159 } from "./orchestration-v2/threadHistoryPaging.ts";
160 import {
161 + makeLiveBodyDeduper,
162 projectDomainEventForWire,
163 projectThreadProjectionForWire,
164 } from "./orchestration-v2/WireProjection.ts";
165 +import { dedupeSnapshotBodies } from "@t3tools/shared/orchestrationV2DedupedBodies";
166 import * as ProjectStore from "./orchestration-v2/ProjectStore.ts";
167 import * as ThreadSearch from "./orchestration-v2/ThreadSearch.ts";
168 import * as OrchestrationEventStore from "./persistence/OrchestrationEventStore.ts";
@@ -788,8 +791,13 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
791 readonly requestCompletionMarker?: boolean;
792 readonly acceptBoundedSnapshot?: boolean;
793 readonly acceptCompactTurnItems?: boolean;
794 + readonly acceptDedupedBodies?: boolean;
795 }) {
796 const threadManagement = yield* ThreadManagementService.ThreadManagementService;
797 + const dedupeBodies = input.acceptDedupedBodies === true;
798 + // Replay, snapshot, and live tail share one deduper: it must see every delivered record.
799 + const withLiveDedupe = <E, R>(stream: Stream.Stream<OrchestrationV2ThreadStreamItem, E, R>) =>
800 + dedupeBodies ? Stream.suspend(() => Stream.map(stream, makeLiveBodyDeduper())) : stream;
801 const applicationEvents = yield* OrchestrationEventStore.OrchestrationEventStore;
802
803 yield* Effect.annotateCurrentSpan({
@@ -888,15 +896,21 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
896 ? buildBoundedThreadStreamSnapshot({
897 ...snapshot,
898 compactTurnItems: input.acceptCompactTurnItems === true,
899 + dedupeBodies,
900 })
892 - : {
893 - kind: "snapshot" as const,
894 - snapshotSequence,
895 - projection: projectThreadProjectionForWire(snapshot.projection),
896 - };
897 - return Stream.concat(
898 - Stream.concat(rpcInitialItems([snapshotItem]), completionMarker),
899 - eventStreamFrom(snapshotSequence),
901 + : (() => {
902 + const projection = projectThreadProjectionForWire(snapshot.projection);
903 + return {
904 + kind: "snapshot" as const,
905 + snapshotSequence,
906 + projection: dedupeBodies ? dedupeSnapshotBodies(projection) : projection,
907 + };
908 + })();
909 + return withLiveDedupe(
910 + Stream.concat(
911 + Stream.concat(rpcInitialItems([snapshotItem]), completionMarker),
912 + eventStreamFrom(snapshotSequence),
913 + ),
914 );
915 });
916
@@ -967,9 +981,11 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
981 if (plan.mode === "snapshot") {
982 return yield* snapshotThenLive();
983 }
970 - return Stream.concat(
971 - Stream.concat(rpcInitialItems(replay), completionMarker),
972 - eventStreamFrom(highWater),
984 + return withLiveDedupe(
985 + Stream.concat(
986 + Stream.concat(rpcInitialItems(replay), completionMarker),
987 + eventStreamFrom(highWater),
988 + ),
989 );
990 }
991
packages/client-runtime/src/state/boundedThreadSnapshotHttp.ts
+1 -1
@@ -30,7 +30,7 @@ export const fetchEnvironmentBoundedThreadSnapshot = Effect.fn(
30 }) {
31 const endpoint = {
32 params: { threadId: input.threadId },
33 - query: { compactTurnItems: "1" },
33 + query: { compactTurnItems: "1", dedupeBodies: "1" },
34 };
35 return yield* executeAuthenticatedEnvironmentHttpRequest({
36 ...input,
packages/client-runtime/src/state/orchestrationV2Projection.test.ts
+78
@@ -638,3 +638,81 @@ it("rechecks visibility when an existing item changes runs without changing ordi
638 ]);
639 expect(projection.visibleTurnItems[0]?.item).toBe(first);
640 });
641 +
642 +it("restores deduped bodies from the records it already holds, also in a partial timeline", () => {
643 + const messageId = MessageId.make("message-deduped");
644 + const subagentId = NodeId.make("node-deduped");
645 + const reply = {
646 + ...commandItem("item-reply", "", 400),
647 + type: "assistant_message",
648 + messageId,
649 + text: "the full reply",
650 + streaming: false,
651 + } as unknown as OrchestrationV2TurnItem;
652 + const subagent = {
653 + id: subagentId,
654 + threadId,
655 + runId,
656 + parentNodeId: NodeId.make("node-root"),
657 + origin: "app_owned",
658 + createdBy: "agent",
659 + driver: "codex",
660 + providerInstanceId: ProviderInstanceId.make("codex"),
661 + providerThreadId: null,
662 + childThreadId: null,
663 + nativeTaskRef: null,
664 + prompt: "the task brief",
665 + title: null,
666 + model: null,
667 + status: "running",
668 + result: null,
669 + startedAt: now,
670 + completedAt: null,
671 + updatedAt: now,
672 + } as OrchestrationV2ThreadProjection["subagents"][number];
673 + const projection = {
674 + ...emptyProjection,
675 + runs: [run],
676 + messages: [
677 + {
678 + id: messageId,
679 + threadId,
680 + runId,
681 + nodeId: null,
682 + role: "assistant",
683 + text: "the full reply",
684 + attachments: [],
685 + streaming: false,
686 + createdBy: "agent",
687 + creationSource: "provider",
688 + createdAt: now,
689 + updatedAt: now,
690 + },
691 + ],
692 + subagents: [subagent],
693 + } as OrchestrationV2ThreadProjection;
694 + const event = (type: string, payload: unknown) =>
695 + ({
696 + id: `event-${type}`,
697 + type,
698 + threadId,
699 + runId,
700 + occurredAt: now,
701 + payload,
702 + }) as OrchestrationV2DomainEvent;
703 +
704 + for (const options of [undefined, { partialTimeline: true, latestLocalTurnOrdinal: 300 }]) {
705 + const withReply = applyOrchestrationV2ProjectionEvent(
706 + projection,
707 + event("turn-item.updated", { ...reply, text: "", textOmitted: true }),
708 + options,
709 + );
710 + expect(withReply?.turnItems).toEqual([reply]);
711 + expect(withReply?.visibleTurnItems.map((row) => row.item)).toEqual([reply]);
712 + }
713 + const progressed = applyOrchestrationV2ProjectionEvent(
714 + projection,
715 + event("subagent.updated", { ...subagent, prompt: "", promptOmitted: true, progress: "step 2" }),
716 + );
717 + expect(progressed?.subagents).toEqual([{ ...subagent, progress: "step 2" }]);
718 +});
packages/client-runtime/src/state/orchestrationV2Projection.ts
+34 -9
@@ -5,6 +5,11 @@ import type {
5 OrchestrationV2ThreadProjection,
6 OrchestrationV2TurnItem,
7 } from "@t3tools/contracts";
8 +import {
9 + restoreSubagentBodies,
10 + restoreTurnItemBodies,
11 + type DedupedBodySource,
12 +} from "@t3tools/shared/orchestrationV2DedupedBodies";
13 import {
14 createOrchestrationV2TurnItemVisibility,
15 isOrchestrationV2TurnItemVisible,
@@ -56,6 +61,16 @@ function upsertEntity<T extends { readonly id: unknown }>(
61 return next;
62 }
63
64 +// A deduped item repeats a record the stream just sent, so search newest first.
65 +function latestBodies(projection: OrchestrationV2ThreadProjection): DedupedBodySource {
66 + return {
67 + messageText: (id) => projection.messages.findLast((message) => message.id === id)?.text,
68 + checkpointFiles: (id) =>
69 + projection.checkpoints.findLast((checkpoint) => checkpoint.id === id)?.files,
70 + subagent: (id) => projection.subagents.findLast((subagent) => subagent.id === id),
71 + };
72 +}
73 +
74 function removeVisibleItem(
75 rows: OrchestrationV2ThreadProjection["visibleTurnItems"],
76 sourceItemId: OrchestrationV2TurnItem["id"],
@@ -307,7 +322,16 @@ export function applyOrchestrationV2ProjectionEvent(
322 case "node.updated":
323 return { ...base, nodes: upsertEntity(base.nodes, event.payload) };
324 case "subagent.updated":
310 - return { ...base, subagents: upsertEntity(base.subagents, event.payload) };
325 + return {
326 + ...base,
327 + subagents: upsertEntity(
328 + base.subagents,
329 + restoreSubagentBodies(
330 + event.payload,
331 + base.subagents.find((subagent) => subagent.id === event.payload.id),
332 + ),
333 + ),
334 + };
335 case "provider-session.attached":
336 case "provider-session.updated":
337 return {
@@ -346,35 +370,36 @@ export function applyOrchestrationV2ProjectionEvent(
370 case "plan.updated":
371 return { ...base, plans: upsertEntity(base.plans, event.payload) };
372 case "turn-item.updated": {
373 + const payload = restoreTurnItemBodies(event.payload, latestBodies(projection));
374 // Streamed updates land on the newest items, so search from the end.
375 let itemIndex = projection.turnItems.length - 1;
351 - while (itemIndex >= 0 && projection.turnItems[itemIndex]!.id !== event.payload.id) {
376 + while (itemIndex >= 0 && projection.turnItems[itemIndex]!.id !== payload.id) {
377 itemIndex -= 1;
378 }
379 if (
380 partialTimeline &&
381 itemIndex === -1 &&
357 - shouldDropMissingPartialTurnItem(projection, event.payload, latestLocalTurnOrdinal)
382 + shouldDropMissingPartialTurnItem(projection, payload, latestLocalTurnOrdinal)
383 ) {
384 return projection;
385 }
386 const turnItems =
387 itemIndex === -1
363 - ? [...projection.turnItems, event.payload]
364 - : replaceProjectionArrayItem(projection.turnItems, itemIndex, event.payload);
388 + ? [...projection.turnItems, payload]
389 + : replaceProjectionArrayItem(projection.turnItems, itemIndex, payload);
390 const next = { ...base, turnItems };
391 // Only interrupt requests can change another item's visibility. Streaming
392 // text/tool updates must not recheck every row against every run.
393 const previous = itemIndex === -1 ? undefined : projection.turnItems[itemIndex];
394 const visible =
370 - event.payload.type === "run_interrupt_request" || previous?.type === "run_interrupt_request"
395 + payload.type === "run_interrupt_request" || previous?.type === "run_interrupt_request"
396 ? { ...next, visibleTurnItems: activeVisibleTurnItems(next) }
397 : next;
398 return {
399 ...next,
375 - visibleTurnItems: shouldShowLocalTurnItem(next, event.payload)
376 - ? upsertVisibleTurnItem(visible, event.payload, partialTimeline, latestLocalTurnOrdinal)
377 - : removeVisibleItem(visible.visibleTurnItems, event.payload.id),
400 + visibleTurnItems: shouldShowLocalTurnItem(next, payload)
401 + ? upsertVisibleTurnItem(visible, payload, partialTimeline, latestLocalTurnOrdinal)
402 + : removeVisibleItem(visible.visibleTurnItems, payload.id),
403 };
404 }
405 case "checkpoint-scope.created":
packages/client-runtime/src/state/threads.ts
+1
@@ -963,6 +963,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
963 ...(acceptBoundedSnapshot
964 ? { acceptBoundedSnapshot: true as const, acceptCompactTurnItems: true as const }
965 : {}),
966 + acceptDedupedBodies: true as const,
967 };
968 }),
969 {
packages/contracts/src/environmentHttp.ts
+1
@@ -673,6 +673,7 @@ const EnvironmentOrchestrationShellSnapshotQuery = Schema.Struct({
673
674 const EnvironmentOrchestrationThreadBoundedSnapshotQuery = Schema.Struct({
675 compactTurnItems: Schema.optionalKey(Schema.String),
676 + dedupeBodies: Schema.optionalKey(Schema.String),
677 });
678
679 const EnvironmentOrchestrationThreadHistoryQuery = Schema.Struct({
packages/contracts/src/orchestrationV2.ts
+23
@@ -757,6 +757,16 @@ export const OrchestrationV2SubagentWorkflow = Schema.Struct({
757 });
758 export type OrchestrationV2SubagentWorkflow = typeof OrchestrationV2SubagentWorkflow.Type;
759
760 +/**
761 + * Wire-only. Set for clients that opted in with `acceptDedupedBodies` or `dedupeBodies=1` when the
762 + * marked field was blanked because the client already holds the same value: in the record it
763 + * received earlier on the same response or stream. Restore with
764 + * `@t3tools/shared/orchestrationV2DedupedBodies` before use; never store or forward a marked
765 + * value. Clients that opted in restore only these fields, so blanking another field needs a new
766 + * opt-in.
767 + */
768 +const DedupedBodyMarker = Schema.optionalKey(Schema.Literal(true));
769 +
770 export const OrchestrationV2Subagent = Schema.Struct({
771 id: NodeId,
772 threadId: ThreadId,
@@ -791,6 +801,9 @@ export const OrchestrationV2Subagent = Schema.Struct({
801 startedAt: Schema.NullOr(Schema.DateTimeUtc),
802 completedAt: Schema.NullOr(Schema.DateTimeUtc),
803 updatedAt: Schema.DateTimeUtc,
804 + /** Same as this subagent's previous record on this stream: its snapshot or `subagent.updated`. */
805 + promptOmitted: DedupedBodyMarker,
806 + resultOmitted: DedupedBodyMarker,
807 });
808 export type OrchestrationV2Subagent = typeof OrchestrationV2Subagent.Type;
809
@@ -1429,6 +1442,8 @@ export const OrchestrationV2TurnItem = Schema.Union([
1442 workflowScript: OrchestrationV2ConversationMessage.fields.workflowScript,
1443 inputIntent: OrchestrationV2UserMessageInputIntent,
1444 text: Schema.String,
1445 + /** Same as the text of message `messageId`. */
1446 + textOmitted: DedupedBodyMarker,
1447 context: Schema.optional(OrchestrationMessageContext),
1448 attachments: Schema.Array(ChatAttachment),
1449 }),
@@ -1437,6 +1452,7 @@ export const OrchestrationV2TurnItem = Schema.Union([
1452 type: Schema.Literal("assistant_message"),
1453 messageId: MessageId,
1454 text: Schema.String,
1455 + textOmitted: DedupedBodyMarker,
1456 attachments: Schema.optional(Schema.Array(ChatAttachment)),
1457 streaming: Schema.Boolean,
1458 }),
@@ -1520,6 +1536,8 @@ export const OrchestrationV2TurnItem = Schema.Union([
1536 checkpointId: CheckpointId,
1537 scopeId: CheckpointScopeId,
1538 files: Schema.Array(OrchestrationV2CheckpointFileSummary),
1539 + /** Same as the files of checkpoint `checkpointId`. */
1540 + filesOmitted: DedupedBodyMarker,
1541 }),
1542 Schema.Struct({
1543 ...OrchestrationV2TurnItemBaseFields,
@@ -1607,6 +1625,9 @@ export const OrchestrationV2TurnItem = Schema.Union([
1625 prompt: Schema.String,
1626 progress: Schema.optional(Schema.String),
1627 result: Schema.NullOr(Schema.String),
1628 + /** Same as the prompt and result of subagent `subagentId`. */
1629 + promptOmitted: DedupedBodyMarker,
1630 + resultOmitted: DedupedBodyMarker,
1631 }),
1632 Schema.Struct({
1633 ...OrchestrationV2TurnItemBaseFields,
@@ -3424,6 +3445,8 @@ export const OrchestrationV2SubscribeThreadInput = Schema.Struct({
3445 requestCompletionMarker: Schema.optionalKey(Schema.Boolean),
3446 acceptBoundedSnapshot: Schema.optionalKey(Schema.Boolean),
3447 acceptCompactTurnItems: Schema.optionalKey(Schema.Boolean),
3448 + /** The client restores `*Omitted` bodies from records it already holds. */
3449 + acceptDedupedBodies: Schema.optionalKey(Schema.Boolean),
3450 });
3451 export type OrchestrationV2SubscribeThreadInput = typeof OrchestrationV2SubscribeThreadInput.Type;
3452
packages/shared/package.json
+4
@@ -379,6 +379,10 @@
379 "types": "./src/orchestrationV2BoundedSnapshot.ts",
380 "import": "./src/orchestrationV2BoundedSnapshot.ts"
381 },
382 + "./orchestrationV2DedupedBodies": {
383 + "types": "./src/orchestrationV2DedupedBodies.ts",
384 + "import": "./src/orchestrationV2DedupedBodies.ts"
385 + },
386 "./orchestrationV2Timeline": {
387 "types": "./src/orchestrationV2Timeline.ts",
388 "import": "./src/orchestrationV2Timeline.ts"
packages/shared/src/orchestrationV2BoundedSnapshot.ts
+8 -3
@@ -3,6 +3,8 @@ import type {
3 OrchestrationV2ThreadProjection,
4 } from "@t3tools/contracts";
5
6 +import { restoreSnapshotBodies } from "./orchestrationV2DedupedBodies.ts";
7 +
8 function isLocalTimelineRow(
9 projection: Pick<OrchestrationV2ThreadProjection, "thread">,
10 row: OrchestrationV2ProjectedTurnItem,
@@ -39,11 +41,14 @@ export function restoreLocalVisibleTurnItems(
41 return { ...projection, turnItems: [...local, ...projection.turnItems] };
42 }
43
44 +/** Undoes both opt-in compactions; bodies carry their own markers, so they need no flag. */
45 export function boundedSnapshotProjection(snapshot: {
46 readonly projection: OrchestrationV2ThreadProjection;
47 readonly turnItemsOmitLocalVisible?: true | undefined;
48 }): OrchestrationV2ThreadProjection {
46 - return snapshot.turnItemsOmitLocalVisible === true
47 - ? restoreLocalVisibleTurnItems(snapshot.projection)
48 - : snapshot.projection;
49 + return restoreSnapshotBodies(
50 + snapshot.turnItemsOmitLocalVisible === true
51 + ? restoreLocalVisibleTurnItems(snapshot.projection)
52 + : snapshot.projection,
53 + );
54 }
packages/shared/src/orchestrationV2DedupedBodies.test.ts new
+196
@@ -0,0 +1,196 @@
1 +import {
2 + CheckpointId,
3 + CheckpointScopeId,
4 + MessageId,
5 + NodeId,
6 + ThreadId,
7 + type OrchestrationV2ThreadProjection,
8 + TurnItemId,
9 + type OrchestrationV2Subagent,
10 + type OrchestrationV2TurnItem,
11 +} from "@t3tools/contracts";
12 +import { describe, expect, it } from "vite-plus/test";
13 +
14 +import {
15 + boundedSnapshotProjection,
16 + omitLocalVisibleTurnItems,
17 +} from "./orchestrationV2BoundedSnapshot.ts";
18 +import {
19 + dedupeSnapshotBodies,
20 + restoreSnapshotBodies,
21 + restoreSubagentBodies,
22 +} from "./orchestrationV2DedupedBodies.ts";
23 +
24 +const THREAD = ThreadId.make("thread:bodies");
25 +const PARENT = ThreadId.make("thread:bodies-parent");
26 +const at = "2026-10-01T00:00:00.000Z";
27 +
28 +function item(id: string, fields: Record<string, unknown>, threadId = THREAD) {
29 + return {
30 + id: TurnItemId.make(id),
31 + threadId,
32 + runId: null,
33 + nodeId: null,
34 + providerThreadId: null,
35 + providerTurnId: null,
36 + nativeItemRef: null,
37 + parentItemId: null,
38 + ordinal: 1,
39 + status: "completed",
40 + title: null,
41 + startedAt: at,
42 + completedAt: at,
43 + updatedAt: at,
44 + ...fields,
45 + } as unknown as OrchestrationV2TurnItem;
46 +}
47 +
48 +const message = (id: string, role: "user" | "assistant", text: string) => ({
49 + id: MessageId.make(id),
50 + threadId: THREAD,
51 + runId: null,
52 + nodeId: null,
53 + role,
54 + text,
55 + attachments: [],
56 + streaming: false,
57 + createdBy: role === "user" ? "user" : "agent",
58 + creationSource: role === "user" ? "web" : "provider",
59 + createdAt: at,
60 + updatedAt: at,
61 +});
62 +const files = [{ path: "src/a.ts", kind: "modified", additions: 3, deletions: 1 }];
63 +const subagent = {
64 + id: NodeId.make("node:task"),
65 + prompt: "do the task",
66 + result: "final result",
67 +} as Partial<OrchestrationV2Subagent>;
68 +
69 +const userMessage = (id: string, messageId: string, text: string) =>
70 + item(id, {
71 + type: "user_message",
72 + messageId: MessageId.make(messageId),
73 + inputIntent: "turn_start",
74 + text,
75 + attachments: [],
76 + createdBy: "user",
77 + creationSource: "web",
78 + });
79 +const assistantMessage = (id: string, messageId: string, text: string, threadId = THREAD) =>
80 + item(
81 + id,
82 + { type: "assistant_message", messageId: MessageId.make(messageId), text, streaming: false },
83 + threadId,
84 + );
85 +const checkpoint = (id: string, checkpointId: string, itemFiles: typeof files) =>
86 + item(id, {
87 + type: "checkpoint",
88 + checkpointId: CheckpointId.make(checkpointId),
89 + scopeId: CheckpointScopeId.make("scope"),
90 + files: itemFiles,
91 + });
92 +
93 +const local: OrchestrationV2TurnItem[] = [
94 + userMessage("user", "m-user", "hello"),
95 + assistantMessage("assistant", "m-assistant", "hi there"),
96 + assistantMessage("stale", "m-stale", "older text"),
97 + userMessage("empty", "m-empty", ""),
98 + checkpoint("checkpoint", "c-1", files),
99 + checkpoint("orphan-checkpoint", "c-missing", files),
100 + checkpoint("no-files", "c-empty", []),
101 + item("task", {
102 + type: "subagent",
103 + subagentId: subagent.id,
104 + origin: "app_owned",
105 + driver: "codex",
106 + providerInstanceId: "codex",
107 + childThreadId: null,
108 + prompt: "do the task",
109 + result: "partial result",
110 + }),
111 +];
112 +// A fork's inherited row may name a message id this thread also has; its record lives elsewhere.
113 +const inherited = assistantMessage("inherited", "m-assistant", "hi there", PARENT);
114 +
115 +const projection = {
116 + thread: { id: THREAD },
117 + messages: [
118 + message("m-user", "user", "hello"),
119 + message("m-assistant", "assistant", "hi there"),
120 + message("m-stale", "assistant", "newer text"),
121 + message("m-empty", "user", ""),
122 + ],
123 + checkpoints: [
124 + { id: CheckpointId.make("c-1"), files },
125 + { id: CheckpointId.make("c-empty"), files: [] },
126 + ],
127 + subagents: [subagent],
128 + turnItems: local,
129 + visibleTurnItems: [
130 + {
131 + position: 0,
132 + visibility: "inherited",
133 + sourceThreadId: PARENT,
134 + sourceItemId: inherited.id,
135 + item: inherited,
136 + },
137 + ...local.map((value, index) => ({
138 + position: index + 1,
139 + visibility: "local",
140 + sourceThreadId: THREAD,
141 + sourceItemId: value.id,
142 + item: value,
143 + })),
144 + ],
145 +} as unknown as OrchestrationV2ThreadProjection;
146 +
147 +function markers(value: OrchestrationV2ThreadProjection) {
148 + return Object.fromEntries(
149 + value.visibleTurnItems.flatMap(({ item }) => {
150 + const marked = ["textOmitted", "filesOmitted", "promptOmitted", "resultOmitted"].filter(
151 + (key) => key in item,
152 + );
153 + return marked.length === 0 ? [] : [[String(item.id), marked]];
154 + }),
155 + );
156 +}
157 +
158 +describe("deduped snapshot bodies", () => {
159 + it("blanks only bodies equal to a record and restores them exactly", () => {
160 + const deduped = dedupeSnapshotBodies(projection);
161 +
162 + expect(markers(deduped)).toEqual({
163 + user: ["textOmitted"],
164 + assistant: ["textOmitted"],
165 + checkpoint: ["filesOmitted"],
166 + task: ["promptOmitted"],
167 + });
168 + expect(restoreSnapshotBodies(deduped)).toEqual(projection);
169 + expect(restoreSnapshotBodies(projection)).toBe(projection);
170 + });
171 +
172 + it("keeps items shared with turnItems as one object, so the compact snapshot still applies", () => {
173 + const compact = omitLocalVisibleTurnItems(dedupeSnapshotBodies(projection));
174 +
175 + expect(compact?.turnItems).toEqual([]);
176 + expect(
177 + boundedSnapshotProjection({ projection: compact!, turnItemsOmitLocalVisible: true }),
178 + ).toEqual(projection);
179 + });
180 +
181 + it("does not restore a field from a subagent record whose field is itself still marked", () => {
182 + const marked = {
183 + prompt: "",
184 + result: null,
185 + promptOmitted: true as const,
186 + resultOmitted: true as const,
187 + };
188 + const previous = { prompt: "", result: "done", promptOmitted: true as const };
189 +
190 + expect(restoreSubagentBodies(marked, previous)).toEqual({
191 + prompt: "",
192 + result: "done",
193 + promptOmitted: true,
194 + });
195 + });
196 +});
packages/shared/src/orchestrationV2DedupedBodies.ts new
+185
@@ -0,0 +1,185 @@
1 +import {
2 + OrchestrationV2CheckpointFileSummary,
3 + type CheckpointId,
4 + type MessageId,
5 + type NodeId,
6 + type OrchestrationV2Checkpoint,
7 + type OrchestrationV2Subagent,
8 + type OrchestrationV2ThreadProjection,
9 + type OrchestrationV2TurnItem,
10 +} from "@t3tools/contracts";
11 +import * as Schema from "effect/Schema";
12 +
13 +/**
14 + * A turn item repeats a body its record already carries: message text, checkpoint files, a
15 + * subagent's prompt and result. For clients that opt in, the server blanks the repeat and marks
16 + * the field (`textOmitted`, `filesOmitted`, `promptOmitted`, `resultOmitted`), and the client
17 + * copies the value back from the record it already holds. Only a body equal to the record's is
18 + * blanked, so the restore is exact.
19 + */
20 +export interface DedupedBodySource {
21 + readonly messageText: (id: MessageId) => string | undefined;
22 + readonly checkpointFiles: (id: CheckpointId) => OrchestrationV2Checkpoint["files"] | undefined;
23 + readonly subagent: (id: NodeId) => WithSubagentBodies | undefined;
24 +}
25 +
26 +type SubagentBodies = Pick<OrchestrationV2Subagent, "prompt" | "result">;
27 +type WithSubagentBodies = SubagentBodies & {
28 + readonly promptOmitted?: true;
29 + readonly resultOmitted?: true;
30 +};
31 +
32 +const sameCheckpointFiles = Schema.toEquivalence(
33 + Schema.Array(OrchestrationV2CheckpointFileSummary),
34 +);
35 +
36 +/** For subagent items and for a repeated `subagent.updated`, whose `previous` was sent earlier. */
37 +export function dedupeSubagentBodies<T extends WithSubagentBodies>(
38 + value: T,
39 + previous: SubagentBodies | undefined,
40 +): T {
41 + const prompt = value.prompt !== "" && previous?.prompt === value.prompt;
42 + const result = value.result !== null && previous?.result === value.result;
43 + if (!prompt && !result) return value;
44 + return {
45 + ...value,
46 + ...(prompt ? { prompt: "", promptOmitted: true as const } : {}),
47 + ...(result ? { result: null, resultOmitted: true as const } : {}),
48 + };
49 +}
50 +
51 +/**
52 + * A marked body whose record is missing, or whose record field is itself still marked, stays
53 + * blank and marked; the server only blanks bodies it sent, so either means a server bug.
54 + */
55 +export function restoreSubagentBodies<T extends WithSubagentBodies>(
56 + value: T,
57 + previous: WithSubagentBodies | undefined,
58 +): T {
59 + if (previous === undefined) return value;
60 + const prompt = value.promptOmitted === true && previous.promptOmitted !== true;
61 + const result = value.resultOmitted === true && previous.resultOmitted !== true;
62 + if (!prompt && !result) return value;
63 + const { promptOmitted, resultOmitted, ...rest } = value;
64 + // Dropping both optional markers from `T` and keeping the unrestored one leaves a `T`.
65 + return {
66 + ...rest,
67 + ...(prompt ? { prompt: previous.prompt } : promptOmitted === true ? { promptOmitted } : {}),
68 + ...(result ? { result: previous.result } : resultOmitted === true ? { resultOmitted } : {}),
69 + } as T;
70 +}
71 +
72 +export function dedupeTurnItemBodies(
73 + item: OrchestrationV2TurnItem,
74 + source: DedupedBodySource,
75 +): OrchestrationV2TurnItem {
76 + switch (item.type) {
77 + case "user_message":
78 + case "assistant_message":
79 + return item.text !== "" && source.messageText(item.messageId) === item.text
80 + ? { ...item, text: "", textOmitted: true }
81 + : item;
82 + case "checkpoint": {
83 + const files = source.checkpointFiles(item.checkpointId);
84 + return item.files.length > 0 && files !== undefined && sameCheckpointFiles(files, item.files)
85 + ? { ...item, files: [], filesOmitted: true }
86 + : item;
87 + }
88 + case "subagent":
89 + return dedupeSubagentBodies(item, source.subagent(item.subagentId));
90 + default:
91 + return item;
92 + }
93 +}
94 +
95 +export function restoreTurnItemBodies(
96 + item: OrchestrationV2TurnItem,
97 + source: DedupedBodySource,
98 +): OrchestrationV2TurnItem {
99 + switch (item.type) {
100 + case "user_message":
101 + case "assistant_message": {
102 + const text = item.textOmitted === true ? source.messageText(item.messageId) : undefined;
103 + if (text === undefined) return item;
104 + const { textOmitted: _textOmitted, ...rest } = item;
105 + return { ...rest, text };
106 + }
107 + case "checkpoint": {
108 + const files =
109 + item.filesOmitted === true ? source.checkpointFiles(item.checkpointId) : undefined;
110 + if (files === undefined) return item;
111 + const { filesOmitted: _filesOmitted, ...rest } = item;
112 + return { ...rest, files };
113 + }
114 + case "subagent":
115 + return item.promptOmitted === true || item.resultOmitted === true
116 + ? restoreSubagentBodies(item, source.subagent(item.subagentId))
117 + : item;
118 + default:
119 + return item;
120 + }
121 +}
122 +
123 +/** Builds each index on first lookup, so a snapshot without marked items builds none. */
124 +function snapshotBodySource(projection: OrchestrationV2ThreadProjection): DedupedBodySource {
125 + let messages: Map<MessageId, string> | undefined;
126 + let checkpoints: Map<CheckpointId, OrchestrationV2Checkpoint["files"]> | undefined;
127 + let subagents: Map<NodeId, OrchestrationV2Subagent> | undefined;
128 + return {
129 + messageText: (id) =>
130 + (messages ??= new Map(projection.messages.map((message) => [message.id, message.text]))).get(
131 + id,
132 + ),
133 + checkpointFiles: (id) =>
134 + (checkpoints ??= new Map(
135 + projection.checkpoints.map((checkpoint) => [checkpoint.id, checkpoint.files]),
136 + )).get(id),
137 + subagent: (id) =>
138 + (subagents ??= new Map(projection.subagents.map((subagent) => [subagent.id, subagent]))).get(
139 + id,
140 + ),
141 + };
142 +}
143 +
144 +/**
145 + * Inherited rows belong to another thread, whose records this projection lacks. Maps each item
146 + * once, so an item shared by `visibleTurnItems` and `turnItems` stays one object.
147 + */
148 +function mapLocalTurnItems(
149 + projection: OrchestrationV2ThreadProjection,
150 + map: (item: OrchestrationV2TurnItem) => OrchestrationV2TurnItem,
151 +): OrchestrationV2ThreadProjection {
152 + const mapped = new Map<OrchestrationV2TurnItem, OrchestrationV2TurnItem>();
153 + let changed = false;
154 + const once = (item: OrchestrationV2TurnItem) => {
155 + let next = mapped.get(item);
156 + if (next === undefined) {
157 + next = map(item);
158 + mapped.set(item, next);
159 + changed ||= next !== item;
160 + }
161 + return next;
162 + };
163 + const visibleTurnItems = projection.visibleTurnItems.map((row) => {
164 + if (row.visibility !== "local" && row.sourceThreadId !== projection.thread.id) return row;
165 + const item = once(row.item);
166 + return item === row.item ? row : { ...row, item };
167 + });
168 + const turnItems = projection.turnItems.map(once);
169 + return changed ? { ...projection, visibleTurnItems, turnItems } : projection;
170 +}
171 +
172 +export function dedupeSnapshotBodies(
173 + projection: OrchestrationV2ThreadProjection,
174 +): OrchestrationV2ThreadProjection {
175 + const source = snapshotBodySource(projection);
176 + return mapLocalTurnItems(projection, (item) => dedupeTurnItemBodies(item, source));
177 +}
178 +
179 +/** Call on every decoded snapshot before reducers or caches see it. */
180 +export function restoreSnapshotBodies(
181 + projection: OrchestrationV2ThreadProjection,
182 +): OrchestrationV2ThreadProjection {
183 + const source = snapshotBodySource(projection);
184 + return mapLocalTurnItems(projection, (item) => restoreTurnItemBodies(item, source));
185 +}