Notice failure, revise
The plan-then-act agent from the last lesson had a quiet assumption baked in: that
every action in the plan succeeds. The real world doesn't honor that. A step that
ran fine in rehearsal fails in production — the file is locked, the editor isn't
focused, the API returns a 429. An agent that marches straight down its plan ignoring
results ends up in a corrupt state: it runs save over a document it never managed
to write, then cheerfully reports success. The plan completed; the work didn't.
Self-reflection closes that gap. The shape is simple but the discipline is the point: after every action, read the result. On success, advance. On failure, don't barrel onward — pause, run the repair, then retry the same step. The agent treats the checker's verdict as live feedback rather than a formality, so a dead end becomes a detour instead of a crash.
Here the plan is ["open","write","save"], but checker rejects write
until the editor has been focused — exactly the dependency a static plan can miss.
The fixes map says the repair for write is focus. So the run should go:
open ok, write FAIL → undo the failed attempt, run focus, retry write
(now ok), save ok. Reflection discovers at runtime the prerequisite the plan
never encoded.
The starter just loops run(step) and plows past the failure. Add the reflection:
when run(step) returns "fail", pop the bad attempt off state, print
step failed -> inserting fix -> retry, run fixes[step], then run step
again. Done means a clean final state of open,focus,write,save.
A plan you can revise mid-flight beats a perfect plan you can't — the checker is what turns a dead end into a detour.