Sub topic 10
Reviewing the diff like a pull request
This chapter assumes you can read a Git diff, run a test suite, and have already set up the test, type-check, and lint loop from the previous chapter.
Why review after the tests pass
Green tests show that the behavior they check still works. They do not show that the agent changed only what you asked for. An agent can reformat unrelated files, add a dependency, or edit a test so that it passes. Review the diff as you would a teammate's pull request, before you commit or merge it.
Start with the shape of the change
Look at the list of files before you read any code.
git diff --stat main...HEAD
git diff --name-only main...HEADCompare the list with the scope you wrote in the task. If the task allowed only pricing.py and tests/test_pricing.py, no other file should appear. A file outside the scope needs an explanation, or it needs to be reverted. If the agent has not committed yet, use git diff for unstaged changes and git diff --staged for staged ones.
Read in passes
A single top-to-bottom read misses things. Make several short passes, each looking for one kind of problem.
- Scope. Check the files and the total size. A "small" step that changes 40 files is not small.
- Tests. Read test changes first. In a refactoring, existing assertions should not change at all.
- Logic. Read each hunk. For an extract-function change, confirm that constants, rounding, and the order of operations are the same.
- Noise. Look for whitespace changes, reordered imports, and renamed variables that nobody asked for.
- Dependencies and config. Check lockfiles, package manifests, and CI files.
Two options make the logic pass faster:
git diff -w main...HEAD
git diff --color-moved=zebra main...HEAD-w ignores whitespace-only changes. --color-moved colors lines that were moved without edits differently from lines that were really changed.
Red flags
| What you see | What it may mean |
|---|---|
| An expected value changed in a test | The agent changed the test to match new behavior |
| A deleted assertion, `skip`, or `xfail` marker | A failing check was silenced |
| A looser comparison, such as exact equality replaced by a range | Behavior drifted and the test was weakened to hide it |
| A new import of a package you do not use | An invented or unwanted dependency |
| Edits in files outside the task scope | An over-broad change |
| A changed numeric constant or rounding call | A silent behavior change |
Ask the agent to explain, then compare
After your own read, ask the agent for a summary: "For each changed file, say what changed and why." Compare its answer with what you saw. A change you found that the summary does not mention is the one to look at hardest. A change in the summary that you cannot find means either you or the agent misread the diff.
Remove what does not belong
You do not need to throw away a whole step because of one bad hunk. Discard hunks interactively and rerun your checks.
git restore -p tests/test_pricing.py
pytestGit shows each hunk and asks whether to discard it. Answer y for hunks you reject and n for hunks you keep. Then commit only the reviewed result.
Exercise
An agent finishes an extract-function refactoring and all tests pass. The diff shows that tests/test_pricing.py has one expected value changed from "$1,234.50" to "$1,234.5". What do you do?
Show answer
Treat it as behavior drift, not a harmless edit. Revert the test change with git restore -p tests/test_pricing.py, rerun the tests, and expect a failure. That failure shows the refactored code now formats prices differently. Ask the agent to fix the code so the original expectation passes, then review the new diff again.