Fix a Red Main Build with an AI Agent
Main goes red at 6 pm. The person who merged has gone home, nobody opens the failed run until morning, and every pull request waiting on a green main is blocked until someone digs through the log, finds the commit that broke it and writes the fix.
This tutorial gives that first hour to an agent. The moment a GitHub Actions run on main fails, GitHub calls AgentRQ, and an agent working in your repository's checkout gets a task. It reads the failing log, tells you what broke and which commit did it, and decides whether the failure is real or flaky. A real failure gets a fix on a new branch, the failing job's command run again until it passes, and a pull request, but only after you approve the push and the pull request. A flaky one gets a re-run and a note saying why. When main turns green again, the agent closes the loop itself.
There is no polling and no glue code: one hook, one event, one trigger. The screenshots come from the AgentRQ app on November 30, 2026, fed with signed workflow_run deliveries in GitHub's own format, the same requests the hook checks in production.
What You Build
| Piece | Setting | What it does |
|---|---|---|
| ci-fixer workspace | Working directory: your repository's checkout. YOLO mode off | The agent that reads logs, fixes code and asks before it pushes |
| ci_failed event | A trigger that gives ci-fixer "Fix main: {{EVENT_SUBJECT}}" | One task for every red run on main, named after the run |
| github hook registration | Branches: main. Route: ci.failed to ci_failed | Turns GitHub's webhook into your event, and drops runs on other branches |
| GitHub webhook | Workflow runs only, application/json, the shared secret | GitHub calls AgentRQ every time a workflow run finishes |
| ci_succeeded event (optional) | A trigger that gives ci-fixer "Close the loop: {{EVENT_SUBJECT}}" | Closes the open fix task when main is green again |
Three ideas make it work:
- → A hook brings GitHub in. A hook checks GitHub's signature on every request and turns a failed workflow run into one of your events. The built-in github hook does this with no code.
- → An event gives the work to a workspace. An event is a name for something that happened, like ci_failed. Its trigger creates a task in a workspace every time it fires, with what GitHub sent pasted in.
- → You stay in charge of what leaves your machine. With YOLO mode off, every command the agent runs waits for your answer, so a push or a pull request never happens without you.
Before You Start
- → An AgentRQ account, and a computer where the agent will run, with a checkout of the repository on it. See machines and the connect agents guidelines.
- → An agent that connects to AgentRQ: Claude Code over MCP, or Codex, Gemini CLI and others through the ACP Gateway.
- → The GitHub CLI on that computer, signed in with gh auth login, with access to the repository's Actions logs, so the agent can read a run's log, re-run a job, push a branch and open a pull request.
- → Whatever the CI job needs to run locally: here, Go for go test ./... in acme/shop.
- → Admin access to the GitHub repository, to add a webhook.
Step 1: Make the ci-fixer Workspace
Create a workspace named ci-fixer. Its mission tells the agent what it is for, every time it starts:
In its settings, set the Working Directory to the repository's checkout, ~/code/shop here, and start its agent. Leave YOLO mode off.
With YOLO off the agent asks before every command, which gets noisy for commands that only read. For Claude Code, the repository's .claude/settings.json can allow the safe ones and keep asking for the two that matter:
Step 2: Create the ci_failed Event and Its Trigger
Open Events, click + New Event, and name it ci_failed. In its payload guidelines, say what it carries: "Sent by the GitHub hook when a GitHub Actions run on main fails or times out: the workflow, the branch, the commit and the link to the run."
On the event's page, click + Add Trigger. 1. ci-fixer gets the task. 2. Its title is "Fix main: {{EVENT_SUBJECT}}". The github hook fills {{EVENT_SUBJECT}} with a one-line name for the run, such as "CI failed on main: CI", so every red run gets its own, readable task. 3. Its instructions start with {{EVENT_PAYLOAD}} and {{EVENT_FAQ}}, which bring in what GitHub sent.
The instructions are the agent's playbook for a red build. This run used:
Below the instructions, 1. the steps that change code stay on a branch and end in a pull request. 2. YOLO mode is off, so the push and the pull request wait for you. 3. No event to chain: nothing runs after this task, so When the task is completed, fire stays on No event. Save the trigger.
Step 3: Register the github Hook
Open Events, then Hooks, and click New registration. Name it, for example "acme/shop CI on main".
1. The built-in github hook. 2. A webhook secret: click Generate, then Copy, because GitHub needs the same value. 3. Only main: type main under Branches. 4. Only this repository: acme/shop under Repositories, which matters if you later point an organization webhook at the same URL.
Under Routes, 1. route ci.failed to ci_failed. 2. Route ci.succeeded to ci_succeeded only if you want Step 9, and make that event first. Leave every other GitHub event Not routed, then click Create Registration.
1. Copy the payload URL, which ends in ?agentrq_hook_token=. It is shown only this once. If you lose it, Rotate Token makes a new one, and the old URL stops working. 2. Both routes are listed on the registration.
Step 4: Add the Webhook in GitHub
In GitHub, open the repository's Settings, then Webhooks, then Add webhook:
- Payload URL: the URL you copied, token included.
- Content type: application/json. GitHub's default, application/x-www-form-urlencoded, is rejected.
- Secret: the webhook secret from the registration. It is not the hook token.
- Which events: Let me select individual events, untick Pushes, and tick Workflow runs. That is the GitHub event behind ci.failed and ci.succeeded.
- Add webhook.
GitHub sends a ping right away. Nothing is routed for a ping, so it fires nothing, and AgentRQ does not keep a row for it. Your first delivery appears with the first workflow run that finishes on main.
Step 5: Main Goes Red
In this run, Lee merged "cart: apply coupons per line item (#241)", and the CI workflow failed on main. GitHub sent a workflow_run delivery. The github hook checked the signature, saw a run on main that finished with failure, and fired ci.failed, which the registration routes to ci_failed. The trigger did the rest.
1. One task per red run on main, named after the run. 2. What GitHub sent: the workflow, the branch, the short commit, the run link and the commit's first line, then a short FAQ with the repository, branch, full commit and where the logs are.
The payload looks like this, with the same lines in every ci_failed task:
Step 6: The Agent Reads the Log and Fixes It
The agent picks the task up, runs gh run view with --log-failed on the run, and replies before it touches any code. 1. The failing lines, from the log: two coupon tests are a cent off. 2. The commit that broke it, and proof that it is real: the same failure locally, not only on the runner. 3. Fixed on a branch: what changed, a new test so it cannot come back, and the job's own command passing.
The flaky-or-real decision is the important part. A failure that reproduces locally on the same commit is real. A timeout, a network error or a test that passes when run again is flaky, and Step 8 shows what happens then.
Step 7: You Approve the Push and the Pull Request
The agent asked to run git push first, in an Authorization Required card, and waited. After that, it asked to open the pull request. 1. It asks before gh pr create, and shows the exact command. 2. You say yes with Allow Once. The card also reaches your phone through notifications, so you can approve from the couch.
Use Allow Once, not Always Allow, so the next push asks again. A push to a shared repository is the one step here that other people see.
1. Two asks, two waits for you: the task timeline shows each time the task needed input. 2. The pull request, with what it fixes and who should review it. 3. Open until main is green: the task stays open, because the pull request is not the finish line.
Step 8: A Flaky Run Gets a Re-run, Not a Branch
Later the E2E workflow timed out on main. A timeout counts as a failure, so the hook fired ci.failed with the subject "CI timed out on main: E2E", and ci-fixer got a second task.
1. A timeout is a failure too. 2. Why it is flaky: the same commit passed this spec before, the spec passes five times out of five locally, and the wait was on an outside sandbox. 3. Re-run, no fix branch: it ran gh run rerun with --failed, linked the new attempt and completed the task.
Step 9: Main Is Green Again (Optional)
If you routed ci.succeeded to a ci_succeeded event, give that event a trigger for ci-fixer, titled "Close the loop: {{EVENT_SUBJECT}}", with these instructions:
After pull request #242 was merged, CI passed on main, and the agent replied in the fix task and completed it. 1. Main is green again, with the commit and the run that proved it.
Every green run on main now makes one short task. When there is no fix task to close, as after the E2E re-run passed, the agent completes it in one line. If main runs dozens of times a day, skip this route.
Step 10: Check Every Delivery
The registration lists what GitHub sent and what became of it. 1. Red on main: one task. 2. GitHub's Redeliver: no second task. The same delivery sent again is marked Duplicate and fires nothing. 3. Sent as a form, not JSON: a webhook left on GitHub's default content type is Rejected, with the reason. 4. Green runs on main, from Step 9.
On the Kanban board, 1. two failures and two green runs are all done.
Traps to Avoid
- → Workflow runs is not ticked. GitHub sends no workflow_run event, so nothing happens when CI fails. Pushes, GitHub's default, fire nothing here either, because only ci.failed is routed.
- → The content type is still form-encoded. Every request is Rejected with "the body is not JSON". Change it to application/json in GitHub's webhook settings. The next finished run arrives as JSON, or re-run the failed job to send one now.
- → A timeout is a failure, a cancel is not. A run that ends with failure or timed_out fires ci.failed, and the subject says which. Cancelled, skipped and other conclusions fire nothing.
- → A re-run is a new delivery. Re-running a failed job sends a new workflow_run with a new delivery id, so it is not a duplicate: if it fails again, ci-fixer gets another task for the same run, and if it passes, ci.succeeded fires. Only GitHub's Redeliver of the same request is caught as a Duplicate.
- → Every workflow counts. CI, E2E, lint and deploy runs all fire ci.failed, each with the workflow's name in the subject. The hook filters by branch and repository, not by workflow, so tell the agent in the instructions which workflows to complete at once.
- → Requests that fire nothing leave no row. A ping, a run on another branch, a cancelled run or a GitHub event you did not route is accepted and dropped without a row in Deliveries. If you expected a task and see nothing, open the webhook's Recent Deliveries in GitHub to see what it sent.
- → Branches are exact globs. main matches main only, and does not cross a slash: release/ matches release/1.2, not release/1.2/hotfix. Pull request runs carry the pull request's branch, so Branches = main keeps them out.
Make It Yours
- → One task per run, not per attempt. Add to the instructions: "Before you start, look for an open Fix main task for the same run link; if there is one, reply there and complete this task."
- → Remember the flaky tests. Ask the agent to save every test it calls flaky to workspace memory, with the date. The next time the same spec times out, it knows the history, and a spec that keeps flaking becomes a real fix.
- → Watch release branches too. Put main, release/* under Branches, and say in the instructions that a fix for a release branch targets that branch.
- → Ask for a reviewer. Tell the agent to request a review from the author of the commit that broke main, with gh pr create --reviewer.
- → Start from a GitHub issue instead. The same hook fires issue.opened. See turn GitHub issues and CI failures into tasks.
FAQ
Can the agent push to main? Not while it has to ask. The instructions say never, and with YOLO mode off every git push waits for Allow Once. If you click Always Allow or turn YOLO on, it stops asking, so don't, for this workspace.
Will a failing pull request start a task? No. A pull request's runs are on its own branch, and the registration's Branches is main, so the hook drops them before any event fires.
What does the agent need on the machine? The repository's checkout, the GitHub CLI signed in, and whatever the CI job runs, such as Go or Node, so it can reproduce the failure and run the job's command before it pushes.
What if the fix is wrong? It is a pull request, not a push to main: your usual review and CI run on it. Reply in the task with what is wrong, and the agent keeps working on the same branch.
Does it work with CI other than GitHub Actions? The github hook reads GitHub Actions workflow runs. For another CI system, send its webhook to the generic hook and route request.received to ci_failed.
Which agents can run ci-fixer? Any that connects to AgentRQ: Claude Code, or Codex, Gemini CLI and others through the ACP Gateway.