# The Build loop

Build is where the code gets written. Each iteration is one coder turn, then the sensors, then one judge turn if the sensors are green. It repeats until nothing blocks or the budget runs out.

## What Build is

Build is the second act of every phase. Its input is the phase plan (the spec) that Plan wrote. Its output is a verdict: `converged` or `not-converged`. Build never commits, never marks the card and never says "done". Seal and the contract check do that.

Every Build runs one coder and one judge. There is no fan-out to several workers and no strategy to choose.

## One iteration

1. Coder turn. The agent reads the phase plan, any guides, and `.koi/run/FEEDBACK.md`, then edits the working tree. It must not commit and must not edit `FEEDBACK.md`.
2. Sensors. Yokai runs every script in `.koi/sensors/` in filename order, from the repository root. The first non-zero exit stops the run and names the sensor.
3. Judge turn, only if every sensor passed. A read-only agent turn reads `git diff` against the phase plan and returns findings as JSON. Each finding is `blocking` or `nit`.

If a sensor failed or the judge reported a blocking finding, Yokai appends the details to `FEEDBACK.md` and starts the next iteration. If the sensors are green and nothing blocks, Build returns `converged`.

<figure class="diagram" aria-labelledby="dg-build-caption">
<figcaption id="dg-build-caption">Sensors decide, the judge can only veto</figcaption>
<div class="diagram-frame" tabindex="0" role="region" aria-labelledby="dg-build-caption">
<svg viewBox="0 0 560 232" width="560" height="232" role="img" aria-labelledby="dg-build-title dg-build-desc">
<title id="dg-build-title">One Build iteration and its two exits</title>
<desc id="dg-build-desc">The coder turn edits the tree, then the sensors run. If any sensor is red, the failure goes to FEEDBACK.md. If all sensors are green, the judge turn reviews the diff. A blocking finding also goes to FEEDBACK.md. With no blocking finding, Build has converged. FEEDBACK.md feeds the next coder turn.</desc>
<defs><marker id="dg-build-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="40" width="160" height="48" rx="4"/>
<text class="dg-label" x="80" y="64">Coder turn</text>
<path class="dg-line" d="M164 64H196" marker-end="url(#dg-build-head)"/>
<rect class="dg-box" x="200" y="40" width="160" height="48" rx="4"/>
<text class="dg-label" x="280" y="64">Sensors</text>
<path class="dg-line" d="M364 64H396" marker-end="url(#dg-build-head)"/>
<text class="dg-note" x="380" y="24">all green</text>
<rect class="dg-box" x="400" y="40" width="160" height="48" rx="4"/>
<text class="dg-label" x="480" y="64">Judge turn</text>
<path class="dg-line" d="M248 92V164" marker-end="url(#dg-build-head)"/>
<text class="dg-note is-end" x="232" y="128">any red</text>
<path class="dg-line" d="M440 92V120H312V164" marker-end="url(#dg-build-head)"/>
<text class="dg-note" x="376" y="108">blocking finding</text>
<path class="dg-line" d="M520 92V164" marker-end="url(#dg-build-head)"/>
<text class="dg-note is-end" x="504" y="140">no blocking</text>
<rect class="dg-box" x="200" y="168" width="160" height="48" rx="4"/>
<text class="dg-label" x="280" y="192">FEEDBACK.md</text>
<rect class="dg-box is-solid" x="400" y="168" width="160" height="48" rx="4"/>
<text class="dg-label is-reverse" x="480" y="192">Converged</text>
<path class="dg-line" d="M196 192H80V92" marker-end="url(#dg-build-head)"/>
<text class="dg-note is-start" x="96" y="152">next iteration</text>
</svg>
</div>
</figure>

## Who can say yes

Only the sensors can let Build converge. The judge can block, but it can never pass a change past a red sensor, and it does not even run while a sensor is red.

| Signal | Can let Build converge | Can block it |
| --- | --- | --- |
| A sensor script | Yes, together with the others | Yes |
| A blocking judge finding | Never | Yes |
| A judge nit | Never | No, it is recorded only |

If Yokai cannot parse the judge's output, it shows the output but does not treat it as a veto.

The judge reads the `judge` row of `CONTEXT.md › Skills` and uses the listed `SKILL.md` files as its review rubric. The coder reads the `coder` row the same way. Do not confuse the judge with `/junji review`: the judge looks at one iteration's diff against one phase plan and writes to `FEEDBACK.md`; `review` reads a whole pull request and adds fix phases to the board.

## Feedback and guides

Two things steer the coder.

- Guides come before it acts. They are file paths or short instructions listed under `build.guides` in `.koi/yokai.yml`, such as `docs/style.md`. They do not change during a run.
- `FEEDBACK.md` comes after a failure. It is append-only, Yokai is its only writer, and it holds the sensor output and judge findings from earlier iterations. The coder prompt tells the agent to read it first and address every unresolved item.

A guide is different from a skill. A skill is a `SKILL.md` in your repository that `CONTEXT.md › Skills` assigns to a role.

## Budget

`build.max_iters` in `.koi/yokai.yml` caps the iterations per Build. Leave it out for no limit. `0` means run no iterations. `yokai --max-iters 3` overrides it for one launch.

When the budget runs out without convergence, Build returns `not-converged` and leaves `FEEDBACK.md` in place. With recovery on (the default), Diagnose reads it and decides what to do next. With `recovery: false`, the run halts before Seal.

```yaml
# .koi/yokai.yml
build:
  max_iters: 5
  judge: true
  guides: [docs/style.md]
roles:
  judge:
    tier: high   # review with a stronger model than the phase tier
```

`build.judge: false` skips the judge turn. Token savings mode turns review back on whatever this setting says.

## Example: two iterations of P2

1. Iteration 1. The coder adds `saveTasks` and `loadTasks`. The sensor `30-test.sh` fails: the test for malformed stored JSON throws. Yokai appends the failing test output to `FEEDBACK.md`.
2. Iteration 2. The coder reads the feedback, wraps the parse in a try/catch and falls back to an empty list. All four sensors pass. The judge returns `[{"severity":"nit","file":"src/storage.ts","line":12,"message":"name the storage key as a constant"}]`. A nit does not block, so Build converges.

Yokai deletes `FEEDBACK.md`, then starts Seal.

## What can go wrong

- The judge never runs. That is expected while any sensor is red. Fix the failure the sensors report first.
- Iterations repeat the same failure. Read `FEEDBACK.md`. The plan may be missing a fact, such as a fixture the test needs. Recovery's Diagnose turn can add a steer to the next Build.
- The judge keeps blocking on taste. Put the rule in a skill or a guide so the coder follows it from the first iteration, or adjust the phase plan.
- Build halts with `FEEDBACK.md` present and recovery off. Read the file, fix the plan or the code, and relaunch.
