From 79fac9fda92ce4e732a4fa81f89f39730c00abb7 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 21 Jun 2026 22:26:22 +0800 Subject: [PATCH] fix(agents): cap new prompt projection output --- .../tool-result-truncation.test.ts | 25 +++++++++++++ .../tool-result-truncation.ts | 36 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/agents/embedded-agent-runner/tool-result-truncation.test.ts b/src/agents/embedded-agent-runner/tool-result-truncation.test.ts index 4fb13ea3f2d5..6df2a8aa879c 100644 --- a/src/agents/embedded-agent-runner/tool-result-truncation.test.ts +++ b/src/agents/embedded-agent-runner/tool-result-truncation.test.ts @@ -496,6 +496,31 @@ describe("truncateOversizedToolResultsInMessages", () => { ); expect(stableFirst.truncatedCount).toBe(0); expect(stableSecond.messages.slice(0, stableHistory.length)).toEqual(stableFirst.messages); + const stableThird = truncateOversizedToolResultsInMessages( + [ + ...stableHistory, + makeToolResult("c".repeat(15_000), "stable_3"), + makeToolResult("d".repeat(15_000), "stable_4"), + ], + 128_000, + 12_000, + 12_000, + stableState, + ); + const stableFourth = truncateOversizedToolResultsInMessages( + [ + ...stableHistory, + makeToolResult("c".repeat(15_000), "stable_3"), + makeToolResult("d".repeat(15_000), "stable_4"), + makeToolResult("e".repeat(15_000), "stable_5"), + ], + 128_000, + 12_000, + 12_000, + stableState, + ); + const lastText = stableFourth.messages.at(-1); + expect(lastText && getToolResultTextLength(lastText)).toBeLessThanOrEqual(12_000); }); it("does not restore filtered image blocks when reusing a projection", () => { diff --git a/src/agents/embedded-agent-runner/tool-result-truncation.ts b/src/agents/embedded-agent-runner/tool-result-truncation.ts index 2c0bdef5f94d..f74f33d10878 100644 --- a/src/agents/embedded-agent-runner/tool-result-truncation.ts +++ b/src/agents/embedded-agent-runner/tool-result-truncation.ts @@ -588,9 +588,45 @@ function buildAggregateToolResultReplacements(params: { remainingReduction -= actualReduction; } + if (remainingReduction > 0) { + for (const candidate of candidates.filter((item) => item.aggregateEligible)) { + if (remainingReduction <= 0) { + break; + } + if (replacements.some((replacement) => replacement.entryId === candidate.entryId)) { + continue; + } + const emptyMessage = clearToolResultText(candidate.message); + const actualReduction = Math.max( + 0, + candidate.textLength - getToolResultTextLength(emptyMessage), + ); + if (actualReduction <= 0) { + continue; + } + replacements.push({ entryId: candidate.entryId, message: emptyMessage }); + remainingReduction -= actualReduction; + } + } + return replacements; } +function clearToolResultText(message: AgentMessage): AgentMessage { + const content = (message as { content?: unknown }).content; + if (!Array.isArray(content)) { + return message; + } + return { + ...message, + content: content.map((block) => + block && typeof block === "object" && (block as { type?: unknown }).type === "text" + ? { ...block, text: "" } + : block, + ), + } as AgentMessage; +} + function buildOversizedToolResultReplacements(params: { branch: ToolResultBranchEntry[]; maxChars: number;