dotFactory

Adversarial AI code review: don't let an agent rubber-stamp its own work

By Simone Mosciatti

Can you trust an AI agent to review another AI agent's pull request? Only if it's set up to disagree. A reviewer that reads the change in the same spirit as the author, often the same model with a similar prompt, will mostly nod along, and you pay for a review that catches almost nothing. The fix is an adversarial reviewer: a separate step, preferably a different model, prompted to find problems or break the change, that posts its findings and never merges. In dotFactory, that reviewer is one step in a .fct file you own, so the rule "it comments, it doesn't merge" is written down, not hoped for.

Part of the Patterns for a software factory series.

Why does a second AI reviewer agree with the first one so often?

Because it usually shares the author's blind spots. Same model, same context, a prompt that says "review this", and you get a polite summary of the change with a thumbs up.

That costs you twice. You spend tokens on every PR, and you get a false sense of safety. People start skimming because "the AI already looked at it". The business problem isn't a missing reviewer. It's a reviewer whose "looks good" means nothing.

What makes a reviewer adversarial?

Its job is to find what's wrong, not to judge whether the change is fine. In practice that's four things:

  1. A separate step. The reviewer isn't the agent that wrote the code, and it doesn't see the author's reasoning, only the change.
  2. A different model or provider, when you can. Different models miss different things. Same model, different persona, is the fallback.
  3. A prompt that asks for problems. "Find bugs, missing tests, security holes and ways this breaks. If you find nothing, say what you tried." Being "incentivized to find problems" is a prompt and eval concern, not a magic setting.
  4. Typed findings, and no merge. The reviewer returns a verdict and a list of findings in a shape you chose. It can comment or request changes. It can't merge, because the file never gives it a merge step.

How is this different from risk-based routing?

Routing decides which path a PR takes. Review tries to break it. They're different jobs, so they should be different steps.

Step Question it answers What it can do
Risk classifier (risk-based review routing) How risky does this change look? Pick a path: merge, gate, or comment
Adversarial reviewer (this post) What's wrong with this change? Post findings, request changes
Human approval gate Does a person say yes? Unblock the merge
github_merge Is the pinned commit still green and unheld? Merge, or refuse

A routing model that's too generous sends too much to auto-merge. An adversarial reviewer on the rest gives the person who does look a head start: a list of suspected problems, not a blank diff.

How do you write an adversarial reviewer in a .fct file?

Trigger on the pull request, run an OpenCode reviewer step with its own model and a "find problems" prompt, and post its typed findings with github_comment. A kind: github trigger fires when a PR is opened, updated (synchronize), or reopened. The reviewer reads the PR's description and changed files through pr_context, and gets the code itself checked out at the commit under review.

type PullRequest = { number: int, url: text }
type GitRepo     = { url: text, ref: text? }
type ReviewCall  = { token: secret, prompt: text, model: text, repo: GitRepo }
type PrContext   = { title: text, body: text, files: [text] }

type Verdict  = enum { no_issues, comments, request_changes }
type Severity = enum { low, medium, high }
type Finding  = { severity: Severity, file: text, problem: text }
type Review   = { verdict: Verdict, findings: [Finding], body: 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
external port pr_context(token: secret, repo: text, number: int) -> PrContext

runner reviewer:
  kind: opencode
  in:   ReviewCall
  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 adversary:
  runner: reviewer
  in:
    token:  credential(key: "reviewer")
    model:  "other-provider/other-model"
    repo:   { url: "https://github.com/acme/app", ref: on_pr.commit }
    prompt: "You did not write this change. Your job is to break it. Find bugs, missing tests, security problems and edge cases. Report each finding with a severity. Never approve. If you find nothing, say what you checked."
    context: pr_context(
      token:  github_token(repo: "acme/app", scope: { "pull_requests": "read" })
      repo:   "acme/app"
      number: on_pr.pr.number
    )
  out: { review: Review }

step request_changes:
  kind: github_comment
  when: adversary.review.verdict == "request_changes"
  config: { event: "REQUEST_CHANGES" }
  in:
    token:  github_token(repo: "acme/app", scope: { "pull_requests": "write" })
    repo:   "acme/app"
    number: on_pr.pr.number
    body:   adversary.review.body

step comment:
  kind: github_comment
  when: adversary.review.verdict != "request_changes"
  config: { event: "COMMENT" }
  in:
    token:  github_token(repo: "acme/app", scope: { "pull_requests": "write" })
    repo:   "acme/app"
    number: on_pr.pr.number
    body:   adversary.review.body

The model token comes from credential, and GitHub calls use a scoped github_token. The model string is a placeholder: pick something other than what your coding agent uses.

A few things worth noticing.

The reviewer can't approve, even if it wants to. github_comment supports APPROVE, but this file only ever posts COMMENT or REQUEST_CHANGES. An over-friendly model can't turn its mood into a green tick.

There's no merge step here at all. The reviewer's output can feed a gate or a routing decision later, but this step's power stops at a review on GitHub.

The findings are typed. Review has a verdict from a fixed list and a list of findings with a severity. The next step reads adversary.review.verdict directly, with no paragraph parsing. More on why that matters in typed dataflow.

Every push gets a fresh review. A new commit fires the trigger again as synchronize, so the reviewer looks at what's actually there now.

When should you use an adversarial reviewer, and when should you skip it?

Use it when agents open more PRs than people can read closely, and you want every one to get a hard first look. It's especially worth it on the PRs that don't auto-merge, so the human reviewer starts from a list of suspects.

Skip it, or run it only on some PRs, when changes are tiny and frequent, like lockfile bumps, where the tokens cost more than the review is worth. A when: guard on a risk level handles that. And don't use it as a replacement for tests. A reviewer can say "this looks like it breaks on empty input". Only CI proves it.

How do you keep the reviewer honest over time?

Read what it says, and tune the prompt. A few tips:

  • Seed known bugs. Every so often, open a PR with a planted mistake and see if the reviewer catches it.
  • Watch the "no issues" rate. If it says "looks good" on almost everything, it's become a rubber stamp again.
  • Watch the noise too. If every PR gets twenty low-severity nits, people stop reading. Ask for fewer, sharper findings.
  • Read the Runs. Every Run records what the reviewer was shown and what it answered, so you can see why it missed something.

One reviewer will still miss things. Running several, say security, tests, and style, in parallel and joining their verdicts is the fan-out and fan-in pattern, later in the series. And an adversarial reviewer makes a great checker inside a loop of cheaper models, which is coming up too.

For the bigger idea, that the agent can say anything but the file decides what it's allowed to do, see Put your software factory in a file and Who signs off on code an agent wrote?.

Frequently asked questions

What is adversarial AI code review?

Adversarial AI code review is a review step where an agent is prompted to find problems in a change rather than to approve it. It works best as a separate step from the author, ideally on a different model, and it posts findings without being able to merge.

Can an AI agent review another AI agent's pull request?

Yes, as long as the reviewer is separate from the author and asked to break the change, not to bless it. Treat its output as findings for a person or a gate, not as a sign-off.

Should the AI reviewer be allowed to approve or merge PRs?

No. Let it comment or request changes, and keep the merge behind CI, a pinned commit, and a person or risk rule.

Do I need a different model for the reviewer?

It helps, because different models tend to miss different things. If you only have one, use a separate step with a clearly adversarial prompt and no access to the author's reasoning.

Is automated PR review a replacement for tests?

No. A reviewer can point at likely bugs, but only tests and CI show whether the code actually works.

Want every agent PR to get a reviewer that actually pushes back? 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.