# Recovery

When a phase misses its contract, Yokai does not stop at once. It repairs bookkeeping, or asks a read-only turn to diagnose the miss and pick the next action. Every path ends with the same contract check.

## What recovery is for

Many phase misses are small. The agent forgot to commit a file. A build folder ended up in the tree. The sensors passed but the card was never marked `done`. Halting the run for these wastes a night of unattended work. Recovery handles them and still refuses to declare anything done on its own.

Recovery is on by default:

```yaml
# .koi/yokai.yml
recovery: true    # false halts immediately on a miss
```

## The cycle

1. A miss happens: a contract check fails, Build does not converge, or a phase stalls (no progress in two runs in a row; the first miss is a plain retry).
2. If the gate is green and only bookkeeping is unfinished, Yokai runs one Repair turn.
3. Otherwise, or if Repair did not resolve it, a Diagnose turn reads the failure and names the next action: `fix`, `repair` or `grant_build`.
4. The supervisor re-checks the contract. If the phase is still not done, the cycle continues.

<figure class="diagram" aria-labelledby="dg-recovery-caption">
<figcaption id="dg-recovery-caption">Every recovery path ends at the contract</figcaption>
<div class="diagram-frame" tabindex="0" role="region" aria-labelledby="dg-recovery-caption">
<svg viewBox="0 0 560 376" width="560" height="376" role="img" aria-labelledby="dg-recovery-title dg-recovery-desc">
<title id="dg-recovery-title">Repair, Diagnose, Fix and another Build</title>
<desc id="dg-recovery-desc">A contract or Build miss goes to Repair when the gate is green and only bookkeeping is left, and to Diagnose otherwise. Diagnose names one of three actions: repair, fix the run files, or grant another Build. Repair, Fix and the new Build all end with the supervisor verifying the contract again.</desc>
<defs><marker id="dg-recovery-head" viewBox="0 0 8 8" refX="7" refY="4" markerWidth="8" markerHeight="8" markerUnits="userSpaceOnUse" orient="auto-start-reverse"><path class="dg-head" d="M0 0 8 4 0 8z"/></marker></defs>
<rect class="dg-box" x="168" y="24" width="224" height="48" rx="4"/>
<text class="dg-label" x="280" y="48">Contract or Build miss</text>
<path class="dg-line" d="M280 76V96H80V116" marker-end="url(#dg-recovery-head)"/>
<path class="dg-line" d="M280 96H480V116" marker-end="url(#dg-recovery-head)"/>
<text class="dg-note" x="168" y="84">green gate, bookkeeping</text>
<text class="dg-note" x="392" y="84">anything else</text>
<rect class="dg-box" x="0" y="120" width="160" height="48" rx="4"/>
<text class="dg-label" x="80" y="144">Repair</text>
<rect class="dg-box" x="400" y="120" width="160" height="48" rx="4"/>
<text class="dg-label" x="480" y="144">Diagnose</text>
<path class="dg-line" d="M396 144H164" marker-end="url(#dg-recovery-head)"/>
<text class="dg-note" x="280" y="132">repair</text>
<path class="dg-line" d="M520 172V212" marker-end="url(#dg-recovery-head)"/>
<text class="dg-note is-start" x="528" y="192">fix</text>
<path class="dg-line" d="M440 172V196H280V212" marker-end="url(#dg-recovery-head)"/>
<text class="dg-note" x="360" y="184">grant_build</text>
<rect class="dg-box" x="200" y="216" width="160" height="48" rx="4"/>
<text class="dg-label" x="280" y="240">Build again</text>
<rect class="dg-box" x="400" y="216" width="160" height="48" rx="4"/>
<text class="dg-label" x="480" y="240">Fix run files</text>
<path class="dg-line" d="M80 172V308" marker-end="url(#dg-recovery-head)"/>
<path class="dg-line" d="M280 268V308" marker-end="url(#dg-recovery-head)"/>
<path class="dg-line" d="M480 268V308" marker-end="url(#dg-recovery-head)"/>
<rect class="dg-box is-solid" x="0" y="312" width="560" height="48" rx="4"/>
<text class="dg-label is-reverse" x="280" y="336">Verify the contract again</text>
</svg>
</div>
</figure>

## The three turns

| Turn | Can edit | Does |
| --- | --- | --- |
| Repair | Commits, `.gitignore`, the card status | Commits a deliverable left behind, ignores regenerable noise (`node_modules/`, `dist/`, caches) with `git rm --cached`, or marks the card `done` when the gate is green and the commit already exists. Never touches product code, never amends. |
| Diagnose | Nothing, it is read-only | Reads `FEEDBACK.md`, earlier saves and the miss, then returns one line of JSON: `{"next": "fix", "diagnosis": "...", "guide": null}`. It cannot halt. If its answer cannot be parsed, Yokai grants another Build. |
| Fix | Files under `.koi/`, the board and notes, `.gitignore` | Repairs the run itself: a wrong sensor path, a broken phase file, a missing ignore rule. Never product source. Product code changes belong to Build. |

A granted Build skips Plan and runs a full Build budget, steered by the `guide` Diagnose wrote. In the cockpit it looks like any other Build.

The turns use their own model tiers. Diagnose (role `triage`), Repair and Fix default to the `ultra` band. You can lower them with `roles.triage.tier`, `roles.repair.tier` or `roles.fix.tier`.

## Reopening a done card

If the gate is red after Seal, the supervisor reopens only that card as `todo` and restores `nextAction` to `/junji next` in the same write. The next run re-enters the phase.

Remember that Seal already replaced the card's `description` with the outcome. A reopened card does not get its original scope back. The scope is still in `.koi/run/phases/phase-NN-*.md`, which Plan reuses because the file exists.

## Records

Each recovery episode is a save: a Markdown file in `.koi/run/saves/` (`0001.md`, `0002.md`, ...) with what failed, the diagnosis, what was tried and the outcome. `.koi/run/RECOVERY.md` indexes them. Saves are records, not snapshots. "Going back" means re-entering the run from the current files, not restoring an old tree.

There is no limit on cycles or saves. What stops recovery:

- `--max-cost` or `--max-tokens`, if you set them.
- You pressing `x` to abort.
- `--guarded`.

## Example

P3 seals and the contract check finds `dist/` uncommitted. The gate is green, so Yokai runs Repair. The agent adds `dist/` to `.gitignore`, makes a new commit and reports `{"decision": "ignored dist/ build output", "why": "regenerated by npm run build", "pointer": "a1b2c3d"}`. The contract passes on the next check and P3 is done. `.koi/run/saves/0001.md` records the episode.

## What can go wrong

- Diagnose keeps granting Build and the sensors stay red. Read `.koi/run/saves/` and `FEEDBACK.md`. The phase plan may be wrong, or the check may expose a real design gap. Stop the run, fix the plan, and relaunch. Set `--max-cost` for unattended nights.
- Repair leaves a path dirty. It refuses to guess about files that look like product source. Diagnose takes over; if it is still unclear, commit or delete the file yourself.
- You want the old fail-fast behaviour. Set `recovery: false`. Any contract or Build miss then halts at once.
