worktree-target-dirs

Worktree target/ dirs accumulate to hundreds of GB — agents don't clean their build artifacts when their worktree is reaped

Metadata

Statusdone
Assignedagent-816
Agent identity3184716484e6f0ea08bb13539daf07686ee79d440505f1fdf2de0357707034c3
Created2026-04-27T17:19:11.063169401+00:00
Started2026-04-27T18:40:55.603574902+00:00
Completed2026-04-27T19:12:08.128618635+00:00
Tagseval-scheduled
Tokens9068831 in / 38150 out

Description

Description

Today (2026-04-27) the disk hit 100% full (16K free on a 1.8T disk) primarily because .wg-worktrees had grown to 504G — 31 agent worktrees each carrying their own ~16G target/ dir.

Of the 31 worktrees, only 3 had live agent processes running (agent-700, agent-742, agent-765). The other 28 had completed/abandoned long ago but their target/ dirs persisted indefinitely.

After manually rm -rfing target/ in the 28 dead worktrees, disk went from 100% → 71% used (518G free).

This is going to keep happening. Every agent that builds the project leaves ~16G behind unless someone manually reaps it.

Required

Build a reaper that runs automatically. Pick whichever fits the existing service architecture:

A. On agent completion — when an agent's task transitions to done/failed/abandoned, the dispatcher (or a finalizer hook) rm -rfs its worktree's target/ before any worktree cleanup. The build artifacts are not needed once the work is committed/merged.

B. Periodic sweepwg sweep (which already exists per memory: 'Sweep command + reconciliation') gains a --reap-targets flag that finds worktrees whose owning agent has been dead > N hours and removes their target/ dirs. Wire this into the periodic-cycle pattern (per memory feedback_periodic_work_via_graph_cycles).

C. Disk pressure trigger — when free disk drops below a threshold, run option B aggressively.

Recommend A + B: clean on completion (most builds get reaped right when the agent finishes), with a periodic safety net for cases where A failed (crash, kill -9, etc).

Files likely to touch

  • src/commands/sweep.rs (or wherever wg sweep lives) — add --reap-targets and the worktree-walk logic
  • src/commands/service/ — agent finalization hook to reap target/ on agent end
  • Worktree management code (where .wg-worktrees/agent-N is created/destroyed)

Validation

  • Failing test first: spawn a fake agent that creates a fake target/ in its worktree, mark agent as done, then assert the target/ was removed
  • Failing test for the periodic sweep: leave a worktree with stale target/ and an agent that's been dead > N hours, run sweep, assert removal
  • Live agents' target/ dirs MUST NOT be touched — test that with a 'live' agent (heartbeat fresh) the reaper skips it
  • cargo build + cargo test pass with no regressions
  • Manual: trigger an agent run, let it complete, observe its worktree's target/ is gone afterward; verify by du -sh .wg-worktrees/agent-N showing only source-tree size, not GB-of-build-artifacts size
  • Document the reaper behavior in the operations docs so users understand it's automatic

Depends on

Required by

Log