An AI agent that looks into failed CI runs and proposes the fix

How to have an AI agent catch each failed CI run, read the logs, sort real bugs from flaky tests, fix what it can on a branch and tell you the rest.

· 7 min read

A red build costs more than the minutes it takes to fix. Someone has to notice it, open the run, scroll through three thousand lines of log to find the one that matters, work out whether it's their change, a flaky test or an expired token, and then fix it. On a small team, that someone is usually in the middle of something else, so main stays red for an afternoon and the next three pushes pile on top.

Most of that work is reading and sorting, which an AI agent does well and fast. This guide covers how to wire an agent to your CI, what it should fix on its own, what it should only explain, and the rules that stop it from "fixing" a build by deleting the test.

#What does the agent do when CI fails?

The loop is the same on GitHub Actions, GitLab CI or anything else:

  1. Hear about it. A failed run triggers the agent within seconds.
  2. Read the failure. Only the failed jobs' logs, not the whole run, plus the commit that triggered it.
  3. Sort it. A real bug, a flaky test, the environment, or something it can't tell.
  4. Act on what it's sure of. Fix it on a branch, or rerun a known-flaky job once.
  5. Report. Two or three lines to you: what failed, why, what it did or proposes, with links.

The report is the important part. Even when the agent can't fix anything, "the deploy job failed because the registry token expired yesterday" saves the ten minutes it would take you to find that out.

#How does the agent hear about a failure?

There are three ways, and they suit different setups.

ApproachHowGood forLimits
Inside CIA job that runs on failure and calls Claude Code headless (claude -p), or Anthropic's Claude Code GitHub ActionTeams that want everything in the pipelineRuns on CI minutes, sees only the runner's checkout, a secret for the API key in CI
A webhookGitHub's workflow_run event (action completed) or GitLab's pipeline events post to an address that starts the agentAn agent that also knows your other repos, your notes, your teamNeeds a public address and a token that can create webhooks
PollingA script checks for failed runs every few minutes (gh run list --status failure)Quick experimentsSlow, wasteful, and it misses context

Webhooks are the usual sweet spot for a small team: nothing runs until something fails, and the agent works with your full context rather than a fresh CI runner. Two details matter. Filter early, so the agent only starts for completed runs that failed, on the branches you care about. And deduplicate: GitHub sends each delivery with an X-GitHub-Delivery id, and a retry of the same delivery should not start a second run.

#Which failures can it fix, and which should it only explain?

Sort by how sure the agent can be, and how much damage a wrong fix does.

Kind of failureExampleWhat the agent should do
Lint, formatting, typesAn unused import, a missing type after a renameFix on a branch, open a pull request
A test broken by a real changeA function now returns null where the test expected []Find which side is wrong; fix the code if the change was a mistake, or explain and propose if the test is outdated
A flaky testPasses on rerun, fails one time in ten, timing-dependentRerun the failed jobs once (gh run rerun <id> --failed), and log it so flaky tests become a list you fix
DependenciesA new minor version broke the buildPin the last working version on a branch, and say which release broke it
EnvironmentAn expired token, a missing secret, a full disk, a rate limitExplain only. These need a person with access
InfrastructureThe runner died, the registry is downExplain, and check the provider's status page
UnknownNothing in the logs makes senseSay so, with the lines it read

On GitLab, a known-flaky job can also be given a retry: setting in .gitlab-ci.yml, which is better than an agent rerunning it by hand. GitHub Actions has no built-in retry for a single step, so the rerun usually goes through gh run rerun.

#How do you stop it from turning the build green the wrong way?

The easiest way to pass a failing test is to delete it. An agent under instructions to "fix CI" will sometimes find that shortcut. Write the rules down where it reads them every run:

  • Never delete, skip or comment out a test to make a build pass.
  • Never loosen an assertion (toEqual to toBeDefined, a tolerance raised) without saying so in the report as the main change.
  • Never change the CI configuration to make a job pass, unless the job's configuration is itself the bug, and then say so.
  • Never touch secrets or credentials to make something work.
  • Fix the code, not the symptom. If the test caught a real bug, the fix goes in the code.

Then make the rules checkable: every fix comes as a diff you review, so a removed test is visible before it lands.

#Where should the fix happen?

Never on main. A CI agent should:

  • Work on its own branch, ideally in a separate git worktree, so it never disturbs a checkout you're using. See Git worktrees with Claude Code.
  • Run the failing job's command locally before claiming a fix. "This should fix it" without a green local run is a guess.
  • Open a pull request, not push to the main branch. Opening a pull request is public in your repository, so it's reasonable to want a yes first, at least for the first weeks.
  • Never deploy. Even if the failed job was the deploy step, the redeploy is a person's call.

#What should it send you?

A good report fits on a phone screen:

CI failed on main: "test" job, commit a1b2c3d (Rename user fields).
Why: tests/users.test.ts still reads user.fullName, renamed to user.name.
Done: fixed the test's field name on branch fix/ci-user-name; local run green.
Next: pull request ready to open, your OK?
Run: https://github.com/larkspur/app/actions/runs/1234567890

When it can't fix anything, the same shape: what failed, the most likely cause, the lines that show it, and who or what can fix it. Over a few weeks, these reports also become a record of what breaks your build most often.

#How do you keep it cheap and quiet?

A CI agent that wakes up for every push is expensive and noisy. A few settings keep it sensible:

  • Failed runs only, on the branches that matter (usually main and release branches). A failing run on a feature branch is that branch's author's business.
  • A cheaper model to sort, a stronger one to fix. Reading a log and naming the failure is light work. Debugging a real test failure deserves a stronger model, and it runs far less often.
  • A daily cap. If something is badly broken, ten failures in a row should produce one useful report, not ten.
  • Remember the known causes. "The e2e job fails when the staging database is asleep" belongs in the agent's notes, so it says so in one line next time.

#In Relais

In Relais you tell an agent "when CI fails on main in larkspur/app, find out why and tell me", and it sets this up with watch_repo: Relais makes the webhook on the GitHub or GitLab repository with the token you gave the agent in the Vault, drops the noise before any run starts (runs still going, passing runs when only failures count, other branches, repeated deliveries), and starts the routine with a few lines, the workflow, the branch, the commit and the link. The agent reads the logs without asking; when the fix needs real code work, it opens a Claude Code session of its own in the project, in a worktree when it changes code, and that session reports back to it when its first turn ends. Opening a pull request, a force-push or a deploy waits for your yes, and you can add "ask me before any git push" in plain words. Each routine runs on the model it was given, and deleting the routine removes its webhook on the repository. See routines, approvals and Claude Code in headless mode.

Your AI team,
on your Claude plan.

$14.99 a month, everything included. On your Mac or on Linux, on your Claude Pro or Max plan. Cancel any time.