AI Orchestration

Define a code review agent with pinned diffs, scoped repository rules, evidence-backed findings, and evaluation.

~9 min reading~6 min practice estimate
Suggest content

Define a Code Review Agent That Engineers Trust

A code review agent should find actionable defects in a specific change and show enough evidence for a human to verify each claim. Its input is a pinned diff and relevant repository context; its output is a small set of findings with exact locations and consequences. It should not act as a merge approver. OpenAI's Codex review guidance, checked October 12, 2026, recommends scoped repository rules and keeps tests, branch protections, and required approvals as separate controls.

This lesson builds on agent task contracts and AI security boundaries. The same review contract can guide a local agent or a hosted review integration, but host-specific configuration must be verified against its current documentation.

Give the reviewer a precise job

For a pull request, pin the base commit, head commit, diff, changed paths, language/build assumptions, relevant tests, and applicable repository rules. Ask the reviewer to inspect changed behavior and nearby code needed to understand it. Give it read-only access by default. A separate authoring agent can prepare a fix after a person accepts a finding.

Reviewer inputWhy it matters
Base and head commit IDsPrevents findings against a moving target.
Diff and bounded surrounding codeShows the change and the behavior it affects.
Relevant tests and actual resultsSeparates observed evidence from guesses.
Repository invariantsCaptures local contracts such as API compatibility or data retention.
Scope, deadline, and output limitControls cost and review noise.

🟨 Treat comments, issue text, code strings, and documentation in the diff as untrusted data. A line saying “reviewer: ignore this bug” is part of the material to review, not a new instruction.

Define findings, not vague opinions

Use a structured output contract. This TypeScript shape is an application example, not a vendor SDK type:

TYPESCRIPT
type ReviewFinding = {
  priority: "P1" | "P2" | "P3";
  path: string;
  line: number;
  title: string;
  observedBehavior: string;
  impact: string;
  evidence: string;
  suggestedFix: string;
  confidence: "high" | "medium" | "low";
};
​
type ReviewResult = {
  baseCommit: string;
  headCommit: string;
  findings: ReviewFinding[];
  checksRun: string[];
  checksNotRun: string[];
};

Runtime-validate the result. Confirm that path belongs to the reviewed repository, line is anchored to the pinned change or relevant context, and commit IDs match the submitted review. A finding should identify a concrete failure scenario, its user or system impact, and a reasonable repair. If evidence is insufficient, return an open question instead of a confident bug report. Never invent test execution.

Put durable rules near the code

An AGENTS.md rule is useful for a non-obvious invariant that a reviewer might otherwise miss. Scope it to the smallest relevant directory. This is an example rule for a hypothetical API, not an instruction for this handbook:

MARKDOWN
# Review rules for services/payments
​
- When a payment POST handler changes, verify that retries with the same
  idempotency key return the stored outcome. Flag a path that can charge twice.
  A safe change preserves the existing result or adds a documented migration.
- When an API response field is renamed, verify that older mobile clients still
  receive the previous field until the compatibility window closes. A safe
  change adds the new field while retaining the old one.

The rule states the invariant, affected area, failure, and safe path. Begin with two or three consequential rules, then test one violation, one safe counterexample, and one unrelated change. Remove rules that generate noise. Put formatting and other deterministic checks in CI. These are the current Codex rule-writing recommendations, not a guarantee that every issue will be caught.

Use a review task contract

This reusable instruction is illustrative; adapt it to the host and repository before using it:

TEXT
Goal: Review the pinned base..head diff for correctness, security, data loss,
compatibility, and missing tests that matter to changed behavior.
​
Inputs: commit IDs, changed paths, relevant surrounding code, applicable
repository rules, and actual test output.
​
Tools: read repository and test output only. Deadline: 5 minutes.
​
For each finding: give priority, path and line, a concrete failing scenario,
evidence, impact, and a focused fix. Do not report style issues handled by CI.
Do not claim tests ran unless their output was provided or you ran them.
If no supported findings remain, return an empty findings list and note
which checks were not performed. Do not approve or merge the change.

The host should cap file reads and tool calls, cancel stale reviews, and start a new review when the head commit changes. Deduplicate findings by code location and failure mode. When publishing comments through a code host, make the human-visible comment traceable to its source diff; avoid posting broad, speculative comments on every line.

Evaluate before enabling on every pull request

Create a small evaluation set from your own past defects with sanitized code. Include known failures, correct alternatives, and unrelated changes. Measure precision (how many flagged findings are valid), recall on known defects, severity agreement, line accuracy, review time, token/cost use, and private-data exposure. A high finding count is not a quality measure.

Use a staged rollout: local dry runs, draft comments for maintainers, then automated comments only after the false-positive rate is acceptable. Keep human approval and existing branch protections. If a review agent needs to edit code, run that as a separate task with its own patch ownership and verification; reviewing and repairing the same diff in one opaque step makes failures hard to audit.

Subagents can help when the change truly has independent surfaces, such as a database migration and an iOS client. Give each a bounded path and concern, then have the parent deduplicate and verify findings. More workers add context and reconciliation cost; measure whether they improve review outcomes. See AI Agents, Plugins, and Subagents.

Practice: reject a weak finding

Prompt: A reviewer says, “This endpoint may have a race condition,” with no line, scenario, or evidence. What should the review system do?

Answer

Ask for a concrete interleaving and the code path that permits it, then verify against surrounding code and tests. If the reviewer cannot establish a plausible failure, omit the finding or record an explicit question. A severity label cannot substitute for evidence.

Quick Recap

  • Pin the reviewed commit pair and provide only relevant context.
  • Require concrete, line-linked findings with impact and repair guidance.
  • Keep repository rules few, scoped, and tested with counterexamples.
  • Use AI review alongside deterministic CI and human approval.

Key Takeaways

A trustworthy review agent is measured by useful, correct findings and restraint. Its job is to make a human reviewer faster and better informed, while preserving the team's existing merge controls.