# How Yokai runs a phase

Yokai runs every phase in three acts: Plan, Build and Seal. Then it checks four facts on disk before it moves on. This page follows one phase from the board to the commit.

## The parts of a run

Yokai's supervisor is a loop. It picks the next phase, runs it, checks the result and decides whether to continue, halt or finish. It knows nothing about what the code should look like. That knowledge lives in the agent and in the files under `.koi/run/`.

Take one phase from the Daylist walkthrough: P2, "Persist tasks in localStorage". When you run `yokai --once`, the supervisor reads `.koi/run/BOARD.json`, finds P2 as the first `todo` card, reads its reviewed model tier, and picks an agent and model for each turn from your settings.

The supervisor never edits product code. It sends a prompt to the agent, one turn at a time, through Tori. Tori speaks the Agent Client Protocol (ACP). For Claude Code and Codex it starts a Node adapter: `claude-agent-acp` or `codex-acp` if that command is on `PATH`, else `npx -y @agentclientprotocol/claude-agent-acp` or `npx -y @agentclientprotocol/codex-acp`. Cursor and OpenCode serve ACP themselves through `cursor-agent acp` and `opencode acp`. Messages travel over the adapter process's standard input and output.

<figure class="diagram" aria-labelledby="dg-shared-ground-caption">
<figcaption id="dg-shared-ground-caption">The repository is shared ground</figcaption>
<div class="diagram-frame" tabindex="0" role="region" aria-labelledby="dg-shared-ground-caption">
<svg viewBox="0 0 560 240" width="560" height="240" role="img" aria-labelledby="dg-shared-ground-title dg-shared-ground-desc">
<title id="dg-shared-ground-title">Yokai and the coding agent meet in the repository</title>
<desc id="dg-shared-ground-desc">The Yokai supervisor talks to Tori, and Tori talks to the coding agent over ACP. The agent edits code and commits to the repository. The supervisor reads files, Git history and test results from the same repository.</desc>
<defs><marker id="dg-shared-ground-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="0" y="24" width="160" height="48" rx="4"/>
<text class="dg-label" x="80" y="48">Yokai supervisor</text>
<rect class="dg-box" x="200" y="24" width="160" height="48" rx="4"/>
<text class="dg-label" x="280" y="48">Tori · ACP</text>
<rect class="dg-box" x="400" y="24" width="160" height="48" rx="4"/>
<text class="dg-label" x="480" y="48">Coding agent</text>
<path class="dg-line" d="M164 48h32" marker-start="url(#dg-shared-ground-head)" marker-end="url(#dg-shared-ground-head)"/>
<path class="dg-line" d="M364 48h32" marker-start="url(#dg-shared-ground-head)" marker-end="url(#dg-shared-ground-head)"/>
<path class="dg-line" d="M80 160V80" marker-end="url(#dg-shared-ground-head)"/>
<text class="dg-note is-start" x="96" y="120">reads files, Git, tests</text>
<path class="dg-line" d="M480 80v80" marker-end="url(#dg-shared-ground-head)"/>
<text class="dg-note is-end" x="464" y="120">edits code, commits</text>
<rect class="dg-box is-solid" x="0" y="168" width="560" height="48" rx="4"/>
<text class="dg-label is-reverse" x="280" y="192">Repository: code, .koi/run/, Git history</text>
</svg>
</div>
</figure>

## One turn over ACP

A turn is one prompt and the agent's work until it stops. Each turn follows the same exchange:

1. `initialize`: Tori and the agent agree on a protocol version and capabilities.
2. `session/new`: Tori opens a session in the repository directory. An interrupted turn can reattach with `session/resume` or `session/load`.
3. `session/prompt`: Tori sends the turn prompt. While the agent works, `session/update` messages stream its text, thoughts, tool calls and file edits back to the cockpit.

When the agent asks permission to use a tool, Tori answers `session/request_permission` from the run's permission mode. The default is `approve-all`, because the safety net is the gate and Git, not prompts. Cost and token counts come from the agent; if it does not report usage, the cockpit shows a dash.

