Skip to content

Fix: findWorkspaceRoot() respects worktree boundaries - #408

Merged
waleedkadous merged 2 commits into
mainfrom
builder/bugfix-407-builder-worktree-leakage-findw
Feb 17, 2026
Merged

waleedkadous merged 2 commits into
mainfrom
builder/bugfix-407-builder-worktree-leakage-findw

Conversation

@waleedkadous

Copy link
Copy Markdown
Contributor

Summary

Fixes #407 — findWorkspaceRoot() in config.ts always resolved worktree paths back to the main repository via git rev-parse --git-common-dir. This caused file writes from builder tools (af commands) to leak into the main tree instead of staying within the builder's isolated worktree.

Root cause: The old code detected a worktree, then immediately resolved to the main repo root. It never checked whether the worktree itself had its own codev/ directory.

Fix: When in a worktree, walk up to find the worktree root (via .git marker), and if that root has its own codev/ directory, return it instead of resolving to the main repo.

  • packages/codev/src/agent-farm/utils/config.ts — Rewrote worktree handling in findWorkspaceRoot() to check worktree root for codev/ before falling back to main repo
  • packages/codev/src/agent-farm/__tests__/findWorkspaceRoot.test.ts — 7 regression tests covering normal, subdirectory, worktree, and edge-case scenarios

Test plan

  • 7 new regression tests pass (worktree root detection, subdirectory walk-up, no-markers fallback)
  • 4 existing config tests pass (getConfig, ensureDirectories)
  • Full test suite: 1574 passed, 13 skipped, 0 failed
  • TypeScript compiles cleanly (npx tsc --noEmit)

findWorkspaceRoot() in config.ts always resolved worktree paths back to the
main repo via getMainRepoFromWorktree(). This caused any codev tooling running
inside a builder worktree to write files to the main tree instead of the
worktree, leading to "untracked files would be overwritten by merge" errors
after merging builder PRs (#404, #406, #370, #383).

Fix: When in a worktree, find the worktree root first (via .git marker), then
check if it has its own codev/ directory. Builder worktrees are full git
checkouts with their own codev/, so we return the worktree root. Only fall
back to the main repo if the worktree lacks codev/.
Address CMAP feedback: the filesystem-only tests don't exercise the
worktree code path (git rev-parse fails in non-git temp dirs). Add
integration tests that create real git repos and worktrees to verify
the getMainRepoFromWorktree() → worktree branch.
@waleedkadous
waleedkadous merged commit 122718e into main Feb 17, 2026
7 checks passed
@waleedkadous
waleedkadous deleted the builder/bugfix-407-builder-worktree-leakage-findw branch February 17, 2026 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Builder worktree leakage: findWorkspaceRoot() resolves to main repo, causing file writes to main tree

1 participant