19 November 2026

·

5 min read

QA & Testingflaky testsAI agents

Flaky Tests and Agent Loops: Why Non-Determinism Costs More When a Machine Reads the Result

A coding agent reads a test result and retries on failure, so a flaky test triggers rewrites a human would have dismissed as noise. Quarantine, deterministic fixtures and a flake baseline become preconditions for agent-assisted delivery.

Anystack Engineering

Validation assumes the signal the agent reads is true. A green build means the code works; a red build means fix it. That contract breaks when a test fails for reasons unrelated to the code. A person who has seen the same test flip red and green on a rerun treats the failure as noise. A coding agent reads the red result, concludes its change was wrong and starts editing. This post covers what non-determinism costs once a machine, not a person, acts on the result.

Martin Fowler's article on eradicating non-determinism in tests catalogues the causes: tests that depend on wall-clock time, on execution order, on shared mutable state, on network calls, on async operations that race. His argument predates coding agents by more than a decade. The failure mode he describes, a test suite that teams stop trusting because some fraction of red builds mean nothing, gets worse when an automated loop sits downstream of the result.

What changes when an agent reads the result

A flaky test has always cost a team. Someone reruns the build, loses ten minutes and moves on. The cost was annoyance and slow erosion of trust in the suite. When a coding agent sits in the loop, the same flaky test produces a more expensive outcome.

The agent runs the suite against its change. A flaky test fails. The agent reads the failure as evidence that its implementation is wrong and edits the code to make the test pass. On the next run the test passes for unrelated reasons, or fails again and the agent edits further. The code now carries changes made in response to noise. A reviewer who trusts the agent's reasoning has to work out which edits addressed real failures and which chased a race condition that was never about the code at all.

Three properties of agent loops make this worse than the human case. The agent retries faster, so it accumulates more spurious edits before anyone looks. The agent has no memory that this specific test flakes, so it cannot discount the signal. And the agent's edits are plausible, so a reviewer skimming the diff sees reasonable-looking code rather than an obvious mistake.

Three things to put in place before an agent touches your suite

Fowler's remedies translate into preconditions for agent-assisted delivery. Each one is a concrete change an engineering leader can make this week.

First, establish a flake baseline. Run your existing suite several times against an unchanged commit and record which tests fail on at least one run without any code change. You cannot reason about agent-driven rework until you know which of your current failures are noise. Most teams have never measured this and are surprised by the count. The baseline is the number you drive down before an agent reads these results.

Second, quarantine the known flaky tests. Move tests that fail non-deterministically into a separate suite that does not gate the build and does not feed the agent's retry decision. A quarantined test still runs and still gets reported, so the flakiness stays visible and someone owns fixing it. What changes is that a race condition in the quarantine can no longer trigger an agent to rewrite working code. Fowler is blunt that quarantine is a holding pen, not a graveyard: a test that stays quarantined for weeks with no one fixing it should be deleted, because a permanently ignored test is worse than no test.

Third, make fixtures deterministic. The common causes Fowler lists, time, ordering, shared state and uncontrolled async, each have a standard fix. Inject a fixed clock instead of reading the system time. Seed any randomness and record the seed. Isolate state between tests so order does not matter. Replace live network calls in the test path with controlled doubles. These are ordinary engineering tasks, and they pay off twice: the human suite gets more trustworthy and the agent's input gets cleaner at the same time.

The check that keeps the agent honest

The three steps above reduce the noise reaching the agent. One more control catches the cases that slip through. Record, per change, how many times the agent reran the suite and whether any of its edits touched test files rather than implementation. A change where the agent rewrote a test to make a build pass needs a human to ask whether the test was right and the code wrong, or the other way round. That attempt count and that edit record belong on the change itself, visible to the reviewer, not buried in a CI log that no one opens.

An agent that iterates against a feedback signal is only as good as the signal. A flaky test is a corrupted signal. So is a test the agent is allowed to edit, because then a green build proves the code and the test agree rather than proving the code is correct. Teams running agents in their pipelines need both a clean signal and a boundary the agent cannot cross, and a record that shows which side of that boundary each edit landed on.

What this looks like in practice

An engineering leader who wants to act on this does not need a new tool. The work is: measure the current flake rate, quarantine what flakes, fix the determinism causes test by test, and put the agent's retry count and test-file edits on the change record where a reviewer sees them. The first two take a day. The third is ongoing engineering that also improves the suite for the humans who read it.

The payoff is a suite you can let an automated loop read without it chasing noise into your codebase. Without that, agent-assisted delivery adds a fast, confident actor on top of a signal you already knew you could not fully trust.

Anystack's quality engineering work starts by measuring what a suite detects and what it merely runs, including the flake baseline that decides whether an agent's retries mean anything. Our pods deliver inside your repositories, so the quarantine, the deterministic fixtures and the per-change record of agent edits land in your pipeline and stay there at handover. You can see how we approach quality engineering and how every change passes machine checks before a named reviewer sees it.


*Anystack Engineering runs three-person engineering pods that deliver inside your repositories with a written spec for every change, a named reviewer of record and delivery measured per engagement. Engagements from £18,000 per month. How the pod delivers.*

Discussions

0

Technical analysis, failure mode challenges, and reproduction observations.

Add to discussion

0/3000

No discussions yet. Share an observation or technical question above.

Start a conversation

Share the engineering context and delivery objective when you are ready to discuss the work.

Contact Anystack →

See the evidence

Read selected engineering work and its provenance.

Browse selected work →