Sub topic 9
Closing the Loop with Tests, Type Checks, and Linters
This chapter assumes you can run a test suite, a type checker, and a linter from the command line. It also assumes you have a branch for the work and characterization tests for the code you are changing. You can set up both with the earlier chapters on checkpoints and characterization tests.
Why the agent needs a loop
A coding agent cannot tell whether an edit is correct by reading its own output. It can only learn that from the results of commands it runs. A verification loop gives it those results after every step: edit, run checks, read the output, fix, repeat.
If you skip the loop, you become the loop. You run the checks by hand, paste in the failures, and wait. Put the commands in the task description and the agent runs them itself.
What each check catches
The three tools find different kinds of mistakes, so use all of them.
| Check | Catches | Misses |
|---|---|---|
| Tests | Changed behavior, broken logic | Code no test exercises |
| Type checker | Wrong argument types, missing attributes, renamed functions still called by the old name | Logic errors with correct types |
| Linter | Unused imports, undefined names, style violations | Most behavior problems |
Type checkers are useful after a rename or an extract-function change. If the agent moves a function and misses one caller, the type checker reports an error at that call site even when no test reaches it.
Put the commands in the task
Name the exact commands and the order. Tell the agent what a passing result means.
After every edit, run these in order and report the output:
1. pytest tests/test_pricing.py
2. mypy pricing.py
3. ruff check pricing.py
Do not move to the next step until all three pass.
If a check fails, fix the cause. Do not edit or delete a test to make it pass.The last sentence matters. An agent that is told only to "make the tests pass" may change the assertion instead of the code. A characterization test that you edit no longer records the old behavior.
Record the baseline first
Run the checks before the agent edits anything. The linter or type checker may already report errors, and you need to know which ones, so you can separate old problems from new ones.
pytest tests/test_pricing.py
mypy pricing.py
ruff check pricing.pySave the output or note the counts. If the baseline has 3 type errors and the result after the change has 5, the agent added 2. Ask the agent to compare against the baseline, not only to check that the run is clean.
Reading a failure
Ask the agent to quote the failing output and say what it thinks caused the failure before it edits again. That prevents a cycle of guesses. Stop the loop yourself if the same check fails three times with the same error. The agent probably has a wrong assumption, and more attempts rarely fix it. Restore the last checkpoint and restate the task with the new information.
Gaps in the loop
- Slow checks. If the full suite takes minutes, run the targeted tests after each step and the full suite before each commit.
- Flaky tests. A test that fails at random teaches the agent the wrong lesson. Re-run it once, and tell the agent which tests are known to be flaky.
- No type checker. Then the tests carry more weight. Add characterization tests for any caller you are worried about.
Exercise
You ask an agent to extract a function. The tests pass, but mypy reports a new error in a file the agent did not mention. What does this tell you, and what do you do next?
Show answer
The type checker found a problem the tests do not cover, most likely a caller affected by the change, such as a changed return type or a renamed function. Ask the agent to quote the error and explain the cause, fix it, and re-run all three checks. Compare the error count to your baseline. Commit only when all checks pass.