Sub topic 11
Reviewing diffs and committing each cycle
This chapter assumes you can read a Git diff, run pytest, and have a coding agent that edits files and runs commands in your repository. It covers what to check in an agent's changes and how to commit one verified cycle at a time.
Why a green run is not enough
A passing suite tells you the agent's change satisfies the tests. It does not tell you the change solves the problem. Agents can hardcode expected values, weaken assertions, or edit tests so they pass. The diff is where you find these problems, so review it after every cycle, before you commit.
Reviewing small diffs is also much easier. If each cycle adds one test and a few lines of code, you can read the whole change in under a minute.
The review loop for one cycle
Run the suite yourself instead of relying on the agent's report. The agent's summary describes what it believes happened. Your own run shows what happened.
python -m pytest -q
git diffWhat to look for in the diff
Read the test changes first, then the implementation.
- Test files. Only the new test should be added. Existing tests should be unchanged. Look for removed lines, loosened assertions, new
skiporxfailmarkers, and deleted test functions. - Implementation. Look for literal values copied from the tests, such as
return "hello-world". These are the hardcoded results described earlier in this tutorial. - Scope. The change should cover the one behavior of this cycle. Unrelated edits, new dependencies, or reformatted files belong in a separate cycle.
- Failure evidence. Check that the agent showed the red failure for the intended reason before it wrote the code.
Consider the slugify project. After the "strips punctuation" cycle, this diff should make you suspicious:
def slugify(text):
if text == "Hello, World!":
return "hello-world"
return text.lower().replace(" ", "-")The suite is green, but the code special-cases the test input. A correct change generalizes, as in the regex version: re.findall(r"[a-z0-9]+", text.lower()). When you see the special case, reject the change and tell the agent why.
Commit one verified cycle at a time
Each commit should be one cycle that you have run and read. This gives you three benefits:
- Safe rollback. If cycle four goes wrong, you can reset to the commit for cycle three and lose very little.
- Readable history. Each commit pairs a test with the code that satisfies it, so the history documents behavior.
- Easy bisecting. If a bug appears later,
git bisectcan point to one small commit.
Stage deliberately, so nothing unreviewed slips in:
git status
git add test_slugify.py slugify_util.py
git diff --staged
git commit -m "slugify: strip punctuation"Use git diff --staged before committing. It shows exactly what the commit will contain, which can differ from your working tree.
A refactor gets its own commit. Keep behavior changes and restructuring apart, so a reviewer can check that the refactor changed no behavior, and the suite stayed green before and after it.
| Cycle step | What you verify | Commit? |
|---|---|---|
| Red | The test fails for the intended reason | Optional, usually not committed alone |
| Green | The suite passes, with no test edits or hardcoding | Yes, test and code together |
| Refactor | The suite stays green, with no behavior change | Yes, separately |
Common mistakes
- Approving from the summary. The agent's text says "all tests pass," but you did not run them.
- Batching several cycles. A large combined diff is hard to read, so problems slip through.
- Letting the agent commit unreviewed. If you allow the agent to run
git commit, review the commit afterward withgit showand be ready to amend or reset it.
Exercise
An agent finishes a cycle and reports that all tests pass. The diff shows the new test, a three-line change in the implementation, and one existing test whose assertion changed from == "a-b" to in ("a-b", "a--b"). What do you do, and why?
Show answer
Reject the change and do not commit. The existing test's assertion was loosened so that a wrong result now passes. Ask the agent to restore the original assertion and fix the implementation instead. If you believe the old test was wrong, decide that yourself. After the revision, run the suite and read the diff again before committing.