By default every turn gets a fresh session, so the agent starts from what is on disk. [Token savings](/en/docs/yokai/configuration#token-savings) reuses two sessions per phase instead.

## Plan, Build, Seal

A phase is divided into three acts. The supervisor writes the card's status before each act, so a relaunch knows where to resume.

1. Plan (`planning`). One agent turn writes `.koi/run/phases/phase-02-<slug>.md`: goal, an "Explicitly out" list, and a numbered approach. Then the agent stops without touching code. If a phase file for P2 already exists, Yokai skips this act.
2. Build (`building`). A loop of coder turns, sensor runs and judge turns until the change converges or the iteration budget runs out. [The Build loop](/en/docs/architecture/build-loop) explains it.
3. Seal (`sealing`). One agent turn records the verified outcome on the card, marks it `done`, updates `NOTES.md` and the phase file's Outcome, sets `nextAction`, and makes one conventional commit with everything the phase produced.

<figure class="diagram" aria-labelledby="dg-acts-caption">
<figcaption id="dg-acts-caption">A phase ends at the contract check</figcaption>
<div class="diagram-frame" tabindex="0" role="region" aria-labelledby="dg-acts-caption">
<svg viewBox="0 0 560 200" width="560" height="200" role="img" aria-labelledby="dg-acts-title dg-acts-desc">
<title id="dg-acts-title">Plan, Build, Seal, then the contract check</title>
<desc id="dg-acts-desc">Plan leads to Build, Build to Seal, and Seal to the contract check. When all four facts of the contract hold, the supervisor moves to the next phase. When Build does not converge, or a contract check fails, the phase goes to recovery, which can grant another Build.</desc>
<defs><marker id="dg-acts-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="0" y="24" width="96" height="48" rx="4"/>
<text class="dg-label" x="48" y="48">Plan</text>
<path class="dg-line" d="M100 48H124" marker-end="url(#dg-acts-head)"/>
<rect class="dg-box" x="128" y="24" width="96" height="48" rx="4"/>
<text class="dg-label" x="176" y="48">Build</text>
<path class="dg-line" d="M228 48H252" marker-end="url(#dg-acts-head)"/>
<rect class="dg-box" x="256" y="24" width="96" height="48" rx="4"/>
<text class="dg-label" x="304" y="48">Seal</text>
<path class="dg-line" d="M356 48H380" marker-end="url(#dg-acts-head)"/>
<rect class="dg-box is-solid" x="384" y="24" width="176" height="48" rx="4"/>
<text class="dg-label is-reverse" x="472" y="48">Contract check</text>
<path class="dg-line" d="M520 76V132" marker-end="url(#dg-acts-head)"/>
<text class="dg-note is-end" x="504" y="104">all hold</text>
<rect class="dg-box" x="384" y="136" width="176" height="48" rx="4"/>
<text class="dg-label" x="472" y="160">Next phase</text>
<path class="dg-line is-dashed" d="M176 76V132" marker-start="url(#dg-acts-head)" marker-end="url(#dg-acts-head)"/>
<text class="dg-note is-start" x="192" y="104">not converged · Build again</text>
<path class="dg-line is-dashed" d="M400 76V160H308" marker-end="url(#dg-acts-head)"/>
<text class="dg-note is-start" x="316" y="176">fails</text>
<rect class="dg-box" x="128" y="136" width="176" height="48" rx="4"/>
<text class="dg-label" x="216" y="160">Recovery</text>
</svg>
</div>
</figure>

## The contract

The agent's message at the end of Seal does not count. After Seal the supervisor checks four facts, in this order:

1. The card advanced: P2's status is `done`.
2. A new commit exists: `HEAD` moved since the phase started.
3. The gate is green: every script in `.koi/sensors/` exits 0. If Seal left the product files exactly as Build verified them, Yokai reuses the green Build result instead of running the sensors again.
4. The tree is clean: nothing the phase touched is left uncommitted. Files that the gate run itself rewrote do not count.

If all four hold, the phase is done and the supervisor moves on. If the card did not advance, the supervisor retries the phase once in a fresh run; a second miss in a row is a stall. Any other miss goes to [recovery](/en/docs/architecture/recovery), or halts the run when recovery is off.

Seal's `done` is provisional. If the gate is red after Seal, the supervisor reopens the card as `todo` and restores `nextAction` to `/junji next`, so a relaunch runs the phase again instead of skipping it. Seal's commit stays in history. [Verification on disk](/en/docs/architecture/verification) covers the gate in detail.

## Example: what P2 leaves behind

After a clean run of P2:

- `git log -1` shows a commit like `feat(storage): persist tasks and recover from invalid data`.
- P2's card has `"status": "done"` and a one-line outcome. `nextAction` is still `/junji next` because P3 is `todo`.
- `.koi/run/phases/phase-02-persistence.md` has an Outcome section.
- `.koi/run/ledger.jsonl` has a new row. It looks like this, with the hashes shortened and some fields left out:

```json
{"phase":"P2","agent":"claude","model":"sonnet","tokens":131526,"cost_usd":0.41,"gate_passed":true,"advanced":true,"commit_made":true,"head_before":"07fe780…","head_after":"7e534a0…"}
```

- `.koi/run/FEEDBACK.md` is gone. Yokai deletes it before Seal when Build converges, so it is never committed.

## If the run stops

Open the same checkout and run `yokai`. The supervisor reads the card's status. If P2 was `building`, it resumes Build; if `sealing`, it resumes Seal. An interrupted turn can reattach to its agent session through `.koi/run/live-session.json`.

## What can go wrong

- `phase P2 marked done but produced no commit`. The agent marked the card but did not commit. With recovery on, Repair commits the work if the gate is green.
- `gate red after phase P2 (sensor 30-test, ...)`. Seal changed product code or a test is flaky. The card is back to `todo`; fix the cause and relaunch.
- `phase P2 left uncommitted changes`. A build output or dependency folder ended up in the tree. Repair adds it to `.gitignore` when it is plainly regenerable.
- `phase P2 made no progress in 2 runs`. The phase is a stall. Recovery's Diagnose turn reads the failure; if it cannot fix it, use `/junji refine` to split the phase.
