Skip to content

Commit edb8efa

Browse files
committed
Handle Codex toolResult blocks in truncation
1 parent b474f42 commit edb8efa

2 files changed

Lines changed: 78 additions & 9 deletions

File tree

‎src/agents/embedded-agent-runner/tool-result-truncation.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,26 @@ describe("getToolResultTextLength", () => {
163163
expect(getToolResultTextLength(msg)).toBe(8);
164164
});
165165

166+
it("counts Codex protocol toolResult content blocks", () => {
167+
const msg = {
168+
role: "toolResult",
169+
toolCallId: "call_1",
170+
toolName: "exec",
171+
isError: false,
172+
content: [
173+
{
174+
type: "toolResult",
175+
toolUseId: "call_1",
176+
text: "codex output",
177+
content: "codex output",
178+
},
179+
],
180+
timestamp: nextTimestamp(),
181+
} as unknown as ToolResultMessage;
182+
183+
expect(getToolResultTextLength(msg)).toBe("codex output".length);
184+
});
185+
166186
it("returns zero for non-toolResult messages", () => {
167187
expect(getToolResultTextLength(makeAssistantMessage("hello"))).toBe(0);
168188
});
@@ -189,6 +209,39 @@ describe("truncateToolResultMessage", () => {
189209
}
190210
expect(getFirstToolResultText(result)).toContain("[persist-truncated]");
191211
});
212+
213+
it("truncates Codex protocol toolResult content blocks and mirrored content", () => {
214+
const oversized = "x".repeat(50_000);
215+
const msg = {
216+
role: "toolResult",
217+
toolCallId: "call_1",
218+
toolName: "exec",
219+
content: [
220+
{
221+
type: "toolResult",
222+
toolUseId: "call_1",
223+
text: oversized,
224+
content: oversized,
225+
},
226+
],
227+
isError: false,
228+
timestamp: nextTimestamp(),
229+
} as unknown as ToolResultMessage;
230+
231+
const result = truncateToolResultMessage(msg, 10_000, {
232+
suffix: "\n\n[persist-truncated]",
233+
minKeepChars: 2_000,
234+
});
235+
expect(result.role).toBe("toolResult");
236+
if (result.role !== "toolResult") {
237+
throw new Error("expected toolResult");
238+
}
239+
const firstBlock = result.content[0] as unknown as { text?: unknown; content?: unknown };
240+
expect(typeof firstBlock.text).toBe("string");
241+
expect(firstBlock.text).toContain("[persist-truncated]");
242+
expect(String(firstBlock.text).length).toBeLessThan(oversized.length);
243+
expect(firstBlock.content).toBe(firstBlock.text);
244+
});
192245
});
193246

194247
describe("calculateMaxToolResultChars", () => {

‎src/agents/embedded-agent-runner/tool-result-truncation.ts‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ export function getToolResultTextLength(msg: AgentMessage): number {
259259
}
260260
let totalLength = 0;
261261
for (const block of content) {
262-
if (block && typeof block === "object" && (block as { type?: string }).type === "text") {
263-
const text = (block as TextContent).text;
262+
if (isToolResultTextBlock(block)) {
263+
const text = block.text;
264264
if (typeof text === "string") {
265265
totalLength += text.length;
266266
}
@@ -297,10 +297,10 @@ export function truncateToolResultMessage(
297297

298298
// Distribute the budget proportionally among text blocks
299299
const newContent = content.map((block: unknown) => {
300-
if (!block || typeof block !== "object" || (block as { type?: string }).type !== "text") {
300+
if (!isToolResultTextBlock(block)) {
301301
return block; // Keep non-text blocks (images) as-is
302302
}
303-
const textBlock = block as TextContent;
303+
const textBlock = block;
304304
if (typeof textBlock.text !== "string") {
305305
return block;
306306
}
@@ -314,17 +314,33 @@ export function truncateToolResultMessage(
314314
1,
315315
Math.min(maxChars, Math.max(minKeepChars + defaultSuffix.length, proportionalBudget)),
316316
);
317-
return Object.assign({}, textBlock, {
318-
text: truncateToolResultText(textBlock.text, blockBudget, {
319-
suffix: suffixFactory,
320-
minKeepChars,
321-
}),
317+
const truncatedText = truncateToolResultText(textBlock.text, blockBudget, {
318+
suffix: suffixFactory,
319+
minKeepChars,
322320
});
321+
const nextBlock = Object.assign({}, textBlock, { text: truncatedText });
322+
if (typeof textBlock.content === "string") {
323+
nextBlock.content = truncatedText;
324+
}
325+
return nextBlock;
323326
});
324327

325328
return { ...msg, content: newContent } as AgentMessage;
326329
}
327330

331+
function isToolResultTextBlock(
332+
block: unknown,
333+
): block is TextContent & { content?: unknown; type: "text" | "toolResult" } {
334+
if (!block || typeof block !== "object") {
335+
return false;
336+
}
337+
const type = (block as { type?: unknown }).type;
338+
return (
339+
(type === "text" || type === "toolResult") &&
340+
typeof (block as { text?: unknown }).text === "string"
341+
);
342+
}
343+
328344
/**
329345
* Truncate oversized tool results in an array of messages (in-memory).
330346
* Returns a new array with truncated messages.

0 commit comments

Comments
 (0)