Skip to content

Commit 62dcc9b

Browse files
committed
fix(agents): freeze prompt projection candidates
1 parent bda4404 commit 62dcc9b

2 files changed

Lines changed: 41 additions & 6 deletions

File tree

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,28 @@ describe("truncateOversizedToolResultsInMessages", () => {
474474
true,
475475
);
476476
expect(messages).toEqual([...prefix, ...suffix]);
477+
478+
const stableState = createToolResultPromptProjectionState();
479+
const stableHistory = [
480+
makeToolResult("a".repeat(4_000), "stable_1"),
481+
makeToolResult("b".repeat(4_000), "stable_2"),
482+
];
483+
const stableFirst = truncateOversizedToolResultsInMessages(
484+
stableHistory,
485+
128_000,
486+
12_000,
487+
12_000,
488+
stableState,
489+
);
490+
const stableSecond = truncateOversizedToolResultsInMessages(
491+
[...stableHistory, makeToolResult("c".repeat(15_000), "stable_3")],
492+
128_000,
493+
12_000,
494+
12_000,
495+
stableState,
496+
);
497+
expect(stableFirst.truncatedCount).toBe(0);
498+
expect(stableSecond.messages.slice(0, stableHistory.length)).toEqual(stableFirst.messages);
477499
});
478500
});
479501

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ export function truncateOversizedToolResultsInMessages(
356356
id: `message-${index}`,
357357
type: "message",
358358
message: (projectionKey && projectionState?.replacements.get(projectionKey)) ?? message,
359-
aggregateEligible: !projectionKey || !projectionState?.replacements.has(projectionKey),
359+
aggregateEligible: !projectionKey || !projectionState?.frozen.has(projectionKey),
360360
};
361361
});
362362
const plan = buildToolResultReplacementPlan({
@@ -365,6 +365,14 @@ export function truncateOversizedToolResultsInMessages(
365365
aggregateBudgetChars,
366366
minKeepChars: RECOVERY_MIN_KEEP_CHARS,
367367
});
368+
if (projectionState) {
369+
for (const message of messages) {
370+
const projectionKey = getToolResultProjectionKey(message);
371+
if (projectionKey) {
372+
projectionState.frozen.add(projectionKey);
373+
}
374+
}
375+
}
368376
if (plan.replacements.length === 0) {
369377
const projectedMessages = branch.map((entry) => entry.message as AgentMessage);
370378
const hasProjectedChanges = projectedMessages.some(
@@ -382,8 +390,11 @@ export function truncateOversizedToolResultsInMessages(
382390
for (const [index, originalMessage] of messages.entries()) {
383391
const projectedMessage = replacedBranch[index]?.message;
384392
const projectionKey = getToolResultProjectionKey(originalMessage);
385-
if (projectionKey && projectedMessage && projectedMessage !== originalMessage) {
386-
projectionState.replacements.set(projectionKey, projectedMessage);
393+
if (projectionKey) {
394+
projectionState.frozen.add(projectionKey);
395+
if (projectedMessage && projectedMessage !== originalMessage) {
396+
projectionState.replacements.set(projectionKey, projectedMessage);
397+
}
387398
}
388399
}
389400
}
@@ -431,21 +442,23 @@ type ToolResultReplacement = {
431442

432443
export type ToolResultPromptProjectionState = {
433444
replacements: Map<string, AgentMessage>;
445+
frozen: Set<string>;
434446
};
435447

436448
export function createToolResultPromptProjectionState(): ToolResultPromptProjectionState {
437-
return { replacements: new Map<string, AgentMessage>() };
449+
return { replacements: new Map<string, AgentMessage>(), frozen: new Set<string>() };
438450
}
439451

440452
function getToolResultProjectionKey(message: AgentMessage): string | undefined {
441453
if (message.role !== "toolResult") {
442454
return undefined;
443455
}
444456
const toolCallId = (message as { toolCallId?: unknown }).toolCallId;
457+
const timestamp = (message as { timestamp?: unknown }).timestamp;
458+
const timestampKey = typeof timestamp === "number" ? `:${timestamp}` : "";
445459
if (typeof toolCallId === "string" && toolCallId.length > 0) {
446-
return `tool:${toolCallId}`;
460+
return `tool:${toolCallId}${timestampKey}`;
447461
}
448-
const timestamp = (message as { timestamp?: unknown }).timestamp;
449462
return typeof timestamp === "number" ? `timestamp:${timestamp}` : undefined;
450463
}
451464

0 commit comments

Comments
 (0)