* fix(prime): honor -C for the local PRIME.md tier, git probes, divergence reminder, and redirect notice (#5509) `bd -C <dir> prime` silently primed the process cwd. -C is implemented as setenv BEADS_DIR (no chdir), so the store and memories already followed the target, but four workspace-relative reads in prime did not: the first-priority .beads/PRIME.md lookup, the AGENTS.md/CLAUDE.md divergence reminder, the git probes (upstream, remotes) behind the default template's git-authority wording, and the template's redirect notice all resolved against the cwd. On the no-PRIME.md path the output for `bd -C repo-b prime` was byte-identical to priming the cwd repo. Add primeWorkspaceDir(), which returns the absolute -C target when set and "" (cwd) otherwise, and resolve all four against it: readCustomPrimeContent takes the workspace dir as a parameter, the reminder takes the same workspace, a new primeGitCmd helper runs isEphemeralBranch and primeHasGitRemote in the -C target (falling back to GitCmdCWD without -C, as before), and primeRedirectInfo asks internal/beads about the -C target through a new GetRedirectInfoFrom(dir). GetRedirectInfo() delegates to the same core for the cwd, so redirect detection without -C is unchanged. The clone-local PRIME.md tier is rebased onto the -C target rather than dropped, so a redirect clone with its own .beads/PRIME.md yields the clone-local file under -C exactly as `cd <dir> && bd prime` does. The workspace is deliberately the -C directory itself, not the parent of the resolved beads dir, which may have followed .beads/redirect or walked up from a subdirectory. Env-only BEADS_DIR redirects are unchanged: without -C the cwd-local PRIME.md still wins (clone-specific customization, GH#876), guarded by TestReadCustomPrimeContent_EnvRedirectStillLocalFirst. Tests: unit coverage for primeWorkspaceDir (absolute, relative, unset), the -C target and redirect-clone PRIME.md tiers, the env-only redirect, the divergence-reminder workspace (redirect and subdirectory cases), and GetRedirectInfoFrom against GetRedirectInfo in both directions plus the non-git walk-up, plus three end-to-end runs (cgo build tag, shared once-per-process bd binary): the issue's two-repo PRIME.md repro, a no-PRIME.md pair where `bd -C repo-b prime` from repo-a must equal `cd repo-b && bd prime` byte for byte while differing from the cwd's own prime (repo-a has a remote and upstream, repo-b has neither and carries divergent AGENTS.md/CLAUDE.md), and a plain-repo/redirect-clone pair where the redirect notice must follow -C in both directions. Fixes #5509 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix(prime): canonicalize the -C repo root and document the shared probes Maintainer review fixes for #6550 (adopt-pr review iteration 1). Nothing here was a merge gate: the scorecard approved at 962/1000 with no required changes. No behavior change on Linux/macOS. - internal/beads: canonicalize the repo root inside repoLocalBeadsDir, the helper both locators share. findLocalBdsDirInRepoFrom handed it raw `git rev-parse --show-toplevel` output while the cwd twin passes git.GetRepoRoot()'s already-normalized value, so on Windows (`C:/...`, msys `/c/...`) the repo-local tier missed and resolution fell through to the walk-up locator - the fallback bd-wayc3 exists to beat. Canonicalizing stays below the "" guard on purpose: utils.CanonicalizePath("") resolves to the process cwd. TestRepoLocalBeadsDirCanonicalizesRepoRoot pins both halves and reds on either mutation (drop the canonicalization; move it above the guard). - internal/beads: document that GetRedirectInfoFrom's fallback drops the worktree-local redirect probe and GetWorktreeFallbackBeadsDir, not only BEADS_DIR. - cmd/bd/prime.go: NOTE that primeHasGitRemote and primeGitCmd have a consumer outside prime (isBackupAutoEnabled), which inherits the -C directory choice. - CHANGELOG: add the Unreleased/Fixed entry for #5509 and disclose that `bd -C dir <any command>` now decides auto-backup enablement from the -C target's git remote rather than the launch directory's. - cmd/bd/prime_embedded_test.go: record why the three -C repros carry the TestEmbedded prefix (fast lanes -skip it, embedded-test-shard.sh discovers shard members by it) and that they need no embedded Dolt. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com> Co-authored-by: Julian Knutsen <julian@knutsen.dev>
200 lines
7.1 KiB
Go
200 lines
7.1 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const markerBlock = "<!-- BEGIN BEADS INTEGRATION v:1 profile:agents hash:abc -->\nbody\n<!-- END BEADS INTEGRATION -->\n"
|
|
|
|
func writePrimeTestFile(t *testing.T, path, content string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
|
|
t.Fatalf("write %s: %v", path, err)
|
|
}
|
|
}
|
|
|
|
func TestPrimeDivergenceReminder_BothIndependentWithMarker(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writePrimeTestFile(t, filepath.Join(dir, "AGENTS.md"), "# Agents\n"+markerBlock)
|
|
writePrimeTestFile(t, filepath.Join(dir, "CLAUDE.md"), "# Claude\n"+markerBlock)
|
|
|
|
got := primeDivergenceReminder(dir)
|
|
if got == "" {
|
|
t.Fatal("expected reminder, got empty string")
|
|
}
|
|
if !strings.Contains(got, "AGENTS.md") || !strings.Contains(got, "CLAUDE.md") {
|
|
t.Fatalf("reminder missing file names: %q", got)
|
|
}
|
|
if strings.Count(got, "\n> ") != 1 {
|
|
t.Fatalf("expected a single one-line note, got: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestPrimeDivergenceReminder_MissingOneFile(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writePrimeTestFile(t, filepath.Join(dir, "AGENTS.md"), "# Agents\n"+markerBlock)
|
|
// CLAUDE.md absent.
|
|
if got := primeDivergenceReminder(dir); got != "" {
|
|
t.Fatalf("expected empty when one file missing, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestPrimeDivergenceReminder_MarkerMissingInOne(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writePrimeTestFile(t, filepath.Join(dir, "AGENTS.md"), "# Agents\n"+markerBlock)
|
|
writePrimeTestFile(t, filepath.Join(dir, "CLAUDE.md"), "# Claude without marker\n")
|
|
if got := primeDivergenceReminder(dir); got != "" {
|
|
t.Fatalf("expected empty when one file lacks marker, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestPrimeDivergenceReminder_Symlink(t *testing.T) {
|
|
dir := t.TempDir()
|
|
agents := filepath.Join(dir, "AGENTS.md")
|
|
claude := filepath.Join(dir, "CLAUDE.md")
|
|
writePrimeTestFile(t, agents, "# Agents\n"+markerBlock)
|
|
if err := os.Symlink(agents, claude); err != nil {
|
|
if runtime.GOOS == "windows" {
|
|
t.Skipf("symlink unsupported: %v", err)
|
|
}
|
|
t.Fatalf("symlink: %v", err)
|
|
}
|
|
// CLAUDE.md is a symlink (to a file with the marker); reminder must be empty.
|
|
if got := primeDivergenceReminder(dir); got != "" {
|
|
t.Fatalf("expected empty when a file is a symlink, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestPrimeDivergenceReminder_Hardlink(t *testing.T) {
|
|
dir := t.TempDir()
|
|
agents := filepath.Join(dir, "AGENTS.md")
|
|
claude := filepath.Join(dir, "CLAUDE.md")
|
|
writePrimeTestFile(t, agents, "# Agents\n"+markerBlock)
|
|
if err := os.Link(agents, claude); err != nil {
|
|
t.Skipf("shared-inode link unsupported: %v", err)
|
|
}
|
|
// Same inode: independent-files condition fails, so no reminder.
|
|
if got := primeDivergenceReminder(dir); got != "" {
|
|
t.Fatalf("expected empty when files share an inode, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestPrimeDivergenceReminder_NeitherPresent(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if got := primeDivergenceReminder(dir); got != "" {
|
|
t.Fatalf("expected empty when neither file present, got %q", got)
|
|
}
|
|
}
|
|
|
|
func TestPrimeDivergenceReminder_EmptyDirArgUsesCwd(t *testing.T) {
|
|
// With "" the helper uses the current working directory; in a temp dir with
|
|
// no agent files it must return empty (and not error).
|
|
dir := t.TempDir()
|
|
t.Chdir(dir)
|
|
if got := primeDivergenceReminder(""); got != "" {
|
|
t.Fatalf("expected empty for cwd without files, got %q", got)
|
|
}
|
|
}
|
|
|
|
// The remaining tests cover the workspace the reminder is resolved against
|
|
// under -C (#5509): primeWorkspaceDir is the -C directory itself, not the
|
|
// parent of the beads dir that -C resolved to.
|
|
|
|
func TestPrimeDivergenceWorkspaceDir_ChangeDirUsesTarget(t *testing.T) {
|
|
t.Cleanup(func() {
|
|
changeDir = ""
|
|
})
|
|
// Only the cwd carries divergent agent files; the -C target has none.
|
|
cwd := primeTestWorkspace(t, map[string]string{
|
|
".beads/metadata.json": primeTestMetadata,
|
|
"AGENTS.md": "# Agents\n" + markerBlock,
|
|
"CLAUDE.md": "# Claude\n" + markerBlock,
|
|
})
|
|
target := primeTestWorkspace(t, map[string]string{".beads/metadata.json": primeTestMetadata})
|
|
t.Chdir(cwd)
|
|
|
|
changeDir = target
|
|
if got := primeDivergenceReminder(primeWorkspaceDir()); got != "" {
|
|
t.Fatalf("with -C, reminder against target must be empty, got %q", got)
|
|
}
|
|
changeDir = ""
|
|
if got := primeDivergenceReminder(primeWorkspaceDir()); got == "" {
|
|
t.Fatal("control: without -C the cwd fixture must emit a divergence note")
|
|
}
|
|
}
|
|
|
|
// CASE A: the -C target is a redirect clone. The reminder must be resolved
|
|
// against the clone (where the agent files live), not against the parent of
|
|
// the external store that -C resolved BEADS_DIR to.
|
|
func TestPrimeDivergenceWorkspaceDir_RedirectUsesChangeDir(t *testing.T) {
|
|
t.Cleanup(func() {
|
|
changeDir = ""
|
|
})
|
|
external := primeTestWorkspace(t, map[string]string{".beads/metadata.json": primeTestMetadata})
|
|
externalBeads := filepath.Join(external, ".beads")
|
|
target := primeTestWorkspace(t, map[string]string{
|
|
".beads/redirect": externalBeads + "\n",
|
|
"AGENTS.md": "# Agents\n" + markerBlock,
|
|
"CLAUDE.md": "# Claude\n" + markerBlock,
|
|
})
|
|
|
|
resolvedBeads, err := resolveChangeDirBeadsDir(target)
|
|
if err != nil {
|
|
t.Fatalf("resolveChangeDirBeadsDir: %v", err)
|
|
}
|
|
if filepath.Clean(resolvedBeads) != filepath.Clean(externalBeads) {
|
|
t.Fatalf("resolved beads = %q, want external %q", resolvedBeads, externalBeads)
|
|
}
|
|
|
|
changeDir = target
|
|
ws := primeWorkspaceDir()
|
|
if ws != target {
|
|
t.Fatalf("CASE A: primeWorkspaceDir = %q, want -C target %q (not parent of redirected beads %q)", ws, target, filepath.Dir(resolvedBeads))
|
|
}
|
|
if got := primeDivergenceReminder(ws); got == "" {
|
|
t.Fatal("CASE A: reminder against -C target must emit divergence note")
|
|
}
|
|
if got := primeDivergenceReminder(filepath.Dir(resolvedBeads)); got != "" {
|
|
t.Fatalf("control: parent-of-resolved-beads must not emit note, got %q", got)
|
|
}
|
|
}
|
|
|
|
// CASE B: the -C target is a subdirectory of the workspace. -C resolves
|
|
// BEADS_DIR by walking up to the root's .beads, but the reminder still reads
|
|
// the -C directory itself, as `cd sub && bd prime` would.
|
|
func TestPrimeDivergenceWorkspaceDir_SubdirUsesChangeDir(t *testing.T) {
|
|
t.Cleanup(func() {
|
|
changeDir = ""
|
|
})
|
|
wsRoot := primeTestWorkspace(t, map[string]string{
|
|
".beads/metadata.json": primeTestMetadata,
|
|
"AGENTS.md": "# Agents\n" + markerBlock,
|
|
"CLAUDE.md": "# Claude\n" + markerBlock,
|
|
"sub/.keep": "",
|
|
})
|
|
sub := filepath.Join(wsRoot, "sub")
|
|
|
|
resolvedBeads, err := resolveChangeDirBeadsDir(sub)
|
|
if err != nil {
|
|
t.Fatalf("resolveChangeDirBeadsDir: %v", err)
|
|
}
|
|
if filepath.Clean(resolvedBeads) != filepath.Join(wsRoot, ".beads") {
|
|
t.Fatalf("resolved beads = %q, want root %q", resolvedBeads, filepath.Join(wsRoot, ".beads"))
|
|
}
|
|
|
|
changeDir = sub
|
|
ws := primeWorkspaceDir()
|
|
if ws != sub {
|
|
t.Fatalf("CASE B: primeWorkspaceDir = %q, want -C subdir %q (not parent-of-beads %q)", ws, sub, filepath.Dir(resolvedBeads))
|
|
}
|
|
if got := primeDivergenceReminder(ws); got != "" {
|
|
t.Fatalf("CASE B: reminder against -C subdir must be empty, got %q", got)
|
|
}
|
|
if got := primeDivergenceReminder(filepath.Dir(resolvedBeads)); got == "" {
|
|
t.Fatal("control: parent-of-beads-dir must emit note (proves CASE B distinguishes the wrong derivation)")
|
|
}
|
|
}
|