-
Notifications
You must be signed in to change notification settings - Fork 219
fix(telemetry): record tool usage once centrally, sanitize raw tool names #1073
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
edelauna
wants to merge
4
commits into
main
Choose a base branch
from
fix/tool-usage-telemetry-attribution
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b7945d9
fix(telemetry): record tool usage once centrally, sanitize raw tool n…
edelauna ad2bd80
fix(telemetry): defer native MCP usage recording until validation passes
edelauna 7a316fa
fix(telemetry): narrow UseMcpToolTool callback, harden test mocks, cl…
edelauna 18172e6
test(telemetry): complete native MCP mock so validateToolExists runs …
edelauna 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
319 changes: 319 additions & 0 deletions
319
src/core/assistant-message/__tests__/presentAssistantMessage-tool-usage-attribution.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,319 @@ | ||
| // 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", | ||
| getAllServers: () => [ | ||
| { | ||
| name: "my_server", | ||
| tools: [{ name: "do_thing", enabledForPrompt: true }], | ||
| }, | ||
| ], | ||
| }), | ||
| }), | ||
| } | ||
|
|
||
| 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() | ||
| }) | ||
| }) | ||
| }) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test currently passes through the validation error fallback because
getAllServersis missing. Add the server and tool so it tests the real success path.