CodePanel

Running coding agents in parallel with Git worktrees

How to isolate parallel coding agents with Git worktrees: dependency bootstrapping, .env files, port clashes, OS sandboxing, and automated PR review.

Updated

Git worktrees let you run several coding agents on the same repository at the same time without file collisions or duplicate full clones. Each worktree checks out its own branch in a separate directory while sharing one .git object store. On its own, a worktree isolates files only; running agents safely also requires dependency setup, port isolation, an OS sandbox, and an independent review gate before merge.

Why worktrees beat full clones and shared checkouts

Running two coding agents in the same working directory fails immediately: edits overwrite each other, git status mixes unrelated changes, and test suites fail on half-written files. Cloning the repository per task avoids collisions but wastes disk space and duplicates git history.

A worktree solves state isolation cleanly:

git worktree add -b codepanel/task-auth ../repo-task-auth main
git worktree add -b codepanel/task-tests ../repo-task-tests main

Both directories share the .git object database, so creating a worktree takes milliseconds and keeps branches cleanly separated. When a task finishes, git worktree remove and git worktree prune clean it up.

Four gotchas of parallel worktrees

A bare worktree only contains tracked files. Four practical problems show up as soon as agents run real test suites in parallel:

Problem Why it breaks Fix
Missing node_modules Untracked dependencies are not copied into a new worktree, so the first test command fails on its first import. Bootstrap dependencies before the agent runs. For Bun repositories with an isolated store, bun install --frozen-lockfile (2.2 to 3.0 s warm on a 414-package repo) beats APFS cp -cR (6.6 s); keep lockfile-keyed clones for npm, pnpm, and yarn.
Missing .env and local configs Gitignored config files do not exist in fresh worktrees. Copy only non-secret templates or bind isolated test configuration per worktree; never copy production secrets into an agent's readable directory.
Port and database collisions Two agents starting dev servers on port 3000 or writing to one local SQLite/Postgres database interfere with each other. Derive a deterministic port from the worktree path (for example, 4100 to 4999) and keep local backend state inside a gitignored worktree folder.
Shared .git directory writes A worktree's .git file points back to the main checkout's .git directory, so an unsandboxed shell command can move main or tamper with another run's branch. Wrap shell commands in an OS sandbox that allows writes only to worktrees/<run>/, objects/, and the run's own branch ref.

Worktrees are not a security boundary

File isolation stops accidental collisions between parallel agents, not deliberate or prompt-injected shell commands. Without an OS sandbox (sandbox-exec on macOS or bwrap on Linux), a command inside a worktree can still read ~/.ssh, cloud credentials, or sibling worktrees.

In CodePanel's desktop app, every run gets its own worktree, its own isolated HOME, and an OS sandbox that blocks credential reads and branch tampering while adding 10.4 ms median overhead per command (Sandboxing a coding agent's shell on a laptop).

Closing the loop with an independent review gate

Running three or four worktrees in parallel multiplies pull request volume, which makes manual line-by-line triage the new bottleneck. When each worktree pushes its branch and opens a pull request, sending it straight to an independent AI code review panel catches cross-file bugs, missing auth checks, and regressions before a person ever opens the diff.

See Coding agents for how CodePanel's local runner automates worktrees, dependency installation, sandboxing, and pull request review in one loop.