Skip to content

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

Merge main into the event-identity branch

Claude8744a62dd0ca
diff --cc cli/beacon-hooks/cmd/endpoint_events.go
index 1592fa6b,0c49aded..f4e6bfe0
--- a/cli/beacon-hooks/cmd/endpoint_events.go
+++ b/cli/beacon-hooks/cmd/endpoint_events.go
@@@ -48,6 -48,16 +48,16 @@@ func emitHookEventWithFidelity(logger *
if platformFlag == "hermes" {
fields["raw"] = mergeNested(fields["raw"], map[string]interface{}{"hermes": input})
}
+ // Qwen carries real signal with no endpoint-schema field of its own: `permission_mode` on every
+ // tool event, `source` on SessionStart, `stop_hook_active` and the `context_usage` /
- // `context_limit` / `input_tokens` trio on Stop, `tool_use_id` linking a pre-tool event to its
- // post-tool result. Keeping the payload under `raw.qwen` preserves it without inventing schema
- // fields for one runtime. It is the whole event's own path through SanitizeMap, so raw is
- // secret-redacted and string-limited like every other field, and it is the first thing dropped
- // when an event exceeds the 64 KiB ceiling.
++ // `context_limit` / `input_tokens` trio on Stop, and the verbatim `tool_use_id` whose promoted
++ // form is the join key applyToolCallID writes just below. Keeping the payload under `raw.qwen`
++ // preserves it without inventing schema fields for one runtime. It is the whole event's own path
++ // through SanitizeMap, so raw is secret-redacted and string-limited like every other field, and
++ // it is the first thing dropped when an event exceeds the 64 KiB ceiling.
+ if platformFlag == "qwen" {
+ fields["raw"] = mergeNested(fields["raw"], map[string]interface{}{"qwen": input})
+ }
if platformFlag == "vscode" {
fields["raw"] = mergeNested(fields["raw"], map[string]interface{}{"vscode": input})
}
diff --cc cli/beacon-hooks/cmd/post_tool.go
index b75ba52c,0d00485a..88777251
--- a/cli/beacon-hooks/cmd/post_tool.go
+++ b/cli/beacon-hooks/cmd/post_tool.go
@@@ -206,11 -238,20 +238,24 @@@ func recordLocalEdit(params *evaluation
fields["session"] = mergeNested(fields["session"], map[string]interface{}{"id": params.sessionID})
}
applyWorkspaceFields(fields, input, filepath.Dir(params.filePath))
- // This path writes its event directly rather than through emitHookEvent, so the raw payload
- // that function attaches never reaches it. For Qwen that gap lands on exactly the events this
- // runtime's taxonomy exists to produce: a successful `write_file` or `edit` is classified
- // `file.modified` and routed here, so `permission_mode` and `tool_use_id` -- which have no
- // schema field, and whose only home is raw.qwen -- would survive on failed and non-edit tools
- // and vanish on the successful edits.
+ // This emitter writes its event without going through emitHookEvent, so it
+ // has to promote the call ID itself. Every file edit in the log is recorded
+ // twice -- once here, once from OTLP -- and this is the field that lets the
+ // writer tell that the two are one edit.
+ applyToolCallID(fields, input)
++ // The same gap costs Qwen its raw payload: emitHookEvent is what attaches it, and it lands on
++ // exactly the events this runtime's taxonomy exists to produce, since a successful `write_file`
++ // or `edit` is classified `file.modified` and routed here. `permission_mode` has no schema field
++ // at all and `tool_use_id` keeps its verbatim spelling only here, so both would survive on
++ // failed and non-edit tools and vanish on the successful edits.
+ //
+ // Scoped to qwen rather than applied to every runtime with a raw convention. grok, hermes and
+ // vscode have the same gap on this path, and closing it would change their recorded event shape
+ // with no fixture here to say what it should become; that is a stated limit rather than a
+ // silent one. TestQwenRawPayloadSurvivesTheFileEditPath is what holds the Qwen half.
+ if platformFlag == "qwen" {
+ fields["raw"] = mergeNested(fields["raw"], map[string]interface{}{"qwen": input})
+ }
logger.EndpointEvent("file.modified", "file", "info", "File edit observed", fields)
}
diff --cc cli/beacon-hooks/cmd/tool_call_id_test.go
index b0d2bbb6,00000000..260c7594
mode 100644,000000..100644
--- a/cli/beacon-hooks/cmd/tool_call_id_test.go
+++ b/cli/beacon-hooks/cmd/tool_call_id_test.go
@@@ -1,202 -1,0 +1,229 @@@
+package cmd
+
+import (
+ "path/filepath"
+ "regexp"
+ "testing"
+)
+
+// leaf reads a string at a dotted path, returning "" for anything missing along
+// the way. nested is the fatal-on-missing variant and is the wrong tool here:
+// half these assertions are about a field legitimately not being there.
+func leaf(event map[string]interface{}, keys ...string) string {
+ var current interface{} = event
+ for _, key := range keys {
+ next, ok := current.(map[string]interface{})
+ if !ok {
+ return ""
+ }
+ current = next[key]
+ }
+ value, _ := current.(string)
+ return value
+}
+
+func callIDOfEvent(event map[string]interface{}) string {
+ return leaf(event, "gen_ai", "tool", "call", "id")
+}
+
+// Claude Code puts tool_use_id at the top level of the hook payload, outside
+// tool_input, so no amount of reading the tool arguments would ever have found
+// it. It is the only value that links this event to the tool's result, to the
+// approval that allowed it, and to the OTLP record of the same command.
+func TestRunPostToolPromotesClaudeToolUseID(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": "Bash",
+ "tool_use_id": "toolu_01abc",
+ "tool_input": map[string]interface{}{"command": "echo hi"},
+ "tool_response": map[string]interface{}{"stdout": "hi"},
+ })
+
+ events := endpointEvents(t, logPath)
+ if len(events) != 1 {
+ t.Fatalf("event count = %d, want one command event: %#v", len(events), events)
+ }
+ if got := leaf(events[0], "event", "action"); got != "command.executed" {
+ t.Fatalf("event.action = %q, want command.executed", got)
+ }
+ if got := callIDOfEvent(events[0]); got != "toolu_01abc" {
+ t.Fatalf("gen_ai.tool.call.id = %q, want the tool_use_id Claude Code assigned", got)
+ }
+}
+
+// A Claude Edit reaches the log through recordLocalEdit, which writes its event
+// without going through emitHookEvent. Every file edit is recorded twice --
+// once here, once from OTLP -- so this is the path that most needs the join key.
+func TestRunPostToolPromotesToolUseIDOnFileEdits(t *testing.T) {
+ setupHookConfigDirs(t)
+ platformFlag = "claude"
+ dir := t.TempDir()
+ logPath := filepath.Join(dir, "runtime.jsonl")
+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)
+
+ runHookWithInput(t, runPostTool, map[string]interface{}{
+ "session_id": "claude-session",
+ "cwd": dir,
+ "hook_event_name": "PostToolUse",
+ "tool_name": "Write",
+ "tool_use_id": "toolu_01write",
+ "tool_input": map[string]interface{}{
+ "file_path": filepath.Join(dir, "notes.md"),
+ "content": "hello",
+ },
+ })
+
+ events := endpointEvents(t, logPath)
+ edits := 0
+ for _, event := range events {
+ if leaf(event, "event", "action") != "file.modified" {
+ continue
+ }
+ edits++
+ if got := callIDOfEvent(event); got != "toolu_01write" {
+ t.Fatalf("file.modified call ID = %q, want the tool_use_id Claude Code assigned", got)
+ }
+ }
+ if edits == 0 {
+ t.Fatalf("no file.modified event was written: %#v", events)
+ }
+}
+
+// A session lifecycle payload that happens to echo an ID is not a tool call.
+// Giving it the join key of one would join it to work it did not do.
+func TestSessionEventsDoNotTakeAToolCallID(t *testing.T) {
+ fields := map[string]interface{}{"session": map[string]interface{}{"id": "s1"}}
+ applyToolCallID(fields, map[string]interface{}{"tool_use_id": "toolu_01abc"})
+ if _, ok := fields["gen_ai"]; ok {
+ t.Fatalf("a session event took a tool call ID: %#v", fields)
+ }
+}
+
+// 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) {
+ fields := toolFieldsWithResponse("mcp__linear__list_issues", map[string]interface{}{
+ "mcp_server": "linear",
+ "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)
+ }
+ if got := leaf(fields, "gen_ai", "tool", "name"); got != "list_issues" {
+ t.Fatalf("gen_ai.tool.name = %q, want the MCP tool name to survive", got)
+ }
+ if got := nested(t, fields, "gen_ai", "tool", "call")["arguments"]; got == nil {
+ t.Fatal("gen_ai.tool.call.arguments was dropped when the call ID was written")
+ }
+}
+
+// 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"},
+ // The response is checked too: it is the tool's own output, so it is no
+ // more a statement of identity than the arguments are.
+ "tool_response": map[string]interface{}{"ok": true, "call_id": "ch_RESPONSE"},
+ })
+
+ 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
+// runtime named the action itself.
+func TestHookEventsCarryAnEventID(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": "Bash",
+ "tool_use_id": "toolu_01abc",
+ "tool_input": map[string]interface{}{"command": "echo hi"},
+ })
+
+ for _, event := range endpointEvents(t, logPath) {
+ id := leaf(event, "event", "id")
+ if !uuidV5Pattern.MatchString(id) {
+ t.Fatalf("event.id = %q, want an RFC 4122 version 5 UUID: %#v", id, event)
+ }
+ }
+}
++
++// recordLocalEdit is where this change and Qwen's raw-payload attachment (#380)
++// landed on the same line while both branches were open, because it is the one
++// emitter that bypasses emitHookEvent and so has to repeat by hand whatever that
++// function does. Each was written without the other in the tree, so a resolution
++// that kept either alone would compile and pass that branch's own tests while
++// silently dropping the other's field -- the same shape of accident as the
++// July/August pair this identity work exists to undo.
++//
++// Asserted on one event rather than in two tests: the risk is not that either
++// line is absent, it is that they cannot both be present at once.
++func TestQwenFileEditCarriesBothTheCallIDAndTheRawPayload(t *testing.T) {
++ logPath := setupQwenHook(t)
++
++ runHookWithInput(t, runPostTool, readQwenFixture(t, "post_tool_write_file.json"))
++
++ event := lastEndpointEvent(t, logPath)
++ if got := qwenAction(t, event); got != "file.modified" {
++ t.Fatalf("event.action = %q, want the file edit path that owns this line", got)
++ }
++ if got := callIDOfEvent(event); got != "toolu_01QwenWrite" {
++ t.Errorf("gen_ai.tool.call.id = %q, want the fixture's tool_use_id promoted", got)
++ }
++ if got := leaf(event, "raw", "qwen", "permission_mode"); got != "default" {
++ t.Errorf("raw.qwen.permission_mode = %q, want the payload Qwen has no schema field for", got)
++ }
++}