Sub topic 3
Scoping the Task and Defining Done Before the Run
This chapter assumes you know basic Git (branches, commits, diffs, staging), can read code in at least one language, can run your project's tests, linter, and build from the command line, and have used a coding agent at least once.
Review starts before the agent runs. A vague task produces a large, loosely related diff. A scoped task produces a diff you can check against a short list of expectations. This chapter covers how to write that list, what to put in the task, and which mistakes make review harder.
Why scope decides review cost
An agent edits files until it believes the task is finished. If the task does not say what "finished" means, the agent picks its own definition. It may stop early, or it may keep going into code you did not intend to change.
Scope gives you two things when you review:
- A boundary. Any changed file outside the boundary is a question you must ask about.
- A target. Any stated outcome that is not met is a defect you can name.
Without both, you review a diff by asking "does this look reasonable?" That question is weak. With both, you ask "does this match what I asked for?" That question has an answer.
The parts of a well-scoped task
A scoped task answers five questions before the run.
- Goal. What should be true after the change that is not true now?
- Boundary. Which files or directories may change? Which must not?
- Constraints. What must stay the same? Typical examples are public function signatures, file formats, and existing test behavior.
- Done criteria. Which commands must pass, and what should they show?
- Reporting. What should the agent tell you at the end?
Here is a weak task and a scoped version of the same work.
| Weak task | Scoped task |
|---|---|
| "Clean up the config loading code." | "Rename `ConfigLoader` to `SettingsLoader` in `src/config/` and update every import. Do not change behavior or public function signatures. Do not edit files outside `src/` and `tests/`. Done when `pytest` and `mypy src` both pass." |
The weak version allows almost any diff. The scoped version lets you predict the diff: a set of renamed references, no new logic, no changes to billing or deployment files.
Write the boundary as paths
State the boundary in terms the file system can confirm. "Only the config module" is ambiguous. "Only files under src/config/ and tests/config/" can be checked with one command later.
A useful boundary has three lists:
- May change: the paths the task needs.
- May read but not change: shared code the agent needs to understand, such as base classes.
- Must not change: generated files, lockfiles, migrations, CI configuration, and anything that carries risk out of proportion to the task.
Lockfiles and CI configuration are common accidental edits. An agent that installs a package to run a test can rewrite a lockfile, and an agent that "fixes" a failing pipeline can edit workflow files. Naming them as off-limits removes the temptation.
Define done with commands, not adjectives
"Works correctly" and "well tested" are not checkable. A command with an expected result is.
Good done criteria are:
- Executable. You can run them yourself without the agent.
- Specific. They name the command and the expected outcome.
- Independent of the agent's claims. They do not depend on the agent's summary being true.
For the rename task, the criteria might be:
Done means all of the following hold:
1. `pytest` exits with status 0 and the number of collected tests is unchanged.
2. `mypy src` reports no errors.
3. `grep -rn "ConfigLoader" src tests` returns no matches.
4. `git diff --name-only` lists only files under src/ and tests/.Criterion 1 includes the test count on purpose. If the agent deletes or skips a failing test, the exit status still shows success, but the count drops. Later chapters cover test integrity in depth. Here the point is that you can write the defense into the task before the run.
Criterion 4 turns the boundary into a command. After the run, one git diff --name-only call tells you whether the agent stayed inside it.
Record the baseline numbers first
Some done criteria compare against the starting state: "the number of tests is unchanged" requires knowing the number. Record it on a clean branch before the run.
git switch -c agent/rename-config-loader
git status --short # no output means the working tree is clean
pytest --collect-only -q | tail -n 1
mypy src | tail -n 1Write the results down. The pytest --collect-only -q line ends with a summary such as the number of tests collected. If the baseline already fails, note which tests fail. Otherwise you cannot tell whether the agent broke something or inherited it.
If the baseline is already red, decide one of two things before the run: fix it first, or list the known failures in the task so the agent does not try to repair them as a side effect.
Choose the size of the task
A task that touches many files is not automatically too big. The right measure is how much you can verify, not how many files change. A mechanical rename across forty files is easy to verify with grep and the compiler. A change to one retry algorithm in one file may be harder to verify because its correctness depends on timing and edge cases.
Use these guidelines:
- Prefer tasks with one reason to change. "Rename this class" has one. "Rename this class, switch to a new logging library, and fix the flaky test" has three, and their diffs mix together.
- Split by verification method. If one part is checked by the type checker and another by manual testing, run them as separate tasks.
- Split when the boundary is hard to state. If you cannot list the paths that may change, you probably do not understand the task well enough to review it.
- Avoid open-ended verbs. "Improve," "clean up," "modernize," and "refactor" do not name an outcome.
Say what the agent should not do
Agents fail in predictable ways, so some constraints are worth stating every time:
- Do not change tests to make them pass. If a test seems wrong, report it instead.
- Do not add dependencies.
- Do not change public interfaces unless the task says so.
- Do not reformat code you are not otherwise changing.
- If something is unclear, stop and ask instead of guessing.
The last line matters most. Without it, an agent resolves ambiguity silently, and you find the decision only if you notice it in the diff. With it, ambiguity comes back to you as a question.
Ask for a report you can check
Request a report with a fixed shape, so you can compare it with reality:
When finished, report:
- the list of files you changed, with one line on why each changed
- the exact commands you ran and their results
- anything you were unsure about or chose not to doTreat the report as a claim, not as evidence. You will compare the file list against git status --short and re-run the commands yourself. A mismatch between the report and the actual diff is a strong signal to review more carefully.
Putting the task together
Here is a complete task statement for the rename example.
Task: Rename the class ConfigLoader to SettingsLoader.
Scope:
- May change: files under src/config/ and tests/config/, plus any file that imports ConfigLoader.
- Must not change: pyproject.toml, lockfiles, .github/, migrations/.
Constraints:
- No behavior changes. Public function signatures stay the same except for the class name.
- Do not edit, skip, or delete tests. If a test fails, report it.
- Do not add dependencies.
- If anything is ambiguous, stop and ask.
Done when:
1. pytest exits 0 with the same number of collected tests as before.
2. mypy src reports no errors.
3. grep -rn "ConfigLoader" src tests returns nothing.
Report: files changed with a reason for each, the exact commands you ran with their results, and any open questions.The following diagram shows how this text connects to the review that follows.
Common scoping mistakes
| Mistake | Effect on review | Fix |
|---|---|---|
| No boundary stated | Every changed file needs a justification you must invent | List allowed and forbidden paths |
| Done defined as "tests pass" | Skipped or deleted tests still pass | Add test count and a rule against editing tests |
| Baseline not recorded | You cannot tell new failures from old ones | Run checks on a clean branch first |
| Several goals in one task | Unrelated changes mix in one diff | Split into separate runs and commits |
| Criteria only the agent can check | You rely on its summary | Use commands you can run yourself |
| Constraints given only in chat | They are forgotten in later runs | Move repeated constraints into project instructions |
The last row is the topic of a later chapter. If you type the same constraints into every task, they belong in a file the agent reads each time.
A short pre-run checklist
Before you start the agent, confirm:
- You are on a new branch and
git status --shortprints nothing. - You recorded the baseline results of the tests, type checker, and linter.
- The task names its goal, boundary, constraints, done criteria, and report.
- Every done criterion is a command you can run yourself.
- You could predict, roughly, which files the diff will contain.
Item 5 is the quickest test of whether the task is scoped. If you cannot predict the file list, you will struggle to judge it afterward.
Exercise
A teammate gives an agent this task: "Make the date parsing more robust." The agent changes 14 files, including a lockfile and two test files, and reports "all tests pass." Rewrite the task so it can be reviewed. Include a boundary, two done criteria that you can run yourself, and one constraint about tests.
Show answer
A good answer names a concrete outcome and paths. For example: "Make parse_date in src/utils/dates.py accept ISO 8601 strings with a trailing Z. May change: src/utils/dates.py and tests/utils/test_dates.py (add tests only). Must not change: lockfiles, .github/, other modules. Do not edit, skip, or delete existing tests. Done when: (1) pytest tests/utils exits 0 and the collected test count is at least the baseline count plus the new tests; (2) git diff --name-only lists only the two allowed files. Report the files changed and the commands you ran." The key points are a concrete outcome in place of "robust," a path boundary, commands you run yourself, and a rule that protects existing tests.
Exercise
Why does the done criterion "the number of collected tests is unchanged" catch a problem that "pytest exits with status 0" misses?
Show answer
Deleting a failing test, or removing it from collection, makes the test run pass even though coverage dropped. Pytest exits 0 when all collected tests pass, and it does not know about tests that are no longer collected. Comparing the collected count to the baseline recorded before the run exposes the loss. (Tests marked skip or xfail are still collected, so check those with a separate search of the diff.)