Skip to content
mentoor

Topic 1 of 4 · 4 min read

Reading an AI-written diff

Goal: Review an AI-generated diff against the task it was given and find logic errors at boundaries, nulls and failure paths.

Review an AI-written diff the way you would review a fast, confident new colleague: start from what was asked, then make the code prove it does exactly that. AI code usually compiles, reads cleanly and follows the happy path. The bugs sit at the edges.

Check yourself · 1 of 3

Before reading an AI-written diff, what should you do first?

Choose one answer

Why it is different from human code

A human author leaves signals of doubt: a TODO, a question in the PR, an odd name. An agent such as Claude Code, Codex, Copilot or Cursor writes everything in the same fluent style, including the wrong parts. It may also "fix" a failing test by changing the test, or handle an error by swallowing it. Fluency is not evidence.

A reading order that works

  1. Re-read the task first. The issue, the prompt or the ticket. Write down in one line what should change. If you cannot, the diff cannot be judged.
  2. Read the tests before the code. Do they test the behaviour you asked for, including the failure cases? Were existing tests edited or deleted?
  3. Read the code at the boundaries. Empty lists, nulls, zero, off-by-one ranges, time zones, concurrent calls, the error path. Ask "what happens when this call fails?" for every I/O line.
  4. Check the fit. Does it use the project's existing helpers, error types and patterns, or does it invent new ones? Duplicated utilities and a second logging style are common agent habits.
  5. Check that it ran. CI green, and for behaviour changes, a reproduction: run the endpoint, the CLI or the test yourself.

Before and after

// Task: "Return the user's 10 most recent orders."
export async function recentOrders(userId: string) {
  const orders = await db.orders.findMany({ where: { userId } });
  return orders.slice(0, 10);
}

It reads well and passes a test with three orders. But nothing sorts the rows, so "most recent" depends on database order, and it loads every order before slicing. The reviewed version pushes both into the query:

export async function recentOrders(userId: string) {
  return db.orders.findMany({
    where: { userId },
    orderBy: { createdAt: "desc" },
    take: 10,
  });
}

What goes wrong

  • Approving on fluency. Clean names and comments make wrong logic look right.
  • Reviewing only the changed lines. A one-line change can break a caller you did not open.
  • Trusting the agent's summary. "All tests pass" may mean it deleted the failing one. Read the diff, not the description.
  • Skipping the run. A diff that has never been executed is a hypothesis.

Examples

The swallowed error

try:
    send_invoice(order)
except Exception:
    pass  # keep the checkout flowing

The agent made a failing test pass by ignoring every error. Customers would silently get no invoice. Ask for a specific exception, a log line and a retry or an alert.

One line, wide impact

A diff changes parseDate(s) to return null instead of throwing on bad input. The change is one line and the unit test was updated. Search for callers: 14 of them use the result without a null check. The review is about those 14 call sites, not the one changed line.

Key points

  • Start from the task: write in one line what should change before you read the diff.
  • Read the tests before the code, and check whether existing tests were edited or deleted.
  • AI bugs cluster at the edges: empty input, nulls, ordering, error paths and concurrency.
  • Check fit with the codebase: existing helpers, error types and patterns, not new duplicates.
  • Fluent code and a confident summary are not evidence; a test or a run is.

Common mistakes

  • Reading the diff top to bottom without first restating what it should do.
  • Approving because the code is clean and well commented.
  • Taking the agent's "all tests pass" summary at face value.
  • Ignoring callers of a changed function because they are not in the diff.

Check yourself · 2 of 3

Where do bugs in AI-generated code most often hide?

Choose one answer

Check yourself · 3 of 3

An agent's summary says "Fixed the bug, all tests pass." What is the best response?

Choose one answer

Practice items, not counted towards your mastery.

Start here