Repository navigation
Description
Problem
Builder worktree changes repeatedly leak into the main working tree. After merging a builder's PR, git pull fails because the main tree already has identical copies of the files the builder created/modified. This has happened on multiple PRs (#404, #406) and was previously reported in #370 and #383.
Root Cause
findWorkspaceRoot() in packages/codev/src/agent-farm/utils/config.ts:58-63 intentionally resolves worktree paths back to the main repo:
function findWorkspaceRoot(startDir: string = process.cwd()): string {
const mainRepo = getMainRepoFromWorktree(startDir);
if (mainRepo && existsSync(resolve(mainRepo, 'codev'))) {
return mainRepo; // ← ALWAYS returns main repo, even from worktree
}
// ...
}getMainRepoFromWorktree() (lines 28-52) uses git rev-parse --git-common-dir to find the main .git directory, then resolves its parent as the "workspace root."
This means when any codev tooling (consult, porch, or anything that imports config.ts) runs inside a builder worktree, findWorkspaceRoot() returns the main repo path. Any file writes using that path go to the main tree, not the worktree.
Evidence
PR #406 (R3 experiment)
- Builder created
codev/resources/vibe-vs-spir-r3-comparison-2026-02.md - After merge,
git pullfailed: "untracked working tree files would be overwritten by merge" - MD5 of leaked file identical to merged version:
54b62ce2221ba993bc824aa6b0979c1f - File timestamp (09:50:46) matches when builder was actively writing
PR #404 (Work page latency)
- Builder modified
App.tsx,overview.ts,overview.test.ts - After merge,
git pullfailed with identical local changes - Diffs were byte-for-byte identical to the PR
Historical
- Gemini --yolo mode allows writes to main worktree during consult general #370: Gemini
--yolowrites to main worktree - Porch project state leaks across workspaces — codev-public projects appear in other repos #383: Porch state leaks to main tree
Affected Code Paths
config.ts:findWorkspaceRoot()— the root cause functionconsult/index.ts:589— spawns Gemini CLI withcwd: workspaceRoot(resolves to main)consult/index.ts:353— Codex SDK getsworkingDirectory: workspaceRoot(resolves to main)- Porch state operations — any porch code using workspaceRoot for file paths
skeleton.ts:findWorkspaceRoot()— separate implementation, may have same issue
Reproduction
- Spawn any builder:
af spawn 42 --protocol bugfix - Let the builder create or modify files in
codev/(specs, plans, resources) - Merge the builder's PR
git pullon main → fails with "untracked files would be overwritten"
Expected Behavior
Files written by builder tooling should go to the worktree, not the main tree. findWorkspaceRoot() should respect worktree boundaries when running in a builder context.
Proposed Fix Direction
When findWorkspaceRoot() detects it's running inside a .builders/ worktree, it should return the worktree path (which has its own codev/ directory), not the main repo. The worktree has a full copy of codev/ — that's the whole point of git worktrees.
Possible approach: if process.cwd() is under .builders/, return the worktree's own root instead of resolving to the main repo. The getMainRepoFromWorktree() helper should only be used for operations that genuinely need the main repo (like af spawn creating new worktrees).
Scope
packages/codev/src/agent-farm/utils/config.ts— primary fixpackages/codev/src/lib/skeleton.ts— audit for same pattern- Verify
consult,porch, and any other callers behave correctly after the fix - Test that builders can still find
codev/protocols/,codev/specs/, etc. in their worktree
Activity
waleedkadous commentedon Feb 17, 2026
On it! Working on a fix now.
[Bugfix #407] Fix: findWorkspaceRoot() respects worktree boundaries
[Bugfix #407] Add integration tests with real git worktrees