Skip to content

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

Fill branch on hook emitters that bypass emitHookEvent

Claudef46a02956020
diff --git a/cli/beacon-hooks/cmd/endpoint_events.go b/cli/beacon-hooks/cmd/endpoint_events.go
index b91f60d9..b7886af6 100644
--- a/cli/beacon-hooks/cmd/endpoint_events.go
+++ b/cli/beacon-hooks/cmd/endpoint_events.go
@@ -65,6 +65,23 @@ func resolveBranch(input map[string]interface{}, cwd string) string {
return git.CurrentBranch(cwd)
}
+// applyBranchField fills fields["branch"] for emitters that write endpoint
+// events without going through emitHookEvent. fallbackDir seeds branch
+// resolution when the payload carries no working directory, such as a file
+// edit identified only by its path.
+func applyBranchField(fields map[string]interface{}, input map[string]interface{}, fallbackDir string) {
+ if existing, ok := fields["branch"]; ok && existing != "" {
+ return
+ }
+ cwd := resolveCwd(input, platformFlag)
+ if cwd == "" {
+ cwd = fallbackDir
+ }
+ if branch := resolveBranch(input, cwd); branch != "" {
+ fields["branch"] = branch
+ }
+}
+
// gitMetadataDisabled accepts 1/true/yes, matching other Beacon disable flags.
func gitMetadataDisabled() bool {
switch strings.ToLower(strings.TrimSpace(os.Getenv("BEACON_DISABLE_GIT_METADATA"))) {
diff --git a/cli/beacon-hooks/cmd/endpoint_events_test.go b/cli/beacon-hooks/cmd/endpoint_events_test.go
index ec9d4041..26ee8335 100644
--- a/cli/beacon-hooks/cmd/endpoint_events_test.go
+++ b/cli/beacon-hooks/cmd/endpoint_events_test.go
@@ -116,6 +116,58 @@ func TestRunPromptSubmitOmitsBranchOutsideRepo(t *testing.T) {
}
}
+func TestRunOpenCodeEventEmitsLocalGitBranch(t *testing.T) {
+ setupHookConfigDirs(t)
+ platformFlag = "opencode"
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)
+
+ repo := t.TempDir()
+ writeGitHead(t, repo, "ref: refs/heads/feature/opencode\n")
+
+ runHookWithInput(t, runOpenCodeEvent, map[string]interface{}{
+ "type": "command.executed",
+ "session_id": "oc-branch",
+ "cwd": repo,
+ "command": "ls",
+ })
+
+ event := lastEndpointEvent(t, logPath)
+ if got := event["branch"]; got != "feature/opencode" {
+ t.Fatalf("branch = %q, want feature/opencode", got)
+ }
+}
+
+func TestRunPostToolFileEditEmitsLocalGitBranch(t *testing.T) {
+ setupHookConfigDirs(t)
+ platformFlag = "cursor"
+ logPath := filepath.Join(t.TempDir(), "runtime.jsonl")
+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)
+
+ repo := t.TempDir()
+ writeGitHead(t, repo, "ref: refs/heads/feature/edit\n")
+ editedFile := filepath.Join(repo, "pkg", "main.go")
+
+ // No workspace_roots or cwd in the payload: branch must resolve from the
+ // edited file's own directory.
+ runHookWithInput(t, runPostTool, map[string]interface{}{
+ "conversation_id": "conv-edit",
+ "hook_event_name": "afterFileEdit",
+ "file_path": editedFile,
+ "edits": []interface{}{
+ map[string]interface{}{"old_string": "old line", "new_string": "new line"},
+ },
+ })
+
+ event := lastEndpointEvent(t, logPath)
+ if action := event["event"].(map[string]interface{})["action"]; action != "file.modified" {
+ t.Fatalf("event.action = %q, want file.modified", action)
+ }
+ if got := event["branch"]; got != "feature/edit" {
+ t.Fatalf("branch = %q, want feature/edit", got)
+ }
+}
+
func TestToolFieldsMapsMCPGenAIStandardFields(t *testing.T) {
tests := []struct {
name string
diff --git a/cli/beacon-hooks/cmd/opencode_event.go b/cli/beacon-hooks/cmd/opencode_event.go
index 17b7a100..ad243cf9 100644
--- a/cli/beacon-hooks/cmd/opencode_event.go
+++ b/cli/beacon-hooks/cmd/opencode_event.go
@@ -44,6 +44,7 @@ func runOpenCodeEvent(cmd *cobra.Command, args []string) {
func opencodeEndpointEvent(input map[string]interface{}, sessionID string) (string, string, string, string, map[string]interface{}) {
eventType := getFirstStr(input, "type", "event_type", "hook")
fields := sessionFields(sessionID, input)
+ applyBranchField(fields, input, "")
fields["raw"] = map[string]interface{}{
"opencode": input,
}
diff --git a/cli/beacon-hooks/cmd/post_tool.go b/cli/beacon-hooks/cmd/post_tool.go
index 1211e5fd..97a337fb 100644
--- a/cli/beacon-hooks/cmd/post_tool.go
+++ b/cli/beacon-hooks/cmd/post_tool.go
@@ -2,6 +2,7 @@ package cmd
import (
"encoding/json"
+ "path/filepath"
"strings"
"github.com/spf13/cobra"
@@ -85,7 +86,7 @@ func runPostTool(cmd *cobra.Command, args []string) {
}
seedCursorCloudRunID(input)
- recordLocalEdit(params, logger)
+ recordLocalEdit(params, input, logger)
maybeUploadCursorCloudTelemetry(logger)
outputJSON(emptyResponse)
}
@@ -196,7 +197,7 @@ func parseClaudeCopilotInput(input map[string]interface{}, logger *logging.Logge
}
// recordLocalEdit logs file-edit metadata without sending code or diffs to a hosted service.
-func recordLocalEdit(params *evaluationParams, logger *logging.Logger) {
+func recordLocalEdit(params *evaluationParams, input map[string]interface{}, logger *logging.Logger) {
logger.Info("File edit observed", "file_path", params.filePath, "tool_name", params.toolName)
fields := map[string]interface{}{}
for key, value := range params.extraFields {
@@ -209,6 +210,7 @@ func recordLocalEdit(params *evaluationParams, logger *logging.Logger) {
if params.sessionID != "" {
fields["session"] = mergeNested(fields["session"], map[string]interface{}{"id": params.sessionID})
}
+ applyBranchField(fields, input, filepath.Dir(params.filePath))
logger.EndpointEvent("file.modified", "file", "info", "File edit observed", fields)
}