Human-in-the-Loop Task Manager for AI Agents
Tested November 30, 2026 | All Tutorials

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.

The flow: once, you make a ci-fixer workspace in your repository's checkout, a ci_failed event with a trigger, and a github hook for main only. On every red run on main, GitHub reports the failed or timed-out run, ci_failed fires, and ci-fixer reads the failing log. If it is real, it fixes it on a branch and runs the job's command, then you allow git push and gh pr create once, and the pull request opens. If it is flaky, it re-runs the job and replies why. Optionally, a green run on main fires ci_succeeded and ci-fixer replies that main is green again and closes the fix task

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:

text
You keep main green in acme/shop. When CI fails on main, you find out why, fix it
on a branch and open a pull request. Never push to main. Ask before every git push
and gh pr create. A test that fails only sometimes is flaky: re-run the job and say
why, do not "fix" it.

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:

json
{
  "permissions": {
    "allow": ["Bash(gh run view:*)", "Bash(gh run rerun:*)", "Bash(go test:*)", "Bash(git switch -c:*)", "Bash(git commit:*)"],
    "ask": ["Bash(git push:*)", "Bash(gh pr create:*)"]
  }
}

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.

Step 2: the ci_failed event's Edit trigger form, with ci-fixer as the workspace,

The instructions are the agent's playbook for a red build. This run used:

text
CI is red on main in acme/shop.

{{EVENT_PAYLOAD}}

{{EVENT_FAQ}}

1. Read the failing logs: gh run view <run id> --log-failed. The run id is the number
   at the end of the run link.
2. Reply with the failing job, the error lines and the commit that broke it.
3. Decide whether it is flaky or real.
   - Flaky (a timeout, a network error, a test that passes when you run it again):
     run gh run rerun <run id> --failed, reply with why you think it is flaky, and
     complete this task.
   - Real: reproduce it locally with the failing job's command.
4. Fix it on a new branch, fix/ci-<run id>. Never push to main.
5. Run the failing job's command again until it passes.
6. Push the branch and open a pull request with gh pr create. Reply with its link.
7. Leave this task open until main is green again.

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 2, continued: the rest of the trigger form, with steps 4 to 7 of the instructions, YOLO mode set to OFF and

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.

Step 3: the New registration form with the github hook picked, a generated webhook secret with Generate and Copy, main under Branches and acme/shop under Repositories, with arrows at each

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.

Step 3, continued: the Routes column, with ci.failed going to ci_failed, ci.succeeded going to ci_succeeded and the other GitHub events Not routed, with arrows at the two routes

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 3, the registration: its payload URL with Copy URL and Rotate Token, the warning that the token is shown only once, and the routes ci.failed to ci_failed and ci.succeeded to ci_succeeded, with arrows at the URL and the routes

Step 4: Add the Webhook in GitHub

In GitHub, open the repository's Settings, then Webhooks, then Add webhook:

  1. Payload URL: the URL you copied, token included.
  2. Content type: application/json. GitHub's default, application/x-www-form-urlencoded, is rejected.
  3. Secret: the webhook secret from the registration. It is not the hook token.
  4. Which events: Let me select individual events, untick Pushes, and tick Workflow runs. That is the GitHub event behind ci.failed and ci.succeeded.
  5. 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.

Step 5: the ci-fixer workspace with a new Not Started task,

The payload looks like this, with the same lines in every ci_failed task:

text
Workflow `CI` failed on `main` @ 9f2c4e1 in acme/shop
Run: https://github.com/acme/shop/actions/runs/13124881071
Commit: cart: apply coupons per line item (#241) (Lee Park)

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.

Step 6: the task's conversation, with the failing test output from the log, the reply naming commit 9f2c4e1 and the local reproduction, and the fix on fix/ci-13124881071 with the files it changed, with arrows at each

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.

Step 7: an Authorization Required card for Bash with the gh pr create command and the buttons Allow Once, Always Allow and Deny, with arrows at the card and at Allow Once

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 7, continued: the task's timeline with two Needs Input stops, the reply

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 8: the completed 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:

text
CI passed on main in acme/shop.

{{EVENT_PAYLOAD}}

If a "Fix main" task in this workspace is still open for this workflow, reply
"Main is green again" with the run link in that task and complete it. Then complete
this task with one line.

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.

Step 9: the completed

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.

Step 10: the registration's Deliveries, newest first: two Published ci.succeeded, a Published ci.failed, a Duplicate, a Published ci.failed and a Rejected

On the Kanban board, 1. two failures and two green runs are all done.

Step 10, continued: the Kanban board with four ci-fixer tasks in Done: two

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.

Start Free