Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

WireProjection.test.ts

400 lines14.5 KB

1import {
2 MessageId,
3 ContextHandoffId,
4 ProviderThreadId,
5 NodeId,
6 EventId,
7 ProjectId,
8 ProviderDriverKind,
9 ProviderInstanceId,
10 ThreadId,
11 TurnItemId,
12 type OrchestrationV2ThreadProjection,
13 OrchestrationV2TurnItem,
14 OrchestrationV2TurnItemJson,
15} from "@t3tools/contracts";
16import { describe, expect, it } from "@effect/vitest";
17import * as DateTime from "effect/DateTime";
18import * as Schema from "effect/Schema";
19​
20import {
21 projectTurnItemForWire,
22 projectTurnItemForDetail,
23 projectDomainEventForWire,
24} from "./WireProjection.ts";
25import { threadShellFromProjection } from "./ProjectionStore.ts";
26import { MAX_TOOL_OUTPUT_IMAGES, toolOutputImages } from "@t3tools/shared/toolOutput";
27​
28const decodeTurnItem = Schema.decodeUnknownSync(OrchestrationV2TurnItem);
29const encodeTurnItemJson = Schema.encodeSync(OrchestrationV2TurnItemJson);
30const decodeTurnItemJson = Schema.decodeUnknownSync(OrchestrationV2TurnItemJson);
31​
32const base = {
33 id: TurnItemId.make("tool-1"),
34 type: "dynamic_tool" as const,
35 threadId: ThreadId.make("thread-1"),
36 runId: null,
37 nodeId: null,
38 providerThreadId: null,
39 providerTurnId: null,
40 nativeItemRef: null,
41 parentItemId: null,
42 ordinal: 1,
43 status: "completed" as const,
44 title: "MCP tool",
45 toolName: "mcp__github__fetch_pr",
46 input: { pr: 42 },
47 startedAt: DateTime.makeUnsafe("2026-08-13T00:00:00.000Z"),
48 completedAt: DateTime.makeUnsafe("2026-08-13T00:00:01.000Z"),
49 updatedAt: DateTime.makeUnsafe("2026-08-13T00:00:01.000Z"),
50};
51​
52describe("orchestration V2 wire projection", () => {
53 it("keeps copied handoff transcripts out of activity items and live events", () => {
54 const item = {
55 ...base,
56 type: "handoff" as const,
57 contextHandoffId: ContextHandoffId.make("handoff:wire"),
58 fromProviderThreadIds: [],
59 toProviderThreadId: ProviderThreadId.make("target"),
60 fromProviderInstanceIds: [],
61 toProviderInstanceId: ProviderInstanceId.make("codex"),
62 strategy: "full_thread_summary" as const,
63 summary: "PRIVATE_HANDOFF_TRANSCRIPT",
64 };
65 const projected = projectTurnItemForWire(item);
66 expect(projected).not.toHaveProperty("summary");
67 expect(decodeTurnItem(projected)).toMatchObject({ contextHandoffId: item.contextHandoffId });
68 const event = {
69 id: EventId.make("handoff:wire"),
70 type: "turn-item.updated" as const,
71 threadId: base.threadId,
72 occurredAt: base.updatedAt,
73 payload: item,
74 };
75 expect(JSON.stringify(projectDomainEventForWire(event))).not.toContain(item.summary);
76 expect(item.summary).toBe("PRIVATE_HANDOFF_TRANSCRIPT");
77 });
78​
79 it("preserves image metadata through wire and JSON contracts while redacting output", () => {
80 const item = {
81 ...base,
82 toolName: "Read",
83 input: { file_path: "/workspace/reference.png" },
84 viewedImagePath: "/workspace/reference.png",
85 output: { data: "private-image-data" },
86 } satisfies OrchestrationV2TurnItem;
87 const projected = projectTurnItemForWire(item);
88 const live = decodeTurnItem(projected);
89 const encoded = encodeTurnItemJson(live);
90 const json = decodeTurnItemJson(encoded);
91 const decoded = decodeTurnItem(json);
92 expect(decoded).toMatchObject({ viewedImagePath: "/workspace/reference.png" });
93 expect(decoded).not.toHaveProperty("output");
94 expect(item.output.data).toBe("private-image-data");
95 });
96​
97 it("preserves provider notices in bounded items and live events", () => {
98 const item = {
99 ...base,
100 type: "system_notice" as const,
101 message: "Safeguards flagged this message. Switched to Opus 4.8.",
102 };
103 expect(projectTurnItemForWire(item)).toEqual(item);
104 const event = {
105 id: EventId.make("notice-wire-event"),
106 type: "turn-item.updated" as const,
107 threadId: item.threadId,
108 occurredAt: item.updatedAt,
109 payload: item,
110 };
111 expect(projectDomainEventForWire(event)).toEqual(event);
112 });
113​
114 it("keeps transcript bodies out of shell rows", () => {
115 const now = DateTime.makeUnsafe("2026-08-13T00:00:00.000Z");
116 const threadId = ThreadId.make("thread-shell-budget");
117 const projection = {
118 thread: {
119 createdBy: "user",
120 creationSource: "web",
121 id: threadId,
122 projectId: ProjectId.make("project-shell-budget"),
123 title: "Shell payload budget",
124 providerInstanceId: ProviderInstanceId.make("codex"),
125 modelSelection: null,
126 runtimeMode: "full-access",
127 interactionMode: "default",
128 branch: null,
129 worktreePath: null,
130 lineage: {
131 parentThreadId: null,
132 relationshipToParent: null,
133 rootThreadId: threadId,
134 },
135 forkedFrom: null,
136 activeProviderThreadId: null,
137 createdAt: now,
138 updatedAt: now,
139 archivedAt: null,
140 settledOverride: null,
141 settledAt: null,
142 deletedAt: null,
143 },
144 runs: [],
145 attempts: [],
146 runtimeRequests: [],
147 messages: [
148 {
149 id: MessageId.make("message-shell-budget"),
150 role: "assistant",
151 text: "x".repeat(1_000_000),
152 updatedAt: now,
153 },
154 ],
155 providerSessions: [],
156 providerThreads: [],
157 turnItems: [],
158 visibleTurnItems: [],
159 plans: [],
160 updatedAt: now,
161 } as unknown as OrchestrationV2ThreadProjection;
162​
163 const shell = threadShellFromProjection(projection);
164​
165 expect(shell.latestVisibleMessage).toBeNull();
166 expect(JSON.stringify(shell).length).toBeLessThan(2_000);
167 });
168​
169 it("omits oversized dynamic tool results without mutating persistence data", () => {
170 const output = { content: [{ type: "text", text: "first line\n" + "x".repeat(100_000) }] };
171 const item = { ...base, output } satisfies OrchestrationV2TurnItem;
172 const projected = projectTurnItemForWire(item);
173​
174 expect(item.output).toBe(output);
175 expect(projected).not.toBe(item);
176 expect(JSON.stringify(projected).length).toBeLessThan(2_000);
177 expect(projected).not.toHaveProperty("output");
178 });
179​
180 it("omits even small dynamic tool results while retaining input", () => {
181 const item = { ...base, output: { ok: true } } satisfies OrchestrationV2TurnItem;
182 expect(projectTurnItemForWire(item)).toEqual({ ...base, outputOmitted: true });
183 expect(item.output).toEqual({ ok: true });
184 });
185​
186 it("keeps undefined dynamic input intact", () => {
187 const item = { ...base, input: undefined } satisfies OrchestrationV2TurnItem;
188 expect(projectTurnItemForWire(item)).toEqual(item);
189 });
190​
191 it.each([
192 [" \t\r\n\n first\t line \r\n" + "x\n".repeat(10_000), "first line"],
193 [" \n".repeat(10_000), "Large tool output"],
194 ["a".repeat(160) + "\n" + "x".repeat(20_000), "a".repeat(160)],
195 ["a".repeat(161) + "\n" + "x".repeat(20_000), "a".repeat(159) + "…"],
196 ["a".repeat(159) + "\t b" + "x".repeat(20_000), "a".repeat(159) + "…"],
197 [" café\u00a0\u2003😀 \t\r\n" + "x".repeat(20_000), "café 😀"],
198 ])("preserves bounded input summary normalization for case %#", (input, summary) => {
199 const projected = projectTurnItemForWire({ ...base, input });
200 expect(projected.type === "dynamic_tool" ? projected.input : null).toEqual({
201 summary,
202 truncated: true,
203 });
204 });
205​
206 it("uses encoded JSON bytes for strings near the dynamic-value limit", () => {
207 const small = '"'.repeat(8_191);
208 const large = '"'.repeat(8_192);
209 const smallItem = { ...base, input: small };
210 expect(projectTurnItemForWire(smallItem)).toEqual(smallItem);
211 const projected = projectTurnItemForWire({ ...base, input: large });
212 expect(projected.type === "dynamic_tool" ? projected.input : null).toEqual({
213 summary: '"'.repeat(159) + "…",
214 truncated: true,
215 });
216 });
217​
218 it("serializes an oversized structured input only once for its summary", () => {
219 let serializations = 0;
220 const input = {
221 toJSON() {
222 serializations += 1;
223 return { text: "x".repeat(100_000) };
224 },
225 };
226 const projected = projectTurnItemForWire({ ...base, input });
227 expect(serializations).toBe(1);
228 expect(projected.type === "dynamic_tool" ? projected.input : null).toMatchObject({
229 truncated: true,
230 });
231 });
232​
233 it("truncates detail at a UTF-8 boundary without changing the source", () => {
234 const progress = "a".repeat(32_767) + "😀" + "x".repeat(100_000);
235 const item = {
236 ...base,
237 type: "subagent" as const,
238 subagentId: NodeId.make("child-agent"),
239 origin: "app_owned" as const,
240 driver: ProviderDriverKind.make("codex"),
241 providerInstanceId: ProviderInstanceId.make("codex"),
242 childThreadId: null,
243 prompt: "Inspect code",
244 result: null,
245 progress,
246 };
247 const projected = projectTurnItemForWire(item);
248 expect(projected.type === "subagent" ? projected.progress : null).toBe(
249 "a".repeat(32_767) + "\n… output truncated for transport",
250 );
251 expect(item.progress).toBe(progress);
252 });
253​
254 it.each(["", "small output", "x".repeat(1_048_576)])(
255 "omits command output of every size, case %#",
256 (output) => {
257 const item = { ...base, type: "command_execution" as const, input: "test", output };
258 const projected = projectTurnItemForWire(item);
259 expect(projected).not.toHaveProperty("output");
260 expect(projected).toMatchObject({ input: "test", status: "completed" });
261 expect(projected.type === "command_execution" ? projected.outputOmitted : null).toBe(
262 output ? true : undefined,
263 );
264 expect(item.output).toBe(output);
265 },
266 );
267​
268 it.each([
269 ["echo ok", "echo ok"],
270 ["a".repeat(262_143) + "😀", "a".repeat(262_143) + "\n… output truncated for transport"],
271 ])("bounds fetched command input without changing persistence, case %#", (input, expected) => {
272 const item = { ...base, type: "command_execution" as const, input, output: "ok" };
273 const projected = projectTurnItemForDetail(item);
274 expect(projected).toMatchObject({ input: expected, output: "ok" });
275 expect(item.input).toBe(input);
276 });
277​
278 it("leaves a screenshot's bytes out of the fetched tool output", () => {
279 const data = "A".repeat(600_000);
280 const item = {
281 ...base,
282 type: "dynamic_tool" as const,
283 toolName: "mcp__t3-code__device_screenshot",
284 input: { deviceId: "phone" },
285 output: {
286 content: [{ type: "image", source: { type: "base64", media_type: "image/png", data } }],
287 },
288 };
289 expect(projectTurnItemForDetail(item)).toMatchObject({
290 output: { content: [{ type: "image", mimeType: "image/png" }] },
291 });
292 expect(item.output.content[0]?.source.data).toBe(data);
293 });
294​
295 it("keeps image markers when the rest of a tool output is too large to send", () => {
296 const image = {
297 type: "image",
298 source: { type: "base64", media_type: "image/png", data: "AAAA" },
299 };
300 const item = {
301 ...base,
302 type: "dynamic_tool" as const,
303 output: { content: [{ type: "text", text: "x".repeat(300_000) }, image] },
304 };
305 const projected = projectTurnItemForDetail(item);
306 const output = projected.type === "dynamic_tool" ? projected.output : null;
307 expect(Array.isArray(output) ? output.slice(1) : null).toEqual([
308 { type: "image", mimeType: "image/png" },
309 ]);
310 expect(JSON.stringify(output).length).toBeLessThan(270_000);
311 });
312​
313 it("bounds a tool output made of many images", () => {
314 const image = { type: "image", data: "AAAA", mimeType: "image/png" };
315 const item = {
316 ...base,
317 type: "dynamic_tool" as const,
318 output: Array.from({ length: 10_000 }, () => image),
319 };
320 const projected = projectTurnItemForDetail(item);
321 const output = projected.type === "dynamic_tool" ? projected.output : null;
322 expect(Array.isArray(output) ? output.length : null).toBe(1 + MAX_TOOL_OUTPUT_IMAGES);
323 expect(Array.isArray(output) ? String(output[0]?.text).length : null).toBeLessThan(263_000);
324 expect(toolOutputImages(output)).toHaveLength(MAX_TOOL_OUTPUT_IMAGES);
325 });
326​
327 it("keeps failure evidence without retaining command output", () => {
328 const item = {
329 ...base,
330 type: "command_execution" as const,
331 input: "cat missing-file",
332 output: "cat: missing-file: No such file or directory",
333 };
334 const projected = projectTurnItemForWire(item);
335 expect(projected).not.toHaveProperty("output");
336 expect(projected).toMatchObject({ outputIndicatesFailure: true });
337 expect(projectTurnItemForWire(projected)).toEqual(projected);
338 expect(projectTurnItemForWire({ ...item, output: "", exitCode: 2 })).toMatchObject({
339 outputIndicatesFailure: true,
340 exitCode: 2,
341 });
342 });
343​
344 it("omits inline file bodies but preserves file identity and change counts", () => {
345 const item = {
346 ...base,
347 type: "file_change" as const,
348 fileName: "src/main.ts",
349 additions: 3,
350 deletions: 1,
351 diffStr: "+new code",
352 oldStr: "old code",
353 newStr: "new code",
354 };
355 const projected = projectTurnItemForWire(item);
356 expect(projected).not.toHaveProperty("diffStr");
357 expect(projected).not.toHaveProperty("oldStr");
358 expect(projected).not.toHaveProperty("newStr");
359 expect(projected).toMatchObject({ fileName: "src/main.ts", additions: 3, deletions: 1 });
360 expect(item.diffStr).toBe("+new code");
361 const failed = projectTurnItemForWire({
362 ...item,
363 status: "failed",
364 diffStr: "String to replace not found",
365 });
366 expect(failed).toMatchObject({ diffStr: "String to replace not found" });
367 expect(failed).not.toHaveProperty("newStr");
368 });
369​
370 it("retains only result identities and failure metadata in live tool events", () => {
371 const output = {
372 isError: true,
373 structuredContent: { threadId: "child-thread", messageId: "message", text: "PRIVATE_BODY" },
374 content: [{ type: "text", text: "PRIVATE_BODY" }],
375 };
376 const event = {
377 id: EventId.make("result-event"),
378 type: "turn-item.updated" as const,
379 threadId: base.threadId,
380 occurredAt: base.updatedAt,
381 payload: { ...base, output },
382 };
383 const projected = projectDomainEventForWire(event);
384 expect(projected.payload).toMatchObject({
385 output: { isError: true, threadId: "child-thread", messageId: "message" },
386 });
387 expect(JSON.stringify(projected)).not.toContain("PRIVATE_BODY");
388 expect(event.payload.output).toBe(output);
389 expect(projectDomainEventForWire(projected)).toEqual(projected);
390 });
391​
392 it("does not serialize raw dynamic output to discard it", () => {
393 const output = {
394 toJSON: () => {
395 throw new Error("Raw output must not be serialized");
396 },
397 };
398 expect(projectTurnItemForWire({ ...base, output })).not.toHaveProperty("output");
399 });
400});