when … (transitions)
Syntax
when it passes and the goal is met: stop
when it fails: reflect, then plan again
when blocked: ask a human
What it does
when … rules are the loop's transitions — they run after each observe and decide what happens next. Conditions: it passes · it passes and the goal is met · it fails · blocked. Actions: stop · stop and warn · reflect · plan · act · observe · ask a human.
These lines are what make a loop self-correcting rather than a one-shot prompt. when it fails: reflect, then plan again is the back-edge: it turns a failed check into a diagnosis that feeds the next plan, so the loop learns within a run instead of repeating the same mistake. For that diagnosis to reach the next plan, end look at: with and the last failure. when blocked: ask a human is the escape hatch for when the agent genuinely can't proceed. Together they cover the three outcomes of every cycle: done, retry, or stuck.
Precedence is fixed by the engine, not by the order you write the lines: blocked > attempts > pass > fail. That ordering is deliberate — after N tries (attempts) is checked before it fails, so once you hit the try ceiling the thrash guard always wins and a failing loop can't reflect-and-retry forever. Never write a reflect back-edge without also giving the loop a hard stop; the last line of a loop is its ceiling.
Example
when it passes and the goal is met: stop
when it fails: reflect on which layer broke, then plan again
when blocked: ask a humantransition rules
How it runs
Common mistakes
- A
when it failsback-edge with no ceiling. Reflect-and-plan-again with noafter N triesis an infinite, token-burning loop. The last line of every loop should be its hard stop — pair the back-edge with a thrash guard. - Expecting write order to change precedence. The engine evaluates blocked > attempts > pass > fail no matter how you order the lines. You can't make
it failsoutrankafter N triesby writing it first. - Reflecting without passing the diagnosis forward.
reflectonly helps the next plan if it can read the failure — endlook at:withand the last failure, or the reflection is discarded and the loop repeats itself. - Confusing
it passeswithit passes and the goal is met. A cycle's check can pass while the overall goal isn't met yet. Useit passes and the goal is met: stopfor the real finish line, not a bareit passesthat stops too early.