|
| 1 | +// npx vitest src/core/assistant-message/__tests__/presentAssistantMessage-error-interception.spec.ts |
| 2 | + |
| 3 | +import { describe, it, expect, beforeEach, vi } from "vitest" |
| 4 | +import { presentAssistantMessage } from "../presentAssistantMessage" |
| 5 | +import { getTaskErrorState } from "../../tools/error-interception" |
| 6 | + |
| 7 | +// Mock heavy dependencies that are not relevant to error interception paths. |
| 8 | +vi.mock("../../task/Task") |
| 9 | +vi.mock("../../tools/validateToolUse", () => ({ |
| 10 | + validateToolUse: vi.fn(), |
| 11 | + isValidToolName: vi.fn(() => true), |
| 12 | +})) |
| 13 | +vi.mock("@roo-code/telemetry", () => ({ |
| 14 | + TelemetryService: { |
| 15 | + instance: { |
| 16 | + captureToolUsage: vi.fn(), |
| 17 | + captureConsecutiveMistakeError: vi.fn(), |
| 18 | + captureEvent: vi.fn(), |
| 19 | + }, |
| 20 | + }, |
| 21 | +})) |
| 22 | + |
| 23 | +function createMockTask() { |
| 24 | + const mockTask: any = { |
| 25 | + taskId: "ei-task-id", |
| 26 | + instanceId: "ei-instance", |
| 27 | + abort: false, |
| 28 | + presentAssistantMessageLocked: false, |
| 29 | + presentAssistantMessageHasPendingUpdates: false, |
| 30 | + currentStreamingContentIndex: 0, |
| 31 | + assistantMessageContent: [], |
| 32 | + userMessageContent: [], |
| 33 | + didCompleteReadingStream: true, |
| 34 | + didRejectTool: false, |
| 35 | + didAlreadyUseTool: false, |
| 36 | + consecutiveMistakeCount: 0, |
| 37 | + clineMessages: [], |
| 38 | + api: { |
| 39 | + getModel: () => ({ id: "test-model", info: {} }), |
| 40 | + }, |
| 41 | + recordToolUsage: vi.fn(), |
| 42 | + recordToolError: vi.fn(), |
| 43 | + toolRepetitionDetector: { |
| 44 | + check: vi.fn().mockReturnValue({ allowExecution: true }), |
| 45 | + }, |
| 46 | + providerRef: { |
| 47 | + deref: () => ({ |
| 48 | + getState: vi.fn().mockResolvedValue({ |
| 49 | + mode: "code", |
| 50 | + customModes: [], |
| 51 | + experiments: {}, |
| 52 | + }), |
| 53 | + getMcpHub: vi.fn().mockReturnValue(undefined), |
| 54 | + }), |
| 55 | + }, |
| 56 | + say: vi.fn().mockResolvedValue(undefined), |
| 57 | + ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }), |
| 58 | + } |
| 59 | + |
| 60 | + mockTask.pushToolResultToUserContent = vi.fn().mockImplementation((toolResult: any) => { |
| 61 | + const existing = mockTask.userMessageContent.find( |
| 62 | + (block: any) => block.type === "tool_result" && block.tool_use_id === toolResult.tool_use_id, |
| 63 | + ) |
| 64 | + if (existing) { |
| 65 | + return false |
| 66 | + } |
| 67 | + mockTask.userMessageContent.push(toolResult) |
| 68 | + return true |
| 69 | + }) |
| 70 | + |
| 71 | + return mockTask |
| 72 | +} |
| 73 | + |
| 74 | +describe("presentAssistantMessage - Error Interception Integration", () => { |
| 75 | + let mockTask: ReturnType<typeof createMockTask> |
| 76 | + |
| 77 | + beforeEach(() => { |
| 78 | + mockTask = createMockTask() |
| 79 | + }) |
| 80 | + |
| 81 | + describe("XML_NATIVE_DUAL_PROTOCOL detection", () => { |
| 82 | + it("strips XML tool markup from text when a native tool_use block is present", async () => { |
| 83 | + mockTask.assistantMessageContent = [ |
| 84 | + { |
| 85 | + type: "text", |
| 86 | + content: 'Here is the result.\n<function=read_file>{"path":"x"}</function>', |
| 87 | + partial: false, |
| 88 | + }, |
| 89 | + { |
| 90 | + type: "tool_use", |
| 91 | + id: "call_native_1", |
| 92 | + name: "nonexistent_tool_xyz", |
| 93 | + params: {}, |
| 94 | + partial: false, |
| 95 | + }, |
| 96 | + ] |
| 97 | + |
| 98 | + await presentAssistantMessage(mockTask) |
| 99 | + |
| 100 | + // The XML markup should be stripped from the user-visible text. |
| 101 | + const sayCalls = mockTask.say.mock.calls.filter((c: any[]) => c[0] === "text") |
| 102 | + expect(sayCalls.length).toBeGreaterThan(0) |
| 103 | + const renderedText = sayCalls[0][1] as string |
| 104 | + expect(renderedText).not.toContain("<function=read_file>") |
| 105 | + expect(renderedText).toContain("Here is the result.") |
| 106 | + |
| 107 | + // The pending guide was queued and then merged into the native |
| 108 | + // tool_result for the tool_use block in the same turn. |
| 109 | + const state = getTaskErrorState(mockTask) |
| 110 | + expect(state.consumePendingNativeProtocolGuide()).toBeUndefined() |
| 111 | + const toolResult = mockTask.userMessageContent.find((item: any) => item.type === "tool_result") |
| 112 | + expect(toolResult).toBeDefined() |
| 113 | + expect(String(toolResult.content)).toContain("XML_NATIVE_DUAL_PROTOCOL") |
| 114 | + }) |
| 115 | + |
| 116 | + it("does not strip XML markup when no native tool_use block exists", async () => { |
| 117 | + const originalText = 'Here is the result.\n<function=read_file>{"path":"x"}</function>' |
| 118 | + mockTask.assistantMessageContent = [ |
| 119 | + { |
| 120 | + type: "text", |
| 121 | + content: originalText, |
| 122 | + partial: false, |
| 123 | + }, |
| 124 | + ] |
| 125 | + |
| 126 | + await presentAssistantMessage(mockTask) |
| 127 | + |
| 128 | + const sayCalls = mockTask.say.mock.calls.filter((c: any[]) => c[0] === "text") |
| 129 | + const renderedText = sayCalls[0][1] as string |
| 130 | + expect(renderedText).toContain("<function=read_file>") |
| 131 | + }) |
| 132 | + |
| 133 | + it("does not strip XML markup for partial text blocks", async () => { |
| 134 | + mockTask.assistantMessageContent = [ |
| 135 | + { |
| 136 | + type: "text", |
| 137 | + content: 'Partial <function=read_file>{"path":"x"}</function>', |
| 138 | + partial: true, |
| 139 | + }, |
| 140 | + { |
| 141 | + type: "tool_use", |
| 142 | + id: "call_native_2", |
| 143 | + name: "nonexistent_tool_xyz", |
| 144 | + params: {}, |
| 145 | + partial: false, |
| 146 | + }, |
| 147 | + ] |
| 148 | + |
| 149 | + // currentStreamingContentIndex=0 processes the text block; partial text |
| 150 | + // must bypass the dual-protocol detection branch entirely. |
| 151 | + await presentAssistantMessage(mockTask) |
| 152 | + |
| 153 | + const sayCalls = mockTask.say.mock.calls.filter((c: any[]) => c[0] === "text") |
| 154 | + const renderedText = sayCalls[0][1] as string |
| 155 | + expect(renderedText).toContain("<function=read_file>") |
| 156 | + }) |
| 157 | + }) |
| 158 | + |
| 159 | + describe("pendingNativeProtocolGuide consumption", () => { |
| 160 | + it("merges a queued guide into the next native tool_result and clears it", async () => { |
| 161 | + // Pre-queue a protocol guide as if a previous text block detected XML markup. |
| 162 | + const state = getTaskErrorState(mockTask) |
| 163 | + state.setPendingNativeProtocolGuide("[XML_NATIVE_DUAL_PROTOCOL occurrence=1] test guide") |
| 164 | + |
| 165 | + const toolCallId = "call_merge_guide" |
| 166 | + mockTask.assistantMessageContent = [ |
| 167 | + { |
| 168 | + type: "tool_use", |
| 169 | + id: toolCallId, |
| 170 | + name: "this_tool_does_not_exist_for_guide_test", |
| 171 | + params: {}, |
| 172 | + partial: false, |
| 173 | + }, |
| 174 | + ] |
| 175 | + |
| 176 | + await presentAssistantMessage(mockTask) |
| 177 | + |
| 178 | + const toolResult = mockTask.userMessageContent.find( |
| 179 | + (item: any) => item.type === "tool_result" && item.tool_use_id === toolCallId, |
| 180 | + ) |
| 181 | + expect(toolResult).toBeDefined() |
| 182 | + |
| 183 | + // The guide must be consumed (cleared) after being merged. |
| 184 | + expect(state.consumePendingNativeProtocolGuide()).toBeUndefined() |
| 185 | + }) |
| 186 | + }) |
| 187 | + |
| 188 | + describe("structural preflight validation", () => { |
| 189 | + it("blocks execute_command with CWD_OBJECT_MISUSE and pushes guided tool_result", async () => { |
| 190 | + const toolCallId = "call_cwd_misuse" |
| 191 | + mockTask.assistantMessageContent = [ |
| 192 | + { |
| 193 | + type: "tool_use", |
| 194 | + id: toolCallId, |
| 195 | + name: "execute_command", |
| 196 | + params: { command: "ls" }, |
| 197 | + nativeArgs: { |
| 198 | + command: "ls", |
| 199 | + cwd: { nested: "object" }, |
| 200 | + }, |
| 201 | + partial: false, |
| 202 | + }, |
| 203 | + ] |
| 204 | + |
| 205 | + await presentAssistantMessage(mockTask) |
| 206 | + |
| 207 | + const toolResult = mockTask.userMessageContent.find( |
| 208 | + (item: any) => item.type === "tool_result" && item.tool_use_id === toolCallId, |
| 209 | + ) |
| 210 | + expect(toolResult).toBeDefined() |
| 211 | + expect(toolResult.is_error).toBe(true) |
| 212 | + // Guided payload should be structured JSON from the interceptor. |
| 213 | + expect(toolResult.content).toContain("guided_tool_error") |
| 214 | + expect(mockTask.consecutiveMistakeCount).toBe(1) |
| 215 | + expect(mockTask.recordToolError).toHaveBeenCalledWith( |
| 216 | + "execute_command", |
| 217 | + expect.stringContaining("CWD_OBJECT_MISUSE"), |
| 218 | + ) |
| 219 | + }) |
| 220 | + |
| 221 | + it("blocks tool_use with NESTED_PARAM_OVERFLOW and pushes guided tool_result", async () => { |
| 222 | + const toolCallId = "call_nested_overflow" |
| 223 | + mockTask.assistantMessageContent = [ |
| 224 | + { |
| 225 | + type: "tool_use", |
| 226 | + id: toolCallId, |
| 227 | + name: "read_file", |
| 228 | + params: {}, |
| 229 | + nativeArgs: { |
| 230 | + path: "a.txt", |
| 231 | + extra: { |
| 232 | + name: "read_file", |
| 233 | + arguments: { path: "b.txt" }, |
| 234 | + }, |
| 235 | + }, |
| 236 | + partial: false, |
| 237 | + }, |
| 238 | + ] |
| 239 | + |
| 240 | + await presentAssistantMessage(mockTask) |
| 241 | + |
| 242 | + const toolResult = mockTask.userMessageContent.find( |
| 243 | + (item: any) => item.type === "tool_result" && item.tool_use_id === toolCallId, |
| 244 | + ) |
| 245 | + expect(toolResult).toBeDefined() |
| 246 | + expect(toolResult.is_error).toBe(true) |
| 247 | + expect(toolResult.content).toContain("guided_tool_error") |
| 248 | + expect(mockTask.consecutiveMistakeCount).toBe(1) |
| 249 | + expect(mockTask.recordToolError).toHaveBeenCalledWith( |
| 250 | + "read_file", |
| 251 | + expect.stringContaining("NESTED_PARAM_OVERFLOW"), |
| 252 | + ) |
| 253 | + }) |
| 254 | + |
| 255 | + it("escalates to STRUCTURAL_MISUSE_REPEAT on second identical misuse", async () => { |
| 256 | + const makeTask = () => { |
| 257 | + const t = createMockTask() |
| 258 | + t.assistantMessageContent = [ |
| 259 | + { |
| 260 | + type: "tool_use", |
| 261 | + id: "call_repeat", |
| 262 | + name: "execute_command", |
| 263 | + params: { command: "ls" }, |
| 264 | + nativeArgs: { |
| 265 | + command: "ls", |
| 266 | + cwd: { nested: "object" }, |
| 267 | + }, |
| 268 | + partial: false, |
| 269 | + }, |
| 270 | + ] |
| 271 | + return t |
| 272 | + } |
| 273 | + |
| 274 | + const task1 = makeTask() |
| 275 | + await presentAssistantMessage(task1) |
| 276 | + expect(task1.recordToolError).toHaveBeenCalledWith( |
| 277 | + "execute_command", |
| 278 | + expect.stringContaining("CWD_OBJECT_MISUSE"), |
| 279 | + ) |
| 280 | + |
| 281 | + // Second occurrence on the SAME Task object triggers the repeat message. |
| 282 | + task1.assistantMessageContent = [ |
| 283 | + { |
| 284 | + type: "tool_use", |
| 285 | + id: "call_repeat_2", |
| 286 | + name: "execute_command", |
| 287 | + params: { command: "ls" }, |
| 288 | + nativeArgs: { |
| 289 | + command: "ls", |
| 290 | + cwd: { nested: "object" }, |
| 291 | + }, |
| 292 | + partial: false, |
| 293 | + }, |
| 294 | + ] |
| 295 | + task1.currentStreamingContentIndex = 0 |
| 296 | + task1.didAlreadyUseTool = false |
| 297 | + task1.userMessageContent = [] |
| 298 | + |
| 299 | + await presentAssistantMessage(task1) |
| 300 | + expect(task1.recordToolError).toHaveBeenCalledWith( |
| 301 | + "execute_command", |
| 302 | + expect.stringContaining("STRUCTURAL_MISUSE_REPEAT"), |
| 303 | + ) |
| 304 | + }) |
| 305 | + }) |
| 306 | + |
| 307 | + describe("missing tool_use.id (legacy XML call)", () => { |
| 308 | + it("transforms the error through interceptor and pushes guided text", async () => { |
| 309 | + mockTask.assistantMessageContent = [ |
| 310 | + { |
| 311 | + type: "tool_use", |
| 312 | + // no id -> legacy XML-style call |
| 313 | + name: "read_file", |
| 314 | + params: { path: "x.txt" }, |
| 315 | + partial: false, |
| 316 | + }, |
| 317 | + ] |
| 318 | + |
| 319 | + await presentAssistantMessage(mockTask) |
| 320 | + |
| 321 | + const textBlocks = mockTask.userMessageContent.filter((item: any) => item.type === "text") |
| 322 | + expect(textBlocks.length).toBeGreaterThan(0) |
| 323 | + expect(textBlocks.some((b: any) => String(b.text).includes("guided_tool_error"))).toBe(true) |
| 324 | + expect(mockTask.consecutiveMistakeCount).toBe(1) |
| 325 | + expect(mockTask.recordToolError).toHaveBeenCalledWith( |
| 326 | + "read_file", |
| 327 | + expect.stringContaining("missing tool_use.id"), |
| 328 | + ) |
| 329 | + }) |
| 330 | + }) |
| 331 | +}) |
0 commit comments