Sub topic 5
Choosing tasks that can run side by side
This chapter assumes you can create worktrees with git worktree add and that you have run a command-line coding agent on a single task. Worktrees stop agents from overwriting each other's files on disk. They do not stop two agents from changing the same lines in different branches. That problem appears later, as a merge conflict. Good task selection is how you prevent it.
Why task choice decides the outcome
Each agent works in its own worktree on its own branch. Every branch starts from the same commit, and you will merge them back one at a time. The merge cost depends on how much the branches overlap, so you control that cost before any agent starts.
Two kinds of overlap matter:
- Textual overlap. Two branches edit the same lines, or adjacent lines, in the same file. Git reports a conflict and a person must resolve it.
- Semantic overlap. The branches touch different files, but one change breaks an assumption in the other. For example, one agent renames a function while another adds a new call to the old name. Git merges this cleanly and the build fails.
Textual overlap is easy to detect. Semantic overlap is not, so decomposition has to account for both.
Properties of a good parallel task
A task suits parallel work when it has these properties:
- A bounded file footprint. You can name the directories or files it will change before it starts.
- A testable outcome. The agent can check its own work, for example with a test suite, a type check, or a command that exits non-zero on failure.
- No dependency on another task's output. It does not need a function, schema, or API that another agent is still writing.
- A small, reviewable diff. A diff you can read in one sitting. Large diffs make review the bottleneck, which cancels the time saved by running agents in parallel.
The following table compares task types that are usually safe with those that usually collide.
| Task type | Parallel-safe? | Why |
|---|---|---|
| Add a new module with its own tests | Usually | New files rarely conflict |
| Fix a bug in one isolated component | Usually | Narrow footprint |
| Write documentation for existing code | Usually | Touches docs, not code |
| Rename a widely used function | No | Touches many files other agents also edit |
| Reformat or lint-fix the whole repository | No | Changes lines everywhere |
| Upgrade a shared dependency | No | Changes lockfiles and may alter behavior everywhere |
| Change a database schema and the code using it | Not with other schema work | Shared contract between tasks |
Find the shared files first
Most conflicts come from a small set of files that many tasks want to touch. Look for these before assigning work:
- Registries and indexes. Route tables, plugin lists,
index.tsre-exports, and dependency-injection setup. Every new feature wants to add a line there. - Lockfiles and manifests.
package-lock.json,poetry.lock,Cargo.lock, andgo.sumchange whenever dependencies change. Lockfile conflicts are painful to resolve by hand. - Generated files. Snapshots, API clients, and compiled schemas. These change wholesale when their inputs change.
- Shared types and configuration. Central type definitions,
.env.example, and CI configuration.
If two tasks both need to edit one of these files, you have three options. You can serialize the tasks and run one after the other. You can assign the shared file to a single task and have the others avoid it. Or you can accept the conflict and plan to resolve it, which is reasonable when each edit is a one-line addition in a registry.
Decompose by seam, not by size
Splitting a large change into equal pieces does not work. Split it along seams, which are natural boundaries in the code where the interface between two parts is already stable.
Useful seams include:
- Separate packages or services in a monorepo.
- Layers behind an existing interface, such as one agent writing an implementation while another writes tests against the interface.
- Independent features that each add new files and register themselves in one place.
- Different file types, such as code, documentation, and CI configuration.
If the seam does not exist yet, create it first. A common pattern is a short preparatory task that you run alone: define the interface, add the empty registry entries, or move shared code into its own module. Merge that task into main, then create the parallel worktrees from the updated main. The setup pass takes minutes, and it can remove most of the conflicts the later passes would have had.
Write a task brief per agent
An agent in its own worktree has no view of the other agents' work. The brief you give it must therefore state its boundaries explicitly. Put one brief per task in a file the agent can read, such as TASKS.md in each worktree, or pass the text on the command line.
A brief should contain:
- The goal, in one or two sentences.
- Files it may change, and files it must not change.
- How to verify, meaning the exact command to run.
- A stop condition, such as "stop after tests pass; do not refactor unrelated code."
Task: auth
Goal: Add token refresh to the auth client.
May change: src/auth/**, tests/auth/**
Must not change: package.json, package-lock.json, src/routes.ts
Verify: npm test -- tests/auth
Stop when: the verify command passes. Do not refactor other code.
If you need a change outside the allowed paths, stop and report it instead.The last line matters. Without it, an agent that hits a problem outside its boundary will often fix it in place, and that fix is the one most likely to collide with another branch. Asking it to report the problem keeps the decision with you.
Boundaries in a brief are instructions, not enforcement. An agent can still edit a forbidden file. You find out at review time, so the overlap check below is how you verify the boundaries held.
Check overlap after the agents finish
You can measure overlap directly. The three-dot form git diff --name-only main...agent/auth lists the files a branch changed since it diverged from main. Comparing those lists across branches shows which pairs touch the same files.
#!/usr/bin/env bash
# Usage: ./overlap.sh agent/auth agent/billing agent/search
set -euo pipefail
base=main
branches=("$@")
for ((i = 0; i < ${#branches[@]}; i++)); do
for ((j = i + 1; j < ${#branches[@]}; j++)); do
a=${branches[$i]}
b=${branches[$j]}
common=$(comm -12 \
<(git diff --name-only "$base...$a" | sort) \
<(git diff --name-only "$base...$b" | sort))
if [ -n "$common" ]; then
echo "Overlap between $a and $b:"
echo "$common"
fi
done
doneThe script prints nothing when no two branches share a changed file. It checks file names only. Two branches can edit the same file in distant regions and merge cleanly, and the script will still report that file. Treat each reported file as something to inspect, not as proof of a conflict.
To test for real textual conflicts without touching your working tree, use git merge-tree. In Git 2.38 and later, this command performs the merge in memory.
git merge-tree --write-tree main agent/auth
git merge-tree --write-tree main agent/billingEach command exits with status 0 when the merge is clean and 1 when there are conflicts, and prints the conflicting paths. This tests each branch against main, not against the other branches, so run it again after merging the first branch to test the next.
Limits of upfront planning
Planning reduces conflicts but does not eliminate them, for these reasons:
- You cannot always predict the footprint. An agent may decide a bug fix needs a change in a shared helper. Brief boundaries reduce this but do not prevent it.
- Semantic conflicts pass Git. Run the full test suite on
mainafter each merge, not only each branch's own tests. - More agents means more review. Each branch needs a person to read it. If review takes longer than the work saved, run fewer agents. Two or three well-bounded tasks often beat six overlapping ones.
- Order matters for merging. Merge the branch that touches the most shared files first, so later branches rebase or merge against it once, rather than every branch absorbing every other branch's changes.
Chapter material on resolving the conflicts that do occur comes later in this tutorial. For now, the rule is to spend a few minutes naming each task's files and shared seams before you create any worktree.
Exercise
You plan three parallel agent tasks: (1) add a billing module in src/billing/, (2) add a search module in src/search/, (3) upgrade the web framework version across the whole project. Both modules also need a one-line entry in src/routes.ts. Which tasks would you run in parallel, and what would you do about the shared file and the upgrade?
Show answer
Run tasks 1 and 2 in parallel. Their code is in separate directories, and the only overlap is a one-line addition to src/routes.ts, which is a small conflict you can resolve at merge time. Alternatively, run a short preparatory task first that adds placeholder entries to src/routes.ts, merge it into main, and then create the worktrees. Do not run task 3 in parallel with the others. A framework upgrade changes the manifest, lockfile, and many source files, so it overlaps with everything. Run it before or after the other two, and merge it alone.
Exercise
Two agent branches changed different files and git merge reports no conflicts, but the build fails after both are merged into main. What kind of overlap is this, and what should you do to catch it earlier?
Show answer
This is semantic overlap. The changes are textually separate but incompatible, for example one branch renamed a function that the other branch calls. Catch it by running the full build and test suite on main after merging each branch, instead of relying only on each branch's own tests. You can also reduce the risk by stating interface boundaries in each task brief and by avoiding parallel tasks that change a shared API.