Skip to content

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

Pair workspace fields with branch and stop broken-worktree walk

Claudedd13f66af216
diff --git a/cli/beacon-hooks/cmd/endpoint_events.go b/cli/beacon-hooks/cmd/endpoint_events.go
index b7886af6..ec2d472f 100644
--- a/cli/beacon-hooks/cmd/endpoint_events.go
+++ b/cli/beacon-hooks/cmd/endpoint_events.go
@@ -65,19 +65,28 @@ 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) {
+// applyWorkspaceFields fills workspace context (session.working_directory,
+// repository, branch) for emitters that write endpoint events without going
+// through emitHookEvent, mirroring emitHookEvent's enrichment. fallbackDir
+// seeds branch resolution when the payload carries no working directory,
+// such as a file edit identified only by its path; it is never used for
+// repository, which must reflect the actual workspace path.
+func applyWorkspaceFields(fields map[string]interface{}, input map[string]interface{}, fallbackDir string) {
+ cwd := resolveCwd(input, platformFlag)
+ if cwd != "" {
+ fields["session"] = mergeNested(fields["session"], map[string]interface{}{"working_directory": cwd})
+ if existing, ok := fields["repository"]; !ok || existing == "" {
+ fields["repository"] = cwd
+ }
+ }
if existing, ok := fields["branch"]; ok && existing != "" {
return
}
- cwd := resolveCwd(input, platformFlag)
- if cwd == "" {
- cwd = fallbackDir
+ branchDir := cwd
+ if branchDir == "" {
+ branchDir = fallbackDir
}
- if branch := resolveBranch(input, cwd); branch != "" {
+ if branch := resolveBranch(input, branchDir); branch != "" {
fields["branch"] = branch
}
}
diff --git a/cli/beacon-hooks/cmd/endpoint_events_test.go b/cli/beacon-hooks/cmd/endpoint_events_test.go
index 26ee8335..1dd4ec87 100644
--- a/cli/beacon-hooks/cmd/endpoint_events_test.go
+++ b/cli/beacon-hooks/cmd/endpoint_events_test.go
@@ -166,6 +166,46 @@ func TestRunPostToolFileEditEmitsLocalGitBranch(t *testing.T) {
if got := event["branch"]; got != "feature/edit" {
t.Fatalf("branch = %q, want feature/edit", got)
}
+ // The file-dir fallback must not masquerade as the workspace path.
+ if repository, ok := event["repository"]; ok {
+ t.Fatalf("repository = %q, want no repository without a payload cwd", repository)
+ }
+}
+
+func TestRunPostToolFileEditEmitsWorkspaceFields(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")
+
+ runHookWithInput(t, runPostTool, map[string]interface{}{
+ "conversation_id": "conv-edit-ws",
+ "hook_event_name": "afterFileEdit",
+ "workspace_roots": []interface{}{repo},
+ "file_path": editedFile,
+ "edits": []interface{}{
+ map[string]interface{}{"old_string": "old line", "new_string": "new line"},
+ },
+ })
+
+ event := lastEndpointEvent(t, logPath)
+ if got := event["repository"]; got != repo {
+ t.Fatalf("repository = %q, want %q", got, repo)
+ }
+ session := event["session"].(map[string]interface{})
+ if got := session["working_directory"]; got != repo {
+ t.Fatalf("session.working_directory = %q, want %q", got, repo)
+ }
+ if got := session["id"]; got != "conv-edit-ws" {
+ t.Fatalf("session.id = %q, want conv-edit-ws", got)
+ }
+ if got := event["branch"]; got != "feature/edit" {
+ t.Fatalf("branch = %q, want feature/edit", got)
+ }
}
func TestToolFieldsMapsMCPGenAIStandardFields(t *testing.T) {
diff --git a/cli/beacon-hooks/cmd/opencode_event.go b/cli/beacon-hooks/cmd/opencode_event.go
index ad243cf9..3fd603d6 100644
--- a/cli/beacon-hooks/cmd/opencode_event.go
+++ b/cli/beacon-hooks/cmd/opencode_event.go
@@ -44,7 +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, "")
+ applyWorkspaceFields(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 97a337fb..238d817b 100644
--- a/cli/beacon-hooks/cmd/post_tool.go
+++ b/cli/beacon-hooks/cmd/post_tool.go
@@ -210,7 +210,7 @@ func recordLocalEdit(params *evaluationParams, input map[string]interface{}, log
if params.sessionID != "" {
fields["session"] = mergeNested(fields["session"], map[string]interface{}{"id": params.sessionID})
}
- applyBranchField(fields, input, filepath.Dir(params.filePath))
+ applyWorkspaceFields(fields, input, filepath.Dir(params.filePath))
logger.EndpointEvent("file.modified", "file", "info", "File edit observed", fields)
}
diff --git a/cli/beacon-hooks/internal/git/git.go b/cli/beacon-hooks/internal/git/git.go
index 2947bbaa..316ac67d 100644
--- a/cli/beacon-hooks/internal/git/git.go
+++ b/cli/beacon-hooks/internal/git/git.go
@@ -54,10 +54,14 @@ func findHeadFile(cwd string) string {
return filepath.Join(gitPath, "HEAD")
}
if gitdir := parseGitDirPointer(gitPath); gitdir != "" {
+ // A parsed pointer marks this directory as its own
+ // checkout. A missing HEAD means the checkout is broken;
+ // walking on would misattribute an enclosing repo's branch.
head := filepath.Join(gitdir, "HEAD")
if _, err := os.Stat(head); err == nil {
return head
}
+ return ""
}
}
diff --git a/cli/beacon-hooks/internal/git/git_test.go b/cli/beacon-hooks/internal/git/git_test.go
index 3f731c4f..142f6c91 100644
--- a/cli/beacon-hooks/internal/git/git_test.go
+++ b/cli/beacon-hooks/internal/git/git_test.go
@@ -103,6 +103,20 @@ func TestCurrentBranchWorktreeRelativeGitDirPointer(t *testing.T) {
}
}
+func TestCurrentBranchBrokenWorktreeDoesNotReportParentBranch(t *testing.T) {
+ parent := t.TempDir()
+ writeTestFile(t, filepath.Join(parent, ".git", "HEAD"), "ref: refs/heads/parent-branch\n")
+
+ // The pointer parses but the referenced gitdir has no HEAD: the checkout
+ // is broken, so no branch should be reported — least of all the parent's.
+ broken := filepath.Join(parent, "broken-worktree")
+ writeTestFile(t, filepath.Join(broken, ".git"), "gitdir: "+filepath.Join(parent, ".git", "worktrees", "missing")+"\n")
+
+ if got := CurrentBranch(broken); got != "" {
+ t.Fatalf("CurrentBranch = %q, want empty for a broken worktree", got)
+ }
+}
+
func TestCurrentBranchRejectsMalformedHead(t *testing.T) {
tests := []struct {
name string