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