Isolation
Have several agents work on the same repository without overwriting each other's work
- Module
- Module 02
- Deliverable
- Three branches merged or ready to be, and compte-rendu-derive.md (base commit, raw output of the audit, files modified by two branches, merge order, what drifted)
- Access
- preview
Before you read
- What you will be able to do
By the end, you give each agent that writes its own worktree, and you check their branches with commands before merging any of them.
- What you will do
- Action 1: Write the isolation contract in an instructions template
- Action 2: Check the current branch of the main checkout
- Action 3: List the shared state of your repository
- Action 4: Run the audit on the branches of a wave
- Action 5: Rank the branches by number of modified files
Then the exercise: Trois tâches en parallèle, chacune dans son espace de travail isolé, puis l'intégration (in French)
- Time
36 min reading, 120 min exercise
- Updated on
- Before you start
- Words used in this module
- toolThe software that runs your coding agents, and that has them read, at launch, the project's instructions files.
- sessionA conversation with the tool, which keeps its own history; a subagent works in its own, and a fresh session starts without the history of the others.
- instructionsThe text an agent receives at launch, from you, from the session that launches it or from the instructions files the tool has it read: what it must achieve, what it must not touch, facts about the code.
- agentA session you open, or a subagent that a session launches; it writes, meaning it modifies files, unless its instructions forbid it: it is then read-only.
- worktreeOne more working copy of the same git repository, on its own branch, with its own files, that separates neither your application's local database server nor the ports it uses.
- main checkoutThe folder where you cloned the repository, as opposed to the worktrees you create next to it.
- waveAgents that write, launched together: three at most to start, a starting number that was not measured, to raise if your merges stay easy; the next wave starts only once the results of this one are merged.
- base commitThe commit all the worktrees of a wave start from: the last commit of main at the moment you launch it, which you note before launching.
- isolation contractThe four clauses in the instructions of an agent that writes: the worktree it uses, the ban on leaving it, the command that judges its result (it fails if the result is wrong), and when to stop and ask you.
- base driftWhat happens to a branch that started from a commit older than the one main is at when you merge it: it works on a state of the repository that main has moved past.
- shared stateWhat several agents or processes write in the same place, without git tracking it or a worktree separating it: local database, port, container volume, common cache.
- lockfileThe file where the package manager pins the exact versions of the dependencies (package-lock.json, pnpm-lock.yaml…); git tracks it like the other files.
- content comparisonThe comparison, file by file, of what a branch modified with the same file in main, to know what it still brings.
- integration branchA separate branch, created for a wave, where you merge its branches one by one before updating main.
- auditThe four checks by commands that you run on a wave (main, the main checkout and the branches) before merging any of its branches, with this module's script.
- silent mergeA merge that git accepts without reporting a conflict, and whose result is wrong.
- isolated workspaceThe name the exercise that closes this module gives to a worktree, or to any other copy of the repository reserved for a single agent, like a clone or a container.
Three agents in the same folder
You launch three agents on the same repository, each with its task and the same starting
instructions: "create a branch, then fix". None has a worktree: all of them work in the folder where
you cloned the repository. An hour later, git branch --show-current displays the third agent's
branch, when you thought you were on main. The first agent's branch contains a commit it did not
write. Two branches modified the same file: git merges them without reporting a conflict, and the
result is wrong. That is a silent merge.
It is easy to conclude that several agents cannot work in the same git repository. That is wrong, and the problem has two halves. The three agents shared one folder, so one current branch: a worktree per agent solves that half. The two branches that modified the same file could merge wrongly even with a worktree each. That half is spotted by checks before and during the merge, which reduce the risk without removing it. Concluding that git does not allow work by several agents has two costs. You look for a defect in git, when it does what it is asked, then you never try worktrees, which would have solved the first half.
Give each agent that writes its own worktree, started from a commit you noted, on a branch you named. Then check the branches with commands before merging any of them.
I am the author of this manual, and "I" always refers to my own practice. A session is a conversation with the tool, which keeps its own history. An agent, here, is a session you open, or a subagent that a session launches. At launch it receives its instructions: the text that tells it what to do, coming from you, from the session that launches it or from the project's instructions files. Its tool has it read those files at launch; the tool is the software that runs your agents. An agent writes, meaning it modifies files, unless its instructions forbid it: it is then read-only. That ban rests only on the instructions, unless your tool can also block writing.
A worktree is one more working copy of the same git repository, on its own branch, with its own
files. It separates neither your application's local database server nor the ports it uses. You
create it with git worktree add. The main checkout is the folder where you cloned the
repository, as opposed to the worktrees you create next to it. A wave groups agents that write,
launched together: three at most to start. This starting number was not measured; raise it if your
merges stay easy. The next wave starts only once the results of this one are merged.
When this module is useless
This module is useless to you in two cases.
First case: a single agent writes, and nobody else writes in the repository during that time, neither another agent nor you by hand. That agent collides with nobody. A worktree would cost you its creation, the installation of the dependencies and its removal, against a risk of zero: work in the main checkout. Read-only agents can run alongside, on the conditions given two paragraphs further down.
Second case: a repository so long to install that the wait outweighs the gain. A new worktree
contains only the files tracked by git; the ignored files, like node_modules or .env, are not in
it. So you have to install the dependencies in it, and copy into it the ignored files it needs,
before its agent works. If a full installation takes twenty minutes on your side, each worktree
asks for those twenty minutes before its agent starts. Compare that wait with the time that
launching the agents in parallel would save you (module 1 makes it its third criterion: does the
gain cover the cost?). If the wait outweighs it, first make the installation cheaper, or give the
tasks to a single agent, one after the other. As long as the wait outweighs the gain, this module
will bring you nothing.
Read-only agents, for their part, do not need a worktree, even many of them, if they never change
branch in the main checkout. A git checkout, even to read another branch, changes the branch of
everyone working there: forbid it in their instructions, and have your tool block it if it can. If
they run tests or a server, failure 3 of this module, further down, concerns them too: a database
or ports used by several. And if they run tests in a folder where an agent writes, those tests can
read files that are being rewritten: in that case give them a worktree of their own.
The isolation contract, in every agent's instructions
In the instructions of each agent that writes, put four clauses: the worktree it uses, the ban on leaving it, the command that judges its result, and when to stop and ask you. This module calls these four clauses the isolation contract. The command that judges the result is the one that fails when the result is wrong: a test, a script (criterion 2 of module 1).
The base commit is the commit all the worktrees of a wave start from: the last commit of main
at the moment you launch it. Note it before launching, with git rev-parse main. The commands in
this module assume your main branch is called main: replace that name if yours has another. They
look only at the local main. If others push to the remote repository, update main with
git pull before noting the base commit, then do not touch it again during the wave. You will run
git pull again at the very end, just before merging the wave into main: the last section says
what to do if main has moved forward. The first clause, the one about the worktree, carries three
things:
- a worktree of its own, created for this agent alone;
- created from the base commit, referred to by its full identifier (the one
git rev-parse maindisplays), and never "from the current branch", which can change while the wave is starting; - on a branch whose name you choose, unique, never left to the agent's choice.
Remove one of the three, and here is what breaks:
- Without a worktree of its own, the agent creates its branch in the main checkout and changes the current branch of everyone working there: that is failure 1, further down.
- Without a base commit noted in the instructions, each agent starts from the current branch at
the moment it starts, and that branch may have changed or moved forward between two starts. The
noted commit gives everyone the same start, and the audit, further on, can check it. The noted
commit is not enough on its own against failure 2, base drift: you must also commit nothing on
mainduring the wave, as the section on that failure says. - Without a name chosen by you, the agent can land on a name already taken. Some tools then
create, without warning, a branch under another name, and the agent works there. You learn it at
the merge. I do not name them: your tool's documentation says what it does with a name already
taken. A name you choose, with the task and the date in it, is very likely to be new; and if it
already exists,
git worktree add -brefuses and says so.
Also start the instructions with a sentence of context: other agents are working at the same time,
in other worktrees of the same repository. An agent that does not know this looks for the cause of
a failure in its own worktree only, when it comes from the others. The block below mentions the
lockfile, the file where the package manager pins the exact versions of the dependencies
(package-lock.json, pnpm-lock.yaml…); git tracks it like the other files. Here is the block, to
complete at each launch:
Other agents are working at the same time as you, in other worktrees of this repository.
1. Your worktree. Create it from the main checkout of the repository:
git worktree add ../wt-<unique-name> -b <unique-branch> <base-commit>
Install the dependencies in it without modifying the lockfile (npm ci,
pnpm install --frozen-lockfile…), and copy into it the ignored files it needs (.env…).
Use the local database and the ports reserved for you: <values>.
Then work only in this worktree.
2. Do not leave it. Never change branch in the main checkout (neither git checkout
nor git switch). Commit only the files you modified yourself.
3. Before concluding, run <the command that judges your result>: it must succeed.
4. Stop and ask me before modifying what other agents may also be
modifying: the dependency lockfile (package-lock.json, pnpm-lock.yaml…),
a database or a cache common to several worktrees.
Sometimes the worktree exists before the agent: you created it, or your tool creates it and launches the agent in it (see "Let the tool refuse", further down). In that case replace clause 1 with this one, and keep the other three:
1. Your worktree is <path>. Install the dependencies in it without modifying the
lockfile, and copy into it the ignored files it needs (.env…). Use the local
database and the ports reserved for you: <values>. Then work
only in this worktree.
In my practice, most agents run with no isolation at all. That is true even for some whose
instructions ask them to create a branch or to commit, and it is the situation where the failures
of this module are born. This block starts from a formula I gave to 69 sessions: "ISOLATED git
worktree, base = clean main". It asked to start from main, with no uncommitted change in the
main checkout. The block replaces main with a noted commit, because main can move and a noted
commit does not move. It adds the command that judges the result and the stop condition.
Action 1: Write the isolation contract in an instructions template
Copy the block into a file of your own, kept outside the repository. You will paste it into the launch message of each agent: its values change at each launch, and a project instructions file cannot carry them. Replace the examples in clauses 1 and 4 with those of your repository: the installation command, the lockfile, the local database if there is one. Leave between angle brackets what changes at each launch.
Expected result: A file that contains the four clauses of the contract, adapted to your repository, with marked slots for the worktree, the branch, the base commit, the reserved database and ports, and the command that judges.
Three failures during the wave, a fourth at the merge
While a wave is running, three failures threaten it; this module numbers them from 1 to 3. Failure 4 waits for the merge: two branches that modify the same file. The audit, four checks by commands that this module gives further on, looks before any merge for failure 4, and for traces of failures 1 and 2. Failure 3 has no check: it is recognised by its symptoms, described in its own section. These failures do not always display an error; when they do display one, it often arrives later, somewhere else, like a test that fails.
Failure 1: the agents share the main checkout
A folder has only one current branch and only one set of files. Several agents working in it change each other's branch, and mix their files.
An agent you tell "create a branch fix/x", without requiring a worktree, runs
git checkout -b fix/x in the main checkout. An agent that the tool did isolate in a worktree can
do the same (evidence below): clause 2 of the contract forbids it in both cases. Everyone working
in that folder ends up on fix/x, you included. Between an agent's checkout -b and its commit,
another has time to change branch in turn: the first then commits on the second one's branch.
Everyone's uncommitted files are in everyone's folder, and a git add -A takes them along.
The sign, when you think of looking for it:
$ git branch --show-current
fix/agent-3-pagination # you thought you were on main
$ git worktree list
/home/dev/mon-app 3a4b5c6 [fix/agent-3-pagination]
# a single line, for three agents
Action 2: Check the current branch of the main checkout
In the main checkout of your repository, run git branch --show-current, then
git worktree list. Then read the output this way:
- a branch other than the expected one: an agent changed branch in the main checkout;
- a single line when agents are supposed to be writing in worktrees right now: they are in fact writing in the main checkout;
- agents in clones or containers:
git worktree listdoes not show them, and only the current branch of the main checkout informs you.
Expected result: The branch you left the main checkout on, usually main, then one line for the main checkout and one line per worktree still present.
Failure 2: base drift
Create all the worktrees of a wave from the base commit, and commit nothing on main while it is
running. At merge time, main is then still at the base commit, and each branch starts from it. A
branch that started from a commit older than main suffers base drift: it works on a state of
the repository that main has moved past.
This failure strikes even when each agent has its worktree. Each branch starts from the commit its
worktree was created from. Suppose each worktree starts from main at the moment you create it,
and main moves forward during the wave. Any branch created before that move then stays on a state
that main has moved past. Such a branch passes its tests and its diff reads well. But it was
written without the changes main has received since it started. It can redo differently a piece
of work that main already contains, or rely on code that main has changed. The merge then gives
a conflict, or a wrong result with no conflict.
The sign, for each branch:
git rev-parse main # must display the base commit noted before the wave
git merge-base <branche> main # must display that same commit, for each branch
Failure 3: shared state outside git
Give each worktree its own local database and its own ports, and its own container volumes and caches when they live outside each worktree's folder. One environment variable per worktree is often enough: another database name, another port, another volume name. What must stay common belongs to a single agent, or is shared without anyone writing to it.
This module calls shared state what several agents or processes write in the same place, without git tracking it or a worktree separating it. A worktree copies the repository's files; it copies neither the local database, nor the ports, nor what lives outside its folder. A port already taken usually gives an error that says so ("address already in use"). A file that two processes rewrite, for its part, has a sign of its own. The failure is intermittent, it hits now one, now the other, and it survives a restart of the service or container it hits. And none of the processes is at fault: each did its work correctly, on a file the other had just rewritten.
Action 3: List the shared state of your repository
For your repository, note what two worktrees would use in common. Think of the local database, the ports of the servers your tests start and the volumes your containers mount. Think also of the caches kept outside each worktree's folder. For each resource, decide how each worktree will have its own; otherwise, who alone writes to it during the wave, or whether nobody needs to write to it. Then carry over into clause 1 of your instructions template what each agent will have of its own.
Expected result: A written list of the resources two worktrees would use together, with for each one the way each worktree will have its own, or failing that the agent that alone will write to it during the wave, or the words 'nobody writes to it'.
Let the tool refuse
If your tool can refuse an agent a command that would leave its worktree, keep that refusal active, even when it slows you down. A refusal stops one command, which the agent must do another way. An agent that writes outside its worktree can cost a wave to untangle (failures 1 and 2). Your tool's documentation says whether it offers this refusal.
My tool does it, in the sessions counted below; I do not name it, and yours may do otherwise. For
an agent it launched itself in a worktree, it refuses a command when it cannot prove that the
command stays in that worktree. It decides before executing, and what it cannot judge, it refuses.
So this refusal protects only an agent launched by the tool in a worktree. If the agent creates its
worktree on the order of its instructions, as in this module's block, the tool does not know it
must stay there: clause 2 then rests only on the instructions. If your tool can itself launch an
agent in a worktree, from the base commit and under a name you choose, that route adds its refusal
to the instructions. It does not replace them: even isolated by the tool, agents ran
git checkout -b in the main checkout (evidence of failure 1). Keep clause 2 in both cases.
What the exercise asks for
The exercise is done on a repository more than six months old that you maintain, with no
uncommitted change. It asks for three tasks that can start at the same time according to the
decision grid of module 1: they modify different files, and a command judges the result of each. On
a toy project, with few files and no shared state, the failures the exercise teaches you to read
are unlikely to appear. If you do not have such a repository, the exercise page describes another
route, planned for that case: an example repository, with its history. It also says whether that
repository is already available. The deliverable: three branches merged, or ready to be, and a
report (compte-rendu-derive.md). The report gives the base commit, the raw output of the audit,
the files modified by two branches, the merge order and what drifted.
The exercise says isolated workspace where this module says worktree: it also accepts any other
copy of the repository reserved for a single agent, like a clone or a container. The commands in
this module assume worktrees, whose branches are visible from the main checkout. With a clone, or a
container that works on its own copy, first fetch each branch into the main checkout
(git fetch <path-to-the-copy> <branch>:<branch>).
Still in this module:
- The audit, before merging anything
- The integration, in order
- In practice
That is about 2,127 words, and the exercise with its deliverable template.