Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Read the tool call ID from the payload envelope, never from tool arguments

Claudee49f76c8d184
diff --git a/cli/beacon-hooks/cmd/cline_event.go b/cli/beacon-hooks/cmd/cline_event.go
index 58933b63..0e35444c 100644
--- a/cli/beacon-hooks/cmd/cline_event.go
+++ b/cli/beacon-hooks/cmd/cline_event.go
@@ -574,7 +574,7 @@ func clineToolFields(input map[string]interface{}, completed bool) map[string]in
call["result"] = toolResponse
}
}
- if callID := toolCallIDIn(input); callID != "" {
+ if callID := toolCallIDFromEnvelope(input); callID != "" {
call["id"] = callID
}
fields["gen_ai"] = map[string]interface{}{
diff --git a/cli/beacon-hooks/cmd/endpoint_events.go b/cli/beacon-hooks/cmd/endpoint_events.go
index 50a38711..7fba61ce 100644
--- a/cli/beacon-hooks/cmd/endpoint_events.go
+++ b/cli/beacon-hooks/cmd/endpoint_events.go
@@ -276,25 +276,34 @@ func toolFieldsWithResponse(toolName string, toolInput, toolResponse map[string]
fields[key] = value
}
}
- // The call ID goes on every tool event, not just the MCP ones that happened
- // to build a gen_ai object for their arguments. It is the join key: without
- // it nothing links this call to its result, or to the OTLP record of the
- // same action. Only the ID is promoted here -- arguments and results are a
- // separate question about what Beacon retains, not about identity.
- if callID := toolCallIDIn(toolInput, toolResponse); callID != "" {
- setToolCallID(fields, callID)
- }
+ // No call ID is read here, deliberately: this function sees only the tool's
+ // arguments and its output, and neither is where identity lives. Callers
+ // promote it from the payload envelope instead -- emitHookEvent for every
+ // hook mapper, and the Cline, OpenCode and file-edit emitters by name.
+ //
+ // Scanning the arguments was worse than merely useless. tool_input is what
+ // the model chose to pass, so a tool that legitimately takes an argument
+ // called call_id -- an MCP wrapper over an API whose objects have call IDs,
+ // say -- had that value written as gen_ai.tool.call.id, and because an ID
+ // already present is treated as authoritative it then blocked the runtime's
+ // real tool_use_id. One wrong join key, and the right one shut out.
+ // Reported by Cursor Bugbot.
return fields
}
-// toolCallIDIn reads the runtime's own identifier for one tool invocation out of
-// the payload maps a hook mapper has to hand.
+// toolCallIDFromEnvelope reads the runtime's own identifier for one tool
+// invocation out of a hook payload's top level.
+//
+// The envelope only. The runtime writes this field itself, whereas tool_input is
+// model-authored and tool_response is the tool's own output -- neither is a
+// statement by the runtime about which call this is, and reading identity out of
+// either lets a tool argument masquerade as the join key.
//
// The alias list is shared with the OTLP path so both capture paths promote the
// same value to the same field, which is what lets duplicate suppression
// recognise the two reports of one action as one action.
-func toolCallIDIn(inputs ...map[string]interface{}) string {
- return firstToolStringAcross(inputs, asymptoteobserve.ToolCallIDKeys...)
+func toolCallIDFromEnvelope(input map[string]interface{}) string {
+ return firstToolStringAcross([]map[string]interface{}{input}, asymptoteobserve.ToolCallIDKeys...)
}
// applyToolCallID promotes a call ID carried at the top level of a hook payload,
@@ -308,7 +317,7 @@ func applyToolCallID(fields, input map[string]interface{}) {
if fields == nil || toolCallIDOf(fields) != "" || !describesToolAction(fields) {
return
}
- if callID := toolCallIDIn(input); callID != "" {
+ if callID := toolCallIDFromEnvelope(input); callID != "" {
setToolCallID(fields, callID)
}
}
diff --git a/cli/beacon-hooks/cmd/opencode_event.go b/cli/beacon-hooks/cmd/opencode_event.go
index de0b99bc..731138d2 100644
--- a/cli/beacon-hooks/cmd/opencode_event.go
+++ b/cli/beacon-hooks/cmd/opencode_event.go
@@ -266,7 +266,7 @@ func opencodeToolFields(input map[string]interface{}, completed bool) map[string
toolInput := firstMap(input, "tool_input", "toolInput")
toolResponse := firstMap(input, "tool_response", "toolResponse")
fields := toolFieldsWithResponse(toolName, toolInput, toolResponse)
- callID := toolCallIDIn(input)
+ callID := toolCallIDFromEnvelope(input)
call := map[string]interface{}{}
if len(toolInput) > 0 {
call["arguments"] = toolInput
diff --git a/cli/beacon-hooks/cmd/tool_call_id_test.go b/cli/beacon-hooks/cmd/tool_call_id_test.go
index b6fc585c..e3101fdf 100644
--- a/cli/beacon-hooks/cmd/tool_call_id_test.go
+++ b/cli/beacon-hooks/cmd/tool_call_id_test.go
@@ -111,9 +111,9 @@ func TestSessionEventsDoNotTakeAToolCallID(t *testing.T) {
func TestToolCallIDDoesNotDisplaceMCPToolFields(t *testing.T) {
fields := toolFieldsWithResponse("mcp__linear__list_issues", map[string]interface{}{
"mcp_server": "linear",
- "call_id": "call_42",
"team": "beacon",
}, nil)
+ setToolCallID(fields, "call_42")
if got := callIDOfEvent(fields); got != "call_42" {
t.Fatalf("gen_ai.tool.call.id = %q, want call_42", got)
}
@@ -125,6 +125,52 @@ func TestToolCallIDDoesNotDisplaceMCPToolFields(t *testing.T) {
}
}
+// Identity comes from the payload envelope, never from the tool's arguments.
+//
+// tool_input is what the model chose to pass, so a tool that legitimately takes
+// an argument called call_id used to have that value written as the join key --
+// and because an ID already present is treated as authoritative, it then shut
+// out the runtime's real tool_use_id. Reported by Cursor Bugbot.
+func TestArgumentNamedCallIDNeverBecomesTheJoinKey(t *testing.T) {
+ setupHookConfigDirs(t)
+ platformFlag = "claude"
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)
+
+ runHookWithInput(t, runPostTool, map[string]interface{}{
+ "session_id": "claude-session",
+ "cwd": "/repo",
+ "hook_event_name": "PostToolUse",
+ "tool_name": "mcp__stripe__get_charge",
+ "tool_use_id": "toolu_REAL",
+ "tool_input": map[string]interface{}{"mcp_server": "stripe", "call_id": "ch_ARGUMENT"},
+ })
+
+ events := endpointEvents(t, logPath)
+ for _, event := range events {
+ if got := callIDOfEvent(event); got != "toolu_REAL" {
+ t.Fatalf("gen_ai.tool.call.id = %q, want the runtime's toolu_REAL rather than a tool argument", got)
+ }
+ }
+ if len(events) == 0 {
+ t.Fatal("no event was written")
+ }
+}
+
+// The same guarantee with no envelope ID to fall back on: a tool argument must
+// not be promoted at all, because a wrong join key mis-joins events, which is
+// worse than no join key.
+func TestArgumentNamedCallIDIsNotPromotedOnItsOwn(t *testing.T) {
+ fields := toolFieldsWithResponse("mcp__stripe__get_charge", map[string]interface{}{
+ "mcp_server": "stripe",
+ "call_id": "ch_ARGUMENT",
+ }, nil)
+ applyToolCallID(fields, map[string]interface{}{"session_id": "s1"})
+ if got := callIDOfEvent(fields); got != "" {
+ t.Fatalf("gen_ai.tool.call.id = %q, want empty: a tool argument is not an identity", got)
+ }
+}
+
var uuidV5Pattern = regexp.MustCompile(`^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`)
// Every event the hook writer emits carries an identity, whether or not the