---
description: Run several coding agents in parallel without colliding on the same checkout
alwaysApply: true
---
# Parallel agents (worktrees)

Checked against Cursor's worktrees and CLI docs, Claude Code's worktrees and "Run agents in parallel" docs, and the official git-worktree page, on 2026-11-11. Where those docs are silent, these rules say so.

## Isolate each agent
- Give each agent its own git worktree (a separate working directory and branch, sharing the same repository). Do not let two agents edit the same working tree at once.
- Git refuses to check out a branch that is already checked out in another worktree, unless you pass `--force`. Do not use `--force` to share a branch across worktrees.
- If worktrees are not available (Claude Code's agent teams do not isolate teammates in worktrees), partition the work so each agent owns a different set of files. The docs do not require a specific folder-naming scheme.

## How to start a worktree
- Cursor, Agents Window: start or move an agent into a worktree. Cursor creates a separate checkout for that agent.
- Cursor, IDE: `/worktree` for one isolated run. `/best-of-n` runs the same task across multiple models, each in its own worktree; it does not merge the results back for you.
- Cursor CLI: `agent --worktree [name]`. Checkouts go under `~/.cursor/worktrees/`.
- Claude Code: `claude --worktree <name>` or `-w`. By default the worktree is created under `.claude/worktrees/` at the repo root, on a new branch. Add `.claude/worktrees/` to `.gitignore`. You can also ask Claude to "work in a worktree", or set `isolation: worktree` on a custom subagent.
- Manual: `git worktree add -b <branch> <path>` and open that folder in the tool. List them with `git worktree list`.

## Set up the fresh checkout
- A worktree is a fresh checkout: install dependencies there. Do not symlink `node_modules` or other dependencies into the worktree (Cursor's docs advise against it; it can affect the main checkout).
- Cursor: put setup commands in `.cursor/worktrees.json` (`setup-worktree`, or OS-specific keys). Typical: package install, then copy `.env` from `$ROOT_WORKTREE_PATH`.
- Claude Code: initialize the environment in the worktree, or add a `.worktreeinclude` file (gitignore syntax) so gitignored files such as `.env` are copied in automatically. Tracked files are not duplicated.

## After the agent finishes
- Review the result in the worktree, then commit and open a PR from that checkout, or bring the changes back into your main checkout (Cursor: `/apply-worktree`).
- Cursor `/best-of-n` compares runs only. Pick a winner yourself, then commit or `/apply-worktree`.
- Merge one worktree at a time. After each merge, re-run the project's tests. The Cursor, Claude Code and git docs do not prescribe a merge order or a required test command; do not skip the tests the project already has.
- If two branches both changed the lockfile or shared config (`package-lock.json`, `pnpm-lock.yaml`, `tsconfig.json`, CI config), resolve that conflict yourself. Prefer letting one agent own dependency and shared-config changes. The docs do not ban two agents from touching those files in separate worktrees; the clash appears at merge time.

## Clean up
- Cursor IDE: `/delete-worktree` when you are done. Cursor can also auto-clean older worktrees (default cap 25 per machine in Cursor 3.5 and later).
- Claude Code: a clean unnamed session is removed on exit; a named session or a worktree with work prompts you. Subagent worktrees with no changes are removed automatically; ones with changes stay until a periodic sweep (`cleanupPeriodDays`). Use `git worktree remove` (and `git worktree unlock` first if git says it is locked).
- Manual: `git worktree remove <path>`. Git only removes a clean worktree unless you pass `--force`.
