---
name: multi-agent-postmortem
description: "Post-mortem of a run from its session transcripts: loops, stalls, root-cause hypotheses on file:line, fixes. Read-only, redacted. Use when a run went wrong or ran far too long."
user-invocable: true
argument-hint: "[#id | transcript.jsonl] [--audit]  -  a run (task id or #N) or a session file; no argument means the most recent run. --audit re-checks the draft against the index"
metadata:
  language: en
---

# multi-agent postmortem  -  Run Post-Mortem

**Input**: $ARGUMENTS

Explains what a run actually did, read from the session transcripts it ran in,
not from what the run log says it decided. The evidence comes from a
deterministic indexer; the model only interprets it.

**Read-only.** Nothing is sent anywhere: no issue comment, no PR, no channel.
The one write is the report itself, saved under the run's log directory.

The indexer reads Claude Code session files (`~/.claude/projects/<dir>/<session>.jsonl`),
Copilot CLI session logs (`~/.copilot/session-state/<session>/events.jsonl`) and
Codex rollouts (`~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl`), detecting the
format per file. Copilot CLI reports tokens per session, so its totals come from
the shutdown record. When a run id finds nothing (exit 3), say so and ask for a
transcript path rather than reporting an empty run.

## Steps

1. **Resolve the target.**
   - A path ending in `.jsonl` (any of the three hosts): index that file
     (Claude Code subagent files beside it are included). There is no run directory, so nothing is saved unless the user
     names one.
   - A task id or `#N`: locate the run and its transcripts:
     ```bash
     node "$HOME/.claude/scripts/transcript-index.mjs" --run "<id>" --locate
     ```
     `matchedBy: "session"` means the run recorded its session id and the match
     is exact. `"time-window"` means it did not, so the files are the sessions,
     on any host, that ran in the run's worktree or checkout while it was active; say that
     this match is a heuristic. Exit 3 with a `reason`: report the reason, and
     offer to take a transcript path instead.
   - No argument: take the most recent run from
     `node "$HOME/.claude/scripts/runs-index.mjs" --json` (latest `startedAt`)
     and continue as above.

2. **Index.**
   ```bash
   node "$HOME/.claude/scripts/transcript-index.mjs" --run "<id>"         # or: <file.jsonl>
   ```
   Every item in the output carries `file:line`, the line of the JSONL it came
   from. Exit 4 means the redaction guard found a credential left in the output
   and printed nothing: report that and stop; never fall back to reading the
   transcript directly. Read only the index, never the transcript files.

3. **Write the post-mortem** in `prefs.global.outputLanguage`, short: what
   happened (five lines or fewer); where it looped or stalled, with citations;
   root-cause hypotheses, each anchored to at least one `file:line` (one with
   no citation is left out); suggested fixes tied to a hypothesis; evidence
   limits (matching method, malformed or truncated lines, the redaction count).
   Quote only excerpts the index already shows.

4. **Audit pass** (`--audit`): every cited `file:line` must exist in the index
   and say what the draft claims, every count must match. Correct or drop what
   does not hold, and add `audit: N claims checked, M corrected`.

5. **Save.** With a run directory, write the report to
   `<runDir>/postmortem.md` (`~` expanded) and give the path. That file is the
   only write this command makes.
