Policy-as-Code for AI-Authored PRs: Beyond the Lint Pass
๐ค The Problem
Somewhere in the last year, the bottleneck in a lot of teams' delivery pipelines quietly shifted. It used to be "writing the code."
Now, with Copilot Workspace, Devin-style agents, and PR-generating bots wired into Jira tickets, the bottleneck is reviewing the code - and reviewing it at a volume no human team was built for.
The instinct has been to bolt on more automation: lint, type-check, run the test suite, require green CI. That's table stakes, and it catches almost nothing that matters for an AI-authored change.
An agent can produce a PR that compiles, passes lint, and even passes the existing tests, while:
- quietly deleting the three assertions that would have caught its own regression
- adding a new transitive dependency with a permissive-but-sketchy license
- touching 40 files when the ticket asked for a one-line config change
- "refactoring" an auth module it was never asked to touch
None of that shows up in a lint pass. It shows up in policy - the stuff senior engineers check by instinct during review but rarely write down anywhere a machine can enforce.
If AI is going to author a growing share of your PRs, the review gate needs to stop asking "does this build" and start asking "does this PR behave like a PR we'd actually approve."
๐ Policy-as-Code, Not Vibes-as-Code
The pattern that's actually working for teams I've talked to is borrowed straight from infra security: Open Policy Agent (OPA) and Rego, the same tooling people use to gate Kubernetes manifests and Terraform plans, pointed instead at the PR diff itself.
The shape of the system is simple:
- A CI step collects structured facts about the PR (diff stats, coverage delta, dependency changes, files touched vs. ticket scope).
- Those facts get serialized as JSON and handed to OPA.
- Rego policies evaluate the facts and return
allow/denyplus human-readable reasons. - The gate posts the reasons as a PR comment and blocks merge on
deny.
This matters because it decouples what the rule is from how you check it. Your policies live in a versioned .rego file that gets reviewed like any other code change - not scattered across five different GitHub Actions if: conditions that nobody remembers the reasoning for.
๐งช Rule 1: Test Coverage Deltas, Not Just Coverage Thresholds
A flat "80% coverage minimum" gate is nearly useless against AI-generated PRs, because agents are extremely good at gaming flat thresholds - they'll happily add a trivial test for a getter while leaving the actual logic branch untested.
What actually catches regressions is the delta on touched lines:
package pr.coverage
import future.keywords.in
default allow := false
input.coverage = {
"touched_lines": 120,
"covered_touched_lines": 60,
"baseline_pct": 82.4,
"new_pct": 79.1
}
allow {
input.coverage.new_pct >= input.coverage.baseline_pct - 1.0
touched_coverage_ratio >= 0.70
}
touched_coverage_ratio := input.coverage.covered_touched_lines / input.coverage.touched_lines
deny[msg] {
not allow
touched_coverage_ratio < 0.70
msg := sprintf("Only %.0f%% of touched lines are covered by tests (need 70%%)", [touched_coverage_ratio * 100])
}
The input.coverage block is produced by a small script that diffs coverage.xml (or lcov.info) against the base branch's report and intersects it with the changed line ranges from git diff. That intersection is the part people skip, and it's the part that actually matters - overall repo coverage can go up while the specific lines an agent just wrote remain completely untested.
๐ฆ Rule 2: Dependency Changes Get Their Own Lane
Agents love to "just add a package" to solve a problem instead of writing ten lines of code, because pulling in a dependency is cheap in token-generation terms and looks like progress. That's exactly the kind of decision that should never ship on autopilot.
package pr.deps
default allow := false
allowed_licenses := {"MIT", "Apache-2.0", "BSD-3-Clause", "ISC"}
allow {
count(new_dependency_violations) == 0
}
new_dependency_violations[dep.name] {
dep := input.new_dependencies[_]
not dep.license in allowed_licenses
}
new_dependency_violations[dep.name] {
dep := input.new_dependencies[_]
dep.weekly_downloads < 1000
}
deny[msg] {
violations := new_dependency_violations
count(violations) > 0
msg := sprintf("New dependencies need human review: %v", [violations])
}
This doesn't block new dependencies outright - it routes them to a required human-reviewer label (needs-dep-review) instead of letting the gate auto-pass. The policy's job isn't to be the final judge on every axis; sometimes it's to correctly decide when a human must look.
๐ฏ Rule 3: Scope Creep Against the Ticket
This is the rule that catches the most AI-specific failure mode: an agent given "fix the null check in parse_invoice" that also decides to reorganize imports across the module, rename a function, and touch an unrelated billing file because it seemed tidy.
package pr.scope
default allow := false
max_files := 8
max_unrelated_dirs := 1
allow {
count(input.files_changed) <= max_files
count(unrelated_dirs) <= max_unrelated_dirs
}
input.ticket_paths comes from parsing the linked issue's referenced files/globs
unrelated_dirs[dir] {
file := input.files_changed[_]
dir := split(file, "/")[0]
not glob_match_any(dir, input.ticket_paths)
}
glob_match_any(dir, patterns) {
glob.match(patterns[_], ["/"], dir)
}
deny[msg] {
count(input.files_changed) > max_files
msg := sprintf("PR touches %d files; ticket scope suggests <= %d", [count(input.files_changed), max_files])
}
Tying ticket_paths to the actual issue requires a bit of glue - we extract file hints from the linked issue body and from a scope: front-matter block the agent is instructed to emit when it opens the PR. It's not bulletproof, but it turns "the PR felt bigger than it should be" from a gut feeling into a number someone can argue with in the PR comments.
๐ ๏ธ Wiring It Into CI
The GitHub Actions side is almost boring, which is the point - all the interesting logic lives in Rego, not YAML:
name: policy-gate
on: pull_request
jobs:
policy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 }
- name: Build policy input
run: python scripts/collect_pr_facts.py > input.json
- name: Evaluate policies
uses: open-policy-agent/setup-opa@v2
with: { version: latest }
- run: |
opa eval --format pretty \
--input input.json \
--data policy/ \
"data.pr" > result.json
python scripts/post_pr_comment.py result.json
collect_pr_facts.py is the unglamorous workhorse: it shells out to git diff, parses coverage reports, hits the GitHub API for the linked issue, and calls pip-audit / npm ls --json for dependency metadata. Expect to spend more time here than on the Rego itself - the policies are only as good as the facts you feed them.
๐ง Where This Still Breaks Down
To be honest about trade-offs: this approach adds real latency (30-90 seconds per PR for fact collection) and real maintenance burden - someone owns the .rego files now, and they will go stale as the codebase's conventions evolve.
It also won't catch semantic scope creep, like an agent that touches exactly the right one file but quietly changes behavior nobody asked for.
Policy-as-code is a floor, not a replacement for a human reading the diff - it just makes sure the human's time goes to judgment calls instead of "wait, why did it touch the auth module."
What's your team doing differently - are you trusting AI PRs enough to auto-merge any of them yet, or is every single one still getting a human set of eyes regardless of what the gate says?
Comments
No comments yet. Start the discussion.