Skip to content

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

Resolve linked worktrees to the project of their main checkout

Lawrence Jones98b64b1bde6e
diff --git a/cli/beacon/internal/learning/store.go b/cli/beacon/internal/learning/store.go
index a678d049..478dd1cb 100644
--- a/cli/beacon/internal/learning/store.go
+++ b/cli/beacon/internal/learning/store.go
@@ -321,8 +321,51 @@ func findGitRoot(path string) string {
}
}
+// gitDirs returns the git directory for root and the common directory that holds its
+// config. In a linked worktree, .git is a file naming a gitdir under the main
+// repository's .git/worktrees/<name>, and that gitdir's commondir file points back at
+// the shared .git. Without following both, a session recorded in a worktree would hash
+// to a path-based project instead of the remote its checkout shares.
+func gitDirs(root string) (gitDir, commonDir string) {
+ dotGit := filepath.Join(root, ".git")
+ info, err := os.Stat(dotGit)
+ if err != nil {
+ return "", ""
+ }
+ if info.IsDir() {
+ return dotGit, dotGit
+ }
+ data, err := os.ReadFile(dotGit)
+ if err != nil {
+ return "", ""
+ }
+ text := strings.TrimSpace(string(data))
+ const prefix = "gitdir:"
+ if !strings.HasPrefix(text, prefix) {
+ return "", ""
+ }
+ gitDir = strings.TrimSpace(strings.TrimPrefix(text, prefix))
+ if !filepath.IsAbs(gitDir) {
+ gitDir = filepath.Join(root, gitDir)
+ }
+ gitDir = filepath.Clean(gitDir)
+ commonDir = gitDir
+ if data, err := os.ReadFile(filepath.Join(gitDir, "commondir")); err == nil {
+ common := strings.TrimSpace(string(data))
+ if !filepath.IsAbs(common) {
+ common = filepath.Join(gitDir, common)
+ }
+ commonDir = filepath.Clean(common)
+ }
+ return gitDir, commonDir
+}
+
func readGitHeadBranch(root string) string {
- data, err := os.ReadFile(filepath.Join(root, ".git", "HEAD"))
+ gitDir, _ := gitDirs(root)
+ if gitDir == "" {
+ return ""
+ }
+ data, err := os.ReadFile(filepath.Join(gitDir, "HEAD"))
if err != nil {
return ""
}
@@ -335,7 +378,11 @@ func readGitHeadBranch(root string) string {
}
func readGitConfigValue(root, section, key string) string {
- data, err := os.ReadFile(filepath.Join(root, ".git", "config"))
+ _, commonDir := gitDirs(root)
+ if commonDir == "" {
+ return ""
+ }
+ data, err := os.ReadFile(filepath.Join(commonDir, "config"))
if err != nil {
return ""
}
diff --git a/cli/beacon/internal/learning/store_test.go b/cli/beacon/internal/learning/store_test.go
index 7d72eac4..c3682cf1 100644
--- a/cli/beacon/internal/learning/store_test.go
+++ b/cli/beacon/internal/learning/store_test.go
@@ -17,6 +17,56 @@ func TestPathForRuntimeLogUsesEndpointBaseDir(t *testing.T) {
}
}
+// A linked worktree's .git is a file pointing at .git/worktrees/<name> in the main
+// checkout, whose commondir points back at the shared .git. The worktree must resolve
+// to the same project ID as the main checkout, with its own branch.
+func TestResolveProjectFollowsLinkedWorktree(t *testing.T) {
+ main := t.TempDir()
+ mainGit := filepath.Join(main, ".git")
+ worktreeGit := filepath.Join(mainGit, "worktrees", "check-brain")
+ if err := os.MkdirAll(worktreeGit, 0755); err != nil {
+ t.Fatal(err)
+ }
+ for path, body := range map[string]string{
+ filepath.Join(mainGit, "HEAD"): "ref: refs/heads/main\n",
+ filepath.Join(mainGit, "config"): "[remote \"origin\"]\n\turl = https://github.com/acme/repo.git\n",
+ filepath.Join(worktreeGit, "HEAD"): "ref: refs/heads/lawrence/check-brain\n",
+ filepath.Join(worktreeGit, "commondir"): "../..\n",
+ } {
+ if err := os.WriteFile(path, []byte(body), 0644); err != nil {
+ t.Fatal(err)
+ }
+ }
+ worktree := filepath.Join(t.TempDir(), "check-brain")
+ if err := os.MkdirAll(filepath.Join(worktree, "server"), 0755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(worktree, ".git"), []byte("gitdir: "+worktreeGit+"\n"), 0644); err != nil {
+ t.Fatal(err)
+ }
+
+ fromMain, err := ResolveProject(main)
+ if err != nil {
+ t.Fatal(err)
+ }
+ fromWorktree, err := ResolveProject(filepath.Join(worktree, "server"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if fromWorktree.Path != worktree {
+ t.Fatalf("worktree path = %s, want %s", fromWorktree.Path, worktree)
+ }
+ if fromWorktree.RemoteURL != "https://github.com/acme/repo.git" {
+ t.Fatalf("worktree remote = %q", fromWorktree.RemoteURL)
+ }
+ if fromWorktree.Branch != "lawrence/check-brain" {
+ t.Fatalf("worktree branch = %q", fromWorktree.Branch)
+ }
+ if fromWorktree.ID != fromMain.ID {
+ t.Fatalf("worktree project %s should match main checkout %s", fromWorktree.ID, fromMain.ID)
+ }
+}
+
func TestResolveProjectUsesGitRootAndOrigin(t *testing.T) {
root := t.TempDir()
if err := os.MkdirAll(filepath.Join(root, ".git"), 0755); err != nil {