fix: check cwd for .beads/ before git-worktree resolution#2823
Open
julianknutsen wants to merge 1 commit intosteveyegge:mainfrom
Open
fix: check cwd for .beads/ before git-worktree resolution#2823julianknutsen wants to merge 1 commit intosteveyegge:mainfrom
julianknutsen wants to merge 1 commit intosteveyegge:mainfrom
Conversation
When a subdirectory has its own .beads/ inside a git worktree that also has .beads/ at its root, the git-worktree check (step 2b) finds the root's .beads/ before the cwd walk (step 3) can discover the local one. This causes incorrect database connections when orchestrators like Gas City manage multiple "rigs" (project subdirectories) that each have their own beads database, nested inside a parent git repo that also tracks beads. Fix: Add a cwd check before git-worktree resolution in both FindBeadsDir() and findDatabaseInTree(). If cwd has a valid .beads/ directory (with project files or a database), return it immediately. Priority order is now: 1. BEADS_DIR env var (unchanged) 1b. cwd .beads/ (NEW — most-local directory wins) 2. Git worktree root .beads/ (unchanged) 3. Walk up from cwd (unchanged) This preserves all existing behavior for single-project repos, worktrees, and redirects. The cwd check only fires when cwd literally contains .beads/ with valid project files. Tests added: - TestFindBeadsDir_CwdPriority: rig .beads/ wins over root .beads/ - TestFindDatabasePath_CwdPriority: same for database discovery - TestFindBeadsDir_CwdWithoutBeads_FallsBackToWalk: normal fallback - TestFindBeadsDir_CwdBeadsDirWithRedirect: redirect in cwd followed - TestFindBeadsDir_BEADS_DIR_StillTakesPriority: env var still wins - TestFindBeadsDir_CwdEmptyBeadsDir_SkipsToCwdWalk: empty dir skipped
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a subdirectory has its own
.beads/inside a git worktree that also has.beads/at its root, the git-worktree check (step 2b inFindBeadsDir) finds the root's.beads/before the cwd walk (step 3) can discover the local one.This causes incorrect database connections when orchestrators like Gas City manage multiple "rigs" (project subdirectories) that each have their own beads database, nested inside a parent git repo that also tracks beads.
Problem
Reproduction scenario:
When
bd listruns fromrig-a/, it should connect totron port46345. Instead,FindBeadsDir()returns/project/.beads/(the git root's), connecting to the wrong database.Root cause:
FindBeadsDir()step 2b checksgit.GetRepoRoot()for.beads/before step 3 walks up from cwd. Sincegit.GetRepoRoot()returns/project/(the worktree toplevel), it finds the root's.beads/first. The same issue exists infindDatabaseInTree()which is used byFindDatabasePath().Fix
Add a cwd check before git-worktree resolution in both
FindBeadsDir()andfindDatabaseInTree(). If cwd has a valid.beads/directory (with project files or a database), return it immediately.Priority order is now:
BEADS_DIRenv var (unchanged).beads/(NEW — most-local directory wins).beads/(unchanged)Tests
6 new tests in
internal/beads/cwd_priority_test.go:TestFindBeadsDir_CwdPriority.beads/wins over git root.beads/TestFindDatabasePath_CwdPriorityTestFindBeadsDir_CwdWithoutBeads_FallsBackToWalk.beads/TestFindBeadsDir_CwdBeadsDirWithRedirect.beads/is followedTestFindBeadsDir_BEADS_DIR_StillTakesPriorityBEADS_DIRenv still wins over cwdTestFindBeadsDir_CwdEmptyBeadsDir_SkipsToCwdWalk.beads/dir (no project files) skippedAll existing tests continue to pass.
Backward Compatibility
This change only affects users who have nested
.beads/directories inside a git worktree — a scenario specific to multi-rig orchestration. Single-project repos, worktrees, and redirects are unaffected because the cwd check only fires when cwd literally contains.beads/with valid project files.