Repository navigation
Description
Symptom
Sub-agents dispatched via the Agent tool with isolation: "worktree" create entries under .claude/worktrees/agent-<id>/. These persist after the agent completes — even after the parent session ends. On this box right now:
$ ls .claude/worktrees/ | wc -l
11
$ du -sh .claude/worktrees/*
725M agent-a0642b45b7ba24e53 ← oldest, has full .build/ from earlier compile
210M agent-a3516f3441272bcfa
100M agent-a5688bbaa60a0823f
99M agent-a56f5e7b9804459b4
97M agent-a6da1f416a75ae8d2
101M agent-a7a1f02405fd51d16
100M agent-a858bf3aff0093294
97M agent-ac070d999cd8797dc
99M agent-ad76f9da427453c17
100M agent-aec6126b9d2c0672a
97M pr-2600-extend
# total ≈ 1.8 GB
All 11 worktrees are from already-completed sub-agents whose PRs have merged.
Worse: git worktree remove --force fails on Windows
$ git worktree remove --force .claude/worktrees/agent-a0642b45b7ba24e53
error: failed to delete 'C:/Users/niteris/dev/fastled10/.claude/worktrees/agent-a0642b45b7ba24e53': Invalid argument
Repeated for all 10 agent worktrees. git worktree prune quietly removes the admin entries (so git worktree list shows only the main checkout), but the on-disk directories stay full. So:
- Subsequent
git worktree addto the same path succeeds (admin entry was pruned). - But the old files remain — adding the same path doesn't clobber them, they just shadow the new clone.
- Manual
rm -rf .claude/worktrees/*works but is destructive and easy to forget.
The "Invalid argument" comes from Windows refusing to delete read-only files inside the worktree (git itself marks some files read-only). On POSIX this would just succeed.
Why it's a problem
- Disk usage: 1.8 GB of dead clones on a single-developer box after one afternoon of agent work.
bash test --cppconfusion: stale.build/directories inside the worktrees can confuse global tooling that walks parent dirs (the FastLED hooks already have a guard against this — see the longpyproject.tomlwalk-up logic in.claude/settings.json).- Hook collisions: the stale worktrees each have their own
pyproject.toml. If a shell's$PWDis ever inside one of them, the FastLED hook walk-up chain finds the wrongpyproject.tomlfirst — chained me into a tool-call deadlock earlier today (the hook tries to exec a script that lives in FastLED but not in the worktree, exits non-zero, blocks every tool call until someone moves the offending worktree out of band).
Asks
- Auto-cleanup on agent completion. When a sub-agent exits and its worktree has no uncommitted changes / no unpushed commits, the harness should
git worktree remove --force+ delete the dir. (Best done from outside the agent so Windows-locked git internals are unlocked.) bash clean-worktreeshelper script. Add a smallci/clean-worktrees.pythat lists.claude/worktrees/*, identifies safe-to-delete (no unpushed commits), and removes them withchmod -R u+wfirst to bypass the Windows read-only attribute issue.- Stop-hook nudge when worktree count > 5. A one-line warning in the Stop hook's stderr ("Note: 11 stale agent worktrees totaling 1.8 GB in .claude/worktrees/, run
bash clean-worktrees") would surface the accumulation early.
Workaround used today
mv .claude/worktrees ~/.clud/trash/worktrees-$(date +%s) then rm -rf from outside the FastLED tree (so the FastLED hooks don't fire while the dirs are being deleted).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
chore(ci): add bash clean-worktrees helper + stop-hook nudge (#2796)