Programming

How to Review AI-Generated Code Before You Merge

A practical checklist for reviewing agent-written pull requests: start with the diff, watch for weakened tests, invented APIs, and drive-by refactors, then run the feature yourself.

Developer at a desk reviewing a code diff on a monitor, soft daylight, shallow depth of field

AI coding agents are now part of everyday shipping. They draft features, fix tests, and open pull requests that look tidy at a glance. The problem is that tidy is not the same as correct. Agent output often reads as confident whether or not the change matches the task, preserves existing tests, or calls APIs that actually exist.

This guide is a review order you can use on any agent-written PR—Claude Code, Copilot, Cursor, or a teammate’s assistant. The goal is not style nits. It is catching the failure patterns that keep showing up in real reviews: scope creep, weakened tests, invented packages, and error paths that disappear.

Start with the diff, not the summary

Skip the agent’s own description until you have seen the change. Summaries tend to describe intent, not what landed in the tree.

bash
git status
git diff --stat
git diff

git status matters because new files do not always show up clearly in a casual glance at the PR UI. git diff --stat tells you whether a “one-line fix” touched eleven files. If the file list is wider than the task, stop and ask why before you read line by line.

Treat every unexpected file as intentional until proven otherwise. Drive-by refactors—renamed locals, reordered imports, reformatted blocks—bury the real change and make bisecting harder later.

Check scope against the task

Read the issue or prompt first, then the diff. For each changed file, ask one question: did this task need this file?

  • Config, CI, or lockfile edits the task did not mention

  • Shared helpers “cleaned up” along the way

  • New dependencies added without a stated requirement

  • Comments or docs that restate the agent’s narrative instead of the system’s behavior

If the PR mixes a bugfix with a refactor, send it back. Small, single-purpose commits are easier to review and safer to revert. Agents will often do this when asked; they rarely do it when the prompt is vague.

Read the tests before the implementation

Agent-written tests are not neutral evidence. A common pattern is to change the test so the new code passes: looser assertions, deleted edge cases, a new skip, or a mock that hides a failure path.

For every test file in the diff:

  • Did an existing assertion get removed or relaxed?

  • Do new tests fail if you revert the production change?

  • Are failure cases covered, or only the happy path?

A green suite that the agent also authored proves less than a green suite built from a pre-existing test or a case you wrote yourself. Prefer at least one check the authoring model did not invent.

Verify packages, APIs, and options exist

Agents still invent plausible names. Before you approve an import or a new dependency, confirm it in the registry and docs you actually use:

  • Package name and version on npm, PyPI, or your language’s registry

  • Method and option names in the library version pinned in the repo

  • Flags and environment variables that the runtime really supports

Copy-paste from the agent’s explanation is not verification. Open the docs. If the call looks slightly off—wrong argument order, deprecated option, imaginary helper—assume it is wrong until the docs say otherwise.

javascript
// Example of the kind of line worth checking, not pasting blindly:
import { parseConfig } from 'config-toolkit'; // does this package exist?
const result = parseConfig(input, { strictMode: true }); // is strictMode real?

The same rule applies to internal APIs. If the agent “remembers” a service method your codebase never had, the PR should not ship that call.

Look for silent failure handling

Agents optimize for code that runs. That often means swallowing errors, returning empty defaults, or catching broadly so the happy path stays green.

Scan for:

  • Empty catch blocks or catch (e) { return null; } without logging or rethrow

  • Default values that hide missing data instead of failing loudly

  • Retries without a limit or without surfacing the final failure

  • Feature flags or conditionals that disable the broken path rather than fix it

Prefer explicit failure for unexpected states. A clear error in development is cheaper than a quiet wrong result in production.

Security and secrets

Treat agent output as untrusted input to your repository.

  • No API keys, tokens, or connection strings in source

  • No client-side exposure of server secrets (for example public env prefixes that should stay server-only)

  • No new trust of query params, headers, or file paths without validation

  • No shell commands built from unsanitized strings

If the agent edited CI workflows, auth, or dependency manifests, raise the review bar. Those paths deserve a second reader.

Run the feature yourself

Automated tests are necessary; they are not sufficient. Exercise the change the way a user would:

  1. Happy path with realistic data

  2. One empty or missing input

  3. One permission or network failure, if relevant

If you cannot explain what the change does without reading the agent’s summary again, you are not ready to approve it. Ownership stays with the human who merges.

A short checklist you can paste into the PR

markdown
## AI PR review
- [ ] `git status` / `git diff --stat` reviewed; scope matches the task
- [ ] No surprise dependencies, CI, or config changes
- [ ] Tests not weakened, skipped, or deleted to pass
- [ ] New tests fail without the production change
- [ ] Imports, APIs, and options verified in real docs
- [ ] Errors surface; no silent catches or hidden defaults
- [ ] No secrets or unsafe client exposure
- [ ] Feature exercised manually on happy path + one failure case

Keep agent PRs small on purpose

Most review pain comes from size. Ask the agent to land one logical step per branch: migration separate from service code, service code separate from UI. You spend a little more time prompting and much less time untangling a mixed diff.

Agents are fast at producing code. Your job is still the same as it was before they showed up: decide what is true, what is safe, and what is allowed to ship.