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

ActivityPayloadProjection.test.ts

346 lines11.8 KB

1import { describe, expect, it } from "vite-plus/test";
2import type { OrchestrationThreadActivity } from "@t3tools/contracts";
3import { projectActivityPayload } from "./ActivityPayloadProjection.ts";
4​
5function activity(payload: Record<string, unknown>): OrchestrationThreadActivity {
6 return {
7 id: "activity-1",
8 tone: "tool",
9 kind: "tool.completed",
10 summary: "Tool",
11 payload,
12 turnId: null,
13 createdAt: "2026-08-01T10:00:00.000Z",
14 } as unknown as OrchestrationThreadActivity;
16​
17/**
18 * Wire-survival regression: the slimming pass rewrites payload.data but must
19 * never strip the top-level per-agent fields the subagent fold depends on.
20 * If slimming ever moves to an allowlist over the whole payload, these
21 * assertions are the tripwire.
22 */
23describe("projectActivityPayload", () => {
24 it("preserves tool attribution (agentId/parentToolUseId) through data slimming", () => {
25 const projected = projectActivityPayload(
26 activity({
27 itemType: "command_execution",
28 agentId: "task-123",
29 parentToolUseId: "toolu_abc",
30 data: {
31 toolName: "Bash",
32 input: { command: "ls" },
33 command: "ls",
34 rawOutput: { content: "x".repeat(10) },
35 somethingClientNeverReads: { big: "blob" },
36 },
37 }),
38 );
39 const payload = projected.payload as Record<string, unknown>;
40 expect(payload.agentId).toBe("task-123");
41 expect(payload.parentToolUseId).toBe("toolu_abc");
42 // Slimming itself still applies to data.
43 const data = payload.data as Record<string, unknown>;
44 expect(data.somethingClientNeverReads).toBeUndefined();
45 });
46​
47 it("keeps a bounded Codex command output summary", () => {
48 const projected = projectActivityPayload(
49 activity({
50 itemType: "command_execution",
51 data: {
52 item: {
53 command: "/bin/zsh -lc 'printf hello'",
54 aggregatedOutput: `hello from codex\n${"x".repeat(5000)}`,
55 },
56 },
57 }),
58 );
59 const data = (projected.payload as Record<string, unknown>).data as Record<string, unknown>;
60 expect(data.item).toEqual({
61 command: "/bin/zsh -lc 'printf hello'",
62 aggregatedOutput: "hello from codex",
63 });
64 expect(JSON.stringify(projected.payload).length).toBeLessThan(500);
65 });
66​
67 it("keeps preview normalization and fence-only fallback while scanning lines", () => {
68 const preview = projectActivityPayload(
69 activity({
70 itemType: "command_execution",
71 data: { rawOutput: `\`\`\`\n actual\tresult \n${"x".repeat(5000)}` },
72 }),
73 );
74 const fences = projectActivityPayload(
75 activity({
76 itemType: "command_execution",
77 data: { rawOutput: "```\r\n \t \n```\n" },
78 }),
79 );
80​
81 expect((preview.payload as { data: { rawOutput: unknown } }).data.rawOutput).toEqual({
82 content: "actual result",
83 });
84 expect((fences.payload as { data: { rawOutput: unknown } }).data.rawOutput).toEqual({
85 content: "2 lines",
86 });
87 });
88​
89 it("keeps bounded Claude and ACP command output summaries", () => {
90 const claude = projectActivityPayload(
91 activity({
92 itemType: "command_execution",
93 data: {
94 command: "printf hello",
95 rawOutput: { stdout: `hello from claude\n${"y".repeat(5000)}` },
96 },
97 }),
98 );
99 const acp = projectActivityPayload(
100 activity({
101 itemType: "command_execution",
102 data: {
103 command: "printf hello",
104 content: [
105 {
106 type: "content",
107 content: { type: "text", text: `hello from acp\n${"z".repeat(5000)}` },
108 },
109 ],
110 },
111 }),
112 );
113​
114 const claudeData = (claude.payload as Record<string, unknown>).data as Record<string, unknown>;
115 const acpData = (acp.payload as Record<string, unknown>).data as Record<string, unknown>;
116 expect(claudeData.rawOutput).toEqual({ content: "hello from claude" });
117 expect(acpData.rawOutput).toEqual({ content: "hello from acp" });
118 expect(JSON.stringify(claude.payload).length).toBeLessThan(500);
119 expect(JSON.stringify(acp.payload).length).toBeLessThan(500);
120 });
121​
122 it("keeps bounded Claude command input and result summaries", () => {
123 const claude = projectActivityPayload(
124 activity({
125 itemType: "command_execution",
126 toolCallId: "claude-call-1",
127 data: {
128 toolName: "Bash",
129 input: { command: "vp test run" },
130 result: {
131 type: "tool_result",
132 content: [
133 { type: "text", text: "tests passed" },
134 { type: "text", text: "x".repeat(5_000) },
135 ],
136 },
137 },
138 }),
139 );
140 const openCode = projectActivityPayload(
141 activity({
142 itemType: "command_execution",
143 toolCallId: "opencode-call-1",
144 data: {
145 tool: "bash",
146 state: {
147 status: "running",
148 input: { command: "vp lint" },
149 output: "x".repeat(5_000),
150 },
151 },
152 }),
153 );
154​
155 expect(claude.payload).toMatchObject({
156 toolCallId: "claude-call-1",
157 data: {
158 toolName: "Bash",
159 command: "vp test run",
160 rawOutput: { content: "tests passed" },
161 },
162 });
163 expect(openCode.payload).toMatchObject({
164 toolCallId: "opencode-call-1",
165 data: { command: "vp lint" },
166 });
167 expect(JSON.stringify(claude.payload).length).toBeLessThan(250);
168 expect(JSON.stringify(openCode.payload).length).toBeLessThan(200);
169 });
170​
171 it("keeps full Claude Read image paths through repeated projection", () => {
172 const imagePath = `/workspace/${"nested folder/".repeat(16)}reference image.webp`;
173 const projected = projectActivityPayload(
174 activity({
175 itemType: "dynamic_tool_call",
176 detail: 'Read: {"file_path":"truncated..."}',
177 data: {
178 toolName: "Read",
179 input: { file_path: imagePath },
180 result: { content: "Image Size: 1280x720." },
181 },
182 }),
183 );
184 const projectedAgain = projectActivityPayload(projected);
185​
186 expect(projected.payload).toMatchObject({ data: { imagePath } });
187 expect(projectedAgain.payload).toMatchObject({ data: { imagePath } });
188​
189 const textRead = projectActivityPayload(
190 activity({
191 itemType: "dynamic_tool_call",
192 data: { toolName: "Read", input: { file_path: "/workspace/src/index.ts" } },
193 }),
194 );
195 expect(textRead.payload).not.toMatchObject({ data: { imagePath: expect.anything() } });
196 });
197​
198 it("slims Codex-shaped mcp_tool_call items to rendered fields plus a result summary", () => {
199 const projected = projectActivityPayload(
200 activity({
201 itemType: "mcp_tool_call",
202 data: {
203 item: {
204 type: "mcpToolCall",
205 id: "item-1",
206 tool: "fetch_pr",
207 server: "github",
208 status: "completed",
209 arguments: { pr: 42 },
210 durationMs: 1200,
211 result: {
212 content: [{ type: "text", text: `PR body line one\n${"x".repeat(5000)}` }],
213 structuredContent: { huge: "y".repeat(5000) },
214 },
215 _meta: { internal: true },
216 },
217 },
218 }),
219 );
220 const data = (projected.payload as Record<string, unknown>).data as Record<string, unknown>;
221 const item = data.item as Record<string, unknown>;
222 expect(item.tool).toBe("fetch_pr");
223 expect(item.server).toBe("github");
224 expect(item.arguments).toEqual({ pr: 42 });
225 expect(item._meta).toBeUndefined();
226 expect(item.result).toEqual({ content: "PR body line one" });
227 expect(JSON.stringify(projected.payload).length).toBeLessThan(500);
228 });
229​
230 it("slims Claude-shaped mcp_tool_call data (toolName/input/result block)", () => {
231 const projected = projectActivityPayload(
232 activity({
233 itemType: "mcp_tool_call",
234 data: {
235 toolName: "mcp__github__fetch_pr",
236 input: { pr: 42 },
237 result: {
238 type: "tool_result",
239 tool_use_id: "toolu_1",
240 content: [{ type: "text", text: `first line of output\n${"z".repeat(5000)}` }],
241 },
242 },
243 }),
244 );
245 const data = (projected.payload as Record<string, unknown>).data as Record<string, unknown>;
246 expect(data.toolName).toBe("mcp__github__fetch_pr");
247 expect(data.input).toEqual({ pr: 42 });
248 expect(data.result).toEqual({ content: "first line of output" });
249 expect(JSON.stringify(projected.payload).length).toBeLessThan(500);
250 });
251​
252 it.each([
253 {
254 item: {
255 server: "t3-code",
256 tool: "preview_open",
257 result: { structuredContent: { url: "https://example.com/" } },
258 },
259 },
260 {
261 toolName: "mcp__t3-code__preview_navigate",
262 result: { content: '{"url":"https://example.com/"}' },
263 },
264 { tool: "t3-code_preview_status", state: { output: '{"url":"https://example.com/"}' } },
265 {
266 toolName: "mcp__t3_code__preview_snapshot",
267 result: {
268 content: [
269 { type: "text", text: '{"url":"https://example.com/"}' },
270 { type: "text", text: "Snapshot text was bounded. Omitted: accessibilityTree." },
271 ],
272 },
273 },
274 {
275 toolName: "mcp__t3-code__preview_click",
276 result: { content: '{"toolIcon":{"_tag":"website","pageUrl":"https://example.com/"}}' },
277 },
278 {
279 toolName: "mcp__t3_code__preview_snapshot",
280 result: { content: '{"url":"https://example.com/"}\n{"accessibilityTree":"truncated' },
281 },
282 ...[false, true].map((truncated) => ({
283 toolName: "mcp__t3_code__preview_snapshot",
284 result: {
285 content: JSON.stringify({
286 content: [{ type: "text", text: '{"url":"https://example.com/"}' }],
287 structuredContent: { url: "https://example.com/", visibleText: "page" },
288 }).slice(0, truncated ? -5 : undefined),
289 },
290 })),
291 ...[
292 "type",
293 "press",
294 "scroll",
295 "resize",
296 "set_appearance",
297 "evaluate",
298 "wait_for",
299 "recording_start",
300 "recording_stop",
301 ].map((action) => ({
302 toolName: `mcp__t3_code__preview_${action}`,
303 result: { content: '{"toolIcon":{"_tag":"website","pageUrl":"https://example.com/"}}' },
304 })),
305 ])("preserves the preview page favicon through result slimming", (data) => {
306 const projected = projectActivityPayload(activity({ itemType: "mcp_tool_call", data }));
307 const icon = { _tag: "website", pageUrl: "https://example.com/" };
308 expect(projected.payload).toMatchObject({ toolIcon: icon });
309 expect(projectActivityPayload(projected).payload).toMatchObject({ toolIcon: icon });
310 });
311​
312 it.each([
313 { toolName: "mcp__other__preview_open", result: { content: '{"url":"https://example.com/"}' } },
314 {
315 toolName: "mcp__t3-code__preview_evaluate",
316 result: { content: '{"url":"https://example.com/"}' },
317 },
318 {
319 toolName: "mcp__t3-code__preview_open",
320 result: { isError: true, content: '{"url":"https://example.com/"}' },
321 },
322 { toolName: "mcp__t3-code__preview_open", result: { content: "malformed JSON" } },
323 { toolName: "mcp__t3-code__preview_open", result: { content: '{"url":"about:blank"}' } },
324 ])("keeps the fallback for unrelated tools, failed navigation, and missing page URLs", (data) => {
325 expect(
326 projectActivityPayload(activity({ itemType: "mcp_tool_call", data })).payload,
327 ).not.toHaveProperty("toolIcon");
328 });
329​
330 it("passes task lifecycle payloads (no data field) through untouched", () => {
331 const source = activity({
332 taskId: "task-9",
333 title: "Audit auth",
334 role: "explorer",
335 model: "opus",
336 effort: "high",
337 workflowName: "audit-flow",
338 phases: [{ index: 0, title: "Audit" }],
339 typedUsage: { totalTokens: 1200 },
340 runHandles: { runId: "run-1", scriptPath: "/tmp/wf.js" },
341 timelineBypass: true,
342 });
343 const projected = projectActivityPayload(source);
344 expect(projected.payload).toEqual(source.payload);
345 });
346});