lysosome

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:

What changed in lysosome

Every item on that list is fixed in lysosome 0.5.1, out on November 30. From its changelog:

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.