Skip to content

Commit 1ef15a4

Browse files
fix: track workspace integration calls where MCP tools run
MCP tools never pass through Tool.define, so the via_workspace row and the integrations count now come from the MCP execution path, keyed on the client name; the id-prefix test in tool.ts is gone, so datamate_manager no longer counts. The TUI publish event carries workspace_id like the CLI and serve paths. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_011AhtJPr3nmT1Po92ZQk4VE
1 parent 88d648a commit 1ef15a4

5 files changed

Lines changed: 148 additions & 15 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// altimate_change — tool_call rows for the bound workspace's integration calls.
2+
import { Telemetry } from "@/altimate/telemetry"
3+
import { mcpToolSource } from "@/altimate/tool-source"
4+
5+
/** True when the MCP client serving a tool is the bound workspace's datamate server. */
6+
export function isWorkspaceClient(clientName: string): boolean {
7+
return mcpToolSource(clientName) === "altimate"
8+
}
9+
10+
/**
11+
* Track one MCP call that went through the bound workspace. MCP tools never pass through the
12+
* `Tool.define` wrapper that tracks registry tools, so this is where a workspace integration
13+
* call becomes a `tool_call` row; a successful one also counts as an integration use for the
14+
* session. Third-party MCP servers stay untracked and registry tools (`datamate_manager`
15+
* included) never reach here. Returns whether the call was tracked.
16+
*/
17+
export function trackWorkspaceToolCall(input: {
18+
key: string
19+
clientName: string
20+
sessionID: string
21+
messageID: string
22+
startedAt: number
23+
outcome: "success" | "error"
24+
error?: unknown
25+
}): boolean {
26+
if (!isWorkspaceClient(input.clientName)) return false
27+
try {
28+
if (input.outcome === "success") Telemetry.noteWorkspaceUse(input.sessionID, "integrations")
29+
const message = input.error instanceof Error ? input.error.message : String(input.error)
30+
Telemetry.track({
31+
type: "tool_call",
32+
timestamp: Date.now(),
33+
session_id: input.sessionID,
34+
message_id: input.messageID,
35+
tool_name: input.key,
36+
tool_type: "mcp",
37+
tool_category: Telemetry.categorizeToolName(input.key, "mcp"),
38+
status: input.outcome,
39+
via_workspace: true,
40+
duration_ms: Date.now() - input.startedAt,
41+
sequence_index: 0,
42+
previous_tool: null,
43+
...(input.error !== undefined && { error: Telemetry.maskString(message).slice(0, 500) }),
44+
})
45+
} catch {
46+
// telemetry never surfaces into the tool call
47+
}
48+
return true
49+
}

‎packages/opencode/src/plugin/tui/altimate/skill-ops.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ export async function publishFromPicker(
550550
skill_name: skillName,
551551
action: report.action,
552552
file_count: report.files,
553+
workspace_id: report.datamateId,
553554
source: "tui",
554555
})
555556
} catch {}

‎packages/opencode/src/session/prompt.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ import { Config } from "../config/config"
103103
import { Tracer } from "../altimate/observability/tracing"
104104
// altimate_change start — stamp an authoritative tool source + humanized MCP title
105105
import { stampRegistryToolSource, describeMcpTool } from "../altimate/tool-source"
106+
import { trackWorkspaceToolCall } from "../altimate/workspace/tool-call-telemetry" // altimate_change — workspace integration rows
106107
// altimate_change end
107108
// altimate_change end
108109
import { Telemetry } from "@/telemetry" // altimate_change — session telemetry
@@ -2515,7 +2516,32 @@ export namespace SessionPrompt {
25152516
)
25162517
// altimate_change end
25172518

2518-
const result = await execute(args, opts)
2519+
// altimate_change start — a workspace integration call is tracked here, where MCP tools run
2520+
const startedAt = Date.now()
2521+
let result
2522+
try {
2523+
result = await execute(args, opts)
2524+
} catch (err) {
2525+
trackWorkspaceToolCall({
2526+
key,
2527+
clientName,
2528+
sessionID: ctx.sessionID,
2529+
messageID: ctx.messageID,
2530+
startedAt,
2531+
outcome: "error",
2532+
error: err,
2533+
})
2534+
throw err
2535+
}
2536+
trackWorkspaceToolCall({
2537+
key,
2538+
clientName,
2539+
sessionID: ctx.sessionID,
2540+
messageID: ctx.messageID,
2541+
startedAt,
2542+
outcome: (result as { isError?: boolean }).isError === true ? "error" : "success",
2543+
})
2544+
// altimate_change end
25192545

