dotFactory

Auto-merge AI-generated pull requests when CI passes

By Simone Mosciatti

Agents open pull requests faster than people can review them. Reviews pile up, or someone clicks merge without looking, and main is one tired click away from a bad commit. Auto-merge is safe when CI has finished and passed, and only the exact commit CI checked is what merges. In dotFactory, that rule lives in a .fct file you own: a merge step pinned to the SHA refuses if the branch moved, if CI isn't green, or if someone left a :hold.

Part of the Patterns for a software factory series.

Can you stop reviewing every agent PR by hand without breaking main?

Yes, if you only auto-merge after CI has finished and passed, and only the commit CI actually checked. That's the whole pattern.

You still shouldn't auto-merge every agent PR. Pair this with risk-based review routing, so only low-risk changes reach the merge, and send the rest to a human approval gate. This post answers the narrower question: once a change can land without a person, how do you merge it without racing CI or landing the wrong commit?

Why isn't "is CI green?" enough on its own?

Because "green" is a question about timing, and about which commit you mean.

A factory that starts when the PR opens often looks at CI too early. If it treats "no failure yet" as "green", it merges too soon. If it treats it as "not green", it gives up on a PR that would have passed five minutes later. The sneakier problem is identity: the commit CI checked and the commit that gets merged aren't always the same one. Someone pushes a fix, CI reruns, and an old decision is still sitting around. You want the thing that merges to be exactly the thing that passed.

How does CI-gated auto-merge work?

It waits until CI has finished and passed, pins the merge to that commit, and rechecks right before the merge. Three parts, all of them required:

  1. Know when CI is done. Start the Run when CI completes successfully, or let the merge step itself refuse until CI on the commit is green.
  2. Pin the merge to that commit. Pass the SHA CI ran on to the merge step.
  3. Let the merge step double-check. Right before merging, it re-reads the PR. If the head moved, if CI isn't green on that commit, or if someone left a hold, it refuses.

In dotFactory, that double-check is built into github_merge. You pass it the PR number and the SHA, and it re-reads the pull request before it does anything.

Why not just read ci_status?

Because ci_status is a read, not a wait. It returns the newest workflow run for a branch whether that run is green, still running, or failed. It will not sit there until tests finish.

If your factory starts from the PR, a ci_status check early in the Run can easily land in the middle of the build. There are two clean ways around that:

  • Start from a kind: github workflow_run trigger with conclusions: ["success"]. The Run only exists because CI finished and passed.
  • Lean on github_merge. Even if the Run started earlier, the merge step refuses unless CI on the pinned commit is green.

The catch with workflow_run is that its seed gives you the commit and the branch, but not the pull request number, and github_merge needs one. An OpenCode agent could look the PR up, but that puts a model in the middle of a step that should be boring. So the simplest version starts from the pull_request event, which gives you pr.number and commit, and lets github_merge refuse until CI on that commit is green.

How do you write this in a .fct file?

Start from the pull request, pin github_merge to the commit, and let it refuse until CI is green. Here's a compact version, combined with a risk check so only low-risk changes reach the merge. The agent token comes from credential, and the merge uses a scoped github_token:

type PullRequest = { number: int, url: text }
type AgentCall   = { token: secret, prompt: text }
type Risk        = enum { low, medium, high }
type RiskCall    = { risk: Risk, reason: text }

type GithubAccess = enum { read, write, admin }
type GithubScope  = map(text, GithubAccess)

external port credential(key: text) -> secret
external port github_token(repo: text, scope: GithubScope) -> secret

runner agent:
  kind: opencode
  in:   AgentCall
  out:  open

trigger on_pr:
  kind: github
  config: {
    path:    "/api/github/webhook"
    target:  { one: { repo: "acme/app" } }
    event:   pull_request
    actions: ["opened", "synchronize", "reopened"]
  }
  out: { pr: PullRequest, commit: text }

step classify:
  runner: agent
  in:
    token:  credential(key: "agent")
    prompt: "Judge how risky this change is. Answer low, medium or high, and say why."
    pr:     on_pr.pr
    commit: on_pr.commit
  out: { review: RiskCall }

