A test has been flaky for a fortnight. It passes on your laptop, fails in CI about one run in six, and everybody on the team has learned to hit re-run. Eventually somebody opens a ticket, and the ticket goes to a coding agent with the obvious instruction: fix the flaky test.
The AI agent reads the test, then the code under test. It works out, correctly, that the assertion depends on ordering, and it changes the assertion so that ordering no longer matters. The test goes green and the suite goes green. The agent's report says the flakiness is resolved and explains what it changed and why, and every word of the explanation is accurate.
The pull request is four lines long and the reasoning reads like something a competent engineer would write. It is approved in under a minute, because everybody has seen a hundred of these.
The test was flaky because the code it was testing had a race in it, and that test was the only thing in the system that had ever noticed. Now it doesn't notice any more, and the race ships to production.
The story is hypothetical (it comes from chapter 1 of my book), but nothing in it is unusual. Chapter 2 tells a real one with the same structure: a test that passed because it never checked the order of events, and learners who failed a final quiz receiving the completion certificate anyway. Each step happens somewhere every week, and each one looks fine.
What the four lines might have looked like
Here is a version of it in RSpec. An order records events as it moves through checkout. The order is created, then a payment webhook marks it paid:
it "records paid order events" do order = place_order_and_pay(cart) expect(order.reload.events.map(&:kind)).to eq(%w[created paid]) end Usually the events come back as created, paid. One run in six, under CI's load, they come back as paid, created. The agent sees an assertion that depends on order and a result whose order varies, and makes the fix that follows, dropping the requirement that 'created' comes before 'paid'.
expect(order.reload.events.map(&:kind)).to contain_exactly("created", "paid") That one line is the whole diff that matters. It is a reasonable change, the sort of thing you would make yourself if the order of those events genuinely didn't matter. The test will never flake again.
The question nobody asked is why the events sometimes arrive in the wrong order. Suppose the answer is that the payment webhook can arrive while the order is still being created. The payment handler marks the order paid, then the creation step finishes and sets the status back to pending. In CI that showed up as events in the wrong order. In production it shows up as paid orders sitting in pending, a number of customers asking "I paid, where is my order?", and a few days of somebody trying to figure out why.
The old assertion was a crude detector for exactly that interleaving. It was noisy and it failed for a reason nobody had diagnosed, but it was the only detector the system had. The new assertion is a correct test of a weaker claim, that both events happened at some point. It passes whether the race fires or not.
What green actually tells you
A passing test tells you one thing: the code did what the test asked, this time. Whether that is useful depends entirely on what the test asks.
There are two ways to turn a red test green. You can change the code until it satisfies the test, or you can change the test until it accepts the code. Both produce a green build, both produce a small and tidy diff, and the CI badge cannot tell them apart. The first kind of change adds evidence and the second removes it, so a pipeline that reports both the same way is reporting less than it seems to.
People mostly know this, and in a team of humans it rarely hurts, because a person who loosens an assertion usually knows they are doing it. They write I'm not sure why this is order-dependent, so I've relaxed it for now in the PR, or leave a TODO line, or look slightly sheepish at stand-up. You learn to read those signals, and they tell you where to look.
An agent gives you none of them. Its explanation when it has found the cause and its explanation when it has made the symptom go away are written in the same confident voice, in the same manner. In the flaky-test case the explanation was even true: the assertion was sensitive to ordering. It just wasn't the reason the test failed.
There is a second habit that lets this slide through. With people, difficulty predicts where the mistakes are. Juniors get easy things right and struggle with hard ones, so you skim the four-line test fix and read the concurrency change carefully. That was a sensible use of attention with people. Agent errors don't cluster by difficulty. The same session can fix a genuine race in one file and paper over another in the next. A four-line diff isn't safe because it is four lines.
The question that would have caught it
The fix is one question, that should be asked of any test you are relying on:
What would make this go red?
Then check that the answer is the thing you care about.
For the old assertion, the answer was "the events arriving out of order", which is the symptom of the race. For the new one, the answer is "one of the events not happening at all", which has nothing to do with the race. Once you write the two answers next to each other, the change no longer looks harmless. It removes the only check on a real defect.
This works for any test, not only flaky ones. A request spec that checks non-admins get a 403 is worth nothing if the authorisation call is stubbed out, and the quickest way to find out is to delete the authorisation line and run the spec. If it still passes, it was never evidence. Mutation testing tools do this thousands of times automatically, breaking the code in small ways and reporting every break no test noticed. You don't need the tool to use the idea. You need the habit of breaking the thing you trust and watching the test go red.
What to change in practice
Asking the question when you remember to is a good start. You can make sure the question is hard to skip with these four changes.
Write down the problem before anyone touches the test. The test is flaky is a symptom. The problem underneath is one of two things: the test is wrong, or the code it tests is wrong, and at the start you don't know which. Say that in the ticket. Then the job becomes find out which is true, and only one of the two outcomes could end with a change to the assertion. The evidence was pointing the right way all along: a test that fails one run in six, only in CI, points at timing or concurrency. An assertion that is simply wrong fails every time.
State the constraint that nobody writes down. For any change to an existing test: the test must still detect whatever it detects now. With that sentence in the instructions, the agent's four-line change has to answer a question it can't answer, which is whether the loosened assertion still catches the ordering problem. It doesn't, and that would have been visible straight away.
Ask for the test that fails without the change. For anything that matters, require a test that fails on the old code and passes on the new one, and say which it is in the PR. A bug fix with no failing test has shown you only that the suite still passes, and the suite passed before the fix too. Put a field for it in the PR template so the question is asked every time, not only when somebody is feeling suspicious.
Treat a weaker assertion as a change to the evidence. When a diff makes an assertion more permissive (eq to contain_exactly, an exact match to include, a tighter tolerance to a looser one, a skipped example, an added retry), it deserves the same attention as a change to the production code it tests. In the flaky-test case it was the whole change. A short note in your review checklist, or a CI check that flags loosened matchers and skip tags for a human to look at, costs very little.
None of this means distrusting green builds in general. Most of them are telling you what you think they are. The point is to know which part of a green build is evidence and which part is a claim you are taking on trust. Changes to the code under test add evidence. Changes to the tests themselves change what counts as evidence, and those need someone to look at the test as well as the result.
Why AI agents make this worse
Agents are very good at making tests pass, which is what you asked for. The trouble is that "make the tests pass" and "make the code correct" are different instructions, and a fast, tireless, literal worker will find the cheapest route through whichever one you gave it. With people, the gap between those instructions was mostly bridged by judgement you didn't have to ask for. With agents you have to ask for it, in the task, in the context and in the review.
So the next time a flaky test is fixed in four lines, before you approve it, ask what used to make it go red, and whether anything still does.
This article is adapted from chapter 1 of Who Checked This? Engineering Judgement When Agents Write the Code. The book covers how to set up an agent so it states the problem before proposing a fix, how to match the evidence you demand to the risk of the change, and how to build gates that a passing test cannot talk its way through.