koi
Documentation / architecture

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.

The repository is shared ground
Yokai and the coding agent meet in the repository 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. Yokai supervisor Tori · ACP Coding agent reads files, Git, tests edits code, commits Repository: code, .koi/run/, Git history

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 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 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.
A phase ends at the contract check
Plan, Build, Seal, then the contract check 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. Plan Build Seal Contract check all hold Next phase not converged · Build again fails Recovery

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, 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 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:
{"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.