# Template: clean up after merge

Copy, fill the `<...>` fields, and paste as the task prompt. Only run AFTER a
real merge is confirmed on remote master.

```text
Task: clean up branch/worktree for PR #<pr> / issue #<n> after merge.

Rules (llm-project-workflow):
- Do NOT clean up until the merge is confirmed on <remote>/master.
- Cleanup would-delete-unmerged-work → STOP. Never --force-remove a dirty tree.

Steps:
1. git fetch <remote> --prune
2. Confirm <remote>/master contains the merge of PR #<pr>
   (git log <remote>/master | grep the merge, or git branch -r --contains <sha>).
   If not merged → STOP; run the recovery template instead.
3. Close issue #<n> if not auto-closed; remove the status:in-progress label.
4. scripts/worktree-clean --delete-branch <type>/issue-<n>-<slug>
   (removes branches/<type>-issue-<n>-<slug>; refuses if dirty; git branch -d is
   safe-delete only — fails on unmerged.)
5. Delete the remote branch if the merge did not already remove it.
6. From the main checkout: git fetch <remote> --prune; git checkout master;
   git reset --hard <remote>/master ONLY if local master safely matches remote.
7. Confirm main checkout clean and current (git status; 0 0 vs <remote>/master).

Handoff: merge confirmed, issue closed, branch+worktree removed, checkout clean.
```

## Branches cleanup audit integrity (#404)

Any bulk or multi-path cleanup under `branches/` must capture auditable before/after
identity for every initial directory and registered worktree. Use
`worktree_cleanup_audit.capture_cleanup_snapshot` before and after cleanup, record
every intentional removal in a removal log (path, method, order, timestamp,
pre-removal proof), then run `reconcile_cleanup_audit` and
`assess_cleanup_audit_integrity`.

The cleanup report must include a reconciliation table:

* initial count
* removed count
* preserved count
* missing-unexplained count
* final count

Fail closed when:

* a preserved (active PR, dirty, claim/lease, or unsafe) worktree disappears without
  a removal log entry or explicit explanation
* the removal log omits a removed clean-stale path
* final counts do not reconcile with initial minus removed

If another session removes or mutates a worktree during cleanup, record the path
under explained missing entries — never treat silent disappearance as success.

## Bulk `branches/` cleanup audit (#404)

Before removing multiple session-owned worktrees:

1. Call `gitea_capture_branches_worktree_snapshot` and record the before snapshot.
2. Remove only paths classified as `clean_stale_removable` with explicit per-path proof.
3. Log every removal with path, method, and timestamp/order.
4. Capture an after snapshot with the same tool.
5. Call `gitea_assess_worktree_cleanup_integrity` with before, after, and the removal log.
6. Fail closed when any protected path (active PR, dirty, claim/lease) disappears
   without an explained state transition.
7. Final report must include the reconciliation table and `git worktree list` proof.
