Blog · data-story
494 agent worktrees filled a Mac to 100%. What we fixed
Published · by lysosome
Remove a git worktree when its pull request merges, not when it has been idle for a month: check that the PR is merged, that the worktree has no uncommitted or untracked files, and that its branch head is the commit the PR merged, then run git worktree remove (never --force). On the Mac we measure, coding agents created 494 worktrees in two days and filled the disk to 100%, and a 30-day idle rule flagged none of them, because none was a month old. The script for the merge rule is at the end of this post.
What happened
On November 27 we measured this Mac: 161 git worktrees holding 150 GB, almost all made by Claude Code. Two days later, on November 29, it had 2.7 GB free of 926 GB. The worktree count had gone from 161 to 494, and they held 336 GB.
It's one machine, one developer running Claude Code in parallel worktrees across a portfolio of repositories. Every number below comes from the --json output of lysosome 0.4.1, the version installed that day, or from du and df where lysosome didn't look. Sizes are in GB (10⁹ bytes).
Why the idle rule missed all of them
lysosome's default rule calls a worktree dormant after 30 days without activity, and only a dormant, clean, pushed worktree is removable. That rule was written for people, who open a worktree for a long-lived branch and forget it. Agents work the other way: a task takes an hour, its pull request merges the same day, and the worktree, with its own node_modules or target/, stays.
With the default 30 days, the scan found 0 of 494 worktrees removable. Every one was ACTIVE. Rerun with a 3-day threshold, it found 57 removable (67 GB); 52 were removed and 50.6 GB came back.
A fresh scan later the same day, after that cleanup, shows the shape of the problem:
| Worktrees on November 29 (after the cleanup) | Count | Size |
|---|---|---|
| All linked worktrees | 466 | 276.4 GB |
| Touched today | 293 | 126.4 GB |
| Touched 1 to 7 days ago | 164 | 125.2 GB |
| Touched 8 days ago or more | 9 | 24.8 GB |
| Merged, clean and pushed | 217 | 84.8 GB |
| of which: git-ignored build and dependency folders | 61.6 GB | |
| Removable under the 30-day rule | 0 | 0 GB |
217 worktrees were finished work: the branch merged, nothing uncommitted, nothing unpushed. They held 84.8 GB, and the idle rule saw none of them, because "merged" and "old" are different questions. For agent worktrees, merged is the one that matters.
What lysosome got right, and what it missed
The cleanup took the Mac from 2.7 GB to 39 GB free. lysosome did most of it, but an agent had to step in by hand every time lysosome fell short, so here is the full list.
It got right: nothing with uncommitted, untracked or unpushed work was touched; everything went to the Trash, not rm; every item was re-checked right before it was removed.
It missed:
- Merged worktrees younger than 30 days. Above: 0 of 494 at the default.
- Cache sizes were upper bounds. The dry run planned 15.2 GB of cache cleanup; 2.3 GB was freed. The pnpm store was planned at 7.0 GB and freed 321 MB, because
pnpm store pruneonly drops packages nothing references, and agent worktrees reference most of them. Conda was planned at 3.5 GB and freed nothing. - A failed command was reported as done.
brew cleanup -sprinted an error about an untrusted tap, exited, and lysosome recorded the item as "done" with 0 bytes freed. - A missing tool meant a skipped cache. The Yarn cache (1.2 GB) was skipped because
yarnwas no longer onPATH; the agent deleted~/Library/Caches/Yarnby hand. - Workspace worktrees were skipped. 5 worktrees made by
claude --worktreeat the root of a multi-repo workspace hold a checkout of each sub-repo. lysosome saw a git checkout inside and refused, even though every nested repo was clean and pushed. - Trashed isn't freed. lysosome counted trashed items as freed space, but macOS doesn't give the space back until the Trash is emptied.
- The scan was too big for an agent. Scanning the code folder returned 2.5 MB of JSON, too much for an MCP client to read, and a confirmed clean meant typing all 57 item ids back and ran past the client's 120-second timeout.
- About 132 GB it never looked at. From
du:~/Library/Application Support69 GB,~/Library/Containers23 GB,~/.docker21 GB (theDocker.rawdisk image, which doesn't shrink when you delete images) and~/Library/Caches19 GB.
What changed in lysosome
Every item on that list is fixed in lysosome 0.5.1, out on November 30. From its changelog:
- Merged worktrees are REMOVABLE at any age: clean, unlocked, merged into the default branch (or squash-merged, checked through
gh), and left alone for a day.--daysnow only protects work that isn't merged. A clean worktree whose every commit is pushed and that has no open PR is removable too; an open PR keeps it ACTIVE and names the PR. - A running agent session keeps its worktree. lysosome reads Claude Code's session records and treats any worktree a live session works in as ACTIVE, whatever its idle time, and checks again right before each removal. We added this after the cleanup above removed two clean, pushed worktrees whose sessions had been idle for 5 days but were still running. Nothing was lost, but those sessions were left pointing at a missing folder.
- Measured sizes. The dry run measures what pnpm, conda and Homebrew would really free before it shows the plan, and marks anything it can't measure as "up to".
- Failures say failed. A cleanup command that prints
Error:orfatal:is reportedfailedwith that line, even when it exits 0. For the Homebrew tap case it names thebrew trustcommand to run. - A missing tool no longer skips its cache. When Yarn, npm, bun, pip, uv, Go, Homebrew or CocoaPods is gone from
PATH, the cache folder goes to the Trash instead. - Workspace worktrees are removable when every nested worktree is, and
cleanremoves each one through its own repository before the outer one. - Trash is reported separately. Results show
trashed_bytes, andclean --empty-own-trashempties only what lysosome itself moved to the Trash. - Home-folder finders. A new
app_cachescategory covers~/Library/Caches, browser and Electron app caches and sandboxed apps' caches. Docker'sDocker.rawis reported with the space it holds beyond what Docker says it uses (lysosome never touches it). - Agent-sized output.
scan --summaryreturns totals and the biggest items instead of every finding,clean --plan-tokenturns a dry run into a token thatclean --plan <token> --yescleans exactly, and a long clean over MCP keeps running in the background instead of hitting the client's timeout.
The merge rule is also what our own fleet now runs: a scheduled job on this Mac, built on lysosome, that removes finished worktrees as their work lands. Its first full pass removed 151 of them and took the disk from 47 GB to 91 GB free.
Scanned again with 0.5.1 and its default options the next evening, the Mac had 135 worktrees holding 182.5 GB. 96 of them had a Claude Code session running in them, 3 had an open pull request, and 1 was finished and REMOVABLE. The finished work was gone; what's left is work in progress. And the disk was back down to 41 GB free, because the agents keep building in the worktrees that stay: 83.6 GB of the 182.5 GB is git-ignored build output.
How to never get there
Cleanup after the disk fills is the wrong time. Chain it to the loop that makes the worktrees.
1. Remove a worktree when its pull request merges. For one repository, with git and a signed-in gh. It removes a worktree only when its PR is merged, its branch head is the commit the PR merged (so no work was added after), and git worktree remove agrees it is clean:
git worktree list --porcelain | awk '/^worktree /{p=$2} /^branch /{sub("refs/heads/","",$2); print p, $2}' |
while read -r path branch; do
pr=$(gh pr view "$branch" --json state,headRefOid -q 'select(.state=="MERGED") | .headRefOid' 2>/dev/null) || continue
[ -n "$pr" ] && [ "$pr" = "$(git -C "$path" rev-parse HEAD)" ] || continue
git worktree remove "$path" && echo "removed $path ($branch)"
done
git worktree prune
Squash merges are why it asks gh instead of git branch --merged: git reports a squash-merged branch as unmerged. Run it from a session-end hook, a post-merge job, or by hand. With lysosome 0.5.1 installed, lysosome clean --worktrees ~/code applies the same rule across every repository, skips worktrees with a running agent session, and is a dry run until you add --yes.
2. Strip build output from the worktrees that stay. Work in progress still holds most of the space. lysosome clean --strip-artifacts --tier regenerable_expensive ~/code takes the git-ignored node_modules, target, dist and similar folders out of worktrees left alone for a day with no session running, and keeps the worktree and every other file in it.
3. Run the safe tier every hour, and alarm before the disk is full. A launchd job on macOS (a systemd timer on Linux) that cleans only what rebuilds by itself and warns when free space drops under a threshold:
#!/bin/sh
# ~/bin/lysosome-hourly: regenerable items only, files go to the Trash
lysosome clean ~/code --tier regenerable --yes --progress off
free_gb=$(df -k / | awk 'NR==2 {print int($4 * 1024 / 1e9)}')
[ "$free_gb" -lt 50 ] && osascript -e "display notification \"$free_gb GB free\" with title \"Disk low\""
Maintained versions, with a Claude Code SessionEnd hook that removes a session's worktree when its PR merges, a Claude Code plugin, a launchd agent, a systemd timer and MCP configs for Claude Code, Claude Desktop, Cursor and Codex, are in the open-source lysosome integrations folder. Each has a README you can follow in two minutes.
4. Look outside your code folder once a month. On this Mac, 132 GB sat in ~/Library and Docker's disk image. The Docker guide covers shrinking Docker.raw; the worktree guide covers the rest of the worktree checks.
The takeaway
If agents make your worktrees, an idle threshold will never catch up with them: 494 appeared in two days, and none was 30 days old. Tie removal to the merge. On this Mac, the 30-day rule covered none of the 217 finished worktrees (84.8 GB); the merge rule, run on a schedule, removed the finished ones, and the next day's scan found 1 left. Then strip the build output from the ones still in progress, because that's where the space goes next.