Risk-based review routing: auto-merge the safe stuff, stop for a person on the rest
Not every pull request deserves the same review. This pattern lets an agent judge how risky a change is, merges the low-risk ones once CI is green, and sends the rest to a human gate or leaves them open with a comment.
Part of the Patterns for a software factory series.
The problem: every PR gets the same treatment
Picture a Monday morning. Agents and people have opened a dozen pull requests overnight. There's a typo fix in the README, a lockfile bump, a small refactor of a helper, and a change to how the app checks session cookies.
Review all twelve the same way and you burn an hour on the typo and the lockfile, then skim the cookie check. Let everything through automatically and the cookie change lands with nobody looking. Neither feels great.
The better question isn't "should agents merge their own PRs?" It's "which PRs can merge on their own, and which ones need a person?" That's what risk-based review routing answers.
The pattern
The pattern has four moves:
- A pull request arrives. Opened, updated with a new commit, or reopened.
- CI decides whether it's even a candidate. If CI is red, nothing merges. Full stop.
- An agent judges the risk. It reads the PR and answers in a shape you chose, like
{ risk: low | medium | high, reason: text }. The agent makes a judgment call, but it can only pick from your list. - The factory routes it. Low risk and green CI: merge the exact commit that was checked. Anything else: either pause at a human gate, or leave the PR open with a review comment that says why.
The important part is who decides what. The agent decides how risky the change looks. The file decides what each risk level is allowed to do. An agent that thinks a risky change is fine still can't merge it if the guard says only low merges.
When to use it, and when to skip it
Use it when:
- Agents open a steady stream of PRs and most of them are small.
- Your CI is trustworthy. If tests are flaky, auto-merge will either stall or let junk through.
- You want your review time to go to the changes that actually need a brain.
Skip it, or start without the merge, when:
- Your test suite doesn't really cover the risky parts. Then "low risk, CI green" doesn't mean much.
- You're in a regulated codebase where every change needs a named reviewer. You can still use the routing to sort PRs, just without the auto-merge branch.
- The team is small and PRs are rare. Doing it by hand is fine.
A good way to start is to run the routing for a while with the merge step replaced by a comment. You'll see what the agent calls "low", and learn where to draw the line before anything merges on its own.
The dotFactory version
Here's the shape. A kind: github trigger starts a Run when a PR is opened, updated (synchronize), or reopened. An OpenCode agent step classifies the risk, reading the PR with pr_context. Then two effect steps are guarded by when:: github_merge lands the low-risk ones, and github_comment posts a review on the rest.
type PullRequest = { number: int, url: text }
type AgentCall = { token: secret, prompt: text }
type GithubAccess = enum { read, write, admin }
type GithubScope = map(text, GithubAccess)
type Risk = enum { low, medium, high }
type RiskCall = { risk: Risk, reason: text }
type PrContext = { title: text, body: text, files: [text] }
external port credential(key: text) -> secret
external port github_token(repo: text, scope: GithubScope) -> secret
external port pr_context(token: secret, repo: text, number: int) -> PrContext
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
head_ref: 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
files: pr_context(
token: github_token(repo: "acme/app", scope: { "pull_requests": "read" })
repo: "acme/app"
number: on_pr.pr.number
)
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
step ask_for_review:
kind: github_comment
when: classify.review.risk != "low"
config: { event: "COMMENT" }
in:
token: github_token(repo: "acme/app", scope: { "pull_requests": "write" })
repo: "acme/app"
number: on_pr.pr.number
body: classify.review.reason
The agent token comes from credential, and GitHub calls use a scoped github_token.
A few things are worth pointing out.
The agent answers in a shape you chose. RiskCall says the answer is a risk from a fixed list plus a reason. The next steps read classify.review.risk directly. Nobody is parsing a paragraph and hoping the word "low" means what it sounds like.
The merge is pinned to a commit. github_merge gets sha: on_pr.commit, the head commit from the trigger. Before merging, it re-reads the PR and refuses unless the head is still that commit, CI on it is green, the branch isn't stale, and no review of that commit carries a :hold. If someone pushes after the agent classified the PR, the new commit doesn't sneak through on the old decision. The push fires the trigger again as synchronize, so it gets its own Run.
Where does CI fit? The merge step checks CI itself, so a red build won't merge. But if you want the classification to happen only after CI has finished, start the Run from a workflow_run trigger with conclusions: ["success"] instead. Reading ci_status doesn't wait for CI. The next post in the series, CI-gated auto-merge, covers this properly.
Gate or comment?
In the snippet, risky PRs get a comment and stay open. That's the light touch: the PR is still there, with a note explaining why it needs a look, and a person reviews it on GitHub like any other PR.
If you'd rather the factory wait for a person, swap the comment step for a human gate:
step approve:
kind: gate
when: classify.review.risk != "low"
config: { timeout: "1d", default: { approved: false } }
in:
pr: on_pr.pr
commit: on_pr.commit
reason: classify.review.reason
out: { approved: bool, note: text? }
step merge_after_approval:
kind: github_merge
when: approve.approved
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
The gate pauses the Run until someone answers its form, and the in: values are the context it shows: which PR, which commit, and the agent's reason. If nobody answers within a day, the default says approved: false, so a timeout never turns into a yes.
Which one to pick? Comments suit teams that already live in GitHub. Gates are better when you want the factory to merge after approval, still pinned to the same commit. There's a fuller walkthrough in human approval gates.
One more thing: routing is not a review. The classify step is only judging risk so the factory can pick a path, and a model that's too generous will send too much to auto-merge. For everything that doesn't merge, a later pattern in the series is an adversarial reviewer: a second agent, preferably a different model, prompted to find problems rather than to agree. It comments or requests changes. It still doesn't merge.
Tuning the line between low and the rest
The routing is only as good as the line you draw. A few tips:
- Tell the agent what "high" means for you. Auth, payments, database migrations, CI config. Put that list in the prompt.
- Consider three outcomes instead of two. Low merges, medium gets a comment, high gets a gate.
- Read the Runs. Every Run records what each step received and produced, so you can see what the agent was shown and what risk it picked, and adjust when it's too generous.
For the bigger idea behind this, that the agent can choose but the file decides what the choice allows, see Put your software factory in a file. And for how this compares with a policy-file approach, see Who signs off on code an agent wrote?
Frequently asked questions
What is risk-based review routing?
Risk-based review routing is a software factory pattern where each pull request is classified by risk, usually by an agent that answers with a fixed set of levels. Low-risk changes that pass CI can merge automatically. Higher-risk changes pause for a person or stay open with a review comment.
Should AI coding agents auto-merge pull requests?
Sometimes. Auto-merge makes sense for low-risk changes when CI is trustworthy and the merge is pinned to the exact commit that was checked. Changes that touch sensitive areas should wait for a person, either at a human gate or as an open PR with a comment.
How does the factory know a change is low risk?
You tell it. An agent step reads the PR, including its description and changed files through pr_context, and returns a typed answer like risk: low with a reason. The factory then uses a when: guard on that value to decide which step runs.
What happens if someone pushes a new commit after the agent classified the PR?
The merge is pinned to the commit the trigger saw, and github_merge refuses if the PR head has moved. The new commit fires the kind: github trigger again with synchronize, which starts a new Run with a fresh classification.
Gate or comment for risky changes?
Use a comment when you want the PR to stay open on GitHub for someone to review there. Use a gate when you want the Run itself to pause and then merge after a person approves. Both keep the risky change from merging on its own.
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.