Two rules for once the agent is running: when to stop, and when to ask.
Stop condition
A rule the agent can check that says when to stop: these commands exit 0, or this many tries have been used. It stands in for the agent's own feeling that it has finished.
With no one watching, it needs two things: a way to succeed, and a limit for when it can't. Without the first it stops when things look done. Without the second it can spend all night on one failure. In our recordings, 1 of 40 loops never passed.
Say it to your agent
“Keep going until bun test and bun run typecheck both exit 0. Stop after 5 attempts at the same failure, and tell me what you tried, with the output. Never change a test to get there.”