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

WireProjection.test.ts

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