Skip to content

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

fix: let top-level hook payload ID override tool argument call_id

Cursor Agent524e2b544394
diff --git a/cli/beacon-hooks/cmd/endpoint_events.go b/cli/beacon-hooks/cmd/endpoint_events.go
index 7fba61ce..bc05f122 100644
--- a/cli/beacon-hooks/cmd/endpoint_events.go
+++ b/cli/beacon-hooks/cmd/endpoint_events.go
@@ -310,11 +310,16 @@ func toolCallIDFromEnvelope(input map[string]interface{}) string {
// which is where Claude Code puts tool_use_id -- outside tool_input, so no
// amount of reading the tool arguments would ever have found it.
//
+// The top-level payload ID is authoritative: it always overwrites any value that
+// toolFieldsWithResponse may have set from inside tool arguments, because tool
+// parameters are user-controlled and may coincidentally carry key names like
+// call_id without being the runtime's invocation identifier.
+//
// Only an event that already describes a tool action takes one. A session
// lifecycle payload that happens to echo an ID is not a tool call, and giving it
// the join key of one would join it to work it did not do.
func applyToolCallID(fields, input map[string]interface{}) {
- if fields == nil || toolCallIDOf(fields) != "" || !describesToolAction(fields) {
+ if fields == nil || !describesToolAction(fields) {
return
}
if callID := toolCallIDFromEnvelope(input); callID != "" {
@@ -331,8 +336,7 @@ func describesToolAction(fields map[string]interface{}) bool {
return false
}
-// toolCallIDOf reads back what setToolCallID wrote, so a mapper that already
-// found the ID somewhere better is not overwritten by a weaker source.
+// toolCallIDOf reads back what setToolCallID wrote.
func toolCallIDOf(fields map[string]interface{}) string {
genAI, _ := fields["gen_ai"].(map[string]interface{})
tool, _ := genAI["tool"].(map[string]interface{})
diff --git a/cli/beacon-hooks/cmd/tool_call_id_test.go b/cli/beacon-hooks/cmd/tool_call_id_test.go
index e3101fdf..44ac02f8 100644
--- a/cli/beacon-hooks/cmd/tool_call_id_test.go
+++ b/cli/beacon-hooks/cmd/tool_call_id_test.go
@@ -106,6 +106,39 @@ func TestSessionEventsDoNotTakeAToolCallID(t *testing.T) {
}
}
+// A tool argument named call_id (common in telephony and similar APIs) must not
+// block the real runtime invocation ID that sits at the top level of the payload.
+func TestTopLevelIDOverridesToolArgumentCallID(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__telephony__dial",
+ "tool_use_id": "toolu_real_id",
+ "tool_input": map[string]interface{}{
+ "mcp_server": "telephony",
+ "call_id": "tel_wrong_id",
+ "number": "+1555000000",
+ },
+ "tool_response": map[string]interface{}{"ok": true},
+ })
+
+ events := endpointEvents(t, logPath)
+ if len(events) == 0 {
+ t.Fatal("no events written")
+ }
+ for _, event := range events {
+ if got := callIDOfEvent(event); got != "toolu_real_id" {
+ t.Fatalf("gen_ai.tool.call.id = %q, want toolu_real_id (the runtime ID, not the tool argument)", got)
+ }
+ }
+}
+
// An MCP event already builds a gen_ai object for its arguments, so the call ID
// has to be written into it rather than over it.
func TestToolCallIDDoesNotDisplaceMCPToolFields(t *testing.T) {