Plausible Isn't Correct: Auditing the Loops Your AI Assistant Hands You
Photo: DFID - UK Department for International Development, CC BY 2.0, via Wikimedia Commons
Every few weeks someone on my team drops a chunk of generated code into review with the same four-word preamble: the assistant wrote this. And more often than not the code is fine. Clean names, sensible types, a comment that explains the intent better than half the comments I write myself.
That is exactly what makes reviewing it uncomfortable.
When a person writes a loop, you can usually reverse-engineer the reasoning from its shape. You can see where they got nervous and added a guard, where a boundary check got bolted on after last quarter's incident. Generated loops show up without any of that history. They show up plausible. And plausibility is not correctness — the distance between the two lives in three places: termination, invariants, and edges.
Termination is a claim, not a vibe
Ask an assistant why a loop stops and you will get something reasonable-sounding. Ask it to prove the loop stops and you will get a restatement of the condition. Those are different things.
Every loop needs a measure: something that strictly decreases on each pass (or an upper bound that strictly advances), bounded below. For a for loop over a fixed array that comes free. For a while loop driving pagination, retries, backoff, or a cursor, it does not come free at all — and that is precisely where generated code quietly goes wrong.
The tell is a loop whose only exit is a short read. It looks bounded because in the happy path it is. On a bad Tuesday with a remote API that ignores your offset, it is not.
The invariant is the thing you actually ship
A loop invariant is the sentence you would write on a whiteboard: what is true before the first pass and still true after every pass. Everything in results came from a batch we already fetched. The window never holds more than N events. left points at the first unprocessed record.
If you cannot state it, you cannot review the loop — you can only review the style. Reviewing style is fine for a getter. It is a bad idea for the job that reconciles payments.
The edges are where the plausible version dies
Empty input. One element. All duplicates. A batch that comes back exactly full. A stream that ends mid-batch. A retry that returns the same cursor twice. Generated code tends to handle the happy path and the empty case, and to skip the exactly-full case — which happens to be the one that produces an infinite loop.
A pre-merge checklist for AI-suggested loops
- Name the measure. Put it in a comment or the PR description: what decreases each iteration?
- Prove progress on the no-op path. If a branch can run without advancing the cursor, index, or bound, that is your hang.
- State the invariant in one sentence. If it takes two paragraphs, split the loop.
- Test the exactly-full batch. Off-by-one bugs live at capacity, not at the ends.
- Feed it an empty collection. Not because it will fail, but because the result tells you whether anyone thought about it.
- Check the tail flush. Two-stream loops love to forget the leftover records in the longer stream.
- Look for mutation of the thing you are iterating. Generated code is usually well-behaved here, but usually is doing a lot of work.
- Confirm retry bounds are counts, not hopes.
while (true)with abreaksomewhere is not a bound. - Check integer and clock arithmetic. The assistant does not know your skew, your timezone defaults, or your
MAX_SAFE_INTEGER. - Run it against production-shaped data. Ten thousand rows with duplicates and one NULL will find more than an hour of reading.
A worked example
Here is the kind of thing that lands in my queue:
async function drain(queue) {
const results = [];
let offset = 0;
while (offset < queue.length) {
const batch = await fetchBatch(offset, 100);
results.push(...batch);
if (batch.length < 100) break;
}
return results;
}
Reads fine. Terminates — usually. It exits only because a short batch eventually shows up, and if the source always returns full pages, this spins until someone kills the process. The measure is missing. The fix is roughly one line:
offset += batch.length;
if (batch.length === 0) break;
Now the loop makes progress whether or not the remote cooperates, and it has a real stop condition instead of a lucky one.
What this means for how we review
Generated code raises the floor, and that is genuinely good — fewer typos, fewer missing null checks, a faster first draft on a boring Tuesday. But it does not raise the ceiling. The ceiling is still set by whether a human can say out loud what the loop maintains and why it ends.
The invariant is the deliverable. The loop is just how you get there.