25202546
await Plugin.trigger(
25212547
"tool.execute.after",

‎packages/opencode/src/tool/tool.ts‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,6 @@ export type InferDef<T> =
131131
: never
132132

133133
// altimate_change start — telemetry helpers (ported from the fork's pre-1.17 Tool.define wrapper)
134-
// altimate_change start — a tool whose id starts with the datamate prefix is an MCP tool of the bound
135-
// workspace (mirrors ALTIMATE_MCP_PREFIXES in altimate/tool-source.ts: `datamate`, `datamates`, `datamate-…`).
136-
function viaWorkspace(id: string): boolean {
137-
return /^datamates?([_-]|$)/i.test(id)
138-
}
139-
// altimate_change end
140-
141134
function altimateTrackError(id: string, args: unknown, ctx: Context, startTime: number, error: unknown) {
142135
try {
143136
const errorMsg = error instanceof Error ? error.message : String(error)
@@ -151,8 +144,6 @@ function altimateTrackError(id: string, args: unknown, ctx: Context, startTime:
151144
tool_type: "standard",
152145
tool_category: Telemetry.categorizeToolName(id, "standard"),
153146
status: "error",
154-
// altimate_change — workspace integration call
155-
...(viaWorkspace(id) && { via_workspace: true }),
156147
duration_ms: Date.now() - startTime,
157148
sequence_index: 0,
158149
previous_tool: null,
@@ -186,9 +177,6 @@ function altimateTrackSuccess(
186177
try {
187178
const isSoftFailure = result.metadata?.success === false
188179
const durationMs = Date.now() - startTime
189-
// altimate_change start — count successful calls to the bound workspace's integrations
190-
if (!isSoftFailure && viaWorkspace(id)) Telemetry.noteWorkspaceUse(ctx.sessionID, "integrations")
191-
// altimate_change end
192180
const toolCategory = Telemetry.categorizeToolName(id, "standard")
193181
// Skip success tool_call for high-volume/low-signal file tools; failures still tracked.
194182
if (isSoftFailure || toolCategory !== "file") {
@@ -201,8 +189,6 @@ function altimateTrackSuccess(
201189
tool_type: "standard",
202190
tool_category: toolCategory,
203191
status: isSoftFailure ? "error" : "success",
204-
// altimate_change — workspace integration call
205-
...(viaWorkspace(id) && { via_workspace: true }),
206192
duration_ms: durationMs,
207193
sequence_index: 0,
208194
previous_tool: null,
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"
2+
import { Telemetry } from "../../../src/altimate/telemetry"
3+
import { isWorkspaceClient, trackWorkspaceToolCall } from "../../../src/altimate/workspace/tool-call-telemetry"
4+
5+
describe("workspace integration tool calls", () => {
6+
let rows: any[]
7+
let spy: ReturnType<typeof spyOn>
8+
beforeEach(() => {
9+
rows = []
10+
spy = spyOn(Telemetry, "track").mockImplementation((e) => {
11+
rows.push(e)
12+
})
13+
Telemetry.clearWorkspaceUse("s1")
14+
})
15+
afterEach(() => {
16+
spy.mockRestore()
17+
Telemetry.clearWorkspaceUse("s1")
18+
})
19+
20+
const call = (clientName: string, outcome: "success" | "error", error?: unknown) =>
21+
trackWorkspaceToolCall({
22+
key: `${clientName}_jira_get_issue`,
23+
clientName,
24+
sessionID: "s1",
25+
messageID: "m1",
26+
startedAt: Date.now() - 5,
27+
outcome,
28+
error,
29+
})
30+
31+
test("the datamate client's calls become tool_call rows with via_workspace; a success counts as an integration use", () => {
32+
expect(call("datamate", "success")).toBe(true)
33+
expect(rows).toHaveLength(1)
34+
expect(rows[0]).toEqual(
35+
expect.objectContaining({
36+
type: "tool_call",
37+
tool_name: "datamate_jira_get_issue",
38+
tool_type: "mcp",
39+
status: "success",
40+
via_workspace: true,
41+
session_id: "s1",
42+
message_id: "m1",
43+
}),
44+
)
45+
expect(rows[0].duration_ms).toBeGreaterThanOrEqual(0)
46+
expect("error" in rows[0]).toBe(false)
47+
expect(Telemetry.workspaceUseOf("s1").integrations).toBe(1)
48+
})
49+
50+
test("a failed call is a tool_call error row with a masked message and no integration use", () => {
51+
expect(call("datamates", "error", new Error("boom at /home/someone/secret.txt"))).toBe(true)
52+
expect(rows[0]).toEqual(expect.objectContaining({ status: "error", via_workspace: true, tool_type: "mcp" }))
53+
expect(rows[0].error).not.toContain("/home/someone")
54+
expect(Telemetry.workspaceUseOf("s1").integrations).toBe(0)
55+
expect(call("datamate-acme", "error", "string failure")).toBe(true)
56+
expect(rows[1].error).toContain("string failure")
57+
})
58+
59+
test("other MCP clients are not tracked here, and a registry tool id is not a client", () => {
60+
expect(call("github", "success")).toBe(false)
61+
expect(call("filesystem", "error", new Error("x"))).toBe(false)
62+
expect(rows).toHaveLength(0)
63+
expect(Telemetry.workspaceUseOf("s1").integrations).toBe(0)
64+
// The registry tool `datamate_manager` runs through Tool.define, never through this path, and its
65+
// id is not a datamate client name either.
66+
expect(isWorkspaceClient("datamate_manager")).toBe(false)
67+
expect(isWorkspaceClient("datamate")).toBe(true)
68+
expect(isWorkspaceClient("Datamates")).toBe(true)
69+
expect(isWorkspaceClient("datamate-acme")).toBe(true)
70+
})
71+
})

0 commit comments

Comments
 (0)