Factorik Factorik Start free trial
← See all articles

How to Fix Failing CI Automatically, Safely

Quick answer: To fix failing CI automatically without trusting a bot blindly, let an agent do the triage a person would (read the end of the failed job's log, find the cause, tell a flaky test from a real break) and have it deliver the fix as a pull request that goes through the same checks and merge policy as anyone else's. On other people's pull requests it should only explain the cause and the fix, and on its own it should get a limited number of attempts before a person takes over.

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.

Start your free trial

Why does a broken main branch cost so much?

A red default branch is a shared problem that nobody owns. Every pull request opened after the break inherits the failure. Authors rerun jobs hoping the red goes away, reviewers hesitate to approve anything with failing checks, and the merge queue stalls. Someone eventually looks, but usually the person who finally does is whoever was most blocked, not whoever knows the failing code best.

The cost grows with how much is being merged. The Harvard and Jellyfish study of 720,542 engineers at 655 firms, reported by BERI in 2026, found that with AI coding agents the number of pull requests opened rose 35.7% and commits rose 36.0%. More changes landing on main means more chances to break it and more people blocked when it is broken. A study of concurrently running coding agents, a 2026 preprint, found that identical agent instances hit a 19.8% textual merge conflict rate, which hints at how often parallel work collides once several changes are in flight at the same time.

Quality pressure adds to it. Faros AI's 2026 report, a vendor report covering about 22,000 developers, found that from low to high AI adoption bugs per developer rose 54%. Not every bug breaks CI, but the ones that do tend to arrive on the default branch first, between the merge and the next person who pulls.

None of this is new. What is new is the volume, and the fact that the triage work is repetitive enough that an agent can do most of it, as long as what it produces is held to the same rules as everything else.

+35.7%
Pull requests opened
Harvard/Jellyfish study of 720,542 engineers at 655 firms, 2026, reported by BERI
19.8%
Textual merge conflict rate
Identical concurrently running agent instances, 2026 preprint
+54%
Bugs per developer
Faros AI 2026 report, low vs high AI adoption (vendor report)
More changes, more collisions: third-party figures on AI-era volume

How do engineers triage a failing CI build?

Before automating anything, it helps to write down what a good engineer actually does when main goes red. The loop is short, and it is the same loop an agent should follow.

Read the log, from the end. The useful part of a CI log is almost always near the bottom of the failed job: the assertion that failed, the compiler error, the missing environment variable, the timeout. Hundreds of lines of setup output above it rarely matter.

Find the change that caused it. Look at what merged since the last green run. Often the culprit is obvious from the file named in the error. Sometimes it is an interaction between two changes that each passed on their own branch, which is exactly what concurrent work produces.

Decide whether it is flaky or real. A test that fails once and passes on rerun, with no related change, is likely flaky: a race, a time-dependent assertion, an external service. A test that fails the same way every time, right after a related change, is real. Rerunning a real failure wastes time; fixing a flaky test as if it were a real bug produces a wrong fix.

Fix forward or revert. If the cause is clear and the fix is small, fix it. If the cause is unclear, or the fix is large, reverting the offending change gets everyone unblocked while the author works it out. Teams that keep main green tend to revert early and without blame.

Prove it. A fix is not done until CI passes on it. A fix that has not been run through the same checks is a guess.

SignalPoints to flakyPoints to real
RerunPasses without changesFails the same way
Recent changesNothing related mergedA change touched the failing code
ErrorTimeout, network, ordering, clockAssertion, type error, missing symbol
HistorySame test failed before, intermittentlyFirst failure of this test
Right responseMake the test deterministicFix the cause or revert the change
Telling a flaky failure from a real one

Can an AI agent fix failing CI automatically?

Mostly, yes, for the common cases: a type error after a refactor, a test asserting an old value, a lint rule tripped by a new file, an import broken by a rename, a snapshot that needs updating for an intended change. These are the failures where the log says almost exactly what is wrong and the fix is a few lines. They are also the failures people find most tedious, which is why main can stay red for hours when nobody owns them.

Where agents struggle is where people struggle: failures that depend on infrastructure the agent cannot see, intermittent failures with no clear cause, and breaks that come from a design disagreement between two changes. In those cases the honest outcome is not a fix but a clear explanation of what is wrong and who needs to decide.

The real question is not whether an agent can write the fix. It is what happens to the fix once it exists. A bot that pushes straight to main to make it green again is a bot you are trusting blindly: if it misreads the failure, it can make main green by deleting the test, loosening the assertion, or skipping the job. The fix has to be reviewable, it has to pass the same checks, and it has to merge under the same rules as any other change.

Commentary on AI-written code points at the same trap. Code generated by AI can run and pass its tests with incomplete coverage, and AI-written tests often repeat the same wrong assumptions as the code they check. A CI fix is especially exposed to this, because making the check pass is the whole goal. The safeguard is structural: the fix is a pull request, its diff is visible, and a test that was weakened looks like a test that was weakened.

How does Factorik's CI Fixer work?

In Factorik, an agent has one job on one GitHub repository. The CI Fixer preset is an agent woken by CI failing on the default branch. When it wakes, the failure comes with the end of the failed job's log, so it starts where a person would: at the error. It works in an isolated sandbox on a checkout of the repository, finds the cause, makes the fix, and finishes with one outcome: a pull request, or "blocked" with the reason when it cannot fix the problem itself.

The fix arrives as a pull request, never as a push to main. The sandbox never writes to GitHub: it gets a short-lived read-only token for one repository, and Factorik's server publishes the change as one commit on a branch it names, never force-pushed and never to the default branch directly. What happens next depends on the agent's level. At the first level, a person merges the fix. At the second, Factorik merges it when it is a low-risk change and the merge policy allows. At the third, it merges whatever the merge policy allows. The merge policy requires passing checks either way, so the fix has to make CI green before anything merges.

The trigger that wakes the agent can optionally include the team's own pull requests too. On someone else's pull request, the CI Fixer does not push anything. It comments on the pull request with what broke and how to fix it, and the author decides. Their branch stays theirs.

When CI fails on a pull request an agent opened, that agent fixes it itself, on the same branch, rather than leaving a red pull request for a person to chase. It gets a limited number of attempts. If CI is still failing after the last one, it stops and a person takes over.

Every run is also told how CI stands on the default branch. That matters more than it sounds. If main is already red when an agent opens its pull request, the agent's checks will fail for a reason that has nothing to do with its change. Knowing the default branch's state, it does not take the existing failure for its own and start "fixing" code it never touched.

What does an automatic CI fix look like, step by step?

Here is an illustration, not a customer story. The repository is acme/storefront, with a CI Fixer at the first level, so a person merges its pull requests.

  1. Pull request #918, "Rename getCartTotal to computeCartTotal", merges after review. Its own checks passed. But pull request #921, merged ten minutes later, added a new call to getCartTotal in src/checkout/summary.ts. Each change was fine on its own branch; together they break the build.
  1. CI fails on the default branch at the merge of #921. The CI Fixer wakes with the end of the failed job's log: a type error, "getCartTotal is not exported from src/cart/totals.ts", raised from src/checkout/summary.ts line 42.
  1. In its sandbox, the agent reads the log, looks at what merged since the last green run, and finds the rename in #918. The failure is real, not flaky: the symbol no longer exists. The fix is one line, calling computeCartTotal in summary.ts. It runs the type check and the checkout tests to confirm.
  1. Factorik's server publishes the change as one commit on a new branch and opens pull request #924, "Fix build: use computeCartTotal in checkout summary", explaining the cause: the rename in #918 and the new call in #921 crossed.
  1. CI runs on #924 and passes. If the repository has a Code Reviewer, it reviews #924 too. Because the CI Fixer is at the first level, the pull request waits for a person, who reads a one-line diff and an explanation, and merges it. Main is green again.

Meanwhile, a teammate's pull request #923 opened before the fix also shows red. If the trigger covers the team's pull requests, the CI Fixer comments on #923 that the failure comes from the default branch, not from its change, and that rebasing after #924 merges will clear it. Nothing is pushed to the teammate's branch.

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.

Start your free trial

Automatic CI fixes vs auto-revert vs rerun-until-green

There are three common ways teams automate around red builds, and they are not equally safe.

Rerun until green treats every failure as flaky. It works for genuinely flaky tests and hides real breaks behind a lucky pass. Over time it teaches the team that red means "try again", which is the opposite of what CI is for.

Auto-revert backs out the change that broke the build. It is fast and predictable, and it is a good fallback. Its weakness is the case above: when two changes collide, which one is "the" culprit is a judgment call, and reverting either may be wrong.

An agent that proposes a fix as a pull request is slower than either, because the fix has to be written, checked and merged. In exchange, it addresses the cause, explains it in the pull request, and goes through the same gate as every other change. The pull request also leaves a record: who broke what, how it was fixed, and who approved it.

These can coexist. A team might keep a manual revert for anything urgent, let a CI Fixer propose fixes for the routine breaks, and fix flaky tests as their own piece of work.

Pros
  • +A red main has an owner as soon as CI fails, without paging anyone
  • +The fix addresses the cause and explains it in the pull request
  • +It goes through the same checks, review and merge policy as any change
  • +Teammates' branches are never pushed to; they get an explanation
Cons
  • −Slower than a revert when the build must be green right now
  • −It can misread a failure that depends on infrastructure it cannot see
  • −A fix that weakens a test still needs a reviewer to notice
  • −Runs spend the organization's AI credits
Letting an agent propose CI fixes as pull requests

What does an automatic CI fixer not do?

Be clear about the boundaries before you turn one on. Factorik's CI Fixer does not push to the default branch, and it never force-pushes. It does not push to a teammate's pull request; it comments there. It does not keep trying forever on an agent's own pull request; after a limited number of attempts a person takes over. And it does not merge anything the merge policy and its level do not allow.

It also cannot see what CI cannot show it. A failure caused by an expired credential in the CI environment, a broken runner, or a third-party outage is not something a code change should fix. In those cases the right outcome is "blocked" with the reason, so the person who can act on it knows what to do.

And a fix that merged can still be wrong. If CI or a deploy fails at the commit a fix merged as, if someone reverts it, or if a person reports a problem with it, Factorik records a finding, tells the team on Slack, webhook, email and a pull request comment, and moves the agent down a level at once. An agent earns the right to merge its own fixes on its record, and loses it the same way.

How do I start fixing failing CI automatically?

Pick one repository where main breaks often enough to annoy people and CI is reliable enough to trust. Add a CI Fixer, leave it at the first level, and watch its pull requests for a couple of weeks. Check that its explanations match what a person would have found, and that it fixes causes rather than loosening tests. Write team rules for anything it gets wrong, such as "never skip or delete a test to make CI pass"; every run reads them.

When its record is good, it can be promoted, by a person pressing Promote or automatically if your organization chose that, and its low-risk fixes can start merging on their own under your merge policy.

You can try this on the free 7-day trial of the Team plan, with no payment details needed. Start your free trial, connect a GitHub repository, and add a CI Fixer before the next time main goes red.

Frequently asked questions

Can an AI agent fix a failing CI build automatically?

Yes, for common failures such as type errors, outdated assertions, broken imports and lint errors, where the log points at the cause. The safe way is to have the agent open the fix as a pull request that passes the same checks and merge policy as any other change, rather than pushing to the default branch.

Does Factorik's CI Fixer push to my pull request?

No. On a pull request someone else opened, it only comments with what broke and how to fix it. Only when CI fails on a pull request an agent opened does that agent fix it on the same branch, and only a limited number of times before a person takes over.

How does the agent know what failed?

The CI failure comes with the end of the failed job's log, which is where the error usually is. Every run is also told how CI stands on the default branch, so an agent does not mistake a failure that was already there for one its own change caused.

Will the CI fix merge without a person?

Only if the agent's level and the repository's merge policy allow it. At the first level a person merges every fix. Auto-merge is off by default, and the merge policy requires passing checks and protects sensitive paths.

What happens if an automatic CI fix turns out to be wrong?

If CI or a deploy fails at the merged commit, the change is reverted, or a person reports a problem, Factorik records a finding, tells the team and moves the agent down a level at once.

What about failures that come from flaky tests?

A flaky test is fixed at its cause (timing, ordering, shared state, the clock), never by retrying, skipping or loosening it. How to fix flaky tests with an AI agent walks through finding and removing each cause.

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.

Start your free trial

How helpful was this article?

Articles by Factorik →