Sub topic 12
Verifying subagent work before you trust it
A subagent's summary describes what it believes it did. It does not prove the change works. This chapter covers how to check subagent output before you merge it or build on it.
This chapter assumes you know what a subagent is, how an orchestrator hands out briefs, and how git branches and worktrees isolate parallel work. It follows the chapter on collecting and merging results.
Why a summary is not evidence
A subagent returns a short summary so the parent's context stays clean. That is the point of context isolation. The cost is that the parent sees a claim, not the work behind it.
Subagents produce unreliable claims in predictable ways:
- Optimistic reporting. The summary says "tests pass" when the agent never ran them, or ran a different test file.
- Scope drift. The agent edits files outside its brief because that made its own task easier.
- Weakened checks. The agent makes a failing test pass by deleting an assertion, adding a skip marker, or loosening a comparison.
- Plausible but wrong code. The change compiles and looks reasonable but misses an edge case.
- Partial completion. Two of three requested changes were made, and the summary mentions only the finished ones.
Treat the summary as a pointer to where to look. Verify against the workspace.
Verify against the done condition
A good brief includes a checkable "Done when" line, such as pytest tests/test_cli.py passes. That line is also your verification plan. Run the command yourself, in the subagent's workspace, instead of trusting the reported result.
# Run inside the subagent's worktree, not the parent checkout
cd ../wt-tests
pytest tests/test_cli.py
echo "exit code: $?"Read the exit code, not only the text. A test run that collected zero tests can print no failures and still prove nothing.
Layers of verification
Verify in layers, cheapest first. Stop and send work back as soon as a layer fails.
- Scope check. Did the agent touch only the files the brief allowed?
- Mechanical checks. Do the formatter, linter, type checker, and build pass?
- Behavior checks. Do the relevant tests pass, and do they test the right thing?
- Human or reviewer read. Does the diff make sense, and is it the smallest change that does the job?
Scope check
Compare the actual changed files with the brief's scope. With a worktree branch this is one command:
git diff --name-only main...agent/testsIf the brief said "edit only tests/test_cli.py" and the output lists src/cli.py as well, you have a scope violation. Reject the change even if the extra edit looks helpful. An unrequested edit to production code may conflict with another subagent's branch or hide a bug the new tests were meant to expose.
You can automate this check in the orchestrator:
import subprocess
def changed_files(repo_dir: str, base: str, branch: str) -> set[str]:
out = subprocess.run(
["git", "diff", "--name-only", f"{base}...{branch}"],
cwd=repo_dir,
capture_output=True,
text=True,
check=True,
).stdout
return {line for line in out.splitlines() if line}
def scope_violations(changed: set[str], allowed: set[str]) -> set[str]:
return changed - allowed
if __name__ == "__main__":
changed = changed_files(".", "main", "agent/tests")
extra = scope_violations(changed, {"tests/test_cli.py"})
if extra:
print("Out of scope:", sorted(extra))
else:
print("Scope OK")The three-dot form main...agent/tests compares the branch against its merge base with main. It shows only what the branch changed, not changes that landed on main afterwards.
Mechanical checks
Run the same checks your CI runs. Use the project's own commands, not ones the subagent chose. A subagent that runs a narrower linter configuration than CI will report success on code CI later rejects.
Behavior checks
Passing tests are only evidence if the tests can fail. Three habits help:
- Check that new tests fail without the change. Stash or revert the production change and run the new tests. If they still pass, they assert nothing useful.
- Look for weakened tests. Search the diff for removed assertions,
skiporxfailmarkers, and widened tolerances. - Run the wider suite. A change to one module can break a distant caller. Run the full test suite at least once before the final merge, even if each subagent ran only its own tests.
# Detect added skip or xfail markers in a subagent's diff
git diff main...agent/tests | grep -E '^\+.*(skip|xfail)'Diff review
Read the diff, not the summary. Look for changes that are larger than the task needs, new dependencies, hard-coded values copied from test data, and swallowed exceptions. Reviewing is faster when the brief limited scope and asked for small changes.
Use an independent reviewer
The agent that wrote the code is a poor judge of it. It shares the same assumptions that produced any mistake. Two ways to add independence:
- A reviewer subagent. Start a fresh subagent with a read-only toolset. Give it the original brief and the diff, not the author's reasoning. Ask it to list defects, not to approve.
- A different check than the author used. If the author wrote tests, review with a type checker, a manual run, or a reviewer who writes their own probe case.
A reviewer is not a replacement for running the checks. It is a way to catch what the checks do not cover, such as a misread requirement. Reviewer output is also a claim, so confirm any defect it reports before acting on it.
Goal: Review the diff on branch agent/tests for defects.
Context: The change was meant to add unit tests for parse_args in src/cli.py. The original task brief is below.
Scope: Read-only. Do not edit any files.
Constraints: Report only problems you can point to in the diff, with file and line. Do not report style preferences.
Done when: You have checked for removed assertions, skipped tests, out-of-scope files, and tests that cannot fail.
Return: A list of defects with file and line, or the single word NONE.Decide what happens on failure
Verification needs a rule for what to do when a check fails. Use a small set of outcomes:
| Outcome | When to use it | Action |
|---|---|---|
| Accept | All layers pass | Merge the branch |
| Revise | Small, clear defect | Send the specific failure back to the same or a new subagent with the failing output |
| Redo | Brief was unclear or work drifted | Rewrite the brief, discard the branch, start again |
| Take over | Repeated failure or subtle problem | The orchestrator or a human does the change directly |
When you send work back, include the exact failing command and output. "Tests fail" gives the subagent nothing to act on. Cap the number of revision rounds, for example two. After that, a repeated failure usually means the brief or the decomposition is wrong, not that the subagent needs another try.
Verify after merging, too
Each branch can pass on its own and still fail when combined. Two subagents might each add a function with the same name in different files, or change a shared interface in incompatible ways. After merging branches one at a time, rerun the build and full test suite on the merged result. Merge in sequence and check after each merge, so a failure points at the branch that caused it.
git checkout main
git merge --no-ff agent/docs
pytest # full suite after the first merge
git merge --no-ff agent/tests
pytest # full suite after the second mergeCommon mistakes
- Accepting "all tests pass" without running them.
- Running checks in the parent checkout instead of the subagent's worktree, so you test the wrong code.
- Verifying only the subagent's own tests and skipping the full suite.
- Letting the author review its own work.
- Giving the reviewer the author's explanation, which biases it toward agreement.
- Retrying indefinitely instead of fixing the brief.
Exercise
A subagent working on branch agent/parser reports "Added tests for the date parser, all pass." The brief allowed edits only to tests/test_dates.py. Name three checks you would run before merging, and say what each one catches.
Show answer
First, run git diff --name-only main...agent/parser to check scope. It catches edits to files outside tests/test_dates.py. Second, run the tests yourself in the subagent's worktree and read the exit code. It catches a false "all pass" claim or a run that collected no tests. Third, revert any production change, or review the diff for removed assertions and skip markers, and confirm the new tests fail without the fix. It catches tests that cannot fail. You should also run the full suite after merging to catch interactions with other branches.
Exercise
Why should a reviewer subagent receive the diff and the original brief but not the author's explanation of its reasoning?
Show answer
The author's explanation carries the same assumptions that may have caused a mistake. A reviewer that reads it tends to accept the same assumptions. With only the brief and the diff, the reviewer judges the change against the requirement independently.