Sub topic 10
Reviewing the diff like a pull request
This chapter assumes you can read a Git diff, run a test suite and a linter from the command line, and have already scoped a task and committed in small steps. Passing tests tell you the code behaves as the tests expect. They don't tell you that the agent changed only what you asked for. Reviewing the diff covers that gap.
Why review when the tests pass
The verification loop checks behavior that your tests describe. A diff review checks everything else:
- Files you did not ask the agent to touch.
- Tests that were weakened so they pass.
- Deleted code that looked unused but was not.
- Calls to functions or options that do not exist in your version of a library.
- Changes that are correct but too large to maintain.
Treat the agent like a contributor you have not worked with before. You would not merge a stranger's pull request because CI is green. Apply the same standard here.
Set up the review
Review one checkpoint at a time. If you committed after each small step, each diff is short and has one purpose. Start with the overall shape, then read the details.
# Which files changed, and how much?
git diff --stat main...HEAD
# Only the last checkpoint
git show --stat HEAD
# Full diff of one file
git diff main...HEAD -- pricing.py
# Ignore whitespace-only noise when you suspect a reformatting
git diff -w main...HEADThe three-dot form main...HEAD compares your branch to the point where it split from main. It ignores changes that landed on main after you branched, so you see only the agent's work.
Review in passes
A single read-through misses things. Use several short passes, each with one question.
Pass 1: scope
Read only the --stat output. Compare the list of files to the scope you wrote in the task description.
- Is every changed file in scope?
- Are there new files you did not expect, such as scratch scripts, backups, or
.origfiles? - Did a lockfile, a config file, or a CI definition change?
- Is the number of changed lines proportionate to the task?
An extract-function refactoring that touches 40 files is a signal to stop. Either the task was larger than you thought, or the agent went beyond it.
Pass 2: tests
Read the test changes before the source changes. This is the pass where agents most often cause quiet damage. Look for:
- Assertions removed or loosened (
==replaced within, an exact value replaced withis not None). - Expected values edited to match new output. In a refactoring, expected values should not change at all.
- Tests marked skipped, expected to fail, or deleted.
- Mocks added that replace the code under test.
- Tolerances widened in numeric comparisons.
You can search the diff for these patterns directly.
git diff main...HEAD -- tests/ | grep -E '^[-+].*(skip|xfail|pytest\.mark|assert|mock|patch)'Lines starting with - that contain assert deserve a close look. A removed assertion needs a reason you agree with.
Pass 3: behavior in the source
Now read the source changes line by line. For an extract-function refactoring, check that the extracted code is equivalent to the original. Consider this change:
# Before
def format_invoice(items):
subtotal = sum(i["price"] * i["qty"] for i in items)
tax = round(subtotal * 0.2, 2)
return {"subtotal": subtotal, "tax": tax, "total": subtotal + tax}
# After: same inputs and outputs, tax logic extracted
TAX_RATE = 0.2
def calculate_tax(subtotal):
return round(subtotal * TAX_RATE, 2)
def format_invoice(items):
subtotal = sum(i["price"] * i["qty"] for i in items)
tax = calculate_tax(subtotal)
return {"subtotal": subtotal, "tax": tax, "total": subtotal + tax}Questions to ask while reading:
- Is the rounding still applied at the same point, with the same number of digits?
- Is the constant
0.2the same value as the literal it replaced? - Is the public signature of
format_invoiceunchanged, including default arguments? - Did any exception type or message change?
- Did the order of operations change in a way that matters for floating-point results?
Small differences such as a changed default, a reordered condition, or a dropped else branch are easy to miss and are exactly what behavior drift looks like.
Pass 4: dependencies and APIs
Check every call to a library function against the documentation for the version you use. Agents can produce plausible names that do not exist, or use an API from a different major version. Also check:
- New imports. Is each package already a dependency of the project?
- Changes to
requirements.txt,package.json, or similar files. - Version bumps you did not request.
Running the type checker and the test suite catches many of these, but not calls that sit in untested paths. Read those by hand.
Pass 5: leftovers
Look for material that should not be merged:
- Debug prints and temporary logging.
- Commented-out code.
- TODO comments the agent added in place of finishing work.
- Hard-coded paths, tokens, or credentials.
- Comments that describe what the agent did ("moved from format_invoice") instead of what the code does.
Review order at a glance
Use tools to narrow what you read
You do not have to read every line with equal attention. Use tools to focus on what is risky.
- Run the linter and type checker on the changed files only, so new warnings stand out.
- Compare test results before and after. The task description asked for both runs, so check that the numbers match.
- Run
git diff --checkto find whitespace errors and leftover conflict markers. - For mechanical changes such as a rename, sample a few call sites instead of reading all of them, then confirm the rest with a search.
# Whitespace problems and conflict markers
git diff --check main...HEAD
# Did the old name disappear completely?
grep -rn "getUser(" src/ || echo "no remaining references"
# Does the rest of the suite still pass?
pytest -qThe grep check matters for renames. If the old name still appears, the agent missed a call site. If the new name appears where the old name never did, the agent made an extra edit.
Ask the agent to explain, then check the explanation
You can ask the agent to summarize what it changed and why, and to list any change that was not strictly required. The answer helps you find areas to read closely. It is not evidence. Agents sometimes describe a change they intended rather than the one they made. Always compare the summary with the diff.
A useful prompt is:
List every file you changed and, for each, one sentence on why.
Then list any change that is not required by the task, and any test you modified.If the summary leaves out a file that appears in git diff --stat, treat that as a finding.
Decide: accept, revise, or discard
After the passes, choose one of three outcomes.
| Outcome | When to choose it | How to do it |
|---|---|---|
| Accept | Scope is correct, tests unchanged or justified, behavior verified | Commit the checkpoint |
| Revise | The idea is right but some edits are wrong | Name the exact file and line, say what to change, and re-run the verification loop |
| Discard | The step went beyond scope or is hard to untangle | `git restore .` for uncommitted work, or `git revert` for a commit, then restate the task more narrowly |
Prefer discarding to patching when a step has gone badly wrong. Several rounds of corrections on top of a flawed change often produce a diff that is harder to review than a fresh attempt from your last good checkpoint.
When you ask for a revision, be specific. "Fix the tests" invites the agent to edit the tests. "The assertion on line 14 now fails because calculate_tax rounds before the subtotal is added. Fix pricing.py, not the test" points at the cause and protects the safety net.
Keep a review checklist
A short checklist makes the review repeatable. Adjust it to your project.
- The changed files match the declared scope.
- Existing tests are unchanged, or each change is explained and approved.
- No tests are skipped, deleted, or loosened.
- Public signatures and return values are unchanged, unless the task said otherwise.
- Every library call exists in the installed version.
- No new dependencies or version bumps appear.
- Linter, type checker, and tests give the same or better results than before.
- No debug code, commented-out code, or secrets remain.
Common mistakes
- Reading only the source and skipping the test changes.
- Trusting the agent's summary instead of the diff.
- Accepting a large diff because the tests pass.
- Fixing a bad step with many follow-up prompts instead of reverting to the last checkpoint.
- Reviewing the agent's work only at the end of the whole task, when the diff has grown too large to read carefully.
Exercise
An agent finishes a refactoring that was meant to be behavior-preserving. All tests pass. In git diff main...HEAD -- tests/, you see that one expected value changed from "$1,234.50" to "$1234.50", and the agent's summary does not mention the test file. What do you do, and why?
Show answer
Do not accept the change. A refactoring must not alter existing expected values, so the edit means the new code produces different output (the thousands separator was lost) and the agent changed the test to hide it. The summary omitting the test file is a second finding. Revert the test edit, re-run the tests so the real failure shows, and instruct the agent to fix the source so the original expected value passes. Then check the rest of the diff for other behavior drift.
Exercise
You asked for a rename of getUser to fetchUser across src/. git diff --stat shows 14 changed files, but you expected 9. How do you investigate?
Show answer
Compare the list of changed files with the files that actually contained getUser (for example with grep -rln "getUser" src/ on the base branch). Any changed file that never contained the old name is an extra edit. Read those diffs and either discard them or ask the agent to justify them. Also confirm that the old name no longer appears in src/ after the change.