Skip to content

Commit 5f7162d

Browse files
committed
feat: add tiered tool-repetition detection (soft warning + hard stop)
Replace the single-threshold ToolRepetitionDetector with a two-tier system: - Soft block (default 2nd identical call): the tool is not executed and the model receives an error asking it to justify the repeat. The user is not involved and the counter keeps incrementing toward the hard limit. - Hard stop: execution stops and the user is asked for guidance (previous behavior).
1 parent 80fb159 commit 5f7162d

53 files changed

Lines changed: 833 additions & 419 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/types/src/provider-settings.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ import {
3030

3131
export const DEFAULT_CONSECUTIVE_MISTAKE_LIMIT = 3
3232

33+
/**
34+
* Number of identical consecutive tool calls allowed before the tool is
35+
* soft-blocked. When reached, the tool is not executed and the model is
36+
* asked to justify why it needs to repeat the call. Set to 0 to disable.
37+
*/
38+
export const DEFAULT_TOOL_REPETITION_SOFT_LIMIT = 2
39+
3340
/**
3441
* DynamicProvider
3542
*
@@ -191,6 +198,7 @@ const baseProviderSettingsSchema = z.object({
191198
modelTemperature: z.number().nullish(),
192199
rateLimitSeconds: z.number().optional(),
193200
consecutiveMistakeLimit: z.number().min(0).optional(),
201+
toolRepetitionSoftLimit: z.number().min(0).optional(),
194202

195203
// Model reasoning.
196204
enableReasoningEffort: z.boolean().optional(),

‎packages/types/src/task.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export interface CreateTaskOptions {
8787
taskId?: string
8888
enableCheckpoints?: boolean
8989
consecutiveMistakeLimit?: number
90+
toolRepetitionSoftLimit?: number
9091
experiments?: Record<string, boolean>
9192
initialTodos?: TodoItem[]
9293
/** Initial status for the task's history item (e.g., "active" for child tasks) */

‎src/core/assistant-message/__tests__/presentAssistantMessage-custom-tool.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe("presentAssistantMessage - Custom Tool Recording", () => {
6363
recordToolUsage: vi.fn(),
6464
recordToolError: vi.fn(),
6565
toolRepetitionDetector: {
66-
check: vi.fn().mockReturnValue({ allowExecution: true }),
66+
check: vi.fn().mockReturnValue({ action: "allow" }),
6767
},
6868
providerRef: {
6969
deref: () => ({

‎src/core/assistant-message/__tests__/presentAssistantMessage-images.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe("presentAssistantMessage - Image Handling in Native Tool Calling", () =
4747
},
4848
recordToolUsage: vi.fn(),
4949
toolRepetitionDetector: {
50-
check: vi.fn().mockReturnValue({ allowExecution: true }),
50+
check: vi.fn().mockReturnValue({ action: "allow" }),
5151
},
5252
providerRef: {
5353
deref: () => ({
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
// npx vitest src/core/assistant-message/__tests__/presentAssistantMessage-tool-repetition.spec.ts
2+
3+
import { describe, it, expect, beforeEach, vi } from "vitest"
4+
import { presentAssistantMessage } from "../presentAssistantMessage"
5+
6+
// Mock dependencies
7+
vi.mock("../../task/Task")
8+
vi.mock("../../tools/validateToolUse", () => ({
9+
validateToolUse: vi.fn(),
10+
isValidToolName: vi.fn(() => true),
11+
}))
12+
13+
const captureConsecutiveMistakeError = vi.fn()
14+
const captureException = vi.fn()
15+
const captureToolUsage = vi.fn()
16+
17+
vi.mock("@roo-code/telemetry", () => ({
18+
TelemetryService: {
19+
instance: {
20+
get captureToolUsage() {
21+
return captureToolUsage
22+
},
23+
get captureConsecutiveMistakeError() {
24+
return captureConsecutiveMistakeError
25+
},
26+
get captureException() {
27+
return captureException
28+
},
29+
captureEvent: vi.fn(),
30+
},
31+
},
32+
}))
33+
34+
describe("presentAssistantMessage - Tool Repetition Detection", () => {
35+
let mockTask: any
36+
37+
beforeEach(() => {
38+
vi.clearAllMocks()
39+
40+
mockTask = {
41+
taskId: "test-task-id",
42+
instanceId: "test-instance",
43+
abort: false,
44+
presentAssistantMessageLocked: false,
45+
presentAssistantMessageHasPendingUpdates: false,
46+
currentStreamingContentIndex: 0,
47+
assistantMessageContent: [],
48+
userMessageContent: [],
49+
didCompleteReadingStream: false,
50+
didRejectTool: false,
51+
didAlreadyUseTool: false,
52+
consecutiveMistakeCount: 0,
53+
consecutiveMistakeLimit: 5,
54+
clineMessages: [],
55+
apiConfiguration: { apiProvider: "anthropic" },
56+
api: {
57+
getModel: () => ({ id: "test-model", info: {} }),
58+
},
59+
recordToolUsage: vi.fn(),
60+
recordToolError: vi.fn(),
61+
toolRepetitionDetector: {
62+
check: vi.fn().mockReturnValue({ action: "allow" }),
63+
},
64+
providerRef: {
65+
deref: () => ({
66+
getState: vi.fn().mockResolvedValue({
67+
mode: "code",
68+
customModes: [],
69+
}),
70+
}),
71+
},
72+
say: vi.fn().mockResolvedValue(undefined),
73+
ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }),
74+
}
75+
76+
mockTask.pushToolResultToUserContent = vi.fn().mockImplementation((toolResult: any) => {
77+
const existingResult = mockTask.userMessageContent.find(
78+
(block: any) => block.type === "tool_result" && block.tool_use_id === toolResult.tool_use_id,
79+
)
80+
if (existingResult) {
81+
return false
82+
}
83+
mockTask.userMessageContent.push(toolResult)
84+
return true
85+
})
86+
})
87+
88+
it("should soft block without involving the user and return an error to the model", async () => {
89+
const toolCallId = "tool_call_soft_block"
90+
mockTask.assistantMessageContent = [
91+
{
92+
type: "tool_use",
93+
id: toolCallId,
94+
name: "read_file",
95+
params: { path: "test.txt" },
96+
nativeArgs: { path: "test.txt" },
97+
partial: false,
98+
},
99+
]
100+
101+
mockTask.toolRepetitionDetector.check = vi.fn().mockReturnValue({
102+
action: "soft_block",
103+
message: "The tool 'read_file' was blocked because it was just called with identical parameters.",
104+
})
105+
106+
await presentAssistantMessage(mockTask)
107+
108+
// The user should NOT have been asked anything for a soft block.
109+
expect(mockTask.ask).not.toHaveBeenCalled()
110+
111+
// A tool_result with the soft block message should have been pushed.
112+
const toolResult = mockTask.userMessageContent.find(
113+
(item: any) => item.type === "tool_result" && item.tool_use_id === toolCallId,
114+
)
115+
expect(toolResult).toBeDefined()
116+
expect(toolResult.content).toContain("read_file")
117+
expect(toolResult.content).toContain("identical parameters")
118+
119+
// No telemetry escalation for a soft block.
120+
expect(captureConsecutiveMistakeError).not.toHaveBeenCalled()
121+
expect(captureException).not.toHaveBeenCalled()
122+
})
123+
124+
it("should hard block, ask the user for guidance, and record telemetry", async () => {
125+
const toolCallId = "tool_call_hard_block"
126+
mockTask.assistantMessageContent = [
127+
{
128+
type: "tool_use",
129+
id: toolCallId,
130+
name: "read_file",
131+
params: { path: "test.txt" },
132+
nativeArgs: { path: "test.txt" },
133+
partial: false,
134+
},
135+
]
136+
137+
mockTask.toolRepetitionDetector.check = vi.fn().mockReturnValue({
138+
action: "hard_block",
139+
askUser: {
140+
messageKey: "mistake_limit_reached",
141+
messageDetail: "Roo appears to be stuck in a loop calling {toolName} repeatedly.",
142+
},
143+
})
144+
145+
mockTask.ask = vi.fn().mockResolvedValue({ response: "yesButtonClicked" })
146+
147+
await presentAssistantMessage(mockTask)
148+
149+
// The user must be asked for guidance with the resolved message key.
150+
expect(mockTask.ask).toHaveBeenCalledWith(
151+
"mistake_limit_reached",
152+
expect.stringContaining("read_file"),
153+
)
154+
155+
// Telemetry escalation should have fired for a hard block.
156+
expect(captureConsecutiveMistakeError).toHaveBeenCalledWith("test-task-id")
157+
expect(captureException).toHaveBeenCalled()
158+
159+
// A tool_result describing the repetition limit should have been pushed.
160+
const toolResult = mockTask.userMessageContent.find(
161+
(item: any) => item.type === "tool_result" && item.tool_use_id === toolCallId,
162+
)
163+
expect(toolResult).toBeDefined()
164+
expect(toolResult.content).toContain("read_file")
165+
})
166+
167+
it("should incorporate user feedback when the user responds to a hard block", async () => {
168+
const toolCallId = "tool_call_hard_block_feedback"
169+
mockTask.assistantMessageContent = [
170+
{
171+
type: "tool_use",
172+
id: toolCallId,
173+
name: "read_file",
174+
params: { path: "test.txt" },
175+
nativeArgs: { path: "test.txt" },
176+
partial: false,
177+
},
178+
]
179+
180+
mockTask.toolRepetitionDetector.check = vi.fn().mockReturnValue({
181+
action: "hard_block",
182+
askUser: {
183+
messageKey: "mistake_limit_reached",
184+
messageDetail: "Stuck calling {toolName} repeatedly.",
185+
},
186+
})
187+
188+
mockTask.ask = vi.fn().mockResolvedValue({
189+
response: "messageResponse",
190+
text: "try a different file",
191+
images: [],
192+
})
193+
194+
await presentAssistantMessage(mockTask)
195+
196+
// User feedback should have been surfaced to the chat.
197+
expect(mockTask.say).toHaveBeenCalledWith("user_feedback", "try a different file", [])
198+
199+
// And appended to the user message content.
200+
const feedbackBlock = mockTask.userMessageContent.find(
201+
(item: any) => item.type === "text" && String(item.text).includes("try a different file"),
202+
)
203+
expect(feedbackBlock).toBeDefined()
204+
expect(feedbackBlock.text).toContain("Tool repetition limit reached")
205+
})
206+
207+
it("should execute the tool normally when the detector allows it", async () => {
208+
const toolCallId = "tool_call_allow"
209+
mockTask.assistantMessageContent = [
210+
{
211+
type: "tool_use",
212+
id: toolCallId,
213+
name: "read_file",
214+
params: { path: "test.txt" },
215+
nativeArgs: { path: "test.txt" },
216+
partial: false,
217+
},
218+
]
219+
220+
mockTask.toolRepetitionDetector.check = vi.fn().mockReturnValue({ action: "allow" })
221+
222+
await presentAssistantMessage(mockTask)
223+
224+
// The detector should have been consulted.
225+
expect(mockTask.toolRepetitionDetector.check).toHaveBeenCalled()
226+
// No hard block ask should have occurred.
227+
expect(mockTask.ask).not.toHaveBeenCalledWith("mistake_limit_reached", expect.anything())
228+
})
229+
})

‎src/core/assistant-message/__tests__/presentAssistantMessage-unknown-tool.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe("presentAssistantMessage - Unknown Tool Handling", () => {
4343
recordToolUsage: vi.fn(),
4444
recordToolError: vi.fn(),
4545
toolRepetitionDetector: {
46-
check: vi.fn().mockReturnValue({ allowExecution: true }),
46+
check: vi.fn().mockReturnValue({ action: "allow" }),
4747
},
4848
providerRef: {
4949
deref: () => ({

‎src/core/assistant-message/presentAssistantMessage.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,8 +629,17 @@ export async function presentAssistantMessage(cline: Task) {
629629
// block directly.
630630
const repetitionCheck = cline.toolRepetitionDetector.check(block)
631631

632-
// If execution is not allowed, notify user and break.
633-
if (!repetitionCheck.allowExecution && repetitionCheck.askUser) {
632+
// Soft block: do NOT involve the user. Return an error to the
633+
// model asking it to justify repeating the call (or try a
634+
// different approach). The detector keeps counting so continued
635+
// repetition will eventually escalate to a hard block.
636+
if (repetitionCheck.action === "soft_block") {
637+
pushToolResult(formatResponse.toolError(repetitionCheck.message))
638+
break
639+
}
640+
641+
// Hard block: stop and ask the user for guidance.
642+
if (repetitionCheck.action === "hard_block") {
634643
// Handle repetition similar to mistake_limit_reached pattern.
635644
const { response, text, images } = await cline.ask(
636645
repetitionCheck.askUser.messageKey as ClineAsk,

‎src/core/config/ProviderSettingsManager.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
isSecretStateKey,
1010
ProviderSettingsEntry,
1111
DEFAULT_CONSECUTIVE_MISTAKE_LIMIT,
12+
DEFAULT_TOOL_REPETITION_SOFT_LIMIT,
1213
getModelId,
1314
type ProviderName,
1415
isProviderName,
@@ -43,6 +44,7 @@ export const providerProfilesSchema = z.object({
4344
rateLimitSecondsMigrated: z.boolean().optional(),
4445
openAiHeadersMigrated: z.boolean().optional(),
4546
consecutiveMistakeLimitMigrated: z.boolean().optional(),
47+
toolRepetitionLimitsMigrated: z.boolean().optional(),
4648
todoListEnabledMigrated: z.boolean().optional(),
4749
claudeCodeLegacySettingsMigrated: z.boolean().optional(),
4850
routerProviderMigrated: z.boolean().optional(),
@@ -68,6 +70,7 @@ export class ProviderSettingsManager {
6870
rateLimitSecondsMigrated: true, // Mark as migrated on fresh installs
6971
openAiHeadersMigrated: true, // Mark as migrated on fresh installs
7072
consecutiveMistakeLimitMigrated: true, // Mark as migrated on fresh installs
73+
toolRepetitionLimitsMigrated: true, // Mark as migrated on fresh installs
7174
todoListEnabledMigrated: true, // Mark as migrated on fresh installs
7275
claudeCodeLegacySettingsMigrated: true, // Mark as migrated on fresh installs
7376
routerProviderMigrated: true, // Mark as migrated on fresh installs
@@ -174,6 +177,12 @@ export class ProviderSettingsManager {
174177
isDirty = true
175178
}
176179

180+
if (!providerProfiles.migrations.toolRepetitionLimitsMigrated) {
181+
await this.migrateToolRepetitionLimits(providerProfiles)
182+
providerProfiles.migrations.toolRepetitionLimitsMigrated = true
183+
isDirty = true
184+
}
185+
177186
if (!providerProfiles.migrations.todoListEnabledMigrated) {
178187
await this.migrateTodoListEnabled(providerProfiles)
179188
providerProfiles.migrations.todoListEnabledMigrated = true
@@ -271,6 +280,19 @@ export class ProviderSettingsManager {
271280
}
272281
}
273282

283+
private async migrateToolRepetitionLimits(providerProfiles: ProviderProfiles) {
284+
try {
285+
for (const [_name, apiConfig] of Object.entries(providerProfiles.apiConfigs)) {
286+
// Default the soft warning threshold.
287+
if (apiConfig.toolRepetitionSoftLimit == null) {
288+
apiConfig.toolRepetitionSoftLimit = DEFAULT_TOOL_REPETITION_SOFT_LIMIT
289+
}
290+
}
291+
} catch (error) {
292+
console.error(`[MigrateToolRepetitionLimits] Failed to migrate tool repetition limits:`, error)
293+
}
294+
}
295+
274296
private async migrateTodoListEnabled(providerProfiles: ProviderProfiles) {
275297
try {
276298
for (const [_name, apiConfig] of Object.entries(providerProfiles.apiConfigs)) {

0 commit comments

Comments
 (0)