step merge:
  kind: github_merge
  when: classify.review.risk == "low"
  config: { method: "squash" }
  in:
    token:  github_token(
      repo:  "acme/app"
      scope: {
        "contents": "write"
        "pull_requests": "write"
        "checks": "read"
        "statuses": "read"
      }
    )
    repo:   "acme/app"
    number: on_pr.pr.number
    sha:    on_pr.commit
    body:   classify.review.reason

Before it merges, github_merge re-reads the PR and refuses unless the head is still the sha you passed, the branch isn't too far behind its base (the reference says no more than 10 commits and 3 days), every CI check on that head is green, and no review of the same commit carries a :hold marker. It also only merges PRs from the same repository, not forks. So even if this Run started before CI finished, a red or unfinished build can't slip through.

What other ways are there to auto-merge?

GitHub already has auto-merge, and merge queues exist too. Here's how they compare, fairly, for agent PRs:

Approach What it's good at Where it stops short for agent PRs
GitHub auto-merge Built in. Waits for required reviews and status checks, then merges. You still enable it on each PR, unless you automate that too. It doesn't judge risk, and the rest of the agent flow isn't in the same file.
A merge queue (GitHub's, or a tool like Mergify) Keeps main green when many PRs land, by testing them in order, often after updating onto the latest base. Built for throughput and freshness, not for deciding which agent PRs can skip a person.
dotFactory CI-gated auto-merge The rule lives in a .fct file: merge only the pinned SHA, recheck CI and :hold, and combine that with risk routing or a human gate. Not a merge queue. If you already need one, keep it for the merge, and use the factory for triage and review.

If you want the wider comparison of factories and Actions, we wrote dotFactory vs GitHub Actions for AI agent workflows.

When should you use this, and when should you skip it?

Use it when you have a CI workflow you trust, and you want small, safe PRs to land without anyone clicking merge. It pays off most next to risk-based review routing.

Skip it when CI is flaky or thin. Auto-merge amplifies whatever CI misses. Skip it too when your repo already has a merge queue or strict branch rules that handle this. Then let that system do the merging, and use the factory for triage and review.

What should you check before you turn it on?

  • Make sure CI runs on pull requests, not only on pushes to main.
  • Decide what "green" means. Required checks should include the ones that actually protect you.
  • Keep a brake. Anyone should be able to add a :hold and stop the merge. That's the hold and kill switch pattern, coming later in the series.
  • Watch the first few merges. Open the Runs and check that the merged commit is the one CI passed.
  • Have a plan for PRs that never merge. Some go stale or get abandoned. A later post covers a scheduled agent that revives or cleans them up.

Merge rules belong outside the agent. That's the idea behind Put your software factory in a file, and the bigger sign-off question in Who signs off on code an agent wrote?.

Frequently asked questions

Is it safe to auto-merge AI-generated pull requests?

It can be, if you only merge after CI has finished and passed, and only the exact commit CI checked. Auto-merge is not safe when you treat "no failure yet" as green, when the branch can move under you, or when CI is too thin to catch what the agent got wrong.

Does ci_status wait for CI to finish?

No. ci_status reads the newest workflow run for a branch and returns it whether it's green, running, or failed. To start a Run only after CI has passed, use a workflow_run trigger with conclusions: ["success"].

How do I make sure the merged commit is the one CI checked?

Pass that commit's SHA to github_merge. Before merging, it re-reads the pull request and refuses if the head is no longer that SHA or if CI on it isn't green.

Can a workflow_run trigger tell me the pull request number?

Not directly. Its seed includes the head commit and head branch, but not a PR number. For merges, it's simpler to start from the pull_request trigger and let github_merge check CI on the pinned commit.

Pick this pattern, copy the snippet into a .fct file, and run it on dotFactory. The Getting started guide walks you through your first factory.

Portrait of Simone Mosciatti

Written by

Simone has spent more than 10 years building software, from research at CERN to big tech at AWS and fast-growing startups like Rippling. These days he's building dotFactory, because he thinks writing software should feel a lot more like running a well-oiled factory.