Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { compactDynamicToolOutput, toolOutputIndicatesFailure } from "./toolOutput.ts";
4
5describe("compactDynamicToolOutput", () => {
6 it("extracts IDs through the MCP result envelopes used by T3 summaries", () => {
7 const metadata = { threadId: "thread-1", messageId: "message-1" };
8 const json = JSON.stringify({ ...metadata, response: "Private response body" });
9 for (const value of [
10 { ...metadata, response: "Private response body" },
11 { structuredContent: JSON.parse(json), content: "Ignored alternative body" },
12 [{ type: "text", text: json }],
13 { content: [{ text: { text: json } }], isError: false },
14 json,
15 ]) {
16 expect(compactDynamicToolOutput(value)).toEqual(metadata);
17 }
18 });
19
20 it("keeps nested thread identity and scalar task IDs without result bodies", () => {
21 const output = {
22 thread: { threadId: "thread-1", messages: ["Private message"] },
23 taskId: "task-1",
24 scheduledTaskId: "scheduled-1",
25 status: "failed",
26 summary: "A child failed; the tool itself succeeded.",
27 };
28 expect(compactDynamicToolOutput(output)).toEqual({
29 thread: { threadId: "thread-1" },
30 taskId: "task-1",
31 scheduledTaskId: "scheduled-1",
32 });
33 expect(output.thread.messages).toEqual(["Private message"]);
34 });
35
36 it("keeps explicit envelope failure while dropping error text and arbitrary output", () => {
37 for (const failure of [
38 { isError: true },
39 { is_error: true },
40 { _tag: "OrchestratorMcpFailure" },
41 { error: { message: "Private error" } },
42 ]) {
43 expect(
44 compactDynamicToolOutput({
45 ...failure,
46 structuredContent: { threadId: "thread-1", error: "Private details" },
47 }),
48 ).toEqual({ isError: true, threadId: "thread-1" });
49 }
50 expect(compactDynamicToolOutput("Private plain text")).toBeUndefined();
51 expect(compactDynamicToolOutput({ result: { body: "Private result" } })).toBeUndefined();
52 });
53
54 it("uses the first result data but combines explicit failure flags across content blocks", () => {
55 expect(
56 compactDynamicToolOutput([
57 { text: JSON.stringify({ threadId: "first" }) },
58 { text: JSON.stringify({ threadId: "second", isError: true, error: "Private error" }) },
59 ]),
60 ).toEqual({ threadId: "first", isError: true });
61 expect(
62 compactDynamicToolOutput([{ text: "{}" }, { text: JSON.stringify({ threadId: "second" }) }]),
63 ).toBeUndefined();
64 });
65
66 it("keeps complete thread creation batches and rollback markers without row bodies", () => {
67 expect(
68 compactDynamicToolOutput({
69 threads: [
70 { threadId: "created", status: "running", messages: ["Private message"] },
71 { threadId: "reverted", status: "rolled_back", error: "Private error" },
72 { status: "rolled_back" },
73 ],
74 }),
75 ).toEqual({
76 threads: [
77 { threadId: "created" },
78 { threadId: "reverted", status: "rolled_back" },
79 { status: "rolled_back" },
80 ],
81 });
82 expect(compactDynamicToolOutput({ threadId: "reverted", status: "rolled_back" })).toEqual({
83 threadId: "reverted",
84 status: "rolled_back",
85 });
86 expect(compactDynamicToolOutput({ threads: [] })).toEqual({ threads: [] });
87 });
88
89 it("omits incomplete or oversized creation evidence instead of returning a partial count", () => {
90 for (const threads of [
91 [{ threadId: "known" }, { title: "No confirmed ID" }],
92 Array.from({ length: 101 }, (_, index) => ({ threadId: `thread-${index}` })),
93 Array.from({ length: 100 }, (_, index) => ({ threadId: `${index}`.padEnd(256, "x") })),
94 [{ threadId: "x".repeat(257) }],
95 ]) {
96 expect(
97 compactDynamicToolOutput({
98 threadId: "must-not-be-counted-as-one",
99 status: "rolled_back",
100 taskId: "task-1",
101 threads,
102 }),
103 ).toEqual({ taskId: "task-1" });
104 }
105 const normalBatch = {
106 threads: Array.from({ length: 100 }, (_, index) => ({
107 threadId: `00000000-0000-4000-8000-${String(index).padStart(12, "0")}`,
108 })),
109 };
110 expect(compactDynamicToolOutput(normalBatch)).toEqual(normalBatch);
111 expect(
112 new TextEncoder().encode(JSON.stringify(compactDynamicToolOutput(normalBatch))).byteLength,
113 ).toBeLessThanOrEqual(8_192);
114 });
115
116 it("bounds parsing and envelope scanning while preserving an outer failure marker", () => {
117 const oversizedJson = JSON.stringify({ threadId: "hidden", body: "😄".repeat(5_000) });
118 for (const content of [
119 "x".repeat(1_000_000),
120 oversizedJson,
121 Array.from({ length: 33 }, () => ({ text: JSON.stringify({ threadId: "hidden" }) })),
122 { content: { content: { content: { content: { threadId: "too-deep" } } } } },
123 ]) {
124 expect(compactDynamicToolOutput({ isError: true, content })).toEqual({ isError: true });
125 }
126 expect(compactDynamicToolOutput({ threadId: "known", body: "x".repeat(1_000_000) })).toEqual({
127 threadId: "known",
128 });
129 });
130
131 it("is idempotent for compact metadata", () => {
132 const compact = compactDynamicToolOutput({
133 isError: true,
134 structuredContent: {
135 threadId: "thread-1",
136 threads: [{ threadId: "thread-1" }, { status: "rolled_back" }],
137 output: "Private output",
138 },
139 });
140 expect(compactDynamicToolOutput(compact)).toEqual(compact);
141 });
142});
143
144describe("toolOutputIndicatesFailure", () => {
145 it("preserves the command failure phrases across shells without copying the output", () => {
146 for (const text of [
147 "FILE NOT FOUND",
148 "No files found",
149 "ENOENT",
150 "No such file or directory",
151 "CommandNotFoundException",
152 "command not found",
153 "Cannot find path 'a' because it does not exist",
154 "The term 'example' is not recognized",
155 "is not recognized as the name of a cmdlet",
156 "A parameter cannot be found that matches parameter name",
157 "<exited with exit code 2>",
158 "Exited with exit code 1",
159 "exit code: 127",
160 ])
161 expect(toolOutputIndicatesFailure(text)).toBe(true);
162 });
163
164 it("does not mark successful exit codes or incomplete failure phrases", () => {
165 for (const text of [
166 "Done",
167 "exit code: 0",
168 "Exited with exit code 0",
169 "cannot find path",
170 "is not recognized",
171 ]) {
172 expect(toolOutputIndicatesFailure(text)).toBe(false);
173 }
174 });
175});