Sub topic 11
Reviewing diffs and committing each cycle
This chapter assumes you can read a Git diff, write unit tests with pytest, and have worked through the red-green-refactor cycle and the agent failure modes from earlier chapters. Here you will learn how to review what an agent changed and how to turn each verified cycle into one commit.
Why review still matters
A passing test run tells you the tests agree with the code. It does not tell you the code is right. The agent has already shown it can reach green in ways that miss the point, such as hardcoded values or edited tests. The test runner cannot catch a problem the tests do not describe.
Review is the step where a human checks three things the runner cannot:
- The tests say what you meant.
- The implementation solves the general problem, not only the listed examples.
- The change touches only what the cycle needed.
Committing each cycle gives you the second benefit. Every commit is a point where the suite was green and a person looked at the result. If a later cycle goes wrong, you can return to the last good state in one command.
The review-and-commit loop
One cycle produces one reviewed commit. The agent works, you inspect, and only then does the change become permanent.
Notice there are two checks. The first happens at red, where you confirm the test is the one you wanted and that it fails for the intended reason. The second happens at green, where you read the implementation diff. Reviewing the test before the implementation exists is cheaper, because there is nothing yet to bias you toward accepting it.
What to look at in a diff
Start with the file list, not the contents. Run:
git status --short
git diff --statThe list of changed files is the first signal. For a cycle that adds one behavior to slugify, you expect two files: the test file and the implementation file. Anything else needs an explanation.
Then read the diff itself, in this order.
- Test files first. Did any existing test line change or disappear? Additions are normal in the red phase. Edits to existing assertions are a warning sign. Look for new
skip,xfail, orpytest.mark.skipmarkers, loosened comparisons, and removed test functions. - The implementation second. Ask whether the code would still work for inputs the tests do not mention.
- Everything else last. Look for changes to configuration, dependency files, and test settings such as
pytest.ini,conftest.py, or CI files. An agent that cannot make a test pass sometimes changes the environment instead.
A quick test for hardcoding
Take the degenerate green from earlier in the tutorial:
def slugify(text):
return "hello-world"This passes the first test and nothing else. A reviewer catches it because the function never uses its argument. A useful habit when reading any green diff is to ask: "If I changed the test input, would this code give a different answer?" If the output cannot depend on the input, the code is not solving the problem.
You can also test this directly. Run the function with an input that is not in any test:
from slugify_util import slugify
print(slugify("Test Driven Development"))With the hardcoded version, this prints hello-world. With the refactored version, it prints test-driven-development. Spending ten seconds on a probe like this often catches what reading alone misses.
Review the diff against the test, not the other way round
When you read the implementation, keep the new test open next to it. Check that each line of new code exists because of something the test requires. Code with no test behind it is a signal that the agent did more than the cycle asked. Extra branches, new parameters, and unrelated helper functions all belong in a later cycle with their own tests.
Review checklist
Use the same short list each time. A fixed list is easier to apply when you are tired and the diff looks harmless.
| Check | What you look for | Typical problem |
|---|---|---|
| Files changed | Only the test and its implementation | Edits to config or unrelated modules |
| Existing tests | No edited, skipped, or deleted tests | Assertion loosened to match wrong output |
| New test | Describes one behavior and failed for the right reason | Test passes immediately, or fails on an import error |
| Implementation | Uses its inputs and handles the general case | Hardcoded return values, special-cased inputs |
| Scope | Nothing beyond what the test needs | Extra features, speculative parameters |
| Full suite | All tests pass, not only the new one | Earlier behavior broken |
Verify the run yourself
Do not rely only on the agent's report that tests pass. Agents summarize accurately most of the time, but a summary is not output. Run the suite yourself before you commit:
python -m pytest -qRead the final line. A result such as 3 passed confirms the count you expect. Compare the number of tests with the previous cycle. If you added one test and the count rose by one, that matches. If the count stayed the same or fell, a test was removed or is no longer collected.
A related check is to confirm that the new test really covers the new code. One way is to temporarily revert only the implementation change and run the suite:
git stash push -- slugify_util.py
python -m pytest -q
git stash popThe new test should fail while the implementation change is stashed, and pass again after git stash pop. If the test still passes without the change, it does not specify the behavior.
Committing each cycle
Once the diff passes review and the suite is green, commit. A cycle maps to a commit in one of two common ways.
- One commit per full cycle. The test and the implementation go in together. Every commit passes the whole suite. This is the simplest option and works well with CI, because no commit in history is red.
- Separate red and green commits. The failing test is committed first and the fix second. This records the red step explicitly, but it leaves a commit where the suite fails. That can break bisecting and CI on any branch that runs on every commit.
For work with an agent, prefer one commit per full cycle. It keeps history green, and each commit pairs a behavior with the test that specifies it. If you want to keep the red step visible, show it in the commit message instead.
Stage deliberately
Avoid git add . or git commit -a when working with an agent. They stage everything, including files you did not review. Name the files, or review hunks one at a time:
git add test_slugify.py slugify_util.py
git diff --cached
git commit -m "Strip punctuation in slugify"The git diff --cached line shows exactly what will be committed. It is your last chance to see a stray file. For files that contain both wanted and unwanted changes, git add -p lets you accept or reject each hunk.
Write a message that names the behavior
A good message states the behavior added, not the process. Compare these.
| Weak message | Better message |
|---|---|
| `Fix tests` | `Strip punctuation in slugify` |
| `Agent changes` | `Collapse repeated separators in slugify` |
| `WIP` | `Lowercase and hyphenate words in slugify` |
If the agent drafts the message, read it. Agents sometimes describe what they intended instead of what the diff does.
When review finds a problem
If the review fails, do not commit and clean up later. Send the specific problem back to the agent as a short instruction, and keep the same cycle open.
- If a test was weakened, restore it with
git checkout -- test_slugify.pyand tell the agent the test must stay as written. - If the implementation is hardcoded, ask for a second test with different input, then let the agent generalize.
- If unrelated files changed, discard them with
git restore <file>and ask for a narrower change.
If the agent has made a tangle of changes across several attempts, it is often faster to discard the working tree changes for that cycle and restart from the last commit than to untangle them. Because you committed the previous cycle, the cost is one small cycle. This is the practical value of small commits.
Edge cases
- A test passes before you write any implementation. The behavior may already exist, or the test may be wrong. Do not commit it as a new behavior. Investigate first.
- The refactor step changes many lines. Commit the refactor separately from the behavior change. Reviewers can then check that the refactor commit changes no test files and keeps the suite green.
- The diff is too large to review. Treat size as a failure. Reject it and ask for a smaller step, or split it with
git add -p. A diff you cannot read in a few minutes is a diff you are not really reviewing. - Formatting noise. If the agent reformats a whole file, the real change is hidden. Ask it to leave formatting alone, or run the formatter as its own commit.
Exercises
Exercise
An agent finishes a cycle for the "collapses repeated separators" behavior. git diff --stat shows test_slugify.py, slugify_util.py, and pytest.ini. The suite reports 3 passed. What do you do before committing?
Show answer
Open the pytest.ini diff and find out why it changed. Config changes can hide problems, for example by changing which tests are collected or adding options that deselect tests. If the change is not needed for this behavior, restore it with git restore pytest.ini, rerun python -m pytest -q, and confirm the suite still passes and the test count is what you expect.
Exercise
You want to confirm that a new test really specifies the new behavior. Describe how you would check this using Git and pytest, and what result tells you the test is sound.
Show answer
Stash only the implementation change with git stash push -- slugify_util.py, run python -m pytest -q, then restore it with git stash pop. The new test should fail while the implementation change is stashed and pass again afterward. If it passes without the change, the test does not depend on the new behavior and needs to be rewritten.
Exercise
Why is one commit per full cycle usually better than committing the failing test and the fix separately when CI runs on every commit?
Show answer
A commit containing only the failing test leaves the suite red, so CI fails on that commit and git bisect can land on a commit that is broken for an unrelated reason. One commit per cycle keeps every commit green and pairs each behavior with its test.