|
| 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 | +}) |
0 commit comments