# garbage-collect: what each phase covers

Read by `/multi-agent:garbage-collect` when a phase's output needs explaining,
or before answering what a sweep would or would not touch.

## /tmp scratch (phase 1)

Each run writes ephemeral scratch under `/tmp`: picker state
(`multi-agent-picker-*`), PR/review diffs (`multi-agent-review-*`), issue
progress bodies (`issue-progress-*`), channel payloads (`channels-*`),
external-context bundles (`context-links-*` / `context-by-type-*`), and analysis
drafts (`analysis-*`). Most are never cleaned up, so a long-lived machine
accumulates them. `gc-tmp.sh` sweeps the known prefixes only.

## Worktree residue (phase 2)

`gc-worktrees.sh` covers: stale worktree admin entries (`git worktree prune`,
applied immediately - it only drops bookkeeping for dirs that are already gone),
orphan `.worktrees/*` dirs no longer registered as worktrees, gitlink residue in
the index (`.worktrees/{id}` recorded as a "Subproject commit" entry by a
pre-guard `git add -A`), and a missing `.worktrees/` line in
`.git/info/exclude`.

## Offloaded payloads (phase 3)

`offload-ref.sh` parks build logs, diffs and test output under
`.multi-agent/refs/<node_id>.md`. In worktree modes that directory dies with the
worktree; when the run chose a local workspace it lands in the real checkout,
and because it is gitignored it never shows up in `git status`. Only files
matching the node-id shape are touched - anything else in that directory is left
alone, and files inside the grace window are spared so a sweep cannot pull a ref
out from under a running phase.

## Abandoned runs (phase 4, `--abandoned`)

Nothing else collects these. `gc-worktrees.sh` skips registered worktrees on
purpose, because a registered worktree belongs to a live run - and a run that
stopped never stops being "live".

The output has six kinds of line and they are not interchangeable:

| Line | Meaning |
|---|---|
| `would remove` / `would remove worktree` | reapable: stopped past its age limit, or finished with its worktree left behind |
| `would stash` | uncommitted work found - it goes into a stash entry labelled `autopilot/abandoned/<task-id>` (`git stash list`) and the worktree is **kept** |
| `would mark abandoned` | the state is stale but there is no worktree left to remove |
| `would mark abandoned and keep the worktree` / `would keep worktree` (unattended run, not inspected with git); after `--yes`, `kept-unattended` in the report | the worktree belongs to an unattended run: its state is below the unattended run root, the runner handed it over with `--unattended`, or the runner's ledger names it. The run could write that worktree and the git config read there, and `status` and `stash` would run the filters that config names, so no git runs inside it. Whether it holds uncommitted work cannot be asked, so it is **kept**; a stopped run is still marked abandoned |
| `refused` | the run's state directory holds a link, or the state resolves outside the run root. The run is left exactly as it is, the line names the reason, and the summary counts it |
| `unattributed` | a worktree with no run state. **Never removed, whatever the flags.** `.worktrees/` is not exclusively ours, and nothing distinguishes a hand-made worktree from a pipeline one whose log was pruned. Report the list and its size; the user decides |

A path outside `<repo>/.worktrees/` is never removed because a state file can
record the repo root as its `worktreePath`.

## Scope of each phase

The /tmp phase only touches `/tmp` scratch; the worktree phase only touches the
current repo's `.worktrees/` residue and index; the payload phase only touches
node-id files under `.multi-agent/refs/`. Persistent per-task logs live under
`~/.claude/logs/multi-agent/` and are cleaned by `/multi-agent:prune-logs`. The
per-repo memory stores under `~/.claude/memory/multi-agent/` are durable
knowledge, not scratch: they are removed only by `/multi-agent:uninstall
--all-data`, or one entry at a time with `learnings-ledger.mjs forget`.
