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

WireProjection.test.ts

304 lines10.8 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("keeps transcript bodies out of shell rows", () => {
98 const now = DateTime.makeUnsafe("2026-08-13T00:00:00.000Z");
99 const threadId = ThreadId.make("thread-shell-budget");
100 const projection = {
101 thread: {
102 createdBy: "user",
103 creationSource: "web",
104 id: threadId,
105 projectId: ProjectId.make("project-shell-budget"),
106 title: "Shell payload budget",
107 providerInstanceId: ProviderInstanceId.make("codex"),
108 modelSelection: null,
109 runtimeMode: "full-access",
110 interactionMode: "default",
111 branch: null,
112 worktreePath: null,
113 lineage: {
114 parentThreadId: null,
115 relationshipToParent: null,
116 rootThreadId: threadId,
117 },
118 forkedFrom: null,
119 activeProviderThreadId: null,
120 createdAt: now,
121 updatedAt: now,
122 archivedAt: null,
123 settledOverride: null,
124 settledAt: null,
125 deletedAt: null,
126 },
127 runs: [],
128 attempts: [],
129 runtimeRequests: [],
130 messages: [
131 {
132 id: MessageId.make("message-shell-budget"),
133 role: "assistant",
134 text: "x".repeat(1_000_000),
135 updatedAt: now,
136 },
137 ],
138 providerSessions: [],
139 providerThreads: [],
140 turnItems: [],
141 visibleTurnItems: [],
142 plans: [],
143 updatedAt: now,
144 } as unknown as OrchestrationV2ThreadProjection;
145​
146 const shell = threadShellFromProjection(projection);
147​
148 expect(shell.latestVisibleMessage).toBeNull();
149 expect(JSON.stringify(shell).length).toBeLessThan(2_000);
150 });
151​
152 it("omits even small dynamic tool results while retaining input", () => {
153 const item = { ...base, output: { ok: true } } satisfies OrchestrationV2TurnItem;
154 expect(projectTurnItemForWire(item)).toEqual({ ...base, outputOmitted: true });
155 expect(item.output).toEqual({ ok: true });
156 });
157​
158 it("uses encoded JSON bytes for strings near the dynamic-value limit", () => {
159 const small = '"'.repeat(8_191);
160 const large = '"'.repeat(8_192);
161 const smallItem = { ...base, input: small };
162 expect(projectTurnItemForWire(smallItem)).toEqual(smallItem);
163 const projected = projectTurnItemForWire({ ...base, input: large });
164 expect(projected.type === "dynamic_tool" ? projected.input : null).toEqual({
165 summary: '"'.repeat(159) + "…",
166 truncated: true,
167 });
168 });
169​
170 it("truncates detail at a UTF-8 boundary without changing the source", () => {
171 const progress = "a".repeat(32_767) + "😀" + "x".repeat(100_000);
172 const item = {
173 ...base,
174 type: "subagent" as const,
175 subagentId: NodeId.make("child-agent"),
176 origin: "app_owned" as const,
177 driver: ProviderDriverKind.make("codex"),
178 providerInstanceId: ProviderInstanceId.make("codex"),
179 childThreadId: null,
180 prompt: "Inspect code",
181 result: null,
182 progress,
183 };
184 const projected = projectTurnItemForWire(item);
185 expect(projected.type === "subagent" ? projected.progress : null).toBe(
186 "a".repeat(32_767) + "\n… output truncated for transport",
187 );
188 expect(item.progress).toBe(progress);
189 });
190​
191 it("leaves a screenshot's bytes out of the fetched tool output", () => {
192 const data = "A".repeat(600_000);
193 const item = {
194 ...base,
195 type: "dynamic_tool" as const,
196 toolName: "mcp__t3-code__device_screenshot",
197 input: { deviceId: "phone" },
198 output: {
199 content: [{ type: "image", source: { type: "base64", media_type: "image/png", data } }],
200 },
201 };
202 expect(projectTurnItemForDetail(item)).toMatchObject({
203 output: { content: [{ type: "image", mimeType: "image/png" }] },
204 });
205 expect(item.output.content[0]?.source.data).toBe(data);
206 });
207​
208 it("keeps image markers when the rest of a tool output is too large to send", () => {
209 const image = {
210 type: "image",
211 source: { type: "base64", media_type: "image/png", data: "AAAA" },
212 };
213 const item = {
214 ...base,
215 type: "dynamic_tool" as const,
216 output: { content: [{ type: "text", text: "x".repeat(300_000) }, image] },
217 };
218 const projected = projectTurnItemForDetail(item);
219 const output = projected.type === "dynamic_tool" ? projected.output : null;
220 expect(Array.isArray(output) ? output.slice(1) : null).toEqual([
221 { type: "image", mimeType: "image/png" },
222 ]);
223 expect(JSON.stringify(output).length).toBeLessThan(270_000);
224 });
225​
226 it("bounds a tool output made of many images", () => {
227 const image = { type: "image", data: "AAAA", mimeType: "image/png" };
228 const item = {
229 ...base,
230 type: "dynamic_tool" as const,
231 output: Array.from({ length: 10_000 }, () => image),
232 };
233 const projected = projectTurnItemForDetail(item);
234 const output = projected.type === "dynamic_tool" ? projected.output : null;
235 expect(Array.isArray(output) ? output.length : null).toBe(1 + MAX_TOOL_OUTPUT_IMAGES);
236 expect(Array.isArray(output) ? String(output[0]?.text).length : null).toBeLessThan(263_000);
237 expect(toolOutputImages(output)).toHaveLength(MAX_TOOL_OUTPUT_IMAGES);
238 });
239​
240 it("keeps failure evidence without retaining command output", () => {
241 const item = {
242 ...base,
243 type: "command_execution" as const,
244 input: "cat missing-file",
245 output: "cat: missing-file: No such file or directory",
246 };
247 const projected = projectTurnItemForWire(item);
248 expect(projected).not.toHaveProperty("output");
249 expect(projected).toMatchObject({ outputIndicatesFailure: true });
250 expect(projectTurnItemForWire(projected)).toEqual(projected);
251 expect(projectTurnItemForWire({ ...item, output: "", exitCode: 2 })).toMatchObject({
252 outputIndicatesFailure: true,
253 exitCode: 2,
254 });
255 });
256​
257 it("omits inline file bodies but preserves file identity and change counts", () => {
258 const item = {
259 ...base,
260 type: "file_change" as const,
261 fileName: "src/main.ts",
262 additions: 3,
263 deletions: 1,
264 diffStr: "+new code",
265 oldStr: "old code",
266 newStr: "new code",
267 };
268 const projected = projectTurnItemForWire(item);
269 expect(projected).not.toHaveProperty("diffStr");
270 expect(projected).not.toHaveProperty("oldStr");
271 expect(projected).not.toHaveProperty("newStr");
272 expect(projected).toMatchObject({ fileName: "src/main.ts", additions: 3, deletions: 1 });
273 expect(item.diffStr).toBe("+new code");
274 const failed = projectTurnItemForWire({
275 ...item,
276 status: "failed",
277 diffStr: "String to replace not found",
278 });
279 expect(failed).toMatchObject({ diffStr: "String to replace not found" });
280 expect(failed).not.toHaveProperty("newStr");
281 });
282​
283 it("retains only result identities and failure metadata in live tool events", () => {
284 const output = {
285 isError: true,
286 structuredContent: { threadId: "child-thread", messageId: "message", text: "PRIVATE_BODY" },
287 content: [{ type: "text", text: "PRIVATE_BODY" }],
288 };
289 const event = {
290 id: EventId.make("result-event"),
291 type: "turn-item.updated" as const,
292 threadId: base.threadId,
293 occurredAt: base.updatedAt,
294 payload: { ...base, output },
295 };
296 const projected = projectDomainEventForWire(event);
297 expect(projected.payload).toMatchObject({
298 output: { isError: true, threadId: "child-thread", messageId: "message" },
299 });
300 expect(JSON.stringify(projected)).not.toContain("PRIVATE_BODY");
301 expect(event.payload.output).toBe(output);
302 expect(projectDomainEventForWire(projected)).toEqual(projected);
303 });
304});