Skip to content
Source
Sign inSign up

orchestrationV2DedupedBodies.test.ts

196 lines5.6 KB

1import {
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";
12import { describe, expect, it } from "vite-plus/test";
13​
14import {
15 boundedSnapshotProjection,
16 omitLocalVisibleTurnItems,
17} from "./orchestrationV2BoundedSnapshot.ts";
18import {
19 dedupeSnapshotBodies,
20 restoreSnapshotBodies,
21 restoreSubagentBodies,
22} from "./orchestrationV2DedupedBodies.ts";
23​
24const THREAD = ThreadId.make("thread:bodies");
25const PARENT = ThreadId.make("thread:bodies-parent");
26const at = "2026-10-01T00:00:00.000Z";
27​
28function 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;
47​
48const 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});
62const files = [{ path: "src/a.ts", kind: "modified", additions: 3, deletions: 1 }];
63const subagent = {
64 id: NodeId.make("node:task"),
65 prompt: "do the task",
66 result: "final result",
67} as Partial<OrchestrationV2Subagent>;
68​
69const 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 });
79const 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 );
85const 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​
93const 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.
113const inherited = assistantMessage("inherited", "m-assistant", "hi there", PARENT);
114​
115const 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​
147function 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 );
157​
158describe("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});