Sub topic 6
Reading the diff in a deliberate order
This chapter assumes you can read a Git diff and run git status, git diff, and git add -p. It also assumes you have already put the agent on its own branch from a clean baseline and triaged the change set. Triage means you know which files changed, how large each change is, and which files were deleted or are untracked. Here you read the code itself, and the question is in what order.
Why order matters
git diff prints files alphabetically. That order has nothing to do with importance. A one-line change to a public function signature can sit between two large, harmless formatting edits. If you read top to bottom, your attention is spent by the time you reach the lines that matter.
Reading in a chosen order has three benefits:
- You form a model of the intended change before you see the details, so surprises stand out.
- You check the contracts (what the code promises) before the bodies (how it keeps the promises). A wrong contract makes every body suspect.
- You spend the most careful reading on the riskiest code, while you are still fresh.
An agent's diff is also different from a colleague's pull request. A colleague's changes usually follow one line of thought. An agent can change twenty files that look equally confident, whether or not each edit was needed. Your order has to supply the structure the diff lacks.
The order
The sequence below runs from the outside of the change inward. Each pass has one question.
- The file list. Which files changed, and do they match the task?
- Interfaces. What do callers now see differently?
- Tests. What do the tests now claim about behavior?
- Implementation. Does the code do what the interfaces and tests say?
- Leftovers. Deletions, generated files, configuration, and dependencies.
Pass 1: the file list
You already triaged the change set. In this pass you compare it against the task you wrote before the run. Sort the files into three groups:
- Files the task required, such as the loader module and its callers.
- Files that are plausible but not required, such as a shared helper the agent chose to touch.
- Files with no connection to the task.
Any file in the third group is a finding by itself. You do not need to read its changes to know that something went wrong. Note it and move on, and decide later whether to revert it. Reading a file you intend to discard is wasted effort.
Pass 2: interfaces
An interface is anything another piece of code, or another person, depends on. Look for these:
- Function and method signatures, including parameter order, defaults, and return types.
- Class names and public attributes.
- Exported names, module paths, and import statements.
- Configuration keys, environment variables, command-line flags.
- Database schemas, file formats, and API request and response shapes.
- Error types that functions raise.
This pass can be partly automated. The following command shows only the added and removed lines that define functions and classes in Python files:
git diff -U0 -- '*.py' | grep -nE '^[+-][[:space:]]*(async def|def|class) 'Read the output as pairs. A removed def load_config(path) followed by an added def load_settings(path, strict=False) tells you the name changed and a new parameter appeared. The task was a rename, so the new parameter is unrequested. You found a scope problem without reading any function body.
Check each interface change against three questions:
- Was this change asked for in the task?
- Is every caller updated, or does the old name still appear somewhere?
- Is the change compatible with callers outside the diff, such as other services, scripts, or documentation?
The second question can be answered with a search over the whole repository, not only the changed files:
git grep -n "load_config"If the task was a rename and this prints results in files the agent did not touch, the rename is incomplete. Files the agent did not change are invisible in the diff, so a diff-only review cannot find this problem.
Pass 3: tests
Read the test changes before the implementation. Tests are a written statement of what the agent believes the behavior should be. If you read them first, you can judge the implementation against that statement, and you can judge the statement against the task.
git diff -- tests/In this pass, read for meaning:
- Which assertions were added, changed, or removed?
- Does each new test describe a behavior from the task?
- Did an existing expected value change? If so, the agent changed the definition of correct, which needs a reason.
Deeper checks for weakened tests, such as skipped tests and silenced linters, belong to a later chapter. Here you only build the picture of claimed behavior. Write down any expected value that changed so you can confirm it against the implementation in the next pass.
Pass 4: implementation
Now read the bodies. By this point you know what each function is supposed to do and which files should have changed. Reading goes faster, and mismatches are easier to spot.
Two options reduce noise in this pass. Refactors often move code between files. The --color-moved option colors moved blocks differently from real edits:
git diff --color-moved=zebra
git diff -wThe first command lets you skip blocks that moved without changes. The second hides whitespace-only changes. Use -w with care in languages where whitespace is syntax, such as Python and YAML, because an indentation change can alter behavior there. When a hunk looks like only a re-indent, check it once without -w.
Read each hunk with surrounding context, not as an isolated fragment. The default three lines of context are often too few. You can ask Git to show the whole enclosing function:
git diff --function-context -- src/config/loader.pyWithin each function, check these things in order:
- Inputs: are new assumptions made about types, ranges, or missing values?
- Calls: does every called function or method exist, with those arguments? Agents sometimes call APIs that do not exist in the installed library version.
- Branches: did a condition change, and what happens on the paths that were not tested?
- Errors: is an exception now caught and ignored, or a failure now turned into a default value?
Pass 5: leftovers
The last pass covers things that are easy to forget because they are not source logic:
- Deleted files and deleted blocks. Run
git diff --diff-filter=D --name-onlyand confirm that each deletion was intended. - New untracked files, which
git diffdoes not show. Usegit status --short. - Dependency manifests and lock files. A new package is a decision, not a detail.
- Configuration, continuous integration files, and build scripts.
- Generated or vendored files, which you should confirm were regenerated by a tool and not edited by hand.
- Comments, docstrings, and documentation that now describe old behavior.
Keeping notes while you read
Keep a short running list with one line per finding, in this form: the file, what you saw, and whether it blocks acceptance. This list becomes your corrective message to the agent, so writing it as you go saves work later. It also stops you from relying on memory across twenty files.
| Pass | Question | Typical finding |
|---|---|---|
| File list | Does the set of files match the task? | An unrelated billing file was edited |
| Interfaces | Did any contract change that was not requested? | A new parameter with a default value |
| Tests | What do the tests now claim? | An expected value was changed |
| Implementation | Does the body match the contract? | A call to a method that does not exist |
| Leftovers | What else changed outside the logic? | A new dependency, a deleted file |
When to stop early
Not every pass has to finish before you act. If pass 1 shows many unrelated files, or pass 2 shows a contract that contradicts the task, stop. Reading the implementation of a change built on the wrong contract is wasted effort. Send feedback, or revert and re-run with a narrower task. The chapters on feedback and escalation cover how to do this.
Exercise
An agent was asked to rename load_config to load_settings in a project. Your interface pass shows -def load_config(path) and +def load_settings(path, strict=False). git grep -n "load_config" still returns three matches in scripts/deploy.py, a file that is not in the diff. List two problems and the pass in which you found each.
Show answer
First, the new strict=False parameter was not requested, so the change goes beyond a rename. You found it in pass 2 (interfaces) by comparing the signatures with the task. Second, the rename is incomplete. scripts/deploy.py still calls the old name and will fail at runtime. You found it in pass 2 as well, with a repository-wide search, because the diff cannot show files the agent never touched. Both findings concern the contract, so you can send feedback now without reading the implementation.