How to Fix Flaky Tests With an AI Agent, Without Skipping Them
Quick answer: An AI agent can fix a flaky test when it treats flakiness as a bug with a cause: timing (a sleep or an unawaited promise), ordering (one test depends on another having run), shared state (a database row, a global, a file), or the outside world (the clock, the network, randomness). It reproduces the failure, finds which of these it is, fixes the cause in the test or the code, and proves the test now passes reliably. It must never get to green by retrying, skipping or loosening the test.
Let an agent earn its place on your team
Define a Factorik agent once and it wakes on an issue, a failed build or a schedule, works in an isolated sandbox and ships a pull request. It starts with a person merging its work and earns more autonomy on its record. Try it free for 7 days, no payment details needed.
What makes a test flaky?
A flaky test is one that passes and fails on the same code. Nothing changed between the red run and the green one except something the test did not control. That something is almost always one of four things, and naming which one is most of the work of fixing it.
Timing: the test waits a fixed time for something asynchronous, or does not wait at all, and the machine was slower or faster than when the test was written. Ordering: the test passes only when another test ran before it, or fails only when one did, because they share setup. Shared state: two tests, or two parallel workers, write the same database row, file, cache key or global variable. The outside world: the test reads the current time, a random value, the network, the time zone or the locale of the machine it runs on.
Flakiness can also be in the code rather than the test. A race between two requests, a cache that expires at the wrong moment, or an unhandled promise that sometimes rejects after the test ended are real bugs that a flaky test happens to expose. Deleting that test removes the only thing that noticed.
| Cause | Typical sign | Fix that removes it |
|---|---|---|
| Timing | Fails more on a busy CI runner; uses sleep or a fixed timeout | Wait for the condition itself, or use fake timers |
| Ordering | Fails alone or only in a certain order | Give each test its own setup and teardown |
| Shared state | Fails when tests run in parallel | Unique data per test; reset globals and caches |
| Clock and time zone | Fails near midnight, month end or on another machine | Inject or freeze the clock; set the time zone explicitly |
| Randomness | Fails rarely with no pattern | Seed the generator and log the seed |
| Network | Fails when a third party is slow | Stub the call at the boundary |
| A real race in the code | Fails under load in the test and in production | Fix the code, keep the test |
Why are retries, skips and quarantine not fixes?
Because each one makes the build green without making the code or the test any better. A retry hides the failure the first time and teaches everyone to ignore red. A skipped test is a test that no longer exists, with a comment promising someone will come back. A loosened assertion, such as checking that a list is not empty instead of checking what is in it, passes now because it checks less.
Quarantine, moving known flaky tests to a job that does not block merges, is a reasonable holding pattern for a team that has many of them, as long as someone is working through the list. It becomes a problem when the list only grows. Every test in it is coverage the team believes it has and does not.
For an agent the rule has to be absolute, because an agent whose goal is "make CI green" will find that skipping a test is the fastest way there. Factorik's CI Fixer preset has it written into its instructions: for a flaky test, fix what makes it flaky (timing, ordering, shared state), do not just rerun it, and do not skip it; never delete a test, skip it or loosen an assertion to get a pass. If it believes a test should go, it says so and leaves the test in place for a person to decide.
How does an AI agent tell a flaky test from a real failure?
The same way a careful engineer does: by looking at what changed. If the commits since the last passing build touched the code the test covers, the failure is probably real. If they did not, and the test fails on a rerun only sometimes, it is probably flaky. If neither, the environment may have changed: a dependency published a new version, a service the tests rely on is down, or a credential expired.
The agent needs the evidence to make that call. In Factorik, when CI fails on the default branch, the server reads the end of the failed job's log and hands it to the CI Fixer in its task, so the agent starts from the error rather than the job's name. In its isolated sandbox it then runs that same check itself and reads the actual error, and looks at the commits since the last green build. Every run is also told how CI stands on the default branch, so a failure that was already there is not taken for something new.
Reproducing a flaky failure on purpose is where an agent helps most. Running one test fifty times, running it alone and in its file, running the suite in a different order or with more parallel workers, freezing the clock at 23:59:59 on the last day of a month: each is a few commands in a sandbox and tedious by hand. A failure that reproduces under one of these conditions has named its own cause. How CI failures are fixed automatically covers the general case; flaky tests are the case where reproduction matters most.
What does fixing a flaky test look like, step by step?
An illustration with made-up names. The default branch of acme/billing goes red. The failing test is "sends a reminder three days before renewal" in test/reminders.test.ts. The last commit changed a README.
The CI Fixer wakes on the failure with the end of the log in its task: expected one reminder, received zero. It runs the test in its sandbox; it passes. It runs it in a loop and it fails a few times. It looks at the commits since the last green build: none touch reminders. So it is flaky, not broken.
It looks for the cause. The test creates a subscription renewing "in three days" from the current time, and the code compares dates in UTC. It freezes the clock at 22:30 in a time zone behind UTC and the test fails every time: "three days from now" crosses a date boundary in one place and not the other. The cause is the test's use of the real clock, and possibly a real bug in how the code counts days.
It fixes the test to use a fixed clock and an explicit time zone, and adds a case at the date boundary. That new case fails against the code, so the code has the bug too. It fixes the day count in src/reminders/schedule.ts to use the subscription's time zone, runs the full set of checks rather than only the one that failed, and finishes. The server publishes one commit on a new branch and opens a pull request that says what was flaky, why, and what changed in the code.
The repository's merge policy decides the rest. A small change to code and a test, at an agent level that allows it, can merge by itself once the checks pass; otherwise it waits in the Inbox for a person to press "Approve and merge". Either way, nobody reran the job until it went green.
Let an agent earn its place on your team
Define a Factorik agent once and it wakes on an issue, a failed build or a schedule, works in an isolated sandbox and ships a pull request. It starts with a person merging its work and earns more autonomy on its record. Try it free for 7 days, no payment details needed.
What can an AI agent not fix about flaky tests?
Some causes are outside the repository. A service the tests depend on was down, a runner ran out of disk, a credential used in CI expired. The right result there is to change nothing and say who needs to act. The CI Fixer is told to do exactly that, and a run that ends without a change is a valid outcome, not a failure.
Some causes live in the pipeline definition: a job that shares a database between parallel shards, a missing service container, a cache keyed wrongly. In Factorik the CI Fixer cannot change workflow files. It finishes as blocked and gives the exact change needed, so a person can apply it in a minute. That is deliberate: a workflow on a pull request's branch can run with the repository's secrets before anyone merges it, so CI configuration stays a person's change.
And some flakiness is a symptom of a design choice, such as tests that share one seeded database for speed. An agent can fix one test at a time, but deciding to change how the whole suite isolates data is a decision for the team. A good agent says so in its summary rather than making that change on its own.
How do pull requests from people get help with flaky failures?
When CI fails on a pull request, the person who opened it usually wants to know one thing: is this me, or is it flaky? With the CI Fixer's trigger set to also run when CI fails on a pull request, it reads that failure the same way and comments on the pull request with the cause and the fix. It does not push to someone else's branch.
When the pull request is the agent's own, the rule is different: CI failing at the commit an agent pushed is that agent's to fix, on the same branch, a limited number of times before a person takes over. An agent cannot dismiss its own red build as flaky and move on; it has to show why, or fix it.
Over time, flaky-test fixes also feed the agent's record. A fix that holds up counts toward the record that earns more autonomy. A fix whose merge commit later fails CI is a finding, and it moves the agent down a level at once.
How do I start fixing flaky tests with an agent?
Whatever you use, start by writing the rule down: flaky tests are fixed at the cause, never retried, skipped or loosened to pass. Then give whoever fixes them, person or agent, the log, a place to reproduce the failure, and the history of what changed.
In Factorik that is the CI Fixer: create it from the preset on a repository with CI, keep "Merge automatically" off in the repository's merge policy at first, and add a standing rule for anything specific to your suite, such as how to run one test in isolation. You can start a free 7-day trial of the Team plan, with no payment details needed, and let it take the next red build.
Frequently asked questions
Can AI fix flaky tests?
Yes, when it finds the cause instead of retrying. An agent can reproduce a flaky failure by running the test many times, alone, in another order or at a frozen time, then fix the timing, ordering, shared state or clock problem behind it and prove the test now passes reliably.
Should flaky tests be retried automatically?
Retries hide failures and teach a team to ignore red builds. They can buy time while the cause is fixed, but a flaky test is a bug in the test or the code, and it should be fixed at the cause.
Is it acceptable to skip a flaky test?
Not as a fix. A skipped test is coverage the team believes it has and does not. If a test truly should go, a person decides that; an agent should say so and leave it in place.
Can the CI Fixer change GitHub Actions workflow files?
No. When the cause is in the pipeline definition, it finishes as blocked and gives the exact change needed, so a person applies it.
Let an agent earn its place on your team
Define a Factorik agent once and it wakes on an issue, a failed build or a schedule, works in an isolated sandbox and ships a pull request. It starts with a person merging its work and earns more autonomy on its record. Try it free for 7 days, no payment details needed.