-
Notifications
You must be signed in to change notification settings - Fork 300
feat: add tiered tool-repetition detection (soft warning + hard stop) #1829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
p12tic
wants to merge
1
commit into
Zoo-Code-Org:main
Choose a base branch
from
p12tic:repetitive-calls-soft-block
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,573
−385
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
313 changes: 313 additions & 0 deletions
313
src/core/assistant-message/__tests__/presentAssistantMessage-tool-repetition.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,313 @@ | ||
| // npx vitest src/core/assistant-message/__tests__/presentAssistantMessage-tool-repetition.spec.ts | ||
|
|
||
| import { providerIdentifiers } from "@roo-code/types" | ||
|
|
||
| import type { ToolUse } from "../../../shared/tools" | ||
| import { ToolRepetitionDetector } from "../../tools/ToolRepetitionDetector" | ||
| import { presentAssistantMessage } from "../presentAssistantMessage" | ||
|
|
||
| type ToolBlock = { | ||
| type: string | ||
| id?: string | ||
| name?: string | ||
| params?: Record<string, string> | ||
| nativeArgs?: Record<string, string> | ||
| partial?: boolean | ||
| } | ||
|
|
||
| type ToolResultBlock = { | ||
| type: string | ||
| tool_use_id?: string | ||
| content?: string | ||
| is_error?: boolean | ||
| text?: string | ||
| } | ||
|
|
||
| type MockTask = Record<string, unknown> | ||
|
|
||
| // Mock dependencies | ||
| vi.mock("../../task/Task") | ||
| vi.mock("../../tools/validateToolUse", () => ({ | ||
| validateToolUse: vi.fn(), | ||
| isValidToolName: vi.fn(() => true), | ||
| })) | ||
|
|
||
| // Translations are not loaded under test (see src/i18n/setup.ts), so the real | ||
| // `t` returns only the bare key and drops interpolation options. Return the | ||
| // key plus serialized options instead, so tests can assert both which message | ||
| // was selected and which tool name was interpolated, without depending on the | ||
| // English wording. | ||
| vi.mock("../../../i18n", () => ({ | ||
| t: (key: string, options?: Record<string, unknown>) => (options ? `${key} ${JSON.stringify(options)}` : key), | ||
| })) | ||
|
|
||
| // Mock the read_file tool so we can assert the normal execution path is taken | ||
| // when the repetition detector allows a tool call. The handle spy simulates a | ||
| // successful tool run by pushing a tool_result, mirroring the real tool. | ||
| // `vi.hoisted` is required because `vi.mock` factories are hoisted above | ||
| // top-level variable declarations. | ||
| const { readFileHandle } = vi.hoisted(() => ({ | ||
| readFileHandle: vi.fn( | ||
| async (_cline: unknown, block: ToolBlock, { pushToolResult }: { pushToolResult: (result: string) => void }) => { | ||
| pushToolResult(`[read_file for '${block?.params?.path ?? block?.nativeArgs?.path}'] Result`) | ||
| }, | ||
| ), | ||
| })) | ||
|
|
||
| vi.mock("../../tools/ReadFileTool", () => ({ | ||
| readFileTool: { | ||
| handle: readFileHandle, | ||
| getReadFileToolDescription: vi.fn(() => "[read_file]"), | ||
| }, | ||
| })) | ||
|
|
||
| const captureConsecutiveMistakeError = vi.fn() | ||
| const captureException = vi.fn() | ||
| const captureToolUsage = vi.fn() | ||
|
|
||
| vi.mock("@roo-code/telemetry", () => ({ | ||
| TelemetryService: { | ||
| instance: { | ||
| get captureToolUsage() { | ||
| return captureToolUsage | ||
| }, | ||
| get captureConsecutiveMistakeError() { | ||
| return captureConsecutiveMistakeError | ||
| }, | ||
| get captureException() { | ||
| return captureException | ||
| }, | ||
| captureEvent: vi.fn(), | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| // Small explicit limits keep the priming in each test short: | ||
| // - 1st identical call: allowed | ||
| // - 2nd identical call: soft blocked | ||
| // - 3rd identical call: hard blocked | ||
| const SOFT_LIMIT = 1 | ||
| const HARD_LIMIT = 2 | ||
|
|
||
| function makeReadFileBlock(id: string, path = "test.txt"): ToolUse<"read_file"> { | ||
| return { | ||
| type: "tool_use", | ||
| id, | ||
| name: "read_file", | ||
| params: { path }, | ||
| nativeArgs: { path }, | ||
| partial: false, | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Feeds `times` identical calls into the detector, simulating the model having | ||
| * already made the same tool call that many times in earlier turns. | ||
| */ | ||
| function primeDetector(detector: ToolRepetitionDetector, block: ToolUse, times: number) { | ||
| for (let i = 0; i < times; i++) { | ||
| detector.check({ ...block }) | ||
| } | ||
| } | ||
|
|
||
| describe("presentAssistantMessage - Tool Repetition Detection", () => { | ||
| let mockTask: MockTask | ||
| let detector: ToolRepetitionDetector | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
|
|
||
| detector = new ToolRepetitionDetector(SOFT_LIMIT, HARD_LIMIT) | ||
|
|
||
| mockTask = { | ||
| taskId: "test-task-id", | ||
| instanceId: "test-instance", | ||
| abort: false, | ||
| presentAssistantMessageLocked: false, | ||
| presentAssistantMessageHasPendingUpdates: false, | ||
| currentStreamingContentIndex: 0, | ||
| assistantMessageContent: [], | ||
| userMessageContent: [], | ||
| didCompleteReadingStream: false, | ||
| didRejectTool: false, | ||
| didAlreadyUseTool: false, | ||
| consecutiveMistakeCount: 0, | ||
| consecutiveMistakeLimit: HARD_LIMIT, | ||
| clineMessages: [], | ||
| getTaskMode: vi.fn().mockResolvedValue("code"), | ||
| apiConfiguration: { apiProvider: providerIdentifiers.anthropic }, | ||
| api: { | ||
| getModel: () => ({ id: "test-model", info: {} }), | ||
| }, | ||
| recordToolUsage: vi.fn(), | ||
| recordToolError: vi.fn(), | ||
| toolRepetitionDetector: detector, | ||
| providerRef: { | ||
| deref: () => ({ | ||
| getState: vi.fn().mockResolvedValue({ | ||
| mode: "code", | ||
| customModes: [], | ||
| }), | ||
| }), | ||
| }, | ||
| say: vi.fn().mockResolvedValue(undefined), | ||
| ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }), | ||
| } | ||
|
|
||
| mockTask.pushToolResultToUserContent = vi.fn().mockImplementation((toolResult: ToolResultBlock) => { | ||
| const userMessageContent = mockTask.userMessageContent as ToolResultBlock[] | ||
| const existingResult = userMessageContent.find( | ||
| (block) => block.type === "tool_result" && block.tool_use_id === toolResult.tool_use_id, | ||
| ) | ||
| if (existingResult) { | ||
| return false | ||
| } | ||
| ;(mockTask.userMessageContent as ToolResultBlock[]).push(toolResult) | ||
| return true | ||
| }) | ||
| }) | ||
|
|
||
| function findToolResult(toolCallId: string) { | ||
| return (mockTask.userMessageContent as ToolResultBlock[]).find( | ||
| (item) => item.type === "tool_result" && item.tool_use_id === toolCallId, | ||
| ) | ||
| } | ||
|
|
||
| async function present() { | ||
| await presentAssistantMessage(mockTask as unknown as Parameters<typeof presentAssistantMessage>[0]) | ||
| } | ||
|
|
||
| it("should execute the tool normally on the first call", async () => { | ||
| const toolCallId = "tool_call_allow" | ||
| const block = makeReadFileBlock(toolCallId) | ||
| mockTask.assistantMessageContent = [block] | ||
| const checkSpy = vi.spyOn(detector, "check") | ||
|
|
||
| await present() | ||
|
|
||
| // The real detector should have been consulted with this block. | ||
| expect(checkSpy).toHaveBeenCalledTimes(1) | ||
| expect(checkSpy).toHaveBeenCalledWith(expect.objectContaining({ name: "read_file", id: toolCallId })) | ||
| expect(checkSpy).toHaveReturnedWith({ action: "allow" }) | ||
|
|
||
| // No hard block ask should have occurred. | ||
| expect(mockTask.ask).not.toHaveBeenCalledWith("mistake_limit_reached", expect.anything()) | ||
|
|
||
| // The tool must have actually continued into normal execution: the | ||
| // read_file tool runner should have been dispatched with this block. | ||
| expect(readFileHandle).toHaveBeenCalledTimes(1) | ||
| const [, dispatchedBlock] = readFileHandle.mock.calls[0] | ||
| expect(dispatchedBlock).toMatchObject({ name: "read_file", id: toolCallId }) | ||
|
|
||
| // And the normal execution path should have produced a tool_result | ||
| // (no soft/hard block error message). | ||
| const toolResult = findToolResult(toolCallId) | ||
| expect(toolResult).toBeDefined() | ||
| expect(toolResult?.content).toContain("read_file") | ||
| expect(toolResult?.content).not.toContain("tools:toolRepetition") | ||
| expect(toolResult?.is_error).toBeUndefined() | ||
| }) | ||
|
|
||
| it("should execute the tool when the previous call had different arguments", async () => { | ||
| primeDetector(detector, makeReadFileBlock("earlier", "other.txt"), SOFT_LIMIT + 1) | ||
|
|
||
| const toolCallId = "tool_call_different_args" | ||
| mockTask.assistantMessageContent = [makeReadFileBlock(toolCallId, "test.txt")] | ||
|
|
||
| await present() | ||
|
|
||
| expect(readFileHandle).toHaveBeenCalledTimes(1) | ||
| expect(mockTask.ask).not.toHaveBeenCalled() | ||
| expect(findToolResult(toolCallId)?.content).toContain("test.txt") | ||
| }) | ||
|
|
||
| it("should soft block without involving the user and return an error to the model", async () => { | ||
| const toolCallId = "tool_call_soft_block" | ||
| const block = makeReadFileBlock(toolCallId) | ||
| primeDetector(detector, block, SOFT_LIMIT) | ||
| mockTask.assistantMessageContent = [block] | ||
|
|
||
| await present() | ||
|
|
||
| // The user should NOT have been asked anything for a soft block. | ||
| expect(mockTask.ask).not.toHaveBeenCalled() | ||
|
|
||
| // The tool must not be executed when it is soft blocked. | ||
| expect(readFileHandle).not.toHaveBeenCalled() | ||
|
|
||
| // A tool_result with the soft block message should have been pushed. | ||
| const toolResult = findToolResult(toolCallId) | ||
| // The content is the JSON produced by formatResponse.toolError(); the | ||
| // detector's message is carried in its `error` field. | ||
| expect(toolResult).toBeDefined() | ||
| const { error } = JSON.parse(String(toolResult?.content)) as { error: string } | ||
| expect(error).toBe(`tools:toolRepetitionSoftBlock ${JSON.stringify({ toolName: "read_file" })}`) | ||
|
|
||
| // No telemetry escalation for a soft block. | ||
| expect(captureConsecutiveMistakeError).not.toHaveBeenCalled() | ||
| expect(captureException).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("should hard block, ask the user for guidance, and record telemetry", async () => { | ||
| const toolCallId = "tool_call_hard_block" | ||
| const block = makeReadFileBlock(toolCallId) | ||
| primeDetector(detector, block, HARD_LIMIT) | ||
| mockTask.assistantMessageContent = [block] | ||
|
|
||
| mockTask.ask = vi.fn().mockResolvedValue({ response: "yesButtonClicked" }) | ||
|
|
||
| await present() | ||
|
|
||
| // The user must be asked for guidance with the resolved message key | ||
| // and a detail message naming the repeated tool. | ||
| expect(mockTask.ask).toHaveBeenCalledTimes(1) | ||
| expect(mockTask.ask).toHaveBeenCalledWith( | ||
| "mistake_limit_reached", | ||
| expect.stringContaining("tools:toolRepetitionLimitReached"), | ||
| ) | ||
| expect(mockTask.ask).toHaveBeenCalledWith( | ||
| "mistake_limit_reached", | ||
| expect.stringContaining('"toolName":"read_file"'), | ||
| ) | ||
|
|
||
| // The tool must not be executed when it is hard blocked. | ||
| expect(readFileHandle).not.toHaveBeenCalled() | ||
|
|
||
| // Telemetry escalation should have fired for a hard block. | ||
| expect(captureConsecutiveMistakeError).toHaveBeenCalledWith("test-task-id") | ||
| expect(captureException).toHaveBeenCalled() | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // A tool_result describing the repetition limit should have been pushed. | ||
| const toolResult = findToolResult(toolCallId) | ||
| expect(toolResult).toBeDefined() | ||
| expect(toolResult?.content).toContain("read_file") | ||
| }) | ||
|
|
||
| it("should incorporate user feedback when the user responds to a hard block", async () => { | ||
| const toolCallId = "tool_call_hard_block_feedback" | ||
| const block = makeReadFileBlock(toolCallId) | ||
| primeDetector(detector, block, HARD_LIMIT) | ||
| mockTask.assistantMessageContent = [block] | ||
|
|
||
| mockTask.ask = vi.fn().mockResolvedValue({ | ||
| response: "messageResponse", | ||
| text: "try a different file", | ||
| images: [], | ||
| }) | ||
|
|
||
| await present() | ||
|
|
||
| // The tool must not be executed when it is hard blocked. | ||
| expect(readFileHandle).not.toHaveBeenCalled() | ||
|
|
||
| // User feedback should have been surfaced to the chat. | ||
| expect(mockTask.say).toHaveBeenCalledWith("user_feedback", "try a different file", []) | ||
|
|
||
| // And appended to the user message content. | ||
| const feedbackBlock = (mockTask.userMessageContent as ToolResultBlock[]).find( | ||
| (item) => item.type === "text" && String(item.text).includes("try a different file"), | ||
| ) | ||
| expect(feedbackBlock).toBeDefined() | ||
| expect(feedbackBlock?.text).toContain("Tool repetition limit reached") | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.