Sub topic 3
The agent loop: read, edit, run, observe
This chapter assumes you can write and run unit tests in Python with pytest, read a Git diff, and have used a coding agent at least once. It explains how an agent works step by step, so you can see why a test runner is the best feedback source you can give it.
The four steps
A coding agent does not produce a whole program in one pass. It repeats a short loop until it decides the task is done or it gets stuck:
- Read. The agent opens files, searches the code, or lists a directory to learn what exists.
- Edit. The agent changes one or more files, or creates new ones.
- Run. The agent executes a command, such as the test suite.
- Observe. The agent reads the command's output and decides what to do next.
The agent only knows what appears in its context, meaning the text it has read so far in the session. Anything it has not read, it guesses. Anything it has not run, it cannot confirm.
Why the observe step matters
Without a run step, the agent edits code and then judges its own work by reading it. That judgment is unreliable. The code may look right and still fail.
A test run replaces this guess with evidence. The command below is the one the agent runs after each change:
python -m pytest -qThe output tells the agent three useful things:
- Which tests failed, by name.
- The assertion that failed, with the actual and expected values.
- Whether an error came from the behavior or from something else, such as an import problem.
Here is a failing run for the slugify example used later in this tutorial:
FAILED test_slugify.py::test_strips_punctuation - AssertionError: assert 'hello,-world!' == 'hello-world'This one line tells the agent what the function returned ('hello,-world!'), what the test wanted ('hello-world'), and where to look. The agent can read it, return to the implementation, and make a targeted edit. That is the loop working as intended.
What the loop looks like in TDD
In test-driven development, each phase maps onto the loop:
| TDD phase | Read | Edit | Run | Observe |
|---|---|---|---|---|
| Red | Existing code and tests | Add one new test | Run the suite | Confirm the new test fails for the right reason |
| Green | The failure message | Change the implementation | Run the suite | Confirm all tests pass |
| Refactor | The working code | Restructure it | Run the suite | Confirm tests are still green |
The loop runs several times inside each phase. For example, the agent may make one edit in the green phase, see a different failure, and edit again. The tests do not change during this time. They are the fixed target.
Common errors with the loop
- Skipping the run. An agent may edit code and report success without running anything. Tell it to run the suite after every change and to show the output.
- Reading partial output. If a run prints hundreds of lines, the agent may miss the important failure. The
-qflag keeps pytest output short. - Treating any failure as the right failure. A new test that fails with
ImportErrorhas not shown the behavior is missing. It has shown the test cannot run. The agent must check the reason in the observe step. - Looping without progress. If the agent makes the same edit and sees the same failure, stop it. Ask it to explain what it observed before it tries again.
Exercise
Exercise
An agent edits slugify to fix a failing test, then reports "done" without running anything. Which step of the loop was skipped, and what could go wrong?
Show answer
The run and observe steps were skipped. The agent judged its change by reading its own code, so it has no evidence the test passes. The edit might not fix the failure, or it might break other tests. Ask the agent to run python -m pytest -q and show the output before it reports completion.
Exercise
A new test fails with "ModuleNotFoundError: No module named 'slugify_util'". Is this a valid red result? Explain.
Show answer
No. The test failed because the module does not exist, not because the slugify behavior is missing. The agent should fix the import (for example by creating an empty module with the function stubbed), run again, and confirm the test now fails on the assertion.