Skip to content

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

Closed

Description

@waleedkadous

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 pull failed: "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 pull failed with identical local changes
  • Diffs were byte-for-byte identical to the PR

Historical

Affected Code Paths

  1. config.ts:findWorkspaceRoot() — the root cause function
  2. consult/index.ts:589 — spawns Gemini CLI with cwd: workspaceRoot (resolves to main)
  3. consult/index.ts:353 — Codex SDK gets workingDirectory: workspaceRoot (resolves to main)
  4. Porch state operations — any porch code using workspaceRoot for file paths
  5. skeleton.ts:findWorkspaceRoot() — separate implementation, may have same issue

Reproduction

  1. Spawn any builder: af spawn 42 --protocol bugfix
  2. Let the builder create or modify files in codev/ (specs, plans, resources)
  3. Merge the builder's PR
  4. git pull on 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 fix
  • packages/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

  1. waleedkadous commented on Feb 17, 2026

    @waleedkadous
    ContributorAuthor

    On it! Working on a fix now.

  2. added 2 commits that reference this issue on Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions