From fe9479d89a22760a5fbe4ef659c0be556c5c3cfd Mon Sep 17 00:00:00 2001 From: Kai Date: Thu, 9 Jul 2026 17:06:19 +0800 Subject: [PATCH] fix: rewrite repeated tool call reminders to redirect instead of prohibit (#1518) The r1/r2/r3 reminders injected into repeated tool results led with prohibition verdicts and, in r2, echoed the repeated tool name and full arguments back into the context, reinforcing the very pattern they were meant to break. Rewrite them to state the situation factually and hand the model a concrete next action: an expectation-setting sentence for the next call (r1), a forced decision menu of falsify / ask-user / conclude (r2), and a final hand-off summary without further tool calls (r3). Detection, thresholds (3/5/8/12), force-stop, and telemetry are unchanged. --- .../agent-core/src/agent/turn/tool-dedup.ts | 34 ++++++++----------- .../test/agent/turn/tool-dedup.test.ts | 34 +++++++++---------- 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/packages/agent-core/src/agent/turn/tool-dedup.ts b/packages/agent-core/src/agent/turn/tool-dedup.ts index 31170f8cf..605408777 100644 --- a/packages/agent-core/src/agent/turn/tool-dedup.ts +++ b/packages/agent-core/src/agent/turn/tool-dedup.ts @@ -7,32 +7,28 @@ import { canonicalTelemetryArgs } from './canonical-args'; const REMINDER_TEXT_1 = '\n\n\n' + - 'You are repeating the exact same tool call with identical parameters.' + - ' Please carefully analyze the previous result. If the task is not yet complete,' + - ' try a different method or parameters instead of repeating the same call.' + + 'The same tool call has been repeated several times in a row. ' + + 'Before making your next call, write one sentence stating what new information you expect it to produce. ' + + 'Then act on that sentence: if it names something this result does not already give you, choose the action that best provides it; otherwise, continue with the evidence you already have.' + '\n'; -function makeReminderText2(toolName: string, repeatCount: number, args: unknown): string { - const argsStr = canonicalTelemetryArgs(args); +function makeReminderText2(repeatCount: number): string { return ( '\n\n\n' + - 'You have repeatedly called the same tool with identical parameters many times.\n' + - 'Repeated tool call detected:\n' + - `- tool: ${toolName}\n` + - `- repeated_times: ${String(repeatCount)}\n` + - `- arguments: ${argsStr}\n` + - 'The previous repeated calls did not make progress. Do not call this exact same tool with the exact same arguments again.\n' + - 'Carefully inspect the latest tool result and choose a different next action, different parameters, or finish the task if enough evidence has been gathered.' + + `The same tool call has now been issued ${String(repeatCount)} times in a row. ` + + 'Choose exactly one of the following and state your choice before acting:\n' + + '(1) Falsification check: run the cheapest test that could conclusively disprove your current approach, if such a test exists.\n' + + '(2) Missing input: tell the user precisely what information or decision you need to proceed, and ask for it.\n' + + '(3) Conclude: deliver your best result based on the evidence already gathered, listing anything that remains uncertain.' + '\n' ); } const REMINDER_TEXT_3 = '\n\n\n' + - 'You are stuck in a dead end and have repeatedly made the same function call without progress.\n' + - 'Stop all function calls immediately. Do not call any tool in your next response.\n' + - 'In analysis, review the current execution state and identify why progress is blocked.\n' + - 'Then return a text-only summary to the user that reports the current problem, what has already been tried, and what information or decision is needed next.' + + 'Write your final response now, without any further tool calls. ' + + 'Cover: the current blocker, each approach you have tried and what it established, and the specific information or decision you need from the user to unblock progress. ' + + 'Text only.' + '\n'; const REPEAT_REMINDER_1_START = 3; @@ -105,8 +101,8 @@ const DEDUP_PLACEHOLDER_RESULT: ExecutableToolResult = { output: '' }; * - Cross-step dedup: when the exact same call is repeated consecutively * across steps, the result returned to the model is suffixed with a system * reminder once the streak hits 3. The reminder escalates as the streak - * grows: r1 (gentle nudge) from streak 3, r2 (concrete repeat report) from - * streak 5, r3 (dead-end stop instruction) from streak 8. From streak 12 + * grows: r1 (expectation-setting nudge) from streak 3, r2 (forced decision + * menu) from streak 5, r3 (final hand-off instruction) from streak 8. From streak 12 * onward the turn is force-stopped via `{ stopTurn: true }` so the loop * cannot keep spinning on the same call. Force-stop does not flip a * successful tool result into an error — the underlying tool's `isError` @@ -239,7 +235,7 @@ export class ToolCallDeduplicator { finalResult = appendReminder(result, REMINDER_TEXT_3); action = 'r3'; } else if (streak >= REPEAT_REMINDER_2_START) { - finalResult = appendReminder(result, makeReminderText2(toolName, streak, args)); + finalResult = appendReminder(result, makeReminderText2(streak)); action = 'r2'; } else if (streak >= REPEAT_REMINDER_1_START) { finalResult = appendReminder(result, REMINDER_TEXT_1); diff --git a/packages/agent-core/test/agent/turn/tool-dedup.test.ts b/packages/agent-core/test/agent/turn/tool-dedup.test.ts index cff03b9bb..087831645 100644 --- a/packages/agent-core/test/agent/turn/tool-dedup.test.ts +++ b/packages/agent-core/test/agent/turn/tool-dedup.test.ts @@ -116,8 +116,8 @@ describe('ToolCallDeduplicator', () => { dedup.endStep(); } expect(last!.output as string).toContain(''); - expect(last!.output as string).toContain('repeating the exact same tool call'); - expect(last!.output as string).not.toContain('repeated_times'); + expect(last!.output as string).toContain('what new information you expect'); + expect(last!.output as string).not.toContain('Choose exactly one'); }); it('keeps injecting reminder1 at 4 consecutive', async () => { @@ -129,7 +129,7 @@ describe('ToolCallDeduplicator', () => { dedup.endStep(); } expect(last!.output as string).toContain(''); - expect(last!.output as string).toContain('repeating the exact same tool call'); + expect(last!.output as string).toContain('what new information you expect'); }); it('injects reminder2 at exactly 5 consecutive', async () => { @@ -141,9 +141,9 @@ describe('ToolCallDeduplicator', () => { dedup.endStep(); } expect(last!.output as string).toContain(''); - expect(last!.output as string).toContain('repeated_times: 5'); - expect(last!.output as string).toContain('tool: Read'); - expect(last!.output as string).toContain('arguments:'); + expect(last!.output as string).toContain('issued 5 times in a row'); + expect(last!.output as string).toContain('Choose exactly one of the following'); + expect(last!.output as string).toContain('Falsification check'); }); it.each([6, 7])('keeps injecting reminder2 at %i consecutive', async (streak) => { @@ -155,8 +155,8 @@ describe('ToolCallDeduplicator', () => { dedup.endStep(); } expect(last!.output as string).toContain(''); - expect(last!.output as string).toContain(`repeated_times: ${String(streak)}`); - expect(last!.output as string).toContain('tool: Read'); + expect(last!.output as string).toContain(`issued ${String(streak)} times in a row`); + expect(last!.output as string).toContain('Choose exactly one of the following'); }); it('injects the dead-end reminder at exactly 8 consecutive', async () => { @@ -168,7 +168,7 @@ describe('ToolCallDeduplicator', () => { dedup.endStep(); } expect(last!.output as string).toContain(''); - expect(last!.output as string).toContain('stuck in a dead end'); + expect(last!.output as string).toContain('without any further tool calls'); }); it('resets streak when a different call is interleaved', async () => { @@ -214,9 +214,9 @@ describe('ToolCallDeduplicator', () => { dedup.endStep(); expect(original.output as string).toContain(''); - expect(original.output as string).toContain('repeating the exact same tool call'); + expect(original.output as string).toContain('what new information you expect'); expect(finalDup.output as string).toContain(''); - expect(finalDup.output as string).toContain('repeating the exact same tool call'); + expect(finalDup.output as string).toContain('what new information you expect'); }); it('same-step spam alone does not trigger reminder', async () => { @@ -273,7 +273,7 @@ describe('ToolCallDeduplicator', () => { const arr = final.output as Array<{ type: string; text: string }>; expect(arr).toHaveLength(1); expect(arr[0]!.type).toBe('text'); - expect(arr[0]!.text).toBe('hello' + makeReminderText2('X', 5, { a: 1 })); + expect(arr[0]!.text).toBe('hello' + makeReminderText2(5)); }); it('pushes a new text part when trailing part is non-text', async () => { @@ -408,8 +408,8 @@ describe('ToolCallDeduplicator', () => { const dedup = new ToolCallDeduplicator(); const last = await runStreak(dedup, 8); expect(last.output as string).toContain(''); - expect(last.output as string).toContain('stuck in a dead end'); - expect(last.output as string).toContain('Stop all function calls immediately'); + expect(last.output as string).toContain('Write your final response now'); + expect(last.output as string).toContain('without any further tool calls'); // 8 is the reminder threshold, not yet force-stop. expect(last.isError).toBeUndefined(); expect(stopTurnOf(last)).toBeUndefined(); @@ -420,7 +420,7 @@ describe('ToolCallDeduplicator', () => { async (streak) => { const dedup = new ToolCallDeduplicator(); const last = await runStreak(dedup, streak); - expect(last.output as string).toContain('stuck in a dead end'); + expect(last.output as string).toContain('Write your final response now'); expect(last.isError).toBeUndefined(); expect(stopTurnOf(last)).toBeUndefined(); }, @@ -429,7 +429,7 @@ describe('ToolCallDeduplicator', () => { it('force-stops the turn at exactly 12 consecutive without marking the tool failed', async () => { const dedup = new ToolCallDeduplicator(); const last = await runStreak(dedup, 12); - expect(last.output as string).toContain('stuck in a dead end'); + expect(last.output as string).toContain('Write your final response now'); // The underlying tool succeeded — force-stop must not flip it to error. expect(last.isError).toBeUndefined(); expect(stopTurnOf(last)).toBe(true); @@ -459,7 +459,7 @@ describe('ToolCallDeduplicator', () => { // The underlying tool was an error — that must survive force-stop. expect(last!.isError).toBe(true); expect(stopTurnOf(last!)).toBe(true); - expect(last!.output as string).toContain('stuck in a dead end'); + expect(last!.output as string).toContain('Write your final response now'); }); });