Summary
On October 18 we counted about 96 dirty git worktrees left by our coding agents, and nobody could say which ones were merged, which held unfinished work, or which were safe to delete.1
We replaced ad-hoc worktrees with one tool that every agent must use, a ledger stored outside the working tree, and four verdicts: merged, blocked with a reason, unknown, or active.
Five weeks later the ledger lists 12 worktrees across five repos, 9 blocked with a written reason and 3 unknown, while at least 315 worktree branches were merged back and removed.2
Running several AI coding agents on one repo works best when each agent gets its own git worktree, and the hard part is not creating them but knowing, weeks later, which ones are finished. Our answer is a ledger: every worktree records its task, its integration branch and its base commit, and one command gives each worktree a verdict.
We run Claude Code, Codex and other agents side by side on the two main ZeroTwo repos, the web app and the API, plus a handful of smaller ones. Most setup guides for parallel agents stop at git worktree add, one directory per agent, and a merge at the end. That holds while one person closes every worktree by hand.
It stopped holding for us in October. This post covers the pile-up, the rule we wrote the same day, how the verdicts are computed, and what the ledger shows today. It does not prove the rule prevents every lost change, and the last section says what we have not measured.
About 96 worktrees piled up because nothing recorded what each one was for
Our agents used to follow a recipe that copied the shared checkout's uncommitted work into a fresh worktree, edited there, and copied the result back without committing. Each step looked safe. Each worktree also ended its life as a folder of uncommitted changes with no record of where they came from.
By October 18 our notes counted about 96 of those dirty worktrees.1 Git could not tell us which were merged, because uncommitted changes have no history to compare. A diff against the shared checkout did not help either: that checkout carries other sessions' work in progress, so every worktree looked different from it. Deleting them risked losing a fix nobody had carried back. Keeping them meant every new agent wandered into a folder of half-finished edits.
The cost showed up in three places:
Disk and builds. Each worktree held its own dependency links and build caches.
Agent confusion. Agents searching for a file found stale copies in other worktrees.
Review. Nobody could answer "is this merged?" without reading the diff by hand.
One tool creates, finishes, blocks and audits every worktree
The same day, the rule went into the AGENTS.md file of every repo.3 Agents manage worktrees only through one script, never with git worktree add directly, never on a detached HEAD, and never outside one folder. A worktree counts as mergeable only once its work is committed, because git cannot tell whether uncommitted changes ever landed.
The script has five verbs that cover a worktree's whole life:
zt-worktrees new <task> <repo>... [--overlay] create .claude-worktrees/<task>/<Repo> on wt/<task>
zt-worktrees finish <task> merge or apply back every repo of <task>, then remove it
zt-worktrees block <task> "<reason>" record why <task> must stay
zt-worktrees status [--summary] verdict for every worktree of every repo
zt-worktrees prune remove merged, inactive worktrees and their branchesOne task name spans every repo the task touches, so a change to the web app and the API is one task with two worktrees. finish merges the branch, checks that it really is an ancestor of the integration branch, and only then removes the worktree and its branch. When a merge conflicts, it aborts and leaves the shared checkout untouched. When anything fails, it exits with an error, so an agent that chains a push after it stops there.4
block is the verb that changed behaviour most. An agent that cannot finish, because of a conflict, a failing test or another agent's work in the way, has to write down why. "Blocked" with a sentence attached is something a person can act on. A silent dirty folder is not.
The ledger lives in git's own folder, so no checkout can lose it
Each worktree's record is a small key=value file called zt-task, kept in the worktree's private git directory rather than in its files. It holds the task name, the integration branch, the base commit, the creation time, a status and, for a blocked worktree, its reason.5 A git clean, a reset or another agent's edit cannot touch it, and it disappears with the worktree when finish removes it.
The --overlay mode handles the case that caused the pile-up. When the shared checkout already holds uncommitted work the task depends on, new --overlay copies that work into the worktree and commits it as one wt-baseline commit. The agent commits its own changes on top. finish then applies only the commits after the baseline back onto the shared checkout, so the other session's work in progress is never merged in under this task's name.
Every worktree gets one of four verdicts
status computes a verdict for each worktree in every repo, from the ledger and from git:
Blocked when the ledger says so, shown with its reason.
Merged when an overlay worktree was applied back and is clean, or when it has no uncommitted files and
git cherryfinds no commits missing from the integration branch.Unknown in every other case, shown with how many files are uncommitted and how many commits are not yet in the branch.
Active on top of any verdict, when a running process sits in the folder or a file changed in the last six hours.
Two details came from bugs. The check treats a missing integration branch as unknown, because git cherry fails without it and would otherwise read as zero commits ahead, which looks merged. And the active check reads the process list once before matching, because a pipe into grep -q can fail on a closed pipe even when it matched.5 prune removes only worktrees that are merged and not active. Blocked and unknown ones stay until a person or the agent that owns them decides.
Today the ledger shows 12 worktrees, and each one has a known state
This morning the summary read:2
worktrees: 0 merged, 9 blocked, 3 unknown (0 active). Run 'zt-worktrees status' for detail.Those 12 worktrees span five repos. Each of the 9 blocked ones carries a sentence saying what it waits for, such as a merge still in progress in another task or another session's uncommitted rewrite in the primary checkout. The 3 unknown ones each show their exact count of commits not yet in their integration branch, so the next step is a decision rather than an investigation. None is merged but forgotten.
Since the rule went in, git records at least 152 merge commits of worktree branches in the web app repo and 167 in the API repo, each a worktree that was finished and removed.6 That count is a floor: overlay worktrees apply back as a patch and leave no merge commit.
The rule has a known weak spot. An overlay finish fails with "patch does not apply" when another session has edited the same file in the shared checkout since the baseline. We hit it on a shared database history document that many sessions write to. The fix was procedural: keep that document out of the worktree commit and add its entries to the shared copy by hand.7
What remains unproved
The starting count. The figure of about 96 comes from our agent notes written on October 18. We did not keep the listing itself, so we cannot break it down by repo or say how many held work that was never carried back.
Lost changes. The rule makes an unmerged worktree visible. It does not prove that no fix was lost before October 18, or that a blocked worktree's work will be finished.
Other setups. Everything here runs on one machine with a shared parent folder. We have not tried the script on a team where several people's agents share a remote but not a disk.
Use the same rule in your own repo
The pattern carries to any repo where more than one agent writes code, and it needs no special tooling beyond git and a shell script. Give each worktree a task name, store its record outside the working tree, refuse to finish uncommitted work, and make "blocked" require a reason.
ZeroTwo's Code agent runs coding tasks locally, in a worktree or in the cloud from the desktop app, and the same habits apply there. For the measurements behind our recent speed work, read how we made a 16,000-step chat open fast, and for everything else in the release, what's new in ZeroTwo 2.0.
If your agents can create worktrees, they need a way to say what each one is waiting for. Without that, the folder of worktrees becomes the next thing nobody dares to clean.
Footnotes
- From our agent notes of October 18, 2026, written the day the old recipe was replaced. The note records "~96 dirty worktrees nobody could tell were merged". The listing behind the count was not kept. ↩
- Output of `zt-worktrees status --summary` on the Mac that runs our agents, November 25, 2026. The five repos are the web app, the API and three smaller repos. ↩
- The worktree rules were added to the web app's and the API's `AGENTS.md` in commits made on October 18, 2026. The current text is in the "Worktree isolation and completion" section of each file. ↩
- The script's own comment gives the reason: a caller chaining on the exit status must not go on after a worktree that did not finish. ↩
- From the `zt-worktrees` script as read on November 25, 2026: the ledger keys written by `new`, `finish` and `block`, the `verdict` function and the `is_active` check. ↩
- Counted with `git log --all --since=2026-10-18 --merges` in each repo, keeping merge commits whose subject starts with a `wt/` branch, on November 25, 2026. That gives 150 distinct branches in the web app repo and 165 in the API repo. ↩
- From our agent notes of November 13, 2026, on overlay worktrees and the shared database update history document. ↩

