Skip to content
Back to skills

Wrap

ASecurity

End-of-session command — verify no unresolved findings, squash merge, sync main, detect per-PR follow-ups, run a full-session loose-ends sweep, and extract lessons. Nearly silent by default — a clean merge emits one line (`merged PR #N`), plus one close-hint line when `thread-boundary.sh` prints one, plus any items needing your decision; pass `--verbose` (or just ask) for the merged + follow-ups summary and the full per-phase report. Accepts an optional PR reference (`/wrap <URL>`, `/wrap #N`...

  • 5 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 3, 2026
developmentrustgoshellbashgitapi

Works with

  • cursor
  • terminal
  • cli
  • api

Security analysis

A100/100

Pro scans all 4 files and shows the line behind each finding

Scanned December 1, 2026

npx -y skills add auerbachb/claude-code-config --skill wrap --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Wrap?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Wrap
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/auerbachb-wrap/badge)](https://www.skillsdirectory.com/skills/auerbachb-wrap)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: wrap
description: End-of-session command — verify no unresolved findings, squash merge, sync main, detect per-PR follow-ups, run a full-session loose-ends sweep, and extract lessons. Nearly silent by default — a clean merge emits one line (`merged PR #N`), plus one close-hint line when `thread-boundary.sh` prints one, plus any items needing your decision; pass `--verbose` (or just ask) for the merged + follow-ups summary and the full per-phase report. Accepts an optional PR reference (`/wrap <URL>`, `/wrap #N`, `/wrap N`); with no argument it infers the PR from the current branch, then thread context, then session-state.
argument-hint: "[URL | #N | N] [--verbose]"
---

> **Human output:** Follow `.claude/reference/decision-only-output.md` over older heartbeat, timestamp, mandatory-table, startup/teardown, or report examples below. Routine checks, dispatches and automatic recovery stay silent; immediately surface owner-action blockers, safety events or exhausted recovery. Explicit status/verbose requests retain tables and reports. Preserve every machine footer, full diagnostic log, gate result and resumable state; use bounded JSON projections before targeted evidence recovery.

Wrap up the current PR and session. This is the "we're done here" command that handles final verification through merge, root-main sync, follow-up detection, a full-session sweep for loose ends, and lessons.

`/wrap` accepts an **optional** PR reference as its argument — a full URL, `#N`, `owner/repo#N`, or a bare number `N`. When invoked with no argument it resolves the target PR through the inference cascade in Step 1.1 (current branch → thread context → session-state). An explicit argument bypasses all inference.

`/wrap` does **not** delete the running worktree or its branch — leaving the thread alive so it can keep working. Stale worktrees and stale local/remote branches are reaped out-of-band by `/pm-clean`, which calls `stale-cleanup.sh`.

## When to use /wrap vs /merge

- Use **/wrap** at end-of-session. Handles merge + root-main sync + follow-up detection + lessons.
- Use **/merge** for a quick mid-session merge when you'll keep working. Skips follow-up detection and lessons.
- /wrap includes everything /merge does, plus follow-ups and lessons. Don't run both.
- Phase C invokes this skill after gate and AC verification; keep merge, main-sync, follow-up, and cleanup behavior here so Phase C and `/wrap` cannot drift.
- **Target PR:** `/wrap` operates on the PR resolved in Step 1.1. Pass an explicit reference (`/wrap <URL>`, `/wrap #N`, `/wrap N`) to wrap a PR that is **not** on the current branch — common in orchestration threads that ran `/fixpr <URL>` against another worktree. With no argument, Step 1.1's cascade infers the PR.
- **Callers of the `/wrap #N` form:** Phase C (`phase-c-merger`), `/pr-monitor-and-manage` (merge-ready fleet PRs), and **`/pm`'s one-shot forgotten-PR triage** (issue #657) all merge an already-open PR by dispatching this workflow rather than reimplementing merge logic — the merge gate, AC verification, and squash-merge live here so every caller stays consistent.

## Execution Model

`/wrap` is a set-and-forget command. Once invoked, it runs all 4 phases end-to-end without mid-run confirmation prompts. It stops early only for explicit stop conditions (for example: no PR on current branch, **human** change requests on HEAD, recovery iteration cap, failed merge gate after recovery, **`/fixpr` delegation failure**, AC verification failure).

> **Always:** Execute all phases end-to-end; proceed immediately between phases when no blocker exists.
> **Ask first:** Never — all phases are autonomous once /wrap is invoked.
> **Never:** Stop to ask "should I continue?" between phases; insert confirmation prompts for non-blocker transitions. Delete the running worktree or its branch — that's `/pm-clean`'s job, not /wrap's. Modify branch protection — suggest `/admin-merge` for any enforcement_admins bypass; `admin-merge.sh --auto-plain` (no protection change) runs automatically per #754.

### Follow-up filing is autonomous (issue #633)

Both Phase 3 passes — **Part A** (per-PR follow-ups) and **Part B** (full-session sweep) — file every novel candidate as a GitHub issue **without asking**. There is no opt-out flag and no "file as new issue?" prompt anywhere in this skill. The deal is two-sided: filing is unconditional, and **every** filed issue is durably **recorded** — the GitHub issue itself, the run-scoped `WRAP_FILED_ISSUES` registry, and `session-state.json` `wrap_sweep.filed_issues` (Step 3.13). **Recording is not narrating (issue #851):** filings do *not* print on the default path. They are listed with number, title, one-line rationale, and clickable link under `--verbose` or on request, and any filing that needs a decision — a failed `gh issue create`, or one carrying a `Possibly duplicates #{N}` caveat — surfaces immediately in either mode. Retraction (`gh issue close`) is the escape hatch, not a confirmation round-trip. The norm is stated once for all skills in `.claude/rules/issue-planning.md`.

Autonomy does not mean filing blind: every candidate goes through the body-aware duplicate check in **Step 3.0** first (issue #652). That check can only ever *redirect* a filing onto an existing issue or *annotate* it — it never drops a finding, and every filing it suppresses is named in the `--verbose` report alongside the issue it deferred to.

### Wrap-Internal Phase Transitions

> These transitions are wrap's internal Phase 1–4 system, distinct from the subagent A/B/C phases in `subagent-orchestration.md` — the two naming schemes are parallel-but-separate.

| Transition | Action | Classification |
|------------|--------|----------------|
| Phase 1 complete (findings scan finished — may have flagged bot findings) | Begin Phase 2 recovery + merge | **Always do** |
| Unresolved review threads detected (Phase 1 scan) | Record `WRAP_UNRESOLVED_THREADS`; proceed to Phase 2 Branch B, which auto-invokes `/fixpr` when threads are the sole gate blocker (issue #455) | **Auto-recover** |
| Unresolved review threads only (Phase 2.1 — `merge-gate.sh` `missing` contains only `unresolved review thread(s)` entries) | Auto-invoke `/fixpr` (Branch B), then re-fetch HEAD + re-run `merge-gate.sh` | **Auto-recover** |
| Phase 2 recovery loop exits cleanly (gate met, ready to merge) | AC check → squash merge → Phase 3 | **Always do** |
| Phase 3 per-PR follow-ups + full-session sweep processed | Begin Phase 4 | **Always do** |
| Phase 4 lessons complete (or skipped as trivial) | Output final report | **Always do** |
| Human `CHANGES_REQUESTED` on current HEAD (`human_changes_requested` non-empty in `merge-gate.sh` JSON) | Stop with reviewer names — **never** auto-dismiss | **Genuine block** |
| Recovery iteration cap or non-recoverable gate failure | Stop with full audit + last `missing` | **Stop and report** |
| AC checkbox verification fails (Phase 2.2) | Stop and report | **Stop and report** |

> **Anti-pattern:** If you find yourself composing "Should I proceed?" or presenting a confirmation button, the answer is always yes — execute immediately.

> **"Threads only" defined (issue #455):** the unresolved-review-threads auto-recovery rows above apply when unresolved review threads are the **sole** blocker — i.e. no other blocker category is present (no CI failing/incomplete, no `BEHIND`/`DIRTY`/`CONFLICTING` merge state, no stale or human `CHANGES_REQUESTED`, no missing fresh bot `APPROVED`). When any of those co-occur, the broader Step 2.1 decision tree (issue #452) owns dispatch — first matching branch wins. The single-attempt / stop-on-mixed-blocker semantics from #455 are realized here as the threads-only branch of the bounded #452 loop.

## Output modes

`/wrap` is **nearly silent by default** (issue #851, #869): it runs all four phases in full, emits exactly `merged PR #N` on a clean merge, plus one close-hint line when `thread-boundary.sh` prints one, and otherwise says nothing — only items that need an explicit decision from you. Pass `--verbose`, or simply ask ("what did you just do?", "summarize"), for the merged + follow-ups summary, the per-phase narration, and the detailed final report. This implements `CLAUDE.md` #3 for this skill.

| Mode | Flag | Effect |
|------|------|--------|
| **Silent** | *(default)* | One line on a clean run: `merged PR #N`, then the close-hint line when Step 4.3's script prints one — no follow-up list, no lessons ack, no sweep summary. Only decision-requiring items print, one terse line each (Step 4.3 **Silent default**). |
| **Verbose** | `--verbose` *(or an explicit request)* | The full report: the `## Wrapped up` merged + follow-ups block, per-cycle recovery heartbeats, the Session Lessons block, and the multi-section "Wrap-Up Complete" report (Issues filed, Filings suppressed as duplicates, Session sweep, Verdict, Lessons). Full template: `references/wrap-report-templates.md`. |

**Verbosity is additive and human-facing only.** Every phase executes **identically** in both modes; only narration differs. Suppression is never deletion — the merge, the filings, the sweep, and the lessons are all recorded (GitHub, `WRAP_FILED_ISSUES`, `session-state.json` `wrap_sweep`, memory), so an explicit request re-renders the verbose report in full from that state. Required notices below follow `decision-only-output.md`; routine coverage and heartbeat details remain in records:

- **State-changing blockers and stop conditions** — a merge that can't proceed, `CONFLICTING`, human `CHANGES_REQUESTED`, no PR found, the recovery cap — surface as a short one-line reason even on the silent default (Step 4.3 **Blocker path**).
- **The `[INFERRED]` merge-safety checkpoint** (Step 1.1). Prints only on the inference path, never on the normal branch path.
- **Genuine stalls or exhausted recovery** surface immediately. Routine `/fixpr` wait ticks remain silent; the bgwork ceiling and recorded tick state provide liveness.
- **Local review coverage** (`both`, `cr-only`, `codeant-only`, `none`) is recorded in the PR and recovery state; notify only if it requires owner action.

When `/wrap` is invoked by a phase-C subagent, the machine **`EXIT_REPORT`** block (per `phase-protocols.md`) is emitted **identically regardless of verbosity**.

## Preamble: shared helpers

Define once, use throughout all phases. The `resolve_script()` function finds the first executable candidate across the three standard install locations:

```bash
resolve_script() {
  local name="$1" candidate
  for candidate in \
    "${HOME:+$HOME/.claude/skills-worktree/.claude/scripts/$name}" \
    "${HOME:+$HOME/.claude/scripts/$name}" \
    ".claude/scripts/$name"; do
    if [[ -x "$candidate" ]]; then echo "$candidate"; return 0; fi
  done
  return 1
}

INFER_PR=$(resolve_script infer-pr.sh || true)
DISMISS=$(resolve_script dismiss-stale-bot-changes.sh || true)
PR_STATE_SH=$(resolve_script pr-state.sh || true)
MERGE_GATE_SH=$(resolve_script merge-gate.sh || true)
RECOVERY_EVIDENCE_SH=$(resolve_script recovery-evidence.sh || true)
AC_CHECKBOXES_SH=$(resolve_script ac-checkboxes.sh || true)
WRAP_AUDIT_STATE_SH=$(resolve_script session-state.sh || true)
CI_STATUS_SH=$(resolve_script ci-status.sh || true)
RELEASE_DECIDE=$(resolve_script release-decide.sh || true)
RELEASE_SWEEP=$(resolve_script release-sweep.sh || true)
PR_ISSUE_REF_SH=$(resolve_script pr-issue-ref.sh || true)
ISSUE_COMPLETION_SH=$(resolve_script issue-completion.sh || true)
ISSUE_CLAIM_SH=$(resolve_script issue-claim.sh || true)
POST_MERGE_BOOKKEEPING_SH=$(resolve_script post-merge-bookkeeping.sh || true)
SPEND_TELEMETRY_LINK=$(resolve_script spend-telemetry-link.sh || true)
REPO_ROOT_SH=$(resolve_script repo-root.sh || true)
PAID_GRANT_SH=$(resolve_script paid-review-grant.sh || true)  # optional: owner-approved paid reviews (#1898)
DIRTY_MAIN_GUARD_SH=$(resolve_script dirty-main-guard.sh || true)
MAIN_SYNC_SH=$(resolve_script main-sync.sh || true)
MERGE_TARGET_ROOT_SH=$(resolve_script merge-target-root.sh || true)
BOUND_MAIN_SYNC_SH=$(resolve_script bound-main-sync.sh || true)
HHG_STATE_SH=$(resolve_script hhg-state.sh || true)
CYCLE_COUNT_SH=$(resolve_script cycle-count.sh || true)
PR_AUTHORSHIP_SH=$(resolve_script pr-authorship.sh || true)
CR_HOURLY_SH=$(resolve_script cr-review-hourly.sh || true)

[[ -n "$PR_STATE_SH" ]] || { echo "ERROR: pr-state.sh not found (checked all three paths) — wrap PR state unavailable" >&2; exit 1; }
[[ -n "$MERGE_GATE_SH" ]] || { echo "ERROR: merge-gate.sh not found (checked all three paths) — wrap merge gate unavailable" >&2; exit 1; }
[[ -n "$RECOVERY_EVIDENCE_SH" ]] || { echo "ERROR: recovery-evidence.sh not found (checked all three paths) — recovery capture unavailable; no gate accepted" >&2; exit 1; }
[[ -n "$AC_CHECKBOXES_SH" ]] || { echo "ERROR: ac-checkboxes.sh not found (checked all three paths) — acceptance verification unavailable" >&2; exit 1; }
[[ -n "$CI_STATUS_SH" ]] || { echo "ERROR: ci-status.sh not found (checked all three paths) — CI diagnosis unavailable" >&2; exit 1; }
[[ -n "$PR_AUTHORSHIP_SH" ]] || { echo "ERROR: pr-authorship.sh not found (checked all three paths) — wrap authorship gate unavailable" >&2; exit 1; }
```

These are resolved before any phase work. `INFER_PR` is used in Step 1.1; `DISMISS` is used in Step 2.1 Branch A. `SESSION_STATE_SH` and `ISSUE_DEDUP` are resolved later in the steps that first need them (Steps 3.5 and 3.0 respectively).

## Phase 1: Pre-Merge Verification — Check for Unresolved Findings

Before merging, verify that all reviewer feedback has been addressed.

### Step 1.1: Identify the PR

**Parse flags first (before any PR resolution).** Scan `$ARGUMENTS` for `--verbose` (anywhere, any order), set `WRAP_VERBOSE`, and **strip it** so only the PR reference remains:

```bash
WRAP_VERBOSE=0
REST=""
for tok in ${ARGUMENTS:-}; do
  if [ "$tok" = "--verbose" ]; then WRAP_VERBOSE=1; else REST="${REST:+$REST }$tok"; fi
done
ARGUMENTS="$REST"
```

**Cascade summary** (first match wins — later sub-steps skipped):

1. **1.1a** Explicit argument — if `$ARGUMENTS` non-empty, resolve via `"$INFER_PR" --explicit "$ARGUMENTS"`; stop if helper missing or reference unparseable. Never fall through to 1.1b.
2. **1.1b** Current branch — `gh pr view` finds a PR; use it (non-inferred, no `[INFERRED]` line). Skip 1.1c–1.1e.
3. **1.1c** Thread context scan (AI judgment) — most recent `/fixpr`/`/wrap` invocations and explicit PR refs in this conversation.
4. **1.1d** Session-state — `"$INFER_PR" --root-repo "$ROOT_TOPLEVEL"`; capture real exit code (do NOT `|| true`).
5. **1.1e** Merge, deduplicate, resolve — single / unambiguous / ambiguous / no-candidates. Stop with `Multiple PRs in scope — please specify: /wrap <N>` on tie.
6. **1.1f** No-candidates stop — non-coding thread vs. lookup-failed (AI judgment; bias toward lookup-failed when uncertain).

Full sub-step bash, the `[INFERRED]` line format, the repo-scoping guard, and the authorship guard: **`references/wrap-pr-inference.md`**.

**Merge-safety:** `/wrap` implicitly authorizes the squash merge — an incorrect inference merges the **wrong** PR. Only auto-proceed when the target is unambiguous. The `[INFERRED]` line (emitted before any Phase 1 verification work) is the user's only catch point.

Once `PR_NUM` is fixed, fetch PR details:

```bash
gh pr view "$PR_NUM" --json number,title,headRefName,body,state \
  --jq '{number, title, headRefName, body, state}'
```

If already merged, use the Phase 3 merged entry below; it reconciles issue completion before bookkeeping. If closed without a merge, report that state and stop.

### Step 1.2: Scan for unresolved review findings

> **pr-state.sh first (NON-NEGOTIABLE):** Before calling `gh api .../pulls/{N}/reviews`, `pulls/{N}/comments`, or `issues/{N}/comments` directly, call `pr-state.sh --pr N` first and read bounded `jq` projections of the cached JSON bundle; select specific finding IDs/bodies only for diagnosis or replies. All review-state queries in this skill read from the `$BUNDLE` returned by `pr-state.sh` — do not add inline `gh api` calls to these three endpoints.

Use the shared `pr-state.sh` helper to fetch and pre-classify review activity from all three endpoints in one call. It filters to the four active review bots — `coderabbitai[bot]`, `cursor[bot]` (BugBot), `codeant-ai[bot]`, and `greptile-apps[bot]` — and tags each comment with `classification.class` (`finding` vs `acknowledgment`). The helper writes the JSON bundle to a tempfile and prints its **path** on stdout — capture the path, then read with `jq < "$BUNDLE"`:

```bash
PR_CREATED=$(gh pr view "$PR_NUM" --json createdAt --jq '.createdAt')
BUNDLE=$("$PR_STATE_SH" --pr "$PR_NUM" --since "$PR_CREATED")
```

Read the findings across all three endpoints with a single jq pass:

```bash
jq '[.new_since_baseline.reviews[], .new_since_baseline.inline[], .new_since_baseline.conversation[]]
    | map(select(.classification.class == "finding"))' < "$BUNDLE"
```

For each finding:

1. Check if there is a reply confirming the fix
2. Check if the code at the referenced location has been updated since the comment
3. Check if the thread is resolved/outdated

**Do not stop here.** Record whether any items remain classified as `finding` as **`WRAP_PHASE1_FINDINGS`** — count + short list. Unresolved bot findings are a **trigger** for Phase 2's `/fixpr` delegation path, not a hard stop.

**Unmarked verdict replies (advisory — issue #1842).** Read the bundle's top-level count and print one line only when it is non-zero:

```bash
UNMARKED=$(jq -r '.unmarked_replies // 0' < "$BUNDLE")
if [[ "$UNMARKED" =~ ^[0-9]+$ ]] && (( UNMARKED > 0 )); then
  echo "Advisory: $UNMARKED User-account repl(y/ies) to review-bot threads carry no review-verdict marker (reply-thread.sh --verdict/--defect)"
fi
```

It counts User-account replies to review-bot threads that skipped the mandatory `--verdict`/`--defect` flags, so the review-cost ledger cannot score them (`.claude/reference/review-stack-audit.md` §The marker). **Information only:** it never joins `WRAP_PHASE1_FINDINGS`, never changes an unresolved-thread count, never dispatches `/fixpr`, and never changes the merge-gate outcome or `/wrap`'s exit status.

**Unresolved-threads detection (issue #455):**

```bash
WRAP_UNRESOLVED_THREADS=$(jq -r '.threads.unresolved_count // 0' < "$BUNDLE")
```

If `> 0`, record it. In `--verbose` mode also emit a timestamped detection heartbeat. Do **not** assert threads are the sole blocker — that is Branch B's call once the gate's full `missing` set is known. Do **not** invoke `/fixpr` from Phase 1 — the single delegation point is Step 2.1 Branch B.

Proceed immediately to Phase 2 — do not ask.

### Bind the physical clone before merge work

Use the absolute invocation/worktree path from this chat's workspace context, an explicit caller path, or a validated delegated handoff that matches this PR, repository, HEAD and holder. That path is `<WRAP_SOURCE_PATH>`; never recover it from a later `pwd` or a shared per-repository/per-PR `.root_repo` hint. A remote identity cannot distinguish two clones of the same repository. `<PR_OWNER>`, `<PR_NAME>` and `<PR_NUMBER>` are the identified PR's own repository and number (`CURRENT_REPO`, `PR_NUM`), never a later current-branch lookup.

**The binding is one plain command with every value typed on it** (issue #2496). Each Bash tool call starts a fresh shell, so a variable set by an earlier call is empty here. A worktree-isolated agent is refused a multi-line block with `$(…)`, and a command whose text holds a `github` URL, so neither appears below. Replace each `<…>` with its literal value before running. `<BOUND_MAIN_SYNC_SH>` is the absolute path `resolve_script bound-main-sync.sh` prints: the first executable of `$HOME/.claude/skills-worktree/.claude/scripts/bound-main-sync.sh`, `$HOME/.claude/scripts/bound-main-sync.sh`, `.claude/scripts/bound-main-sync.sh`.

<!-- test-anchor: wrap-merge-root-binding -->

```bash
"<BOUND_MAIN_SYNC_SH>" --print-root --repo-path "<WRAP_SOURCE_PATH>" --pr-owner "<PR_OWNER>" --pr-name "<PR_NAME>" --pr-number "<PR_NUMBER>"
```

It prints `WRAP_BOUND_ROOT=<physical root>` and `WRAP_ROOT_BINDING_STATUS=bound` (exit 0), or an empty `WRAP_BOUND_ROOT=` and `WRAP_ROOT_BINDING_STATUS=failed: …` (exit 1). It mutates nothing. When no `bound-main-sync.sh` resolves, do not run it; record this instead:

<!-- test-anchor: wrap-merge-root-binding-unavailable -->

```bash
printf '%s\n' 'WRAP_BOUND_ROOT=' 'WRAP_ROOT_BINDING_STATUS=failed: explicit clone binding unavailable'
```

Record the printed `WRAP_BOUND_ROOT` value, empty or not, and carry it unchanged across later tool calls and delegated `/fixpr` work; persist it with `<WRAP_SOURCE_PATH>`, `<PR_OWNER>`, `<PR_NAME>` and `<PR_NUMBER>` in the validated run handoff when handing off this invocation. On resume without that binding, keep local mutations skipped. Never repopulate it from whatever clone the tool happens to enter. Binding failure does not undo or bypass the remote merge gate; it makes post-merge local cleanup/sync report a failure without mutating a guessed clone.

## Phase 2: Merge

### Step 2.1: Merge gate + autonomous recovery loop (issue #452)

**Authority:** `"$MERGE_GATE_SH"` JSON on stdout is the single source of truth for merge readiness. After **every** recovery action, re-fetch PR HEAD SHA and re-run `merge-gate.sh` — **no stale cache**.

```bash
WRAP_RECOVERY_MAX_ITERATIONS="${WRAP_RECOVERY_MAX_ITERATIONS:-5}"
```

**Polling ownership (issue #454):** `/wrap` has NO polling cadence — all waiting happens inside `/fixpr`'s Step 4d review-wait loop. `/wrap` delegates, trusts the returned verdict, and re-runs `merge-gate.sh` immediately.

**Per-iteration heartbeat** (verbose mode only):

```bash
if [ "$WRAP_VERBOSE" = "1" ]; then
  TS=$(TZ='America/New_York' date +'%a %b %-d %I:%M %p ET')
  echo "[$TS] /wrap recovery cycle $i/$WRAP_RECOVERY_MAX_ITERATIONS — gate check"
fi
```

**The silent default preserves required signals:** owner-action blockers, safety events and exhausted recovery surface immediately; a clean merge emits `merged PR #N`, then the close-hint line when one is due. Dispatch transitions and ordinary waits remain recorded and silent.

After each action, append to **`WRAP_RECOVERY_AUDIT`**: cycle number, blocker summary, action taken, result. Then run the write below before continuing or exiting, including cap/blocker exits. It saves the complete current-run audit and last gate JSON under `.prs["<PR>"].wrap_sweep.recovery_audit`; Step 3.13's sibling-field writes preserve it. The explicit `CURRENT_REPO` from Step 1.1's repo-scoping guard overrides an inherited `CLAUDE_SESSION_REPO`; never fall back to that ambient variable for this write. Later status/verbose requests read the target repo's record rather than relying on a child shell's variables. A failed write stops recovery with an evidence-storage error, never a clean result.

<!-- test-anchor: wrap-persist-recovery-audit -->
```bash
[[ -n "${WRAP_AUDIT_STATE_SH:-}" ]] || { echo "ERROR: recovery audit storage unavailable" >&2; exit 1; }
[[ -n "${CURRENT_REPO:-}" ]] || { echo "ERROR: verified PR repository unavailable; audit not saved" >&2; exit 1; }
[[ -n "${RECOVERY_EVIDENCE_SH:-}" ]] || { echo "ERROR: raw recovery capture unavailable; no gate accepted" >&2; exit 1; }
if [[ -z "${WRAP_GATE_CAPTURE:-}" ]]; then
  WRAP_GATE_CAPTURE=$("$RECOVERY_EVIDENCE_SH" new --kind gate --repo "$CURRENT_REPO" --pr "$PR_NUM") || exit 1
  printf '%s' "${GATE_JSON:-}" > "$WRAP_GATE_CAPTURE/stdout" || exit 1
  printf '%s' "${GATE_STDERR:-}" > "$WRAP_GATE_CAPTURE/stderr" || exit 1
fi
"$RECOVERY_EVIDENCE_SH" check --kind gate --repo "$CURRENT_REPO" --pr "$PR_NUM" --directory "$WRAP_GATE_CAPTURE" || exit 1
printf '%s' "${WRAP_RECOVERY_AUDIT:-}" > "$WRAP_GATE_CAPTURE/audit" || exit 1
WRAP_AUDIT_JSON=$("$RECOVERY_EVIDENCE_SH" record --kind gate --repo "$CURRENT_REPO" --pr "$PR_NUM" \
  --directory "$WRAP_GATE_CAPTURE" --exit-code "${GATE_RC:--1}") || exit 1
GATE_CLASS=$(jq -r '.classification' <<<"$WRAP_AUDIT_JSON") || exit 1
GATE_JSON=$(jq -c '.last_gate // empty' <<<"$WRAP_AUDIT_JSON") || exit 1
WRAP_SAVE_ARGS=(--repo "$CURRENT_REPO" --set ".prs[\"$PR_NUM\"].wrap_sweep.recovery_audit=$WRAP_AUDIT_JSON")
if [[ "$(jq -r '.failure' <<<"$WRAP_AUDIT_JSON")" == true ]]; then
  WRAP_CAPTURE_ID=$(jq -r '.capture_id' <<<"$WRAP_AUDIT_JSON") || exit 1
  WRAP_SAVE_ARGS+=(--set ".prs[\"$PR_NUM\"].wrap_sweep.recovery_failures[\"$WRAP_CAPTURE_ID\"]=$WRAP_AUDIT_JSON")
fi
"$WRAP_AUDIT_STATE_SH" "${WRAP_SAVE_ARGS[@]}" || {
  echo "ERROR: recovery audit not saved; stopping; raw capture: $WRAP_GATE_CAPTURE" >&2
  exit 1
}
case "$GATE_CLASS" in
  valid_pass|valid_blocked|not_open) ;;
  *) echo "ERROR: gate $GATE_CLASS; recovery stopped; evidence: $WRAP_GATE_CAPTURE" >&2; exit 1 ;;
esac
```

Capture every gate execution with this block, including the merge-ready shortcut. Then execute the audit block above **before any routing or early exit**. A malformed/empty/error result is recorded and stops recovery; it can never reach AC/merge merely because a prior `GATE_JSON` was valid. Full raw captures and failure indexes survive later clean snapshots; retrieval/lifecycle: `recovery-evidence.md`.

<!-- test-anchor: wrap-capture-gate -->
```bash
[[ -n "${CURRENT_REPO:-}" && -n "${RECOVERY_EVIDENCE_SH:-}" ]] || { echo "ERROR: verified recovery capture unavailable" >&2; exit 1; }
WRAP_GATE_CAPTURE=$("$RECOVERY_EVIDENCE_SH" new --kind gate --repo "$CURRENT_REPO" --pr "$PR_NUM") || exit 1
GATE_RC=0
"$MERGE_GATE_SH" "$PR_NUM" --live > "$WRAP_GATE_CAPTURE/stdout" 2> "$WRAP_GATE_CAPTURE/stderr" || GATE_RC=$?
GATE_JSON=""  # filled only from the newly classified record, never stale memory
```

**Merge-ready shortcut:** Use the capture and audit blocks once before the loop. Only `GATE_RC=0` **and** `GATE_CLASS=valid_pass`, with no pending findings or half-applied recovery, reaches Step 2.2. `GATE_JSON` is the complete parsed current verdict; Step 2.4 reads its merge state.

**Claim renewal before every wait (issue #1927).** A `/fixpr` wait can outlast the claim's stale window, so before **each** Branch B/C/D dispatch below — the first and every later one, including the iterations Step 2.4's `BEHIND` re-entry adds — renew every linked issue whose holder this run was **handed**: the Phase C prompt (`/subagent`) or the `/start-issue` Form B handoff names the issue and its holder. Keep holders per issue; never apply one to every linked issue, never adopt a `claimant_holder` read off GitHub, never use `--claim` as a check.

```bash
# CLAIM_PAIRS: "<issue> <holder>" lines from the caller; empty in a standalone /wrap.
CLAIM_PAIRS="${CLAIM_PAIRS:-}"
WRAP_STOP=0
if [[ -z "$CLAIM_PAIRS" ]]; then
  echo "note: no claim holder handed to /wrap — claim not renewed before the wait"
elif [[ -z "$ISSUE_CLAIM_SH" ]]; then
  echo "DEGRADED: issue-claim.sh not found (checked all three paths) — claim not renewed"
else
  CLAIM_ERR=$(mktemp)
  while read -r C_ISSUE C_HOLDER; do
    [[ -n "$C_ISSUE" && -n "$C_HOLDER" ]] || continue
    for attempt in 1 2; do
      [[ "$attempt" -eq 2 ]] && sleep 5   # let a transient gh failure clear
      "$ISSUE_CLAIM_SH" "$C_ISSUE" --renew --holder "$C_HOLDER" >/dev/null 2>"$CLAIM_ERR"; rc=$?
      [[ "$rc" -eq 0 || "$rc" -eq 5 || "$rc" -eq 2 ]] && break
    done
    case "$rc" in
      0) ;;
      5) echo "BLOCKED: lost ownership of issue #$C_ISSUE — another thread holds or released the claim; not merging"; WRAP_STOP=1 ;;
      2) echo "DEGRADED: issue-claim.sh --renew unavailable for #$C_ISSUE: $(head -1 "$CLAIM_ERR")" ;;
      *) echo "WARN: claim renewal for #$C_ISSUE failed (exit $rc) — continuing to the wait" ;;
    esac
  done <<< "$CLAIM_PAIRS"
  rm -f "$CLAIM_ERR"
fi
```

Check `WRAP_STOP` right after the block: `1` (exit 5) is a **genuine block** — stop and report; do not dispatch the wait, do not merge. Exit 4 (or any other) warns, retries once, and continues; exit 2 is degraded (an older helper, or a usage error its stderr names). Step 2.4 performs post-merge claim release through the shared bookkeeping boundary.

**Recovery loop** (`i` from `1` through `$WRAP_RECOVERY_MAX_ITERATIONS`):

1. **Terminal checks** — read PR state via `gh pr view "$PR_NUM" --json state`. `MERGED` → use the Phase 3 merged entry. `CLOSED` → stop.
2. **Refresh gate** — run `wrap-capture-gate`, then `wrap-persist-recovery-audit` before routing. Invalid/empty/error or exit/verdict mismatch stops after durable recording. Exit `3` rechecks the native state with `wrap-confirm-merged-entry`; only `MERGED` reaches Phase 3; exit `2`/`4` stops with its raw evidence pointer. Only exit `0` plus `GATE_CLASS=valid_pass` and no pending findings reaches Step2.2; pending findings still dispatch BranchB.
3. **Exit `1` — classify and dispatch** (first matching branch wins; full per-branch detail in `references/wrap-merge-gate-recovery.md`). Every B/C/D wait dispatch runs the claim renewal above first; its exit 5 stops here:
   - **`human_changes_requested` non-empty** → stop; name each login.
   - **A. Stale bot `CHANGES_REQUESTED`** → invoke `"$DISMISS" "$PR_NUM"`.
   - **`mergeable == CONFLICTING`** → stop; recommend `/merge-conflict`.
   - **`BEHIND` is transparent to branch selection** (issue #1425) → classify on `REMAINDER` = `missing[]` **minus** the `BEHIND` entry, because Step 2.4 is what clears `BEHIND`. `REMAINDER` empty and no `WRAP_PHASE1_FINDINGS` pending → **break out to Step 2.2**, then Step 2.4's clean-`BEHIND` path; do not rebase. `REMAINDER` non-empty → match the branches below **against `REMAINDER`**, carrying `BEHIND` forward to be re-evaluated next iteration. Never leave a `BEHIND`-plus-something `missing[]` unclassified.
   - **B. Delegate `/fixpr`** → when `REMAINDER` has unresolved threads, failing CI, `DIRTY`, or `WRAP_PHASE1_FINDINGS` pending. `BEHIND` alongside any of these reaches `/fixpr` here — a rebase is needed regardless; a `BEHIND`-only `missing[]` exits above instead. Threads-only check via structured gate signals (issue #455). Execute **full** `fixpr/SKILL.md` workflow (Steps 0–7 including Step 4d). Parse `FIXPR_WRAP_STATUS` and `FIXPR_WAIT_SUMMARY` from `=== fixpr complete ===` footer; emit control-returned heartbeat. Full handoff semantics: `.claude/reference/wrap-fixpr-delegation.md`.
   - **C. Missing fresh bot review signal** → trigger the one bot needed (CR rate-check first); delegate wait to `/fixpr`.
   - **D. CI incomplete** → delegate wait to `/fixpr` (idempotent — no push). Fires when incomplete CI is the whole of `REMAINDER`, with or without `BEHIND` alongside.
   - **E. Branch-protection block** → fires when the protection note is the whole of `REMAINDER`, with or without `BEHIND` alongside; suggest `/admin-merge <PR>`; never modify branch protection.
   - **`merge_state == UNKNOWN`** → re-run gate next iteration.
4. **End of iteration** — if no branch matched, append "unclassified blocker" + `missing` to audit.

**Loop exit:** gate exit `0` with no `WRAP_PHASE1_FINDINGS` → Step 2.2; gate exit `1` with `BEHIND` as the sole `missing[]` item and no `WRAP_PHASE1_FINDINGS` → Step 2.2, then the Step 2.4 clean-`BEHIND` path (issue #1425); gate exit `0` with `WRAP_PHASE1_FINDINGS` still pending → Branch B (findings unresolved despite clean gate). Iteration cap → stop with last `missing` + full `WRAP_RECOVERY_AUDIT`. Genuine block (human CR, `CONFLICTING`, rate-limit, hard `/fixpr` failure) → stop per branch above.

**Re-entry from Step 2.4 (issue #1425).** Step 2.4's `BEHIND` fall-throughs rebase, which moves HEAD — invalidating the bot approval and restarting CI — so they must **return to this loop**, not merge and not end `/wrap`. Re-enter at the top of the next iteration against the **same** `i` counter and `WRAP_RECOVERY_MAX_ITERATIONS` cap, so a rebase/re-review cycle can never spin unbounded; on exhausting the cap, stop with the last `missing` and the audit. The re-entered loop re-runs the gate on the new SHA, waits out fresh CI and bot review via the branches above, and reaches Step 2.2 again — AC is re-verified on the new SHA before Step 2.4 retries the merge.

### Step 2.2: Verify acceptance criteria

Verify the full issue scope for every native closing link before merging.
GitHub can close linked issues itself at merge. If scope is partial, ambiguous
or foreign, stop before merge until the closing association is removed or
changed to a non-closing reference. Verify the updated links and rerun the gate.
See [issue scope verification](../../reference/issue-completion.md#verify-scope-before-closing).

Commit messages and the PR title close issues the same way the PR body does.
A partial-scope edit that leaves the body as `Refs #N` must also leave the
commit messages and the title non-closing. The nine closing keywords, in any
case, with an optional colon, count in those texts whether they use `#N`,
`OWNER/REPO#N`, or an issue URL. See [Verify scope before closing](../../reference/issue-completion.md#verify-scope-before-closing).

The ledger (`merge`, exit 10) and `admin-merge.sh` (`--auto-plain` and
`--execute`, exit 9) block with `unverified-closing-reference` when a commit
message, title, or body still closes an issue that is not a native closing
link, or when that evidence cannot be read. Stop. Do not rebase and do not
offer `/admin-merge`. Read stderr. When it names a reference, choose by
scope. Partial, ambiguous, or foreign scope: rewrite that keyword to `Refs`
in the field it names (`title`, `body`, or `commit <sha>`), including the PR
title when that is the only place the keyword appears. Verified full scope:
keep the closing keyword and make it a native link — put the same `Closes #N`
in the PR body when that body only says `Refs`, then rerun after
`closingIssuesReferences` lists the issue. Rewriting the commit to `Refs` in
that case drops the close without a native link. Push when a
commit changed, then re-enter the gate loop. Editing a commit leaves a title-only
or body-only reference in place. When stderr says the guard is missing, a query
failed, or the title or body changed after the check, there is no keyword to
rewrite: surface that stderr, restore the guard or revalidate the current text,
and rerun. The helpers do not rewrite a closing keyword. An executed merge
passes the checked title and body as the squash commit message.

```bash
AC_VERIFIED_HEAD=$(printf '%s' "$GATE_JSON" | jq -er '.head_sha | select(type == "string" and test("^[0-9a-f]{40}$"))') || exit 4
ITEMS=$("$AC_CHECKBOXES_SH" "$PR_NUM" --extract)
AC_EXIT=$?
```

For each item with `checked == false`: read the criterion, read relevant source files, confirm the criterion is satisfied. Tick passing items:

```bash
"$AC_CHECKBOXES_SH" "$PR_NUM" --tick "0,2,3"
# Or:
"$AC_CHECKBOXES_SH" "$PR_NUM" --all-pass
```

Exit codes: `0` OK; `1` no Test Plan section — stop; `3` PR not found — stop; `2`/`4` script/gh error — stop. If any item fails verification, do NOT tick it — stop and report. Do NOT merge with unchecked boxes.

### Step 2.3: Pre-merge safety & CI (handled by Step 2.1)

After the recovery loop, Step 2.1 must have returned gate exit `0` immediately before Step 2.2 — implying SHA freshness, BEHIND/CI/unresolved threads cleared — **with one exception (issue #1425):** the `BEHIND`-only loop exit arrives on gate exit `1` with `BEHIND` still present, cleared later in Step 2.4. Every other precondition holds on both paths. For deeper CI forensics:

```bash
"$CI_STATUS_SH" "$PR_NUM"
"$CI_STATUS_SH" "$PR_NUM" --format summary
```

**Never add `eslint-disable`, `@ts-ignore`, `@ts-expect-error`, or any suppression comment to work around CI.** Fix the actual code.

### Step 2.3a: Pre-merge TestFlight label (iOS repos — issue #1169)

Repos that ship an iOS app cut TestFlight builds from a `release:ios` label on the merged PR. GitHub does **not** re-fire `pull_request: [closed]` for a label added to an already-closed PR, so this is the one release step that must run *before* the merge; everything else happens in Step 3.14. A repo with no `.claude/release-policy.json` — or with it disabled, which is the default — is inert here.

```bash
RELEASE_PREMERGE_RC=2; RELEASE_PREMERGE_OUT=""
if [ -x "$RELEASE_DECIDE" ]; then
  RELEASE_PREMERGE_RC=0
  RELEASE_PREMERGE_OUT=$("$RELEASE_DECIDE" --pr "$PR_NUM" --phase pre-merge --apply 2>&1) \
    || RELEASE_PREMERGE_RC=$?
fi
```

**Every failure path here is non-fatal and nothing is printed yet.** A missing script, a malformed policy, a `gh` error, or any non-zero exit leaves the merge untouched — release logic must never gate or delay a merge (AC4). The result is carried to Step 3.14 and reported only once the merge has actually landed, so a merge that fails after the label was applied never claims a build that did not happen. (A label left on an unmerged PR is ignored by the workflow until that PR merges — recoverable and harmless.)

### Step 2.4: Squash merge

After blockers clear (Phase 1 + Step 2.1 recovery + Step 2.2), squash-merge the resolved PR — per `CLAUDE.md` "PR MERGE AUTHORIZATION" and `cr-merge-gate.md` Step 3. Always pass the explicit identifier so `/wrap #N` invocations merge the correct PR, not the current-branch PR.

**The merge command depends on `merge_state` (issue #1425)** — read it from the last `merge-gate.sh` JSON, not from a fresh guess:

```bash
MERGE_STATE=$(printf '%s' "$GATE_JSON" | jq -r '.merge_state // empty')
```

`printf '%s'`, never `echo` — zsh's builtin corrupts the JSON payload (issue #574).

- **`CLEAN`** (override not applied) → resolve the ledger helper, then run `"$LEDGER_SH" merge --pr "$PR_NUM" --repo "$CURRENT_REPO" --sha "$AC_VERIFIED_HEAD"`. It clears unused override evidence and runs a head-pinned squash under the same lock. Exit `9` from the ledger's `merge --gate-json` means the gate is a normal approved gate, not an override, and nothing merged — use this `--sha` route. Exit `10` means the closing-reference guard refused or could not read commit messages, the title, or the body (`unverified-closing-reference`). Stop. Do not rebase onto the base to clear this refusal, and do not offer `/admin-merge`. Read stderr. When it names a reference, choose by scope. Partial, ambiguous, or foreign scope: rewrite that keyword to `Refs` in the field it names (`title`, `body`, or `commit <sha>`), including the PR title when that is the only place the keyword appears. Verified full scope: keep the closing keyword and make it a native link — put the same `Closes #N` in the PR body when that body only says `Refs`, then rerun after `closingIssuesReferences` lists the issue. Rewriting the commit to `Refs` in that case drops the close without a native link. Push when a commit changed, then re-enter the gate. Editing a commit leaves a title-only or body-only reference in place. When stderr says the guard is missing, a query failed, or the title or body changed after the check, there is no keyword to rewrite: surface that stderr, restore the guard or revalidate the current text, and rerun. Gate-shape-to-helper table: `.claude/scripts/docs/merge-gate-sequencing.md` §Which merge helper for which gate (issue #2432).
- **No-review override** (issue #1899) → after AC verification, run `wrap-capture-gate` and `wrap-persist-recovery-audit` again. Use the new full `GATE_JSON` only when the refreshed classification is `valid_pass`, the override still applies, and its refreshed `merge_state` still passes this same test (`CLEAN`, or `UNSTABLE` with nonempty `excluded_reviewer_checks`). Otherwise re-enter Step 2.1. An override on `BEHIND` never uses the admin path. Use /fixpr and return to Step 2.1 for the new HEAD.
- **`BEHIND`** → the clean-`BEHIND` path below. A plain squash is **rejected** here by `required_status_checks.strict`.
- **Anything else** → **not merge-ready** per `cr-merge-gate.md` Step 1d — stop with the state and the last `missing[]`. Never merge on an unenumerated state: `BLOCKED` and `UNSTABLE` add no `missing[]` entry, so a gate exit `0` does not by itself clear them.

For an eligible override, execute the following as one Bash call after the fresh gate and audit. Use the complete refreshed gate and retain `AC_VERIFIED_HEAD` from Step 2.2. If they name different commits, rerun acceptance verification before any merge attempt.

<!-- test-anchor: wrap-no-review-override-merge -->
```bash
wrap_merge_with_override() {
  local ledger head
  ledger=$(resolve_script unreviewed-merge-ledger.sh) || return 5
  head=$(printf '%s' "$GATE_JSON" | jq -er '.head_sha | select(type == "string" and test("^[0-9a-f]{40}$"))') || return 4
  if [[ "$head" != "${AC_VERIFIED_HEAD:-}" ]]; then
    echo "WARN: HEAD moved since acceptance verification; rerun the gate and verify the new HEAD" >&2
    return 4
  fi
  printf '%s' "$GATE_JSON" | "$ledger" merge --pr "$PR_NUM" --repo "$CURRENT_REPO" --gate-json -
}
wrap_merge_with_override
```

A merge command error may still mean GitHub merged it. Verify native state before routing. On `MERGED` continue to the recorder. Otherwise stop or recover through Step 2.1. Before a clean-BEHIND admin merge, resolve the same ledger helper and run `snapshot --pr "$PR_NUM" --repo "$CURRENT_REPO" --discard`. Continue only on success. This clears unused evidence for a verified unmerged PR. If no snapshot exists it is a no-op. Both plain squash paths hold the snapshot lock through the merge request.

The merge-state mechanics are owned by `cr-merge-gate.md` Step 1d and implemented identically in `fixpr/SKILL.md` Step 6; the safety conditions live there and are not restated here.

**A losing race still routes correctly.** `merge_state` is a snapshot: a sibling PR can land between the gate read and the merge call, so a PR that read `CLEAN` may be `BEHIND` by the time `gh pr merge` runs. That command's `head branch is not up to date` refusal **is** the `BEHIND` signal — take the clean-`BEHIND` path below rather than reporting a failed merge.

**Clean-`BEHIND` path.** Two helpers, resolved lazily (same pattern as `ESTIMATE_LOG_SH` below) because only this branch needs them:

```bash
CLEAN_BEHIND_SH=$(resolve_script clean-behind-check.sh || true)
ADMIN_MERGE_SH=$(resolve_script admin-merge.sh || true)
[[ -n "$CLEAN_BEHIND_SH" ]] || { echo "ERROR: clean-behind-check.sh not found (checked all three paths) — clean-BEHIND verification unavailable" >&2; exit 1; }
[[ -n "$ADMIN_MERGE_SH" ]] || { echo "ERROR: admin-merge.sh not found (checked all three paths) — clean-BEHIND merge unavailable" >&2; exit 1; }
```

Both are required **only** on this branch; the `CLEAN` branch never reads them. If either is empty when this branch runs, **stop** — never fall back to a plain merge, which `strict` would reject anyway.

Then run `"$CLEAN_BEHIND_SH" "$PR_NUM"` and branch on its exit code:

| Exit | Action |
|------|--------|
| `0` (`safe_to_offer: true`) | Run `"$ADMIN_MERGE_SH" "$PR_NUM" --auto-plain --ac-verified --defer-bookkeeping`. On its exit `0` the PR is **already merged** — relay the `AUTO_PLAIN_MERGED` evidence block and continue to the post-merge actions below. No `AskUserQuestion`: the plain shape modifies no branch protection, so it needs no user turn. |
| `1` (`safe_to_offer: false`) | Not clean — surface `reasons_not_safe`, then take the `/fixpr` non-clean `BEHIND` path (rebase + force-push, `fixpr/SKILL.md` Step 6) and **re-enter Step 2.1** (below). |
| `2` / `3` / `4` | Tooling or usage failure — **stop** and surface stderr. Never merge on an indeterminate answer. |

`admin-merge.sh`'s own exits after a clean probe: `8` → the shape needs a protection change, or an auto attempt already ran — **offer `/admin-merge <PR>`** and surface the printed command, never auto-run it. `1` → the clean-`BEHIND` state no longer held at merge time (main advanced) — take the `/fixpr` non-clean `BEHIND` path and **re-enter Step 2.1** (below). `9` → the closing-reference guard refused or could not read the merge text (`unverified-closing-reference`). Stop with that reason. Do not rebase onto the base to clear this refusal, and do not offer `/admin-merge`. Read stderr. When it names a reference, choose by scope. Partial, ambiguous, or foreign scope: rewrite that keyword to `Refs` in the field it names (`title`, `body`, or `commit <sha>`), including the PR title when that is the only place the keyword appears. Verified full scope: keep the closing keyword and make it a native link — put the same `Closes #N` in the PR body when that body only says `Refs`, then rerun after `closingIssuesReferences` lists the issue. Rewriting the commit to `Refs` in that case drops the close without a native link. Push when a commit changed, then rerun the gate. Editing a commit leaves a title-only or body-only reference in place. When stderr says the guard is missing, a query failed, or the title or body changed after the check, there is no keyword to rewrite: surface that stderr, restore the guard or revalidate the current text, and rerun. Any other exit → stop and surface stderr.

**Both fall-throughs re-enter the Step 2.1 recovery loop — they never merge from here and never end `/wrap` (issue #1425).** The rebase moves HEAD, which invalidates the bot approval that satisfied the gate and restarts CI, so the post-rebase SHA is unreviewed by construction: merging it would bypass the gate, and stopping would abandon a PR that is one review round from mergeable. Re-entry runs against the **same** `i` counter and `WRAP_RECOVERY_MAX_ITERATIONS` cap, so rebase → re-review → `BEHIND`-again cannot spin unbounded; exhausting the cap stops with the last `missing` and the full `WRAP_RECOVERY_AUDIT`. Because re-entry passes back through Step 2.2, AC is re-verified against the rebased SHA before Step 2.4 runs again — which is what keeps the later `--ac-verified` attestation truthful on the retry.

`--ac-verified` is justified because **Step 2.2 already ran unconditionally** — every Test Plan checkbox was verified against the source at this SHA on both paths. `clean-behind-check.sh` counts unchecked boxes as `reasons_not_safe`, so running it before Step 2.2 would report exit `1` for bookkeeping rather than for a real blocker.

**Override recorder (issue #1899).** Once GitHub confirms the merge, run this boundary before issue reconciliation. Use the saved snapshot, even if the merge and recovery are in different sessions.

<!-- test-anchor: wrap-record-no-review-merge -->
```bash
LEDGER_SH=$(resolve_script unreviewed-merge-ledger.sh || true)
NRO_SUFFIX="${NRO_SUFFIX:-}"
if [[ -z "$LEDGER_SH" ]]; then
  echo "WARN: unreviewed-merge-ledger.sh unavailable; check retained override snapshots" >&2
else
  NRO_RECORD_RC=0
  "$LEDGER_SH" record --pr "$PR_NUM" --repo "$CURRENT_REPO" --from-snapshot >/dev/null || NRO_RECORD_RC=$?
  case "$NRO_RECORD_RC" in
    0) NRO_SUFFIX=" (no AI review — logged)" ;;
    7) ;;  # No snapshot, or already recorded earlier in this wrap.
    *) NRO_SUFFIX=" (no AI review — ledger write failed)" ;;
  esac
fi
```

A failed record keeps the snapshot for retry. Do not run the gate on a merged PR to reconstruct this evidence, or pass a projection that dropped gate fields.

**Reconcile issue completion first.** Load the canonical adapter from [verified issue completion](../../reference/issue-completion.md) in the same Bash invocation as the anchor below. If the assessment reports an open linked issue for this delivery, verify its full scope and rerun the boundary with the merge and issue revision from the evidence. Record deliberately retained scope with the reference’s retention flags. For an issue GitHub already closed, verify full scope and pass `--scope-verified --expected-merge "$MERGE_SHA"` without `--complete-issue` before reconciling it. A CLOSED issue with unfinished or ambiguous work remains pending for manual resolution. If completion is still pending, report `PR already merged; completion pending` with the receipt and preserve the claim and handoff. Continue after verified closure or justified retained scope.

**Preserve pickup evidence before claim cleanup.** After either confirmed merge path, run the same retryable boundary. It verifies the explicit repo/PR is merged, appends the estimate while renewal `since` and the claim timestamp remain available, then releases every winning-tier linked issue claim only while its pre-log observed comment identity still matches. Logging failures warn with the observed pickup timestamp and still attempt cleanup. Repeat invocations deduplicate the estimate and safely retry partial cleanup; foreign-account claims remain protected by the claim helper.

<!-- test-anchor: wrap-post-merge-bookkeeping -->

```bash
wrap_post_merge_bookkeeping() {
  local completion_rc=0
  MERGED_ISSUES=""
  if ! declare -F reconcile_issue_completion >/dev/null; then
    echo 'ERROR: completion adapter unavailable — issue completion pending' >&2
    return 6
  fi
  OWNER_REPO="$CURRENT_REPO" reconcile_issue_completion "$@" || completion_rc=$?
  if [[ "$completion_rc" -ne 0 ]]; then
    echo "WARN: PR #$PR_NUM already merged; issue completion pending (exit $completion_rc)" >&2
    return "$completion_rc"
  fi
  if [[ -n "$POST_MERGE_BOOKKEEPING_SH" ]]; then
    BOOKKEEPING_RC=0
    BOOKKEEPING_JSON=$("$POST_MERGE_BOOKKEEPING_SH" "$PR_NUM" --repo "$CURRENT_REPO") || BOOKKEEPING_RC=$?
    if [[ "$BOOKKEEPING_RC" -eq 0 ]]; then
      MERGED_ISSUES=$(printf '%s' "$BOOKKEEPING_JSON" | jq -r '.issues[]?' 2>/dev/null || true)
    else
      echo "WARN: post-merge bookkeeping not confirmed for PR #$PR_NUM (exit $BOOKKEEPING_RC)" >&2
    fi
  else
    echo "WARN: post-merge-bookkeeping.sh missing; restore helpers and retry /wrap for PR #$PR_NUM" >&2
  fi
  return 0
}
wrap_post_merge_bookkeeping
```

The estimate and claim boundary is warning-only after a confirmed merge and successful completion assessment. For a verified single-issue close, intentionally retained scope, or a delivery with no issue, call the same function with the corresponding flags from the reference. Reuse those exact flags on this run’s Phase 3 entry. It never authorizes a merge or changes historical estimate rows. A closed-unmerged or unreadable PR does no merged bookkeeping.

**Record explicit telemetry correlation after the merge gate and AC verification.** This optional metadata-only helper is described in `.claude/reference/spend-telemetry-pipeline.md`. Pass `--gate-passed` only after Steps 2.1 and 2.2 succeed, with the full `head_sha` and structured result from that successful `GATE_JSON` (`met:true`). An already-merged or special-path run without that passing result remains unverified telemetry. Pass a session ID only when supplied by the runtime; never infer one. A merge alone is not verified completion. Failures are non-blocking.

```bash
if [[ -n "$SPEND_TELEMETRY_LINK" && -n "$MERGED_ISSUES" ]]; then
  GATE_HEAD_SHA=$(printf '%s' "$GATE_JSON" | jq -r '.head_sha // empty' 2>/dev/null || true)
  printf '%s\n' "$MERGED_ISSUES" | while IFS= read -r MERGED_ISSUE; do
    [ -n "$MERGED_ISSUE" ] || continue
    TELEMETRY_ARGS=(--issue "$MERGED_ISSUE" --pr "$PR_NUM" \
      --source wrap_post_merge)
    if [[ "$GATE_HEAD_SHA" =~ ^[a-fA-F0-9]{40}$ ]] && printf '%s' "$GATE_JSON" | jq -e '.met == true' >/dev/null 2>&1; then
      TELEMETRY_ARGS+=(--gate-passed --gate-head-sha "$GATE_HEAD_SHA" --gate-result "$GATE_JSON")
    fi
    TELEMETRY_SESSION_ID="${CLAUDE_CODE_SESSION_ID:-${CLAUDE_SESSION_ID:-}}"
    [[ -n "$TELEMETRY_SESSION_ID" ]] && TELEMETRY_ARGS+=(--session "$TELEMETRY_SESSION_ID")
    "$SPEND_TELEMETRY_LINK" "${TELEMETRY_ARGS[@]}" >/dev/null 2>&1 || true
  done
fi
```

Do NOT use `--delete-branch`. The current worktree is still checked out on the feature branch — git refuses to delete a branch held by a worktree. The branch is cleaned up out-of-band by `/pm-clean` via `stale-cleanup.sh`.

### Step 2.5: Sync the bound root repo main (aggressive reset)

**Step 2.5 is one plain call with every value typed on it, and the path it names is the worktree.** A worktree-isolated Phase C agent is refused `(cd "$ROOT_REPO" && …)` ("too complex to verify that it stays inside the worktree") and `git -C "$ROOT_REPO" …` (issue #1411). It is also refused a multi-line block with `$(…)` and any command whose text holds a `github` URL, so the root-main sync never ran (issue #2496). Each Bash tool call starts a fresh shell, so nothing from the binding call is still set: type the same `<BOUND_MAIN_SYNC_SH>`, `<WRAP_SOURCE_PATH>`, `<PR_OWNER>`, `<PR_NAME>` and `<PR_NUMBER>` used to bind, and `<WRAP_BOUND_ROOT>` as the value the binding printed (empty if it failed). `bound-main-sync.sh` re-checks the worktree path against the PR and resolves the physical root. It runs `dirty-main-guard.sh` and `main-sync.sh --reset` only when that root is non-empty and exactly `--bound-root`. A missing or different binding skips local mutation and reports `failed: bound clone unavailable or identity changed; local mutation skipped`. Full catalog: `.claude/reference/worktree-isolation-command-shapes.md` (issue #1470). The repo's model of that harness check is a test-only script; `/wrap` never runs it.

**If no `bound-main-sync.sh` resolves, do not invoke it.** Print the same two stdout lines the helper prints, both valued `unavailable`, and continue. A failed sync does not stop `/wrap` after the merge, and a missing helper does not either: do not `exit`.

<!-- test-anchor: wrap-bound-main-sync-unavailable -->

```bash
printf '%s\n' 'QUARANTINE_STATUS=unavailable' 'MAIN_SYNC_STATUS=unavailable'
```

Run that printf only in the missing-helper case. Otherwise run this one plain call instead, and do not also run the printf:

<!-- test-anchor: wrap-bound-main-sync -->

```bash
"<BOUND_MAIN_SYNC_SH>" --repo-path "<WRAP_SOURCE_PATH>" --bound-root "<WRAP_BOUND_ROOT>" --pr-owner "<PR_OWNER>" --pr-name "<PR_NAME>" --pr-number "<PR_NUMBER>"
```

Either command prints `QUARANTINE_STATUS=` and `MAIN_SYNC_STATUS=` on stdout. See `bound-main-sync.sh --help`, `main-sync.sh --help`, and `dirty-main-guard.sh --help` for the full contracts.

**If `MAIN_SYNC_STATUS` starts with `aborted:`**: surface the full status line in the final report so the user can run `git log origin/main..main` against the root repo and decide. The PR merge has already succeeded — main-sync failure does not un-merge anything.

Store `MAIN_SYNC_STATUS` and `QUARANTINE_STATUS` from those lines for the final report.

## Phase 3: Follow-Up Detection and Full-Session Sweep

**Confirm a real merge on every entry, including recovery.** Run this before bookkeeping or follow-up detection. A closed-unmerged PR stops without creating merged-work tickets; a read failure also stops.

<!-- test-anchor: wrap-confirm-merged-entry -->

```bash
MERGED_ENTRY_STATE=$(gh pr view "$PR_NUM" --repo "$CURRENT_REPO" --json state --jq '.state') || {
  echo "WARN: could not verify merged state for PR #$PR_NUM; no merged-work follow-ups" >&2
  exit 4
}
if [[ "$MERGED_ENTRY_STATE" != MERGED ]]; then
  echo "PR #$PR_NUM is $MERGED_ENTRY_STATE without a confirmed merge; no merged-work follow-ups."
  exit 3
fi
```

**Every merged entry, including resume:** run `wrap-record-no-review-merge` above, then execute `wrap-post-merge-bookkeeping` before the follow-up sweep, using the same scope checks, completion adapter and verified disposition flags as Step 2.4, even if it already ran. This also covers terminal recovery exits and interrupted/already-merged runs. Use the explicit `CURRENT_REPO` and `PR_NUM` from Phase 1; restore `POST_MERGE_BOOKKEEPING_SH` through the prelude resolver if needed. A closed-unmerged PR skips it. If assessment is pending, stop here and keep its receipt and handoff. Never release a claim or append actuals separately; the shared boundary owns that order.

Phase 3 has **two parts**, run in order:

- **Part A — Per-PR follow-up detection (Steps 3.1–3.4):** derive follow-ups from the merging PR and its linked issue, dedup, and auto-create GitHub issues.
- **Part B — Full-session sweep (Steps 3.5–3.13):** answer "is there anything I should have ticketed but didn't?" across the entire session. Eight categories: loose ends, ticket coverage, external/process state, memory persistence, PM hygiene, churn hotspots, time-sensitive items, future-self handoff.

**Shared filed-issue registry (`WRAP_FILED_ISSUES`) — initialize before Step 3.1.** Both parts file into the same run-scoped registry. Each entry is `{number, title, keywords, rationale}`. Part A appends in Step 3.3; Part B checks the registry before its own dedup search in Step 3.7.

### Step 3.0: Dedup helper setup (both parts — issue #652)

Resolve the helper once, before Step 3.1:

```bash
ISSUE_DEDUP=$(resolve_script issue-dedup.sh || true)
DEDUP_EXCLUDE=""
DEDUP_DEGRADED=""
DEDUP_ANY_DEGRADED=""
WRAP_FILED_ISSUES=""   # newline-separated JSON objects: {number, title, keywords, rationale}; both Part A and Part B append here

dedup_search() {   # sets DEDUP_JSON/DUP_NUM/DUP_STATE
  local kw="$1" rc=0
  DEDUP_JSON='[]'
  DEDUP_DEGRADED=""
  if [ -n "$ISSUE_DEDUP" ] && [ -n "$kw" ]; then
    local dedup_args=("$kw")   # an array: zsh keeps `${X:+--flag "$X"}` as one word
    [ -n "$DEDUP_EXCLUDE" ] && dedup_args+=(--exclude "$DEDUP_EXCLUDE")
    DEDUP_JSON=$("$ISSUE_DEDUP" "${dedup_args[@]}") || rc=$?
    if [ "$rc" -gt 1 ]; then
      DEDUP_DEGRADED="helper exit $rc"
      DEDUP_ANY_DEGRADED="$DEDUP_DEGRADED"
      DEDUP_JSON=$(gh issue list --search "${kw} in:title" --state open \
        --json number,title,state --jq '[.[0] // empty]' 2>/dev/null || echo '[]')
    fi
  elif [ -n "$kw" ]; then
    DEDUP_DEGRADED="helper not installed"
    DEDUP_ANY_DEGRADED="$DEDUP_DEGRADED"
    DEDUP_JSON=$(gh issue list --search "${kw} in:title" --state open \
      --json number,title,state --jq '[.[0] // empty]' 2>/dev/null || echo '[]')
  fi
  DUP_NUM=$(printf '%s' "$DEDUP_JSON" | jq -r '.[0].number // empty')
  DUP_STATE=$(printf '%s' "$DEDUP_JSON" | jq -r '.[0].state // empty' | tr '[:lower:]' '[:upper:]')
  WEAK_DUP_NUM=""
}
```

`issue-dedup.sh <keywords>` prints ranked candidates as JSON, scoring **title and body** across open plus recently-closed issues. Exit `0` = candidates, `1` = none ("no duplicate"), `2`/`4` = failure (degrade to title-only; never treat as "no match").

**A degraded search is never a strong match.** `DEDUP_DEGRADED` resets per call so one transient failure does not force every later candidate down the degraded path. **The helper only finds candidates — it never decides.** Classification (strong / weak / none) and the four strong-match criteria are specified in **`.claude/reference/autofile-dedup.md`**. Two invariants: bias toward filing; suppression is never silent.

> **Phase C / subagent context:** Part B degrades gracefully (transcript-derived categories find little); Part A and git-data categories still run normally. Never block a Phase C merge on a Part B finding — Part B is advisory.

### Part A — Per-PR follow-up detection

### Step 3.1: Detect follow-up items

1. Extract the linked issue number from the PR body via `pr-issue-ref.sh --first` (matches all nine GitHub closing keywords). `--first` is required: follow-up attribution wants one primary issue, and the default mode is set-valued, so a PR closing two issues would otherwise hand `gh issue view` a two-line argument. Distinguish exit `1` (no link — expected) from exits `2`/`3`/`4` (real errors):

   ```bash
   PR_NUMBER="$PR_NUM"
   PR_TITLE=$(gh pr view "$PR_NUMBER" --json title --jq '.title')
   ISSUE_N=""
   if [[ -n "$PR_ISSUE_REF_SH" ]] && RAW_REF=$("$PR_ISSUE_REF_SH" --first "$PR_NUMBER" 2>&1); then
     ISSUE_N="$RAW_REF"
   else
     REF_RC=$?
     if [ "$REF_RC" -ne 1 ]; then
       echo "Warning: pr-issue-ref.sh exit $REF_RC: $RAW_REF — skipping linked-issue lookup" >&2
     fi
   fi
   ISSUE_TITLE=""
   ISSUE_BODY=""
   if [ -n "$ISSUE_N" ]; then
     ISSUE_TITLE=$(gh issue view "$ISSUE_N" --json title --jq '.title' 2>/dev/null || echo "")
     ISSUE_BODY=$(gh issue view "$ISSUE_N" --json body --jq '.body' 2>/dev/null || echo "")
   fi
   ```

2. If a parent issue exists (check for "parent" or "epic" references in the issue body), fetch sibling issues:
   ```bash
   gh issue view {parent_N} --json body --jq .body
   ```
   Look for task lists or child issue references. Check which are still open.

3. Check for related issues mentioned in the PR or issue thread:
   ```bash
   gh api "repos/{owner}/{repo}/issues/{N}/comments?per_page=100" --jq '.[].body'
   ```
   Scan for issue references (`#NNN`), "follow-up", "TODO", "next step", "migration", "deploy" mentions.

4. Check if the issue itself has sub-tasks (task list checkboxes) that are unchecked.

Collect each detected follow-up as a `{title, body, keywords}` record.

### Step 3.2: HHG two-ticket pattern detection

If the PR title, linked issue title, or linked issue body contains "HHG" (case-insensitive), **override** any generic follow-ups with exactly **two** HHG follow-ups (scraping + ETL):

```bash
HHG_MATCH=$(printf '%s\n%s\n%s\n' "$PR_TITLE" "$ISSUE_TITLE" "$ISSUE_BODY" | grep -iE 'HHG' || true)
if [ -n "$HHG_MATCH" ]; then
  COMBINED=$(printf '%s %s %s' "$PR_TITLE" "$ISSUE_TITLE" "$ISSUE_BODY")
  STATE=$([[ -n "$HHG_STATE_SH" ]] && "$HHG_STATE_SH" "$COMBINED" || true)
  if [ -z "$STATE" ]; then
    STATE=""
    echo "WARNING: HHG PR detected but no state code found in PR title, issue title, or issue body — skipping HHG auto-creation. Create the scraping and ETL issues manually once you know the state."
  fi
fi
```

**If `STATE` is empty, skip HHG auto-creation entirely** — do NOT create issues with placeholder titles. Report the skip in Step 3.4.

Two HHG follow-up titles: `{STATE} HHG — Export carriers and run scraper` and `{STATE} HHG — Seed product codes and load scrape results to Neon`. Create scraping issue first (capture number as `SCRAPE_NUM`), then ETL with `Depends on #${SCRAPE_NUM}` in its body.

**HHG override trade-off:** Replaces any generic follow-ups to keep the two-ticket pattern clean.

### Step 3.3: Dedup check and create

For each follow-up item (the HHG pair or the generic list):

1. **Dedup check:**
   ```bash
   dedup_search "$KEYWORDS"   # Step 3.0 — sets DEDUP_JSON, DUP_NUM, DUP_STATE, WEAK_DUP_NUM
   ```
   Classify per `.claude/reference/autofile-dedup.md`:
   - **Strong** → comment the follow-up onto `#{DUP_NUM}`; record `"{title}" — appended to #{DUP_NUM}` for Step 4.3.
   - **Weak / ambiguous or closed** → set `WEAK_DUP_NUM="$DUP_NUM"` and file with a `Possibly duplicates #{DUP_NUM}` body line.
   - **None** → file.

2. **Create the issue** (strong match excepted):
   ```bash
   LINKED_SOURCE=""
   if [ -n "$ISSUE_N" ]; then
     LINKED_SOURCE=$'\n\n'"Linked source: #${ISSUE_N}"
   fi
   POSSIBLE_DUP=""
   if [ -n "${WEAK_DUP_NUM:-}" ]; then
     POSSIBLE_DUP=$'\n\n'"Possibly duplicates #${WEAK_DUP_NUM} — {one line on the overlap and what is unclear}."
   fi
   ISSUE_TITLE="{derived title}"
  ISSUE_BODY="Follow-up from PR #${PR_NUMBER}.${POSSIBLE_DUP}

{context from detection}${LINKED_SOURCE}

_Filed via /wrap._"
  # Surface title+body before filing (--verbose only; the silent default records without printing):
  if [ "$WRAP_VERBOSE" = "1" ]; then
    echo "Filing follow-up: $ISSUE_TITLE"
    echo "$ISSUE_BODY"
  fi
  if NEW_URL=$(gh issue create \
     --title "$ISSUE_TITLE" \
     --body "$ISSUE_BODY" 2>&1); then
     NEW_NUM=$(echo "$NEW_URL" | grep -oE '[0-9]+$')
     if [ -z "$NEW_NUM" ]; then
       echo "WARNING: created issue but could not parse number from: $NEW_URL"
     fi
     # Append {number, title, keywords, rationale: "follow-up from PR #${PR_NUMBER}"} to WRAP_FILED_ISSUES.
     # Append $NEW_NUM to DEDUP_EXCLUDE.
   else
     echo "WARNING: gh issue create failed: $NEW_URL"
   fi
   ```

### Step 3.4: Carry Part A results into the unified report

Part A does **not** print its own "Created" list — every issue it filed is in `WRAP_FILED_ISSUES` and rendered once in Step 4.3's verbose report. Carry forward: filed issues (already in registry), suppressed-as-duplicates, filed-with-caveat, and failures. Filed-with-caveat entries and creation failures are the two that also print on the silent default. If nothing detected and nothing filed, the verbose report reads "No follow-up items detected." Proceed immediately to Part B.

### Part B — Full-session sweep

Part B sweeps the **whole session** for loose ends the per-PR detection in Part A cannot see. It produces two buckets — **Auto-handled** and **Needs your decision** — and a one-line **verdict**.

**Safety boundaries (non-negotiable — issue #471):** never auto-file without recording the full body (rendered under `--verbose` or on request — issue #851); never auto-act on anything affecting shared state (live monitors, active subagents, human-owned issues/PRs, recovery branches); auto-handling limited to: stopping a **dead** session Monitor task and deleting a **stale** handoff file.

#### Step 3.5: Sweep setup & idempotency guard

```bash
SESSION_STATE_SH=$(resolve_script session-state.sh || true)

SWEEP_PRIOR_FILED=""
if [[ -n "$SESSION_STATE_SH" ]]; then
  SWEEP_PRIOR_FILED=$("$SESSION_STATE_SH" --get ".prs[\"$PR_NUMBER\"].wrap_sweep.filed_issues" 2>/dev/null || echo "")
fi
```

Initialize `SWEEP_AUTO_HANDLED` and `SWEEP_NEEDS_DECISION` (free-form bullet lists). Cap each rendered section at 3–5 bullets (overflow → "+ N more" summary); auto-filed tickets are **exempt** from the cap — every created issue's title + body is surfaced in full under `--verbose`.

#### Step 3.6: Category 1 — Session loose ends (transcript introspection)

**Model-introspection step** — no shell command can read the transcript. Review your own recent messages and tool-call history for deferral signals: `later`, `TODO`, `follow-up`, `come back to`, `worth investigating`, `for now`, `deferred`, `out of scope`, `punt`, `we should eventually`, `not in this PR`, `leaving X for a separate change`. After compaction, fall back to tool-call history and `git diff origin/main...HEAD` scan for new `TODO`/`FIXME` comments.

Collect each loose end as `{summary, context, keywords}`.

#### Step 3.7: Category 2 — Ticket coverage (dedup, then file)

For **each** loose end from Step 3.6, dedup before filing:

**Stage 1 — cross-batch (this run).** Check against `WRAP_FILED_ISSUES` first. Collapse only when same primary artifact and already-filed scope covers this finding. Record `Collapsed into #{N} (filed earlier this run) — "<summary>"` in `SWEEP_AUTO_HANDLED`.

**Stage 2 — cross-session (the repo).**

```bash
dedup_search "$KEYWORDS"   # Step 3.0
```

Classify per `.claude/reference/autofile-dedup.md` and take exactly one path:

- **Strong match** (open, same artifact, quotable criterion, `coverage ≥ 0.6`) → comment onto `#{DUP_NUM}`; if `DUP_NUM` is in `SWEEP_PRIOR_FILED`, **omit entirely** (idempotent). Record in `SWEEP_AUTO_HANDLED`.
- **Weak / closed** → set `WEAK_DUP_NUM` and file with `Possibly duplicates #{DUP_NUM}` body line; add to `SWEEP_NEEDS_DECISION`.
- **None** → file.

On filing: create immediately, **surface title + body under `--verbose`**, append to `WRAP_FILED_ISSUES` (rationale `loose end: <summary>`), append number to `DEDUP_EXCLUDE`, add to `SWEEP_AUTO_HANDLED`. Use same `gh issue create` guard as Step 3.3 (validate parsed number; on failure record and continue):

```bash
POSSIBLE_DUP=""
if [ -n "${WEAK_DUP_NUM:-}" ]; then
  POSSIBLE_DUP=$'\n\n'"Possibly duplicates #${WEAK_DUP_NUM} — <one line on the overlap and what is unclear>."
fi
if NEW_URL=$(gh issue create \
    --title "<derived title>" \
    --body "Follow-up surfaced by /wrap session sweep on PR #${PR_NUMBER}.${POSSIBLE_DUP}

<context from transcript>

_Filed via /wrap._" 2>&1); then
  NEW_NUM=$(echo "$NEW_URL" | grep -oE '[0-9]+$')
  if [ -n "$NEW_NUM" ]; then
    SWEEP_FILED="${SWEEP_FILED} ${NEW_NUM}"
    # Append {number, title, keywords, rationale} to WRAP_FILED_ISSUES.
  else
    echo "WARNING: created issue but could not parse number from: $NEW_URL"
  fi
else
  echo "WARNING: gh issue create failed: $NEW_URL"
fi
```

#### Step 3.8: Category 3 — External / process state

```bash
[ -n "$SESSION_STATE_SH" ] && POLLING_JOBS=$("$SESSION_STATE_SH" --get '.polling_jobs' 2>/dev/null || echo "null")
# A map keyed by agent id since issue #1631 — test emptiness with `length`, not
# an array check. Its entries are surfaced below, never auto-acted on.
[ -n "$SESSION_STATE_SH" ] && ACTIVE_AGENTS=$("$SESSION_STATE_SH" --get '.active_agents' 2>/dev/null || echo "null")
ACTIVE_AGENT_COUNT=$(jq -r 'if type == "object" or type == "array" then length else 0 end' <<<"$ACTIVE_AGENTS" 2>/dev/null || echo 0)
```

- **Dead Monitor tasks (auto-stop).** For each watcher whose target PR is merged/closed: read the complete `.prs["$N"].babysit.monitor_task_id` + `.monitor_generation` identity pair, stop that exact task with `TaskStop` when present, then atomically set `stop_requested=true`, `active=false`, `monitor_task_id=null`, and `monitor_generation=null` via one `session-state.sh --set` batch. Clear neither identity field unless exact `TaskStop` succeeds. Record `Stopped stale Monitor task (PR #N watcher — PR already merged)` in `SWEEP_AUTO_HANDLED`. A missing/incomplete identity or failed task stop is a decision item, never a claimed successful stop.
- **Handoffs with pending completion (retain).** PR MERGED alone never authorizes a stale-handoff delete. Preserve the current handoff until completion is independently verified or its remaining scope is explicitly justified and Phase C publication succeeds. Parent cleanup deletes only that exact verified scoped handoff. Leave other or legacy handoffs to the finished-artifact cadence, which requires known ownership and inactive sessions; unavailable or uncertain cleanup retains them. Never delete all merged-PR handoffs from this sweep or discard recovery evidence after a failed reconciliation.
- **Surface (never auto-act).** Add to `SWEEP_NEEDS_DECISION`: live `CronCreate` jobs, any `active_agents` entries (`ACTIVE_AGENT_COUNT` above 0), `monitoring_active=true`, and any `recovery/dirty-main-*` branches.

#### Step 3.9: Category 4 — Memory persistence (defers to Phase 4)

Memory writes are owned by **Phase 4**. Category 4 only *flags* as `SWEEP_MEMORY_CANDIDATES` (handed to Phase 4 Step 4.2): decisions made this session worth persisting, and existing memories the session contradicted. Surface a memory item in the sweep report only if Phase 4 is skipped as trivial.

#### Step 3.10: Category 5 — PM hygiene (all touched issues/PRs)

Build the repo-qualified set of issues/PRs touched this session — scoped session-state PRs, the merging PR and its linked issue, and in-scope tool history. Use [completion reconciliation](../../reference/issue-completion.md) only for the current merged delivery. The rest of the sweep records missed closures without making issue writes. Preserve partial/parent work and unresolved retry evidence. For other shared-state drift, flag `SWEEP_NEEDS_DECISION` without auto-editing:

- **Status accuracy** — issue still open but its work merged? PR merged but linked issue not auto-closed?
- **Linkage** — PR missing `Closes #N` for work that clearly resolves an issue?
- **AC checkbox truthfulness** — Test Plan boxes checked that the code does not actually satisfy?

#### Step 3.10a: Category 5a — Churn hotspots (issue #755)

Run **after** Step 2.5's root-main sync:

```bash
CHURN_SH=$(resolve_script churn-hotspots.sh || true)
CHURN_PLAN_SH=$(resolve_script churn-hotspot-wrap-plan.sh || true)
CHURN_JSON=""; CHURN_PLAN=""; CHURN_RC=0
if [ -n "$CHURN_SH" ]; then
  CHURN_JSON=$("$CHURN_SH" --json \
      --exclude ".claude/reference/script-extraction-audit.md" \
      2>/dev/null) || CHURN_RC=$?
fi
```

Exit `1` (nothing crossed threshold) or missing detector → end category immediately, file nothing, add no report line. `existing_lookup_failed == true` → add one `SWEEP_NEEDS_DECISION` bullet: ``Churn hotspot check ran but the existing-issue lookup failed — re-run `churn-hotspots.sh --json` before filing.`` Then **end this category immediately**: do not invoke the classifier, comment, or file. The classifier independently rejects this envelope as a defense in depth.

On detector exit `0`, classify the envelope with the read-only consumer before any mutation:

```bash
if [ -z "$CHURN_PLAN_SH" ]; then
  SWEEP_NEEDS_DECISION="${SWEEP_NEEDS_DECISION}
- Churn hotspot classifier is unavailable — no hotspot issue or comment was changed; restore \`churn-hotspot-wrap-plan.sh\` and re-run /wrap."
elif ! CHURN_PLAN=$(printf '%s' "$CHURN_JSON" | "$CHURN_PLAN_SH" --pr "$PR_NUMBER" 2>/dev/null); then
  SWEEP_NEEDS_DECISION="${SWEEP_NEEDS_DECISION}
- Churn hotspot classifier failed — no hotspot issue or comment was changed; run \`churn-hotspot-wrap-plan.sh --help\` and re-run /wrap."
fi
```

An empty `CHURN_PLAN` ends the category after recording that failure. A valid plan has disjoint `comment_set`, `file_set`, `material_growth_set`, `unknown_state_set`, and `suppressed_set` arrays. It also selects the top file candidate and computes the aggregate summary. **Do not reimplement those predicates inline** — that would let the tested consumer and the mutating workflow drift.

Score formula, threshold calibration, and the capping rationale: **`.claude/reference/churn-hotspots.md`**.

**Four independent branches operate on the classifier's different sets** (do NOT select once and then branch):

- **Comment set** — every row in `CHURN_PLAN.comment_set`, uncapped. The classifier includes an open match only when its `pr_numbers` contain the PR this wrap just merged, preserving comment idempotency. For each member:

  ```bash
  gh issue comment "$EXISTING" --body "Still churning: ${PR_COUNT} distinct merged PRs have touched \`${FILE}\` since ${SINCE}${CONFLICT_CLAUSE}: ${PR_LIST}.

  _Evidence appended by /wrap after PR #${PR_NUMBER} merged._"
  ```

  Record `Appended evidence to #{EXISTING} — churn hotspot \`{file}\`` for Step 4.3.

- **File set** — **at most one new issue per run**, still. Use `CHURN_PLAN.selected_file`; the classifier sorts all eligible `file_set` rows by score, PR count, then path. Eligible hotspots remain exactly two kinds:
  1. `existing_hotspot_issue == null` — never ticketed. Files clean, exactly as before.
  2. `existing_hotspot_issue_state == "closed"` **and** `conflict_rounds > 0` — a **closed-match re-file** (issue #915). Adds the caveat line below and is reported under `SWEEP_NEEDS_DECISION`, not `SWEEP_AUTO_HANDLED`.

  A closed match with `conflict_rounds == 0` is **not eligible** — it falls to the growth-or-suppression branches below. Title: `Refactor hotspot: {file}`. Body must include the `<!-- churn-hotspot: {file} -->` marker verbatim (re-find fallback):

  ```bash
  # Set only for kind 2 — a closed exact match is a WEAK match in the
  # `autofile-dedup.md` sense, so it re-files with a caveat, never cleanly.
  CLOSED_DUP=""
  if [ -n "${CLOSED_DUP_NUM:-}" ]; then
    CLOSED_DUP=$'\n\n'"Possibly duplicates #${CLOSED_DUP_NUM} — same file, closed after review; re-filed because the churn has since cost ${CONFLICT_ROUNDS} conflict re-resolution(s)."
  fi
  if NEW_URL=$(gh issue create \
      --title "Refactor hotspot: ${FILE}" \
      --body "Filed by /wrap churn detection after PR #${PR_NUMBER} merged.${CLOSED_DUP}

  \`${FILE}\` was touched by ${PR_COUNT} distinct merged PRs since ${SINCE}${CONFLICT_CLAUSE}: ${PR_LIST}.

  This is an observational report, not a prescription — it flags a file worth a
  closer look, not a decision that it should be split.

  <!-- churn-hotspot: ${FILE} -->

  _Filed via /wrap._" 2>&1); then
    NEW_NUM=$(echo "$NEW_URL" | grep -oE '[0-9]+$')
    if [ -n "$NEW_NUM" ]; then
      SWEEP_FILED="${SWEEP_FILED} ${NEW_NUM}"
      # Append {number, title, keywords, rationale: "churn hotspot: {file}"} to WRAP_FILED_ISSUES.
      # Append $NEW_NUM to DEDUP_EXCLUDE.
      # Kind 2 only: a re-file over a closed issue is a decision, not an
      # auto-handled filing, so it must reach SWEEP_NEEDS_DECISION (Step 3.13
      # renders from that list — appending to SWEEP_FILED alone would ship the
      # re-file with no owner-visible flag).
      if [ -n "${CLOSED_DUP_NUM:-}" ]; then
        SWEEP_NEEDS_DECISION="${SWEEP_NEEDS_DECISION}
- Re-filed #${NEW_NUM} for churn hotspot \`${FILE}\` over closed #${CLOSED_DUP_NUM} — the churn has now cost ${CONFLICT_ROUNDS} conflict re-resolution(s). Confirm the re-file or close it as still-by-design."
      fi
    else
      echo "WARNING: created issue but could not parse number from: $NEW_URL"
    fi
  else
    echo "WARNING: gh issue create failed: $NEW_URL"
  fi
  ```

  `CONFLICT_CLAUSE` is empty when `conflict_rounds` is 0; otherwise ` and re-resolved conflicts {conflict_rounds} time(s)`. `CONFLICT_ROUNDS` is that hotspot's `conflict_rounds`, and `CLOSED_DUP_NUM` its `existing_hotspot_issue` — both set only on a kind-2 re-file, left unset for kind 1.

- **Material-growth set** — every row in `CHURN_PLAN.material_growth_set`. These are closed matches with zero conflict cost whose current score is at least **2×** the score recorded in `.claude/reference/churn-hotspot-baselines.json`. File nothing, but add one `SWEEP_NEEDS_DECISION` bullet naming the file, issue, current score, baseline score, and baseline date. Doubling is the material-growth threshold: it is a step-change in centrality rather than the routine linear growth expected from catalogs and junction files. The five standing no-action verdicts on Issues #816, #860, #900, #916, and #1223 remain suppressed until this gate trips.

- **Suppressed set** — every row in `CHURN_PLAN.suppressed_set`. Add **no per-file decision bullet**. These are closed matches with zero conflict cost that are below the 2× gate, plus closed matches with no valid file-and-issue baseline. A missing baseline means growth is unproven, not that growth occurred; the row remains visible through the aggregate count. When a zero-conflict hotspot issue is closed or the owner reaffirms a keep verdict, add or refresh its file-keyed baseline with the issue number, score, PR count, and decision date. A baseline records a real owner decision and must never be raised merely to silence a growth signal.

- **Unknown-state set** — every row in `CHURN_PLAN.unknown_state_set`. **Neither comment nor file**, and add one `SWEEP_NEEDS_DECISION` bullet naming the issue and file. The classifier treats any non-null issue whose state is not open/closed as unknown; only a null issue is a never-ticketed file candidate.

After all five branches, append exactly one `SWEEP_AUTO_HANDLED` entry from `CHURN_PLAN.summary`, followed by `` — run `churn-hotspots.sh` to see them.`` The verbose shape is: ``{total} churn hotspots, {conflict-cost count} with conflict cost, {decision count} surfaced for decision, {suppressed count} suppressed (closed, no conflict cost)``. This replaces N closed/no-cost decision bullets with one auditable count and does not print on the silent default. When `held_back_file_count > 0`, append the existing file-cap clause to that same entry: ``; filed the top hotspot, {N} further candidate(s) held back``. Do not create a second churn summary line.

This category's authoritative re-find is the script's **exact** title/marker match — do not run `dedup_search` here.

#### Step 3.11: Category 6 — Time-sensitive items

Scan transcript for deadline/date/day-of-week phrases: `by Thursday`, `before the release`, `next week`, `end of month`, `by EOD`, `in N days`. Convert every relative reference to an absolute date:

```bash
TODAY=$(date +%Y-%m-%d)
```

`/schedule` available → propose a `/schedule` task per item (surface the proposed command; do not auto-schedule). Unavailable → plain-text reminder with absolute date. No phrases found → **produce no output**.

Add any items to `SWEEP_NEEDS_DECISION`.

#### Step 3.12: Category 7 — Future-self handoff

**Only if the session deferred something meaningful** (Category 1, 2, 5, or 6 produced surfaced items). On a clean session, **skip entirely**. When warranted, generate **one paragraph**: task, what shipped, what was deliberately left, the single most important next step. Add as the final `SWEEP_NEEDS_DECISION` entry.

#### Step 3.13: Persist sweep state & compute verdict

```bash
if [ -n "$SESSION_STATE_SH" ]; then
  # Build from the run-scoped WRAP_FILED_ISSUES registry (includes both Part A and Part B filings),
  # not SWEEP_FILED alone (Part B only). Extract the .number field from each entry.
  ALL_FILED_JSON=$(printf '%s\n' "$WRAP_FILED_ISSUES" | jq -sc '[.[].number] | map(numbers)')
  NEW_FILED_JSON=$ALL_FILED_JSON
  PRIOR_FILED_JSON="$SWEEP_PRIOR_FILED"
  case "$PRIOR_FILED_JSON" in ""|null) PRIOR_FILED_JSON='[]' ;; esac
  FILED_JSON=$(jq -cn --argjson a "$PRIOR_FILED_JSON" --argjson b "$NEW_FILED_JSON" '($a + $b) | unique')
  AUTO_JSON=$(printf '%s\n' "$SWEEP_AUTO_HANDLED" | jq -R 'select(length>0)' | jq -cs .)
  NEEDS_JSON=$(printf '%s\n' "$SWEEP_NEEDS_DECISION" | jq -R 'select(length>0)' | jq -cs .)
  if ! "$SESSION_STATE_SH" \
    --set ".prs[\"$PR_NUMBER\"].wrap_sweep.swept_at=\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"" \
    --set ".prs[\"$PR_NUMBER\"].wrap_sweep.filed_issues=$FILED_JSON" \
    --set ".prs[\"$PR_NUMBER\"].wrap_sweep.auto_handled=$AUTO_JSON" \
    --set ".prs[\"$PR_NUMBER\"].wrap_sweep.needs_decision=$NEEDS_JSON"; then
    echo "WARNING: failed to persist wrap_sweep state — a re-run may not be idempotent; the Step 3.7 dedup search still guards against open duplicates." >&2
  fi
fi
```

**UNION** this run's filed numbers with any recorded by a prior sweep — never overwrite. A re-run with no new filings must NOT erase the earlier record.

Compute **verdict** (one of exactly two canonical strings — no improvised wording):

- `0` pending → **`Clear to archive`**
- `N > 0` pending → **`N items pending your decision before archive`**

Proceed immediately to Step 3.14 — do not ask.

#### Step 3.14: Post-merge TestFlight release (iOS repos — issue #1169)

The merge has landed, so the release decision can be reported and the deferred half executed. Three things happen, in order; all are non-fatal.

```bash
# RELEASE_DECIDE and RELEASE_SWEEP were resolved in the preamble.

# 1. Mechanisms that must fire AFTER the merge (workflow_dispatch, none).
#    Dispatching before the merge would have built the pre-merge default branch.
REL_RC=0; REL_OUT=""
if [ -x "$RELEASE_DECIDE" ]; then
  REL_OUT=$("$RELEASE_DECIDE" --pr "$PR_NUM" --phase post-merge --apply 2>&1) || REL_RC=$?
fi

# 2. ONE success line for the whole step, no matter how many phases applied or
#    how many repos the sweep touched. `/wrap`'s silent path allows exactly one
#    `merged PR #N` line, so a successful release must not add several more.
#    Failures and blockers still print one terse line each.
#
#    A blocker is reported for EVERY exit 3, including one that produced no
#    parseable output — a release that failed is exactly what must not vanish.
#    `.reason // "unknown"` alone would not cover that: jq's default fills a
#    MISSING KEY, but on non-JSON input (a crash message, a partial write) jq
#    exits non-zero and prints nothing, leaving an empty reason. So each field
#    falls back explicitly, ending at the raw output and then at the exit code.
#    Diagnostics are collapsed to one line before printing — release-decide.sh
#    stderr can be multi-line, and a blocker that spans six lines is not "one
#    terse line".
RELEASE_CUT=""
one_line() { printf '%s' "$1" | tr '\n' ' ' | sed 's/  */ /g; s/^ //; s/ $//' | cut -c1-300; }

for pair in "$RELEASE_PREMERGE_RC:$RELEASE_PREMERGE_OUT" "$REL_RC:$REL_OUT"; do
  rc="${pair%%:*}"; out="${pair#*:}"
  applied=$(printf '%s' "$out" | jq -r '.applied // false' 2>/dev/null)
  repo=$(printf '%s' "$out" | jq -r '.repo // empty' 2>/dev/null)
  reason=$(printf '%s' "$out" | jq -r '.reason // empty' 2>/dev/null)
  [ -n "$repo" ] || repo="this repo"
  [ -n "$reason" ] || reason="${out:-no output from release-decide.sh (exit $rc)}"
  if [ "$rc" = "0" ] && [ "$applied" = "true" ]; then
    RELEASE_CUT="$repo"
  elif [ "$rc" = "3" ]; then
    echo "TestFlight release blocked — $repo: $(one_line "$reason")" >&2
  fi
done

# 3. Follow in-flight builds to a terminal state and cut any pending build whose
#    window has opened — including markers left by threads that have since ended.
#    Scoped to the repo just merged: sweeping every other repo carrying release
#    state is the periodic surfaces' job (session start, each PMM tick).
#    --quiet keeps its human lines out of the silent path; attention events are
#    re-emitted below, one line each.
MERGED_REPO=$(gh repo view --json nameWithOwner --jq .nameWithOwner 2>/dev/null || true)
if [ -x "$RELEASE_SWEEP" ]; then
  SWEEP_ARGS=()   # an array: zsh keeps `${X:+--repo "$X"}` as one word (issue #1857)
  [ -n "$MERGED_REPO" ] && SWEEP_ARGS=(--repo "$MERGED_REPO")
  SWEEP_OUT=$("$RELEASE_SWEEP" ${SWEEP_ARGS[@]+"${SWEEP_ARGS[@]}"} --json 2>/dev/null) || true
  if [ -n "${SWEEP_OUT:-}" ]; then
    # A build the sweep cut folds into the single success line rather than adding
    # one of its own.
    SWEEP_CUT=$(printf '%s' "$SWEEP_OUT" | jq -r '[.[]? | select(.kind == "build_cut")] | first | .repo // empty' 2>/dev/null)
    [ -n "$SWEEP_CUT" ] && [ -z "$RELEASE_CUT" ] && RELEASE_CUT="$SWEEP_CUT"
    printf '%s' "$SWEEP_OUT" | jq -r '.[]? | select(.needs_attention == true) | .line' 2>/dev/null >&2
  fi
fi

# 4. The single success line, emitted once, only when something actually shipped.
[ -n "$RELEASE_CUT" ] && echo "cut TestFlight build — $RELEASE_CUT (PR #$PR_NUM)"
```

Exit `3` from either phase is a blocker worth one line (a trigger that failed, or a build cut whose state could not be saved and so will not be followed). Exit `1` (pending or suppressed), `2` (inert), and `4` (environment) are all silent and all non-fatal — `/wrap` never fails a completed merge over release automation.

Mechanism, policy schema, and the per-repo trigger table: `.claude/reference/release-cadence.md`.

Proceed immediately to Phase 4 — do not ask.

## Phase 4: Lessons Learned (Depth-Adaptive)

### Step 4.1: Assess session complexity

- **Cycle count** — resolve it without conflating an unavailable/failed helper with a real zero:
  ```bash
  CYCLES=""
  CYCLE_COUNT_VALID=0
  if [[ -z "$CYCLE_COUNT_SH" ]]; then
    echo "DEGRADED: cycle-count.sh not found (checked all three paths) — cycle count unavailable" >&2
  elif CYCLES=$("$CYCLE_COUNT_SH" "$PR_NUM") && [[ "$CYCLES" =~ ^[0-9]+$ ]]; then
    CYCLE_COUNT_VALID=1
  else
    CYCLES=""
    echo "DEGRADED: cycle-count.sh failed or returned a non-numeric value — cycle count unavailable" >&2
  fi
  ```
- **Thread length** — count user + assistant messages. "Short" = fewer than 15 total.
- **PR size** — `gh pr view N --json files --jq '.files | length'`

**Trivial threshold:** `CYCLE_COUNT_VALID=1` AND cycle count = 0 AND conversation short (<15 messages) AND <5 files changed. An unknown cycle count is non-trivial.

### Step 4.2: Run lessons (or skip)

**If trivial:** set `WRAP_LESSONS_COUNT=0` and skip to Step 4.3.

**If non-trivial:** reflect on the session — task, what was accomplished, what went wrong, patterns, surprises, workarounds to codify. **Merge in `SWEEP_MEMORY_CANDIDATES`** from Phase 3 Category 4 and dedup against the lessons before writing.

For each actionable, novel lesson:
- Check `MEMORY.md` for duplicates — update existing memories rather than creating new ones
- Write memory files with proper frontmatter (`feedback`, `project`, or `user` type)
- Add pointers to `MEMORY.md`

Memory writes happen in both modes. Record `WRAP_LESSONS_COUNT`. Verbose mode prints the full `## Session Lessons` block; the silent default prints no lessons ack at all — the memory files are the record.

### Step 4.3: Final report

`/wrap` has three output paths: the **silent default**, the **verbose report**, and the **Blocker path** (prints in *both* modes).

#### Silent default

On a clean run, print exactly one line — `merged PR #N`, plus `$NRO_SUFFIX` when the no-review override merged it (`merged PR #N (no AI review — logged)`, issue #1899) — then the close-hint line from the block below when it is non-empty, and nothing else: no follow-up list, no lessons ack, no sweep summary. All of it is already recorded (GitHub, `WRAP_FILED_ISSUES`, `session-state.json` `wrap_sweep`, memory) and re-renders in full under `--verbose` or on request.

**Close hint (issue #2605).** After a verified merge, record it and check whether this thread is idle. Resolve `thread-boundary.sh` with `resolve_script`. Skip the block when the script is missing or none of `CLAUDE_CODE_SESSION_ID`, `CLAUDE_SESSION_ID`, and `CLAUDE_BGCEILING_SESSION_ID` is set — do not mention the skip, and do not assign `CLAUDE_SESSION_ID`. Reuse Step 3.1's `ISSUE_N` (the issue this PR closes); if this shell does not still have it, resolve it again with `pr-issue-ref.sh --first`. Count in-flight work with `safe-close-inventory.sh --json` when it resolves. `verdict` `safe` covers only the shell-readable categories. `--in-flight 0` requires that verdict and `needs_agent_checks` literally `false` (missing counts as in flight). When `needs_agent_checks` is true, inspect each `agent_checks` entry the way `/safe-close` does, then set `IN_FLIGHT=0` and run `close-hint` only if every one is clear. Any other verdict, a check you cannot finish, or a missing or failed inventory means `--in-flight 1` (unverified is not idle).

```bash
THREAD_BOUNDARY=$(resolve_script thread-boundary.sh || true)
THREAD_SESSION_ID="${CLAUDE_CODE_SESSION_ID:-${CLAUDE_SESSION_ID:-${CLAUDE_BGCEILING_SESSION_ID:-}}}"
IN_FLIGHT=1
CLOSE_HINT=""
if [[ -n "$THREAD_BOUNDARY" && -n "$THREAD_SESSION_ID" ]]; then
  SAFE_CLOSE=$(resolve_script safe-close-inventory.sh || true)
  if [[ -n "$SAFE_CLOSE" ]] && INV=$("$SAFE_CLOSE" --json 2>/dev/null) \
    && jq -e '.verdict == "safe" and .needs_agent_checks == false' <<<"$INV" >/dev/null 2>&1; then
    IN_FLIGHT=0
  fi
  if [[ -n "${ISSUE_N:-}" ]]; then
    "$THREAD_BOUNDARY" note-merge --session "$THREAD_SESSION_ID" --issue "$ISSUE_N" --pr "$PR_NUM" >/dev/null || true
  fi
  CLOSE_HINT=$("$THREAD_BOUNDARY" close-hint --session "$THREAD_SESSION_ID" --in-flight "$IN_FLIGHT" || true)
fi
```

Print `merged PR #N` first (with `$NRO_SUFFIX` when it applies). If `CLOSE_HINT` is non-empty, print that line next, exactly as the script wrote it: `Safe to close this thread — nothing else is in flight.` Do not ask, do not wait, and do not print it on a later `/wrap`. `/pm` and `/subagent` threads get no line; the script already suppresses it. A non-zero exit does not stop `/wrap`.

This bounds `/wrap`'s **own** output only. The thread's single end-of-task wrap-up (`.claude/reference/final-wrapup-format.md`) is a task-level always-emit exception under `CLAUDE.md` #3 — it is not `/wrap` output, and it does not violate this default. Its trigger is the **task** reaching terminal state, never a `/wrap` return: a fleet loop such as `/pr-monitor-and-manage`, which calls `/wrap` once per merge-ready PR and keeps managing the fleet, emits no wrap-up on those returns — only one when the fleet run itself ends.

Print **only** items that need an explicit decision from the user, one terse line each:

- **Blocker or stop condition** → the Blocker path below. Mandatory — silence must never swallow a stop.
- **A filing that needs your judgment** → `gh issue create` failed, or the issue was filed with a `Possibly duplicates #{N}` caveat: `Filed [#{a}](url) — possibly duplicates #{b}.` A filing with no caveat is recorded silently.
- **Sweep verdict, only when items are pending** → `{N} item(s) pending your decision — run /wrap --verbose for detail.` Omit entirely on `Clear to archive`.
- **`[COVERAGE]` degraded** and the **`[INFERRED]` checkpoint** — per the always-print list in **Output modes**.

When none of those apply, `/wrap` emits `merged PR #N` and, when the close-hint block printed one, that single line after it. (`CLAUDE.md` #3)

#### Verbose report (`--verbose`, or on explicit request)

Print this block first:

```
## Wrapped up

**Merged:** PR #{N} ({title}){$NRO_SUFFIX — no-review override only} — {≤3 sentences on the gist of what changed}.{ Only when main-sync was noteworthy — an `aborted:` outcome or a quarantine actually ran: " Main: {one-line status}."}

**Follow-ups:** {either "Opened [#{a}](url) — {one line}; [#{b}](url) — {one line}." listing every WRAP_FILED_ISSUES entry (≤3 sentences), or "No follow-ups opened."}{ When any filing was suppressed as a duplicate: " {M} finding(s) appended to existing issues."}

{lessons ack, one line: "{WRAP_LESSONS_COUNT} lesson(s) captured to memory." | "Clean session — no lessons."}
{sweep line — ONLY when verdict is "N items pending your decision before archive": "Session sweep: {N} item(s) pending your decision — run `/wrap --verbose` for detail." Omit on "Clear to archive".}
```

Rules: **Merged ≤3 sentences; Follow-ups ≤3 sentences.** Every opened issue is listed (number + one-line + clickable link) — an issue not listed is indistinguishable from one silently dropped. Suppressed-as-duplicate findings: never silently dropped (surface count). Sweep verdict: appears only when decisions are pending.

Then the full `## Wrap-Up Complete` multi-section template, rendering rules, section caps, and exemptions: **`references/wrap-report-templates.md`**.

#### Blocker path (both modes)

Print a **single short line** naming the reason. Under `--verbose`, also append the full `WRAP_RECOVERY_AUDIT` / `missing` detail:

- **Merge gate not met after recovery cap** → `Merge blocked: {last missing summary}. Re-run /wrap or /fixpr.`
- **`mergeable == CONFLICTING`** → `Merge blocked: merge conflicts — run /merge-conflict.`
- **Human `CHANGES_REQUESTED` on HEAD** → `Merge blocked: changes requested by {login(s)}.`
- **AC checkbox verification failure (Step 2.2)** → `Merge blocked: AC item "{text}" not yet satisfied — fix the code first.`
- **`/fixpr` delegation failure (Step 2.1 Branch B)** → `Merge blocked: /fixpr could not resolve {blocker} — {FIXPR_WRAP_STATUS}.`
- **No PR found** → the Step 1.1f message verbatim.

The blocker line is **mandatory on the silent default** — silence must never swallow a stop.

The worktree and feature branch are intentionally left in place, reaped out-of-band by `/pm-clean`'s stale-cleanup pass. See `stale-cleanup.sh --help`.

Files in this skill

  • SKILL.md68 KB
  • references/wrap-merge-gate-recovery.md12 KB
  • references/wrap-pr-inference.md6.2 KB
  • references/wrap-report-templates.md3.9 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.