Module 4 of 11 · Expert · 10 min read
Coding loops: keep going until the tests pass
Use tests as the stopping condition for coding agents, keep them honest, and stay in control of the result.
- 1Tests turn 'looks right' into an objective stop condition for a coding loop.
- 2Write or approve the tests first, and never let the agent weaken tests to make them pass.
- 3Set iteration and time budgets, and commit working checkpoints.
- 4Passing tests is necessary, not sufficient: review the diff.
01Why tests make great loop conditions
Coding is where loops shine, because code can be checked objectively. A coding agent can write a change, run the test suite, read the failures, fix them and run again, without you in the loop for each step. The tests are the critic, and unlike a model's opinion, they can't be talked round.
02Tests first
The loop is only as good as its tests. For a new feature, write the tests first, or ask the agent to propose them and review them before it writes the implementation. Include edge cases and at least one test for behaviour that must not change.
03The brief for the loop
Once the tests are agreed, give the agent a brief with a clear exit and clear limits.
04Keeping the agent honest
Optimising hard for a metric invites gaming it. These rules close the most common loopholes.
- Forbid editing tests, or review any test changes separately.
- Watch for special-casing: code that detects the test inputs and returns hard-coded answers.
- Check that tests actually ran: a command can report success after silently running zero tests.
- Run the suite yourself, or in CI, before merging.
05Budgets, checkpoints and flaky tests
Give every coding loop an iteration cap and a time or cost budget. Commit or checkpoint whenever the suite goes green, so a later misstep can be rolled back. If a test fails intermittently, the loop can thrash forever; tell the agent to re-run a failing test once before changing code, and to report suspected flakiness rather than 'fixing' it.
06Green is not the same as good
Passing tests means the code meets the tests, not that it is well designed, secure or what you meant. Read the diff. Ask the agent, or a second model in a fresh context, to review the change for security, performance and readability. For anything touching money, personal data or authentication, a human review is non-negotiable.
Finished reading?
Mark it complete to track your progress, then test yourself with the Loops, Graphs & Agents quiz.