after N tries (thrash guard)
Syntax
after N tries: stop and warn "<message>"
What it does
The thrash guard โ the floor that keeps a self-correcting loop from grinding forever. A loop with a reflect back-edge will retry on every failure; without a ceiling it would loop until it hits the engine's absolute hard cap of 25 attempts, burning tokens on a problem it may never solve unaided. after N tries sets your own, lower limit: once the loop has made N full attempts without meeting the goal, it stops and surfaces the message so a human knows exactly which loop got stuck and why.
Reach for it on any loop that reflects. Because attempts is checked before fail in the transition order (see How it runs), the guard always wins once the threshold is crossed โ the loop can't sneak in one more reflect-and-retry past the limit. The right N is a budget decision: a cheap, fast done when check can afford 8โ10 tries; an expensive one should stop at 3โ4. The warning message is your handoff note โ write it for the human who reads it, naming the likely cause so they can pick up where the loop left off.
Example
after 6 tries: stop and warn "thrashing โ needs a human look"the thrash guard
loop "fix the flaky auth test":
goal: the login suite is green and stays green
done when "pnpm test auth" passes
when it fails: reflect on which assertion broke, then plan again
after 4 tries: stop and warn "auth test still red โ likely a race in the token refresh"the last line of the loop is its hard stop
How it runs
Common mistakes
- A reflect back-edge with no guard. A loop that says
when it fails: reflectbut has noafter N triesline will run to the hard cap of 25 before it stops. Never emit a back-edge without a try ceiling โ it should be the last line of the loop. - Setting
Nwithout pricing the cycle. Each try re-runs plan, act, and thedone whencheck. A slow or costly check with a high N is a big bill for no result. Match the ceiling to the cost: expensive check โ low N. - An empty or generic message.
stop and warn "failed"tells the human nothing. Name the loop's likely blocker so they can resume from the diagnosis, not from scratch. - Confusing tries with the flake guard.
after N triescaps attempts;passes N timesin a predicate re-runs the same check to catch a lucky green. They solve different problems โ you often want both.