Skip to content

agents: .claude/worktrees/agent-* accumulate (11 dirs, 1.8GB after one session) — git worktree remove fails on Windows #2610

Closed

Description

@zackees

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 add to 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

  1. Disk usage: 1.8 GB of dead clones on a single-developer box after one afternoon of agent work.
  2. bash test --cpp confusion: 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 long pyproject.toml walk-up logic in .claude/settings.json).
  3. Hook collisions: the stale worktrees each have their own pyproject.toml. If a shell's $PWD is ever inside one of them, the FastLED hook walk-up chain finds the wrong pyproject.toml first — 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

  1. 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.)
  2. bash clean-worktrees helper script. Add a small ci/clean-worktrees.py that lists .claude/worktrees/*, identifies safe-to-delete (no unpushed commits), and removes them with chmod -R u+w first to bypass the Windows read-only attribute issue.
  3. 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).

Activity

  1. added a commit that references this issue on Jun 5, 2026
    e5437ef
  2. moved this from Triage to Done in FastLED Trackeron Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions