-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathpresentAssistantMessage-tool-usage-attribution.spec.ts
More file actions
313 lines (277 loc) · 9.31 KB
/
Copy pathpresentAssistantMessage-tool-usage-attribution.spec.ts
File metadata and controls
313 lines (277 loc) · 9.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// npx vitest src/core/assistant-message/__tests__/presentAssistantMessage-tool-usage-attribution.spec.ts
import type { Anthropic } from "@anthropic-ai/sdk"
import { describe, it, expect, beforeEach, vi } from "vitest"
import { presentAssistantMessage } from "../presentAssistantMessage"
import { validateToolUse } from "../../tools/validateToolUse"
import { getModeBySlug } from "../../../shared/modes"
import type { Task } from "../../task/Task"
vi.mock("../../task/Task")
vi.mock("../../../shared/modes", async (importOriginal) => {
const actual = await importOriginal<typeof import("../../../shared/modes")>()
return {
...actual,
getModeBySlug: vi.fn(actual.getModeBySlug),
}
})
// isValidToolName is left as the real implementation (only validateToolUse is
// mocked): it has its own independent mcp_ prefix carve-out, and a hand-rolled
// mock allowlist here would mask a regression in toTelemetryToolName's
// ordering relative to isValidToolName.
vi.mock("../../tools/validateToolUse", async (importOriginal) => {
const actual = await importOriginal<typeof import("../../tools/validateToolUse")>()
return {
...actual,
validateToolUse: vi.fn(),
}
})
vi.mock("@roo-code/core", () => ({
customToolRegistry: {
has: vi.fn(() => false),
get: vi.fn(),
},
}))
vi.mock("@roo-code/telemetry", () => ({
TelemetryService: {
instance: {
captureToolUsage: vi.fn(),
captureConsecutiveMistakeError: vi.fn(),
captureEvent: vi.fn(),
},
},
}))
import { TelemetryService } from "@roo-code/telemetry"
interface MockTask {
taskId: string
instanceId: string
abort: boolean
presentAssistantMessageLocked: boolean
presentAssistantMessageHasPendingUpdates: boolean
currentStreamingContentIndex: number
assistantMessageContent: unknown[]
userMessageContent: Anthropic.ToolResultBlockParam[]
didCompleteReadingStream: boolean
didRejectTool: boolean
didAlreadyUseTool: boolean
consecutiveMistakeCount: number
clineMessages: unknown[]
api: { getModel: () => { id: string; info: Record<string, unknown> } }
recordToolUsage: ReturnType<typeof vi.fn>
recordToolError: ReturnType<typeof vi.fn>
toolRepetitionDetector: { check: ReturnType<typeof vi.fn> }
providerRef: {
deref: () => {
getState: ReturnType<typeof vi.fn>
getMcpHub?: () => { findServerNameBySanitizedName: (name: string) => string | undefined }
}
}
say: ReturnType<typeof vi.fn>
ask: ReturnType<typeof vi.fn>
pushToolResultToUserContent: ReturnType<typeof vi.fn>
}
describe("presentAssistantMessage - tool usage attribution", () => {
let mockTask: MockTask
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(validateToolUse).mockImplementation(() => undefined)
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,
clineMessages: [],
api: {
getModel: () => ({ id: "test-model", info: {} }),
},
recordToolUsage: vi.fn(),
recordToolError: vi.fn(),
toolRepetitionDetector: {
check: vi.fn().mockReturnValue({ allowExecution: true }),
},
providerRef: {
deref: () => ({
getState: vi.fn().mockResolvedValue({
mode: "code",
customModes: [],
}),
}),
},
say: vi.fn().mockResolvedValue(undefined),
ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }),
pushToolResultToUserContent: vi.fn(),
}
mockTask.pushToolResultToUserContent = vi
.fn()
.mockImplementation((toolResult: Anthropic.ToolResultBlockParam) => {
const existingResult = mockTask.userMessageContent.find(
(block) => block.type === "tool_result" && block.tool_use_id === toolResult.tool_use_id,
)
if (existingResult) {
return false
}
mockTask.userMessageContent.push(toolResult)
return true
})
})
it("records exactly one attempt for a normal static tool", async () => {
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: "call_1",
name: "read_file",
params: { path: "test.txt" },
nativeArgs: { path: "test.txt" },
partial: false,
},
]
await presentAssistantMessage(mockTask as unknown as Task)
expect(mockTask.recordToolUsage).toHaveBeenCalledTimes(1)
expect(mockTask.recordToolUsage).toHaveBeenCalledWith("read_file")
expect(TelemetryService.instance.captureToolUsage).toHaveBeenCalledTimes(1)
expect(TelemetryService.instance.captureToolUsage).toHaveBeenCalledWith(mockTask.taskId, "read_file")
})
it("records a valid dynamic mcp_ tool name as use_mcp_tool", async () => {
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: "call_mcp",
name: "mcp_my_server_do_thing",
params: {},
nativeArgs: {},
partial: false,
},
]
await presentAssistantMessage(mockTask as unknown as Task)
expect(mockTask.recordToolUsage).toHaveBeenCalledWith("use_mcp_tool")
expect(TelemetryService.instance.captureToolUsage).toHaveBeenCalledWith(mockTask.taskId, "use_mcp_tool")
})
it("records a malformed mcp_ tool name as use_mcp_tool, not the raw name", async () => {
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: "call_mcp_bad",
name: "mcp_",
params: {},
nativeArgs: {},
partial: false,
},
]
await presentAssistantMessage(mockTask as unknown as Task)
expect(mockTask.recordToolUsage).toHaveBeenCalledWith("use_mcp_tool")
expect(mockTask.recordToolUsage).not.toHaveBeenCalledWith("mcp_")
})
it("records a safe failure key without leaking the raw tool name when validation fails", async () => {
vi.mocked(validateToolUse).mockImplementation(() => {
throw new Error('Tool "read_file" is not allowed in this mode.')
})
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: "call_bad_mode",
name: "read_file",
params: { path: "test.txt" },
nativeArgs: { path: "test.txt" },
partial: false,
},
]
await presentAssistantMessage(mockTask as unknown as Task)
// A known static tool that fails validation still maps to its own name
// (it's a real, recognized tool - just disallowed here), never left raw/unmapped.
expect(mockTask.recordToolError).toHaveBeenCalledWith("read_file", expect.any(String))
// No success attempt should be recorded for a validation failure.
expect(mockTask.recordToolUsage).not.toHaveBeenCalled()
})
it("records invalid_tool_call, not the raw name, when an arbitrary unknown tool fails validation", async () => {
vi.mocked(validateToolUse).mockImplementation(() => {
throw new Error('Unknown tool "totally_made_up_tool". This tool does not exist.')
})
mockTask.assistantMessageContent = [
{
type: "tool_use",
id: "call_unknown",
name: "totally_made_up_tool",
params: {},
nativeArgs: {},
partial: false,
},
]
await presentAssistantMessage(mockTask as unknown as Task)
expect(mockTask.recordToolError).toHaveBeenCalledWith("invalid_tool_call", expect.any(String))
expect(mockTask.recordToolError).not.toHaveBeenCalledWith("totally_made_up_tool", expect.anything())
expect(mockTask.recordToolUsage).not.toHaveBeenCalled()
})
describe("native mcp_tool_use block", () => {
it("records exactly one attempt once the MCP tool's own validation passes", async () => {
mockTask.providerRef = {
deref: () => ({
getState: vi.fn().mockResolvedValue({
mode: "code",
customModes: [],
}),
getMcpHub: () => ({
findServerNameBySanitizedName: () => "my_server",
}),
}),
}
mockTask.assistantMessageContent = [
{
type: "mcp_tool_use",
id: "call_native_mcp",
name: "mcp_my_server_do_thing",
serverName: "my_server",
toolName: "do_thing",
arguments: {},
partial: false,
},
]
await presentAssistantMessage(mockTask as unknown as Task)
expect(mockTask.recordToolUsage).toHaveBeenCalledTimes(1)
expect(mockTask.recordToolUsage).toHaveBeenCalledWith("use_mcp_tool")
expect(TelemetryService.instance.captureToolUsage).toHaveBeenCalledTimes(1)
expect(TelemetryService.instance.captureToolUsage).toHaveBeenCalledWith(mockTask.taskId, "use_mcp_tool")
})
it("records no attempt when the MCP server is not on the mode's allow-list", async () => {
vi.mocked(getModeBySlug).mockReturnValueOnce({
slug: "code",
name: "Code",
roleDefinition: "",
groups: [],
allowedMcpServers: ["some-other-server"],
})
mockTask.providerRef = {
deref: () => ({
getState: vi.fn().mockResolvedValue({
mode: "code",
customModes: [],
}),
getMcpHub: () => ({
findServerNameBySanitizedName: () => "my_server",
}),
}),
}
mockTask.assistantMessageContent = [
{
type: "mcp_tool_use",
id: "call_native_mcp_disallowed",
name: "mcp_my_server_do_thing",
serverName: "my_server",
toolName: "do_thing",
arguments: {},
partial: false,
},
]
await presentAssistantMessage(mockTask as unknown as Task)
// The server is disallowed, so the call never reaches onValidated:
// no success attempt is recorded for a call that was never permitted to execute.
expect(mockTask.recordToolUsage).not.toHaveBeenCalled()
expect(TelemetryService.instance.captureToolUsage).not.toHaveBeenCalled()
})
})
})