diff --git a/cmd/bd/BUILD.bazel b/cmd/bd/BUILD.bazel index aacc413c3..38feed9b0 100644 --- a/cmd/bd/BUILD.bazel +++ b/cmd/bd/BUILD.bazel @@ -935,6 +935,7 @@ go_test( "prime_divergence_test.go", "prime_embedded_test.go", "prime_gemini_hook_test.go", + "prime_git_remote_test.go", "prime_memory_caps_test.go", "prime_proxied_integration_test.go", "prime_shared_server_test.go", diff --git a/cmd/bd/prime.go b/cmd/bd/prime.go index 2aed543ce..d8b5ce1b2 100644 --- a/cmd/bd/prime.go +++ b/cmd/bd/prime.go @@ -298,6 +298,11 @@ func primeWorkspaceDir() string { // // NOTE: the probes built here are not prime-only — see primeHasGitRemote for // the auto-backup consumer that inherits this directory choice. +// +// NOTE: since GH#4927 every return path pairs with a nil error — the +// GetRepoContext() failure falls back to a cwd probe instead of propagating — +// so the err != nil arms at both call sites are unreachable today. The error +// result is retained for future callers that can genuinely fail. func primeGitCmd(ctx context.Context, args ...string) (*exec.Cmd, error) { if ws := primeWorkspaceDir(); ws != "" { cmd := exec.CommandContext(ctx, "git", args...) @@ -306,7 +311,14 @@ func primeGitCmd(ctx context.Context, args ...string) (*exec.Cmd, error) { } rc, err := internalbeads.GetRepoContext() if err != nil { - return nil, err + // GH#4927: an unusable BEADS_DIR must not be reported as "no git + // remote" / "ephemeral branch". An external BEADS_DIR under another + // account's home (common in agent sandboxes) makes buildRepoContext + // fail, but the process cwd is frequently a perfectly good git + // workspace, so probe it directly instead of giving up. The SEC-003 + // boundary on BEADS_DIR stays enforced by the callers that consume + // BEADS_DIR itself; these probes only ask git about its own workspace. + return exec.CommandContext(ctx, "git", args...), nil } return rc.GitCmdCWD(ctx, args...), nil } @@ -372,7 +384,9 @@ func isMCPActive() bool { return false } -// isEphemeralBranch detects if current branch has no upstream (ephemeral/local-only) +// isEphemeralBranch detects if current branch has no upstream (ephemeral/local-only). +// Runs through primeGitCmd, so it honors -C (#5509) and still falls back to the +// process CWD git workspace when BEADS_DIR is unusable (GH#4927). var isEphemeralBranch = func() bool { // git rev-parse --abbrev-ref --symbolic-full-name @{u} // Returns error code 128 if no upstream configured @@ -405,6 +419,13 @@ var primeAgentProfile = func() config.AgentProfile { // remote rather than the cwd's (#5509). That is the consistent answer, because // the store being backed up is the -C-resolved one, but it is a behavior // change beyond prime: check backup_auto before changing what this probes. +// +// GH#4927: this must not require a valid RepoContext / BEADS_DIR. An external +// BEADS_DIR under another account's home (common in agent sandboxes) fails to +// build a RepoContext and must not be misreported as "no git remote" when +// `git remote` in the workspace succeeds. primeGitCmd supplies that fallback, +// which keeps the -C behavior above intact. The SEC-003 boundary on BEADS_DIR +// remains enforced elsewhere. var primeHasGitRemote = func() bool { cmd, err := primeGitCmd(context.Background(), "remote") if err != nil { @@ -417,6 +438,31 @@ var primeHasGitRemote = func() bool { return len(strings.TrimSpace(string(out))) > 0 } +// gitCWDHasRemote reports whether the process CWD git repo has any remote. +// Delegates to gitDirHasRemote (no BEADS_DIR coupling). It is the +// BEADS_DIR-independent primitive the GH#4927 regression test drives directly, +// alongside primeHasGitRemote. +// +// This pair is a test-only oracle with no production callers: the production +// probes build their own command in primeGitCmd. +func gitCWDHasRemote() bool { + return gitDirHasRemote("") +} + +// gitDirHasRemote reports whether the git repo at dir has any remote +// configured. dir == "" runs git in the process's current working directory +// (this is what gitCWDHasRemote uses); a non-empty dir lets tests probe an +// explicit fixture repo without chdir-ing the whole process. +func gitDirHasRemote(dir string) bool { + cmd := exec.Command("git", "remote") + cmd.Dir = dir + out, err := cmd.Output() + if err != nil { + return false + } + return len(strings.TrimSpace(string(out))) > 0 +} + // primeHasSyncRemote detects if a Dolt sync remote is configured (stubbable for tests) var primeHasSyncRemote = func() bool { return resolveSyncRemote() != "" diff --git a/cmd/bd/prime_git_remote_test.go b/cmd/bd/prime_git_remote_test.go new file mode 100644 index 000000000..cd3875cc4 --- /dev/null +++ b/cmd/bd/prime_git_remote_test.go @@ -0,0 +1,110 @@ +package main + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "github.com/steveyegge/beads/internal/beads" + "github.com/steveyegge/beads/internal/git" +) + +// GH#4927: prime's git probes must not depend on BEADS_DIR / RepoContext. +// +// buildRepoContext (internal/beads/context.go) can fail when FindBeadsDir() +// returns "" (context.go:107, e.g. BEADS_DIR doesn't exist). The pre-fix bug +// collapsed that GetRepoContext() error into "no git remote" / "ephemeral +// branch". This test poisons BEADS_DIR with a nonexistent path and verifies +// (a) that GetRepoContext() actually fails via FindBeadsDir()=="", and (b) +// that both probes the fix repairs -- primeHasGitRemote and isEphemeralBranch +// -- are unaffected by it. +// +// The probe assertions below use t.Error rather than t.Fatal so a regression +// in one probe does not mask the state of the others. +func TestPrimeGitProbes_IndependentOfBeadsDir(t *testing.T) { + dir := t.TempDir() + run := func(args ...string) { + t.Helper() + cmd := exec.Command(args[0], args[1:]...) + cmd.Dir = dir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("%v: %v\n%s", args, err, out) + } + } + run("git", "init", "-q") + run("git", "config", "user.name", "fixture") + run("git", "config", "user.email", "fixture@example.com") + run("git", "config", "commit.gpgsign", "false") + + // gitDirHasRemote is the test-only oracle described in prime.go; these two + // assertions check that the fixture itself is built correctly, not the fix. + if gitDirHasRemote(dir) { + t.Fatal("expected no remote after init") + } + + run("git", "remote", "add", "origin", "https://example.invalid/repo.git") + if !gitDirHasRemote(dir) { + t.Fatal("expected remote after git remote add origin") + } + + // A committed branch that has a remote but no upstream -- the state in + // which the branch genuinely is ephemeral. The fixed branch name avoids + // depending on init.defaultBranch. + run("git", "commit", "-q", "--allow-empty", "-m", "fixture") + run("git", "checkout", "-q", "-B", "fixture-branch") + + t.Cleanup(func() { + beads.ResetCaches() + git.ResetCaches() + }) + + // CWD-based probe: chdir into the fixture repo + wd, err := os.Getwd() + if err != nil { + t.Fatal(err) + } + if err := os.Chdir(dir); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = os.Chdir(wd) }) + + // BEADS_DIR points at a path that simply doesn't exist on disk — this is + // the FindBeadsDir()=="" failure mode (context.go:107). CWD is the + // fixture repo (no .beads anywhere in its ancestry), so the walk in + // FindBeadsDir also comes up empty. + t.Setenv("BEADS_DIR", filepath.Join(dir, "does-not-exist", ".beads")) + beads.ResetCaches() + git.ResetCaches() + if _, err := beads.GetRepoContext(); err == nil || !strings.Contains(err.Error(), "no .beads directory found") { + t.Fatalf("expected GetRepoContext() to fail via FindBeadsDir()==\"\", got err=%v", err) + } + + if !gitCWDHasRemote() { + t.Error("gitCWDHasRemote should see origin even with a nonexistent BEADS_DIR set") + } + + // The two probes the fix actually repairs. Both route through primeGitCmd, + // so both regress if its GetRepoContext() fallback is removed. + if !primeHasGitRemote() { + t.Error("primeHasGitRemote should see origin even with a nonexistent BEADS_DIR set") + } + + // isEphemeralBranch, both polarities under the same poisoned BEADS_DIR. + // The branch has no upstream yet, so "ephemeral" is the correct answer + // here; this control is what proves the assertion below is reading git + // rather than returning a constant. + if !isEphemeralBranch() { + t.Error("isEphemeralBranch should report ephemeral while the branch has no upstream") + } + + // Configure a real upstream -- entirely local, the remote is never + // contacted -- and the same probe must flip. Pre-fix it stayed true here, + // because the GetRepoContext() failure was itself reported as "ephemeral". + run("git", "update-ref", "refs/remotes/origin/fixture-branch", "HEAD") + run("git", "branch", "--set-upstream-to=origin/fixture-branch", "fixture-branch") + if isEphemeralBranch() { + t.Error("isEphemeralBranch should see the configured upstream even with a nonexistent BEADS_DIR set") + } +}