Use agents to hunt flaky tests systematically
Flaky tests teach a team to ignore CI. They also teach agents the wrong move: add sleep and rerun until green.
Flaky tests teach a team to ignore CI. They also teach agents the wrong move: add sleep and rerun until green. If you want agents to help you ship, you need CI failures that mean something.
This guide is about restoring signal with a repeatable loop: quarantine visibly, reproduce under control, fix determinism, then re-enable with a probation window.
Decide: flaky test or real regression
Before you let an agent change code, collect three facts from CI:
- Failure signature: the exact assertion, exception, or timeout.
- Frequency: how often it fails over the last week.
- Surface area: which packages and which environments (OS, Node, browser, DB).
If the failure is consistent and reproduces locally, treat it as a normal bug. If it is intermittent, treat it as a flake and start with quarantine and reproduction.
Quarantine, but keep it honest

Failure modes to catch early
Quarantine is not “skip and forget”. It is “protect main while we fix the test”.
Good quarantine rules:
- Visible: quarantined tests run in a separate job or are marked clearly in logs.
- Owned: one person or rotation owns the quarantine.
- Expires: every quarantine has a date and a cleanup ticket.
- Limited: quarantine the smallest scope you can (one test, one file, one environment).
If a test is quarantined for months, it is not a quarantine. It is a dead test. Either fix it or delete it.
The agent-friendly flake hunt loop

Hunt Loop
Ask the agent to do the hunt in this order:
- Summarize the failure signature from CI logs and point to the exact test name and file.
- List the likely flake classes: time, randomness, order dependence, shared state, network, resource limits.
- Propose a minimal reproduction command and a repeat plan (for example 50 runs, not “try a few”).
- Design a bisect for the suspicious variable:
- Order dependence: shuffle test order, run in isolation, run with a fresh DB each time.
- Time: fake clock, freeze time, control timezone, remove real sleeps.
- Network: stub calls, use a local fake server, remove remote dependencies.
- Randomness: seed RNG, log the seed on failure, remove nondeterministic IDs.
- Apply the smallest stabilizing fix and prove it: repeated runs locally and at least one CI run without reruns.
Stop rules that prevent noise fixes:
- Do not add
sleepunless you can prove the race and explain why a deterministic sync primitive is not possible. - Do not increase timeouts as a first move. Timeouts hide the bug and make CI slower.
- Do not delete a test until you can name what safety you are losing and where it is still covered.
Stabilization strategies that actually work
Most flakes come from the same sources. These are the fixes that usually survive.
- Isolation: no shared global state, fresh DB per test, no shared cache between tests.
- Deterministic time: fake clock, freeze time, explicit timezone, no “now” in assertions.
- Deterministic data: fixed fixtures, stable IDs, seed all RNG.
- Deterministic async: wait on explicit events, not wall-clock sleeps.
- Deterministic environment: pin versions, avoid depending on CPU speed, remove parallelism where needed.
If the test asserts something nobody cares about, delete it and replace it with a cheaper assertion that protects the real behavior.
Three traps to ban in reviews
- Sleep patches. Fix: replace with a deterministic sync point, or fake time.
- Silent quarantines. Fix: quarantine is visible, owned, and expires.
- Rerun culture. Fix: cap reruns, then block merge until the failure has an explanation.
A short weekly loop
Once a week, spend 30 minutes so you do not bleed hours later:
- Review the top 5 flakes and assign owners.
- Expire quarantines that reached their date.
- Delete tests that have been quarantined for months and provide no coverage value.
- Add one guardrail to CI or the test harness based on what flakes taught you this week.
Bottom line

When this fits vs when it does not
Agents can accelerate flake fixes when the loop is controlled and the success criteria are real. Quarantine honestly, reproduce under control, stabilize determinism, then re-enable with probation.
If your agent sessions tend to “retry forever”, pair this with stop rules for agents so the hunt does not become a spending loop.
Share