Here is a function of the kind that goes through review every day. It applies a discount code to an order.
async function applyDiscount(orderId, code) {
const discount = await db.discounts.findOne({ code, active: true });
if (!discount) return { ok: false, reason: "invalid_code" };
const order = await db.orders.findOne({ id: orderId });
if (order.discountApplied) return { ok: false, reason: "already_applied" };
await db.orders.update(orderId, {
total: order.total - discount.amount,
discountApplied: true,
});
return { ok: true };
}
Read it the way a pull request actually gets read: does it do what the ticket asked? The ticket asked for a discount code to reduce an order total, and for the same code not to apply twice. It does both. It is short, the names are good, the early returns are clean, and a test that calls it once and then calls it again will pass every time.
Now read it as though it is trying to get something past you.
What one small function was hiding
It can be applied twice. Between reading discountApplied and writing it, there is a gap exactly as wide as the time between two requests. A customer with a slow connection and a double click gets the discount twice, for real money. The check exists. The check is not atomic, which is a different thing.
The order might not exist. order.discountApplied is read without checking that order is anything at all. A wrong or deleted order id does not return a clean error, it throws, and what the caller does with that throw is anybody's guess.
The total can go negative. Nothing compares discount.amount to order.total. A fixed amount larger than the order turns a payment into a refund, which is a feature nobody asked for and finance will find first.
The two writes are one write in the code and two facts in the world. The total and the flag are set together here, but anything that later reads the total while believing the flag is independent will be wrong. There is no transaction and no version check, so a concurrent write to the same order from another feature simply overwrites.
The discount's own rules are not checked. The query filters on active. Not on expiry, not on usage limit, not on whether this customer or this product is eligible. Whatever exists in the discounts table besides active is being ignored, and the longer the table lives, the more there is to ignore.
Five problems. One of them loses money quietly, one throws, one inverts a payment, and none of them is a style issue. A reviewer who commented "nice, maybe extract a helper" would not have been wrong about anything, just useless.
Why the ordinary read misses all five
This is not a story about careless reviewers. It is a story about what review is good at.
The largest published case study of code review, run by SmartBear at Cisco across 2,500 reviews and 3.2 million lines of code, found two things worth keeping. Defect discovery falls off sharply once a reviewer takes in more than two to four hundred lines in one sitting. And 61% of those reviews surfaced no defects at all.
That was before a single agent was in the loop. The volume of code arriving at review has gone up since, and the minutes available per line have not.
So review optimises, without anyone deciding to. It becomes a check that the code says what it should say: names, structure, obvious duplication, whether tests exist. All real. None of it reaches a race condition, because a race condition is not visible in what the code says. It is visible only in what happens when two copies of it run at the same time, which no amount of careful reading of a single path will show you.
The test has the same blind spot. Call it once, check the total dropped. Call it again, check it is rejected. Green. It was always going to be green, because a sequential test cannot produce the only condition under which the bug exists.
The read that does reach them
Five questions, asked of any function that changes state that matters. The discount function fails on all five, which is why it is a good example rather than a cruel one.
1. What runs twice? Not "is there a guard" but "is the guard safe when two requests are inside it". Anything that reads a value, decides, and then writes is a candidate until proven otherwise.
2. What does the state actually look like, not what does the code say about it? Follow the rows, not the narration. Which fields change, in which order, and what would another reader of those fields believe halfway through.
3. What happens when this fails in the middle? The process dies after the write and before the response. The database accepts one update and rejects the next. The caller times out and retries. Each of those is an ordinary Tuesday, and each produces a different kind of wrong.
4. What is this trusting? Inputs, yes, but also rows. Here it trusts that the order exists, that the discount amount is sane, and that active is the only rule that matters. Part 9 was about assumptions that never get written down. This is where you meet them in the code.
5. Would you say this out loud to the person who owns the money? "We check whether the discount was applied, then apply it, and between those two steps another request can do the same." Nobody writes that sentence into a ticket on purpose. It is true anyway, and saying it aloud is what makes it obvious.
Three minutes per consequential function. Not per file, not per line.
Why agent-generated code deserves this specifically
Not because agents write worse code. The race condition above is one of the most common bugs human engineers have ever written, and the agent produced it for the same reason we do: it is the pattern that appears most often for "check then update", and it reads correctly.
The difference is what is missing around the code.
When you write a function by hand, you accumulate doubt as you go. The moment where you think "wait, what if this runs twice" arrives, unbidden, somewhere between typing the read and typing the write. It does not always arrive, and it does not always get acted on. But it is a by-product of construction, and it is free.
Code that arrives finished has no such by-product. There was no moment, so there is no doubt attached to it. It looks the way code looks after someone has already thought about it, which is exactly what it is not. The doubt has to be supplied afterwards, deliberately, by the reader, and the reader is the only remaining place it can come from.
That is the whole skill. Not suspicion of AI, suspicion of finished-looking things.
What this means for you
On the next pull request, agent-generated or not, pick the single function that changes the most consequential state and run the five questions on it before you look at anything else. Leave the naming comments for after.
If you want to practise against code that was built to be reviewed, the System Engineer question bank includes review exercises with the scoring rubric attached, so you can see what a strong finding looks like next to a plausible one.
The next article turns this into a drill you can run on yourself: a piece of code with a bug planted on purpose, and the question of how far you read before you found it.
The code review figures are from "Code Review at Cisco Systems", the SmartBear case study of 2,500 reviews across 3.2 million lines of code.
© Gabor Mayer. Licensed under Creative Commons Attribution 4.0 (CC BY 4.0). Free to share and adapt with attribution.
