xonecas.io
64875
Mc

mini-coder / commit ba47455

fix(llm-api): preserve tool-result output schema in compaction

xonecas2026-03-12 ba47455ee78722fc5dd0f66fb8e58853e1276e95 parent 0387112

2 files changed

src/llm-api/turn.test.ts+22−5
@@ -557,8 +557,7 @@ describe("getMessageDiagnostics", () => {
557557 });
558558
559559 describe("compactToolResultPayloads", () => {
560- test("compacts oversized tool payloads with truncation metadata", () => {
561- const largePayload = "x".repeat(10_000);
560+ function expectCompactedJsonOutput(output: unknown): void {
562561 const messages: CoreMessage[] = [
563562 {
564563 role: "tool",
@@ -567,7 +566,7 @@ describe("compactToolResultPayloads", () => {
567566 type: "tool-result",
568567 toolCallId: "1",
569568 toolName: "read",
570- output: { blob: largePayload },
569+ output,
571570 },
572571 ],
573572 } as unknown as CoreMessage,
@@ -577,8 +576,26 @@ describe("compactToolResultPayloads", () => {
577576 const part = (compacted[0] as { content: Array<Record<string, unknown>> })
578577 .content[0];
579578 expect(part?.output).toMatchObject({
580- truncated: true,
581- strategy: "head-tail",
579+ type: "json",
580+ value: {
581+ truncated: true,
582+ strategy: "head-tail",
583+ },
582584 });
585+ }
586+
587+ test("compacts oversized tool payloads without breaking output schema", () => {
588+ const largePayload = "x".repeat(10_000);
589+ expectCompactedJsonOutput({ type: "json", value: { blob: largePayload } });
590+ });
591+
592+ test("wraps compacted legacy raw output into json output schema", () => {
593+ const largePayload = "x".repeat(10_000);
594+ expectCompactedJsonOutput({ blob: largePayload });
595+ });
596+
597+ test("rewraps compacted typed text output into json schema", () => {
598+ const largePayload = "x".repeat(10_000);
599+ expectCompactedJsonOutput({ type: "text", value: largePayload });
583600 });
584601 });
src/llm-api/turn.ts+17−2
@@ -175,6 +175,15 @@ function compactHeadTail(
175175 };
176176 }
177177
178+function wrapCompactedToolResultOutput(
179+ compactedPayload: Record<string, unknown>,
180+): unknown {
181+ return {
182+ type: "json",
183+ value: compactedPayload,
184+ };
185+}
186+
178187 export function compactToolResultPayloads(
179188 messages: CoreMessage[],
180189 capBytes = DEFAULT_TOOL_RESULT_PAYLOAD_CAP_BYTES,
@@ -214,12 +223,18 @@ export function compactToolResultPayloads(
214223 contentMutated = true;
215224 const partRecord = part as Record<string, unknown>;
216225 if ("output" in partRecord) {
217- return { ...partRecord, output: compactedPayload };
226+ return {
227+ ...partRecord,
228+ output: wrapCompactedToolResultOutput(compactedPayload),
229+ };
218230 }
219231 if ("result" in partRecord) {
220232 return { ...partRecord, result: compactedPayload };
221233 }
222- return { ...partRecord, output: compactedPayload };
234+ return {
235+ ...partRecord,
236+ output: wrapCompactedToolResultOutput(compactedPayload),
237+ };
223238 });
224239
225240 if (!contentMutated) return message;