Parallel Coding Agents Keep Colliding? Give Each One a Lane
You start two coding agents on the same repo. One adds a discount to the pricing API, the other shows the new price in the cart. Both report success. Then you merge, and one of them has rewritten a file the other one own
You start two coding agents on the same repo. One adds a discount to the pricing API, the other shows the new price in the cart. Both report success. Then you merge, and one of them has rewritten a file the other one owned.
Running agents in parallel is easy to start. Keeping them out of each other's way is the actual work. This article is a practical setup for that: give each agent a lane, write the lanes down, define how agents hand work to each other, merge in a fixed order, and keep one reviewer. If you're still deciding whether you need more than one agent at all, read Single Agent, Subagents or an Agent Team? first. This one assumes you've decided, and the agents are already colliding.
What a lane is
A lane is three things that belong to one agent and nobody else:
- A checkout. Its own working directory, so edits on disk can't overwrite each other.
- A branch. Its own line of history, so you can review and merge it separately.
- A set of files. The paths it is allowed to change.
The first two are solved by tooling. The third is a decision you have to make and write down, and it's the one that's usually missing.
Step 1: Give each agent its own checkout with git worktrees
The git worktree docs put it in one line: "A git repository can support multiple working trees, allowing you to check out more than one branch at a time." Each worktree is a separate directory on its own branch, sharing the same repository history.
The main coding agents now build on this:
-
Claude Code:
claude --worktree feature-authcreates a worktree under.claude/worktrees/feature-auth/on a new branchworktree-feature-auth, per the worktrees docs. Custom subagents can always run in their own worktree withisolation: worktreein their frontmatter. - Codex (ChatGPT desktop app): the worktrees page says worktrees "let Codex run multiple independent chats in the same project without interfering with each other." By default Codex works in a detached HEAD state, and you can create a branch from the chat header.
-
Cursor: in the Agents Window, Cursor creates a separate checkout when you start an agent in a worktree, and runs setup commands from
.cursor/worktrees.json, per the worktrees docs.
Two practical notes from those docs. A worktree is a fresh checkout, so dependencies and gitignored files such as .env aren't there until you set them up (Claude Code has .worktreeinclude, Cursor has setup-worktree). And, as the Codex page puts it, "Git only allows a branch to be checked out in one place at a time." That rule protects you: two agents can't silently share one branch.
If you'd rather do it by hand, plain git works with any agent:
git worktree add -b lane/api .worktrees/api
git worktree add -b lane/ui .worktrees/ui
Step 2: Write the lanes in a shared task file
Worktrees isolate files on disk. They don't stop two agents from changing the same file in their own copies. You find that out at merge time, which is the most expensive moment to find out.
So the file ownership goes in a file every agent reads before it starts. I keep it at the repo root as TASKS.md:
# TASKS.md (one lane per agent)
## lane: api
branch: lane/api
owns: src/api/
status: in progress
task: add a discount parameter to get_price
## lane: ui
branch: lane/ui
owns: src/ui/
status: in progress
task: show the discounted price in the cart
Each agent's prompt then says: work only in your branch, change only the paths under owns, and if you need a change outside them, write a hand-off note instead (Step 3). Claude Code's agent-teams docs give the same advice for teams, which don't get worktrees: "Two teammates editing the same file leads to overwrites. Break the work so each teammate owns a different set of files."
Instructions alone aren't proof, so I check them. This script reads TASKS.md, asks git which files each lane branch changed since main, and flags files outside a lane and files touched by more than one lane. It only reads.
#!/usr/bin/env python3
"""Check that each agent's branch only touched the files its lane owns.
Reads TASKS.md and git history only. Usage: python3 lane_check.py [BASE]"""
import re, subprocess, sys
from collections import defaultdict
base = sys.argv[1] if len(sys.argv) > 1 else "main"
lanes = re.findall(r"^## lane: (\S+)\nbranch: (\S+)\nowns: (.+)$",
open("TASKS.md").read(), re.M)
def changed(branch):
out = subprocess.run(["git", "diff", "--name-only", f"{base}...{branch}"],
capture_output=True, text=True, check=True).stdout
return out.split()
touched_by = defaultdict(list)
problems = 0
for name, branch, owns in lanes:
prefixes = [p.strip() for p in owns.split(",")]
files = changed(branch)
print(f"lane {name} ({branch}): {len(files)} file(s) changed")
for f in files:
touched_by[f].append(name)
if not any(f.startswith(p) for p in prefixes):
problems += 1
print(f" OUTSIDE LANE {f}")
for f, names in sorted(touched_by.items()):
if len(names) > 1:
problems += 1
print(f"SHARED FILE {f} <- {', '.join(names)}")
print("OK: every lane stayed in its files" if not problems
else f"{problems} problem(s): fix before merging")
sys.exit(1 if problems else 0)
I tested it on a small invented repo with the two lanes above. To be clear: no agent ran here. I made the "agent" edits by hand with commits, to reproduce the collision from the intro: the UI lane also changed src/api/client.py, which belongs to the API lane, and the API lane changed it too. This is the real output, run on 26 Nov 2026 with git 2.47.3 and Python 3.13.5:
$ git worktree add .worktrees/api2 lane/api
Preparing worktree (checking out 'lane/api')
fatal: 'lane/api' is already used by worktree at '/tmp/shop/.worktrees/api'
[exit 128]
$ python3 /tmp/lane_check.py
lane api (lane/api): 2 file(s) changed
lane ui (lane/ui): 2 file(s) changed
OUTSIDE LANE src/api/client.py
SHARED FILE src/api/client.py <- api, ui
2 problem(s): fix before merging
[exit 1]
$ git merge-tree --write-tree --name-only lane/api lane/ui
876743633a0717c0cd987ad594fbd1b26bbc968d
src/api/client.py
Auto-merging src/api/client.py
CONFLICT (content): Merge conflict in src/api/client.py
[exit 1]
The first command shows git refusing to put one branch in two worktrees. The lane check names the file and both lanes before any merge. git merge-tree --write-tree performs a trial merge of the two branches; per its docs it "does not make any new commits and does not read from or write to either the working tree or index". It confirms the conflict.
Step 3: Hand off instead of reaching across
The UI agent had a real reason to touch client.py: it needed something from the API side. The fix isn't to forbid that need. It's to route it to the lane that owns the file. I use a short hand-off note, appended to TASKS.md under the owner's lane. For the demo, an example would look like this:
### hand-off: ui -> api
need: client.py should expose the discounted price endpoint
why: cart.html shows the discounted price (lane/ui)
blocking: yes, ui shows a placeholder until this lands
done when: get_price(item_id, discount) is reachable from client.py
Five lines: who asks whom, what, why, whether it blocks, and how to tell it's done. The owning agent picks it up, and the asking agent reverts its out-of-lane edit. In the demo, after the UI lane reverted client.py:
$ python3 /tmp/lane_check.py
lane api (lane/api): 2 file(s) changed
lane ui (lane/ui): 1 file(s) changed
OK: every lane stayed in its files
[exit 0]
Step 4: Merge in a fixed order
My rule (my own, not from the docs): merge the lane that others depend on first. Here the UI needs the API, so lane/api goes first, then lane/ui, and the tests run after each merge, not once at the end. If the second merge breaks something, you know which lane did it. Write the order into TASKS.md before the agents start, so nobody argues about it at the end.
Both merges went through cleanly in the demo, and afterwards git worktree remove cleaned up both lanes.
Step 5: One reviewer for all lanes
Parallel agents can produce work faster than one person can read it. My rule here is also my own: one reviewer, human or a dedicated review agent, reads every lane's diff before it merges, and nothing merges on the writing agent's own say-so. A reviewer who sees all lanes is the one who notices that two "independent" tasks made opposite assumptions.
When to stop adding agents
Claude Code's agent-teams docs are direct about this: token costs scale linearly, coordination overhead grows, and "beyond a certain point, additional teammates don't speed up work proportionally." They suggest starting with 3 to 5 teammates for most workflows and add: "Three focused teammates often outperform five scattered ones." Their overview of parallel approaches adds that running several sessions or subagents at once multiplies token usage.
My own signals that the next agent won't help:
- You can't give it a set of files no other lane touches.
- Hand-off notes start outnumbering finished tasks.
- The reviewer is the queue: lanes wait for review more than they wait for work.
At that point, fewer lanes with bigger tasks usually beats more lanes with smaller ones.
Checklist
- One agent, one worktree, one branch.
- Write each lane's files in
TASKS.mdbefore any agent starts. - Set up each worktree: dependencies and
.envaren't copied by default. - Out-of-lane change needed? Hand-off note to the owner, not a direct edit.
- Run a lane check before merging: files outside lanes, files shared by lanes.
- Merge the depended-on lane first; run tests after each merge.
- One reviewer reads every diff.
- Can't give a new agent its own files? Don't add it.
Where to go next
Lanes, hand-offs and shared state are one part of a bigger design question. Multi-Agent Orchestration: The Complete Guide covers it framework-agnostic in 40 pages: when not to go multi-agent, five coordination patterns (sequential pipeline, fan-out/fan-in, supervisor-workers, dynamic handoff, maker-checker/debate), task boundaries and roles, handoffs and shared state, and five failure modes, including state collisions and diffused accountability.
How do you split work between parallel agents today: by files, by branches, or by trusting them to stay out of each other's way?
This article was written with the assistance of AI and reviewed by the author.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.