Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { summarizeT3ToolCalls, type T3ToolSummaryCall } from "./t3ToolSummary.ts";
4
5function completed(input: unknown, output?: unknown): T3ToolSummaryCall {
6 return { input, output, outcome: "completed" };
7}
8
9describe("summarizeT3ToolCalls", () => {
10 it("counts registered projects, repository destinations, and accepted thread launches", () => {
11 expect(
12 summarizeT3ToolCalls("project-create", [
13 completed({}, { id: "project-1" }),
14 completed({}, { id: "project-1" }),
15 completed({}, { id: "project-2" }),
16 ]).label,
17 ).toBe("Registered 2 projects");
18 expect(
19 summarizeT3ToolCalls("project-clone", [
20 completed({}, { cwd: "/tmp/first" }),
21 completed({}, { cwd: "/tmp/second" }),
22 ]).label,
23 ).toBe("Cloned 2 repositories");
24 expect(
25 summarizeT3ToolCalls("thread-create", [
26 completed({}, { threadId: "launched-thread", status: "preparing" }),
27 ]).label,
28 ).toBe("Created 1 thread");
29 });
30
31 it.each([
32 ["queue-read", "Read 1 queued message"],
33 ["queue-edit", "Edited 1 queued message"],
34 ["queue-cancel", "Requested cancellation of 1 queued run"],
35 ["queue-reorder", "Reordered 1 queued run"],
36 ["queue-steer", "Requested steering with 1 queued message"],
37 ] as const)("deduplicates the queued run target for %s", (action, label) => {
38 expect(
39 summarizeT3ToolCalls(action, [
40 completed({ queuedRunId: "queued-1" }),
41 completed({ queuedRunId: "queued-1" }),
42 { input: { queuedRunId: "queued-2" }, output: undefined, outcome: "unfinished" },
43 ]),
44 ).toEqual({ label, failedCount: 0 });
45 });
46
47 it("counts answered requests rather than pretending every request contains one question", () => {
48 expect(
49 summarizeT3ToolCalls("question-respond", [
50 completed({ requestId: "request-1", answers: { one: ["Yes"], two: ["No"] } }),
51 completed({ requestId: "request-1" }),
52 completed({ requestId: "request-2" }),
53 ]).label,
54 ).toBe("Answered 2 pending question requests");
55 });
56
57 it("counts attachments in distinct messages and falls back when attachment counts are missing", () => {
58 const first = completed(
59 { threadId: "thread-1", attachments: [{ id: "one" }, { id: "two" }] },
60 { messageId: "message-1", threadId: "thread-1" },
61 );
62 const second = completed(
63 { threadId: "thread-2", attachments: [{ id: "one" }] },
64 { messageId: "message-2", threadId: "thread-2" },
65 );
66 expect(summarizeT3ToolCalls("attachment-send", [first, first, second])).toEqual({
67 label: "Sent 3 attachments to 2 threads",
68 failedCount: 0,
69 });
70 expect(
71 summarizeT3ToolCalls("attachment-send", [first, completed({ threadId: "thread-1" })]).label,
72 ).toBe("Sent attachments to 1 thread 2 times");
73 });
74
75 it("keeps repeated manual runs separate and describes asynchronous controls as requests", () => {
76 expect(
77 summarizeT3ToolCalls("schedule-run", [
78 completed({ taskId: "schedule-1" }, { lastRunStatus: "running" }),
79 completed({ taskId: "schedule-1" }, { lastRunStatus: "skipped" }),
80 ]).label,
81 ).toBe("Requested 2 scheduled task runs");
82 expect(
83 summarizeT3ToolCalls("thread-fork", [completed({}, { targetThreadId: "fork", sequence: 3 })])
84 .label,
85 ).toBe("Requested 1 thread fork");
86 expect(
87 summarizeT3ToolCalls("thread-merge", [
88 completed({ targetThreadId: "parent" }, { sequence: 4 }),
89 ]).label,
90 ).toBe("Requested 1 context merge");
91 });
92
93 it.each([
94 "WorktreeMcpFailure",
95 "DeviceOperationError",
96 "PreviewAutomationExecutionError",
97 "PullRequestOperationError",
98 ])("treats a returned %s as a failed call even if the provider says completed", (_tag) => {
99 const output = [
100 {
101 type: "content",
102 content: { type: "text", text: JSON.stringify({ _tag, message: "Unavailable" }) },
103 },
104 ];
105 expect(summarizeT3ToolCalls("browser", [completed({}, output)])).toEqual({
106 label: "Tried to use browser 1 time",
107 failedCount: 1,
108 });
109 });
110 it("counts messages and distinct destinations across delivery modes, deduplicating retries", () => {
111 const calls = Array.from({ length: 5 }, (_, i) =>
112 completed(
113 { threadId: `thread-${i % 2}`, mode: ["auto", "queue", "steer", "restart"][i % 4] },
114 { messageId: `message-${i}`, threadId: `thread-${i % 2}` },
115 ),
116 );
117 expect(summarizeT3ToolCalls("thread-send", [...calls, calls[0]!])).toEqual({
118 label: "Sent 5 messages to 2 threads",
119 failedCount: 0,
120 });
121 });
122
123 it("reads provider result envelopes without treating JSON in the message as result data", () => {
124 const result = { messageId: "message-1", threadId: "actual-thread" };
125 const json = JSON.stringify(result);
126 const outputs = [
127 result,
128 { structuredContent: result },
129 [{ type: "text", text: json }],
130 { content: [{ text: { text: json } }], isError: false },
131 json,
132 ];
133 const calls = outputs.map((output) =>
134 completed(
135 {
136 toolName: "t3_thread_send",
137 args: { threadId: "input-thread", message: '{"threadId":"fake"}' },
138 },
139 output,
140 ),
141 );
142 expect(summarizeT3ToolCalls("thread-send", calls).label).toBe("Sent 1 message to 1 thread");
143 expect(
144 summarizeT3ToolCalls("thread-send", [
145 completed({ toolName: "t3_thread_send", args: { threadId: "input-thread" } }),
146 completed({ threadId: "input-thread" }),
147 ]).label,
148 ).toBe("Sent 2 messages to 1 thread");
149 });
150
151 it("falls back to message counts when a destination is missing or a result is malformed", () => {
152 expect(
153 summarizeT3ToolCalls("thread-send", [
154 completed({ threadId: "known" }),
155 completed({ message: '{"threadId":"not-a-destination"}' }, "{truncated"),
156 completed(undefined, "Message sent"),
157 ]).label,
158 ).toBe("Sent 3 messages");
159 });
160
161 it("counts batch-created threads, excludes rollbacks, and deduplicates returned thread IDs", () => {
162 const threads = Array.from({ length: 4 }, (_, i) => ({
163 threadId: `thread-${i}`,
164 status: "running",
165 }));
166 expect(
167 summarizeT3ToolCalls("thread-create", [
168 completed(
169 {},
170 { threads: [...threads, { threadId: "rolled-back", status: "rolled_back" }] },
171 ),
172 completed({}, { threadId: "thread-0", status: "running" }),
173 ]).label,
174 ).toBe("Created 4 threads");
175 expect(
176 summarizeT3ToolCalls("thread-create", [
177 completed({ threads: [{ title: "Requested, not confirmed" }] }),
178 ]).label,
179 ).toBe("Requested thread creation 1 time");
180 });
181
182 it("excludes failed and unfinished sends even if the provider reports completed", () => {
183 const calls: T3ToolSummaryCall[] = [
184 completed({ threadId: "success" }, { messageId: "ok", threadId: "success" }),
185 completed(
186 { threadId: "failed" },
187 { isError: true, structuredContent: { threadId: "failed" } },
188 ),
189 completed({ threadId: "failed" }, [
190 { type: "text", text: JSON.stringify({ _tag: "OrchestratorMcpFailure" }) },
191 ]),
192 { input: { threadId: "cancelled" }, output: undefined, outcome: "unfinished" },
193 ];
194 expect(summarizeT3ToolCalls("thread-send", calls)).toEqual({
195 label: "Sent 1 message to 1 thread",
196 failedCount: 2,
197 });
198 expect(summarizeT3ToolCalls("thread-send", [calls[1]!])).toEqual({
199 label: "Tried to send 1 message to 1 thread",
200 failedCount: 1,
201 });
202 });
203
204 it("does not confuse a child's failure or wait timeout with failure of the orchestration call", () => {
205 const failedChild = { taskId: "task-1", status: "failed", summary: "command not found" };
206 expect(
207 summarizeT3ToolCalls("delegate", [completed({}, failedChild), completed({}, failedChild)]),
208 ).toEqual({
209 label: "Delegated 1 task",
210 failedCount: 0,
211 });
212 expect(
213 summarizeT3ToolCalls(
214 "task-status",
215 Array.from({ length: 4 }, () => completed({ taskId: "task-1" }, failedChild)),
216 ).label,
217 ).toBe("Checked task status 4 times");
218 expect(
219 summarizeT3ToolCalls("thread-wait", [
220 completed({ threadId: "thread-1" }, { threadId: "thread-1", timedOut: true }),
221 ]),
222 ).toEqual({
223 label: "Waited on 1 thread",
224 failedCount: 0,
225 });
226 });
227
228 it("describes control requests without claiming that a thread stopped or a task was deleted", () => {
229 expect(
230 summarizeT3ToolCalls("thread-interrupt", [
231 completed(
232 { threadId: "thread-1" },
233 { threadId: "thread-1", status: "interrupt_requested" },
234 ),
235 ]).label,
236 ).toBe("Requested interrupts for 1 thread");
237 expect(
238 summarizeT3ToolCalls("task-cancel", [
239 completed({ taskId: "task-1" }, { taskId: "task-1", status: "cancel_requested" }),
240 ]).label,
241 ).toBe("Requested cancellation of 1 task");
242 expect(
243 summarizeT3ToolCalls("schedule-delete", [
244 completed(
245 { scheduledTaskId: "schedule-1" },
246 { scheduledTaskId: "schedule-1", deleted: false },
247 ),
248 ]).label,
249 ).toBe("Requested deletion of 1 scheduled task");
250 });
251});