Autonomous Agents: Who fixes main at 6 pm? GitHub Actions failures to an agent-opened PR
You know the pattern. Someone merges late in the day, CI on main fails, and they've already logged off. The failed run sits there until morning, and every open PR that needs a green base is stuck behind it. The first hou
You know the pattern. Someone merges late in the day, CI on main fails, and they've already logged off. The failed run sits there until morning, and every open PR that needs a green base is stuck behind it. The first hour of the fix is always the same grind: open the run, scroll the log, find the commit, reproduce, patch.
That first hour is a good fit for an agent. Not because it's clever, but because it's mechanical, and the inputs are all on the runner and in the repo.
This post walks through a setup where a failed GitHub Actions run on main lands as a task for an agent sitting in your repository's checkout. The agent diagnoses, decides whether the failure is real or flaky, and either re-runs the job or fixes it on a branch, opening a pull request only after you say yes.
TL;DR
- GitHub webhook (Workflow runs) → AgentRQ's built-in github hook → a
ci_failedevent → a trigger that creates a task in aci-fixerworkspace. - The agent runs
gh run view <id> --log-failed, names the failing lines and the offending commit, then classifies: flaky →gh run rerun --failed; real → reproduce, fix onfix/ci-<run id>, open a PR. - YOLO mode stays off, so
git pushandgh pr createeach wait for Allow Once. - Optional: route
ci.succeededso the agent closes its own fix task when main is green again. - No polling, no glue service: one hook, one event, one trigger.
What does the flow actually look like?
GitHub Actions run on main finishes with failure / timed_out
│ (workflow_run webhook, signed)
▼
github hook ── checks signature, branch = main, repo = acme/shop
│ fires ci.failed → routed to ci_failed
▼
trigger ── creates "Fix main: <subject>" in ci-fixer
▼
agent ── reads log → flaky? rerun + explain
→ real? reproduce, branch, fix, run job cmd
→ asks: git push (you: Allow Once)
→ asks: gh pr create (you: Allow Once)
▼
(optional) ci.succeeded on main → ci_succeeded → agent closes the fix task
Why a hook → event → trigger chain instead of a bot?
Each link does one job, which keeps the moving parts small:
- The hook is the security boundary. It verifies GitHub's signature on every request and filters by branch and repository before anything else happens. The built-in github hook needs no code.
-
The event (
ci_failed) is just a name for "something happened". It decouples the source from the work, so you can later point a different CI system at the same event. - The trigger turns the event into a task in a specific workspace, pasting in what GitHub sent.
And the human stays in the loop at the one point that matters: anything that leaves your machine. With YOLO mode off, every command the agent runs needs your approval, so it can't push or open a PR on its own.
What do I need before starting?
- An AgentRQ account and a machine with a checkout of the repo, where the agent will run.
- An agent that connects to AgentRQ: Claude Code over MCP, or Codex, Gemini CLI and others via the ACP Gateway.
- The GitHub CLI signed in (
gh auth login) with access to the repo's Actions logs, so the agent can read logs, re-run jobs, push a branch and open a PR. - Whatever your CI job needs locally. The example repo,
acme/shop, runsgo test ./.... - Admin access on the repo to add a webhook.
How do I set it up?
1. A workspace with a narrow mission
Create a workspace called ci-fixer, point its working directory at the checkout (~/code/shop in the example), start its agent, and leave YOLO mode off. The mission is what the agent re-reads every time it starts:
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.
With YOLO off, even read-only commands prompt. If you're on Claude Code, cut the noise in .claude/settings.json and keep the two dangerous commands gated:
{
"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:*)"]
}
}
2. The ci_failed event and its trigger
Under Events, create ci_failed and add a trigger: workspace ci-fixer, title Fix main: {{EVENT_SUBJECT}}. The hook fills the subject with something like "CI failed on main: CI", so each red run gets its own readable task. The instructions are the agent's runbook:
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.
Leave "When the task is completed, fire" on No event; nothing chains after this.
3. Register the github hook
Events → Hooks → New registration, pick the built-in github hook, then:
- Generate a webhook secret and copy it (GitHub needs the same value).
-
Branches:
main. Repositories:acme/shop(useful if you ever reuse the URL for an org-level webhook). -
Routes:
ci.failed→ci_failed. Optionallyci.succeeded→ci_succeeded(create that event first). Everything else stays Not routed.
Copy the payload URL (it ends in ?agentrq_hook_token=). It's only shown once; Rotate Token issues a new one and kills the old URL.
4. Add the webhook in GitHub
Repo Settings → Webhooks → Add webhook:
- Payload URL: the one you copied, token included.
- Content type:
application/json. The default form encoding gets rejected. - Secret: the webhook secret (not the hook token).
- Events: pick individually, untick Pushes, tick Workflow runs.
GitHub's initial ping isn't routed, so it fires nothing and leaves no delivery row. The first real delivery shows up when a run on main finishes.
What happens when main goes red?
In the example, a commit titled "cart: apply coupons per line item (#241)" breaks CI. The hook verifies the delivery, sees a failed run on main, and fires ci.failed. A task appears in ci-fixer with a payload like:
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)
The agent pulls the failing log and replies before touching code: two coupon tests off by one cent, the commit responsible, and confirmation that the failure reproduces locally, so it isn't a runner quirk. Then it fixes on a branch, adds a regression test, and re-runs the job's command until it passes.
Then it stops and asks. First for git push, then for gh pr create, each as an Authorization Required card showing the exact command. Those cards also show up as phone notifications.
Pick Allow Once, not Always Allow. A push to a shared repo is the step other people see, and you want to be asked every time. The task stays open after the PR, because an open PR doesn't mean main is green yet.
How does it handle flaky tests?
This is the decision that makes the agent worth having. Later in the example, the E2E workflow times out on main. Timeouts count as failures, so the hook fires ci.failed with the subject "CI timed out on main: E2E".
The agent notices the same commit passed this spec before, the spec passes 5/5 locally, and the wait was on an external sandbox. So it runs gh run rerun --failed, links the new attempt, explains why it thinks it's flaky, and completes the task without creating a branch.
Can it close its own loop?
If you routed ci.succeeded, give ci_succeeded a trigger titled Close the loop: {{EVENT_SUBJECT}}:
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.
When the fix PR merges and CI passes, the agent posts "Main is green again" in the original task and closes it. The trade-off: every green run on main now creates a small task. If main builds dozens of times a day, skip this route.
The registration's Deliveries list is your audit trail: published deliveries, GitHub Redeliver caught as a Duplicate (no second task), and form-encoded requests marked Rejected with a reason.
What are the gotchas?
-
Workflow runs not ticked → GitHub never sends
workflow_run, nothing happens. - Content type left as form → every request is Rejected with "the body is not JSON".
-
Timeouts fire, cancels don't.
failureandtimed_outtriggerci.failed; cancelled, skipped and other conclusions don't. - A re-run is a new delivery, with a new delivery id, so it isn't deduplicated. Fail again and you get another task for the same run. Only GitHub's Redeliver is a Duplicate.
-
Every workflow counts. CI, E2E, lint and deploy all fire
ci.failed. The hook filters by branch and repo, not workflow, so tell the agent in the instructions which workflows to just complete. - Dropped requests leave no row. Pings, other branches, cancelled runs and unrouted events are accepted and discarded silently. Check GitHub's Recent Deliveries if you expected a task.
-
Branch globs are exact.
*doesn't cross/:release/*matchesrelease/1.2, notrelease/1.2/hotfix. PR runs carry the PR branch, somainkeeps them out.
Ideas to extend it
- Dedupe by run: have the agent look for an open "Fix main" task with the same run link and reply there instead.
- Ask it to save every test it calls flaky to workspace memory with a date, so a repeat offender becomes a real fix.
- Add
release/*to Branches and tell the agent that release fixes target that branch. - Request a review from the breaking commit's author with
gh pr create --reviewer. - Not on GitHub Actions? Send your CI's webhook to the generic hook and route
request.receivedtoci_failed.
FAQ
Can the agent push to main? Not while it has to ask. The instructions forbid it and every git push waits for Allow Once. Clicking Always Allow or enabling YOLO removes that guard, so don't on this workspace.
Will a failing PR start a task? No. PR runs are on the PR's branch, and the registration only accepts main.
What if the fix is wrong? It's a PR, so your normal review and CI apply. Reply in the task and the agent keeps working on the same branch.
Which agents work? Anything that connects to AgentRQ: Claude Code, or Codex, Gemini CLI and others via the ACP Gateway.
How does your team handle a red main today? Is it a rotation, whoever notices first, or does it just wait until morning? I'd like to hear in the comments.
Originally published on agentrq.com. The full tutorial has every screenshot and step.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.


