How to Create Custom Code Review Rules for Codex with AGENTS.md: Complete Repository Governance Playbook

Introduction
This playbook is a practical guide for repository owners and platform teams who want to define, enforce, and evolve custom code review rules for Codex using AGENTS.md. It assumes teams run automated reviews with annotations such as @codex review and use the automatic review workflow that surfaces prioritized findings (P0/P1). The goal is not to describe every Codex API surface; instead, this document shows governance patterns that translate automated findings into repeatable rules, branch protection gates, reviewer assignment, and remediation workflows that scale across monorepos and modular codebases.
Successful repository governance treats code review rules as living policy: rules must be auditable, scoped to the right code areas, resilient to false positives, and able to escalate critical findings to an enforced, human-reviewed state. This first section frames the governance outcomes you should expect, summarizes the playbook structure, and explains how AGENTS.md files at the repository root and in nested directories inherit and specialize review rules. The inheritance model is foundational: it enables a single source of baseline policy while permitting directory-level teams to tighten rules where risk is highest.
Table of Contents for the Playbook
- Section 1: Introduction, governance outcome, and AGENTS.md inheritance model (this section)
- Section 2: Designing rule sets and severity mappings; branch protection and reviewer ownership
- Section 3: Reducing false positives and iterative rule tuning; monitoring and metrics
- Section 4: Auditability, change management, and remediation workflows including automated fixes
Within How to Create Custom Code Review Rules for Codex with AGENTS.md: Complete Repository Governance Playbook, the Rule Tuning and False Positives decision connects directly to The Codex Guardian Auto-Review Playbook — 10 Prompts for Automated Code Review, PR Feedback, and Quality Gates. That linked article specifically examines the Codex Guardian Auto-Review Playbook — 10 Prompts for Automated Code Review, PR Feedback, and Quality Gates, giving teams concrete background for applying the present article’s Rule Tuning and False Positives recommendations without duplicating this workflow’s scope.
Governance Outcome: What This Playbook Delivers
Adopting the patterns below should produce measurable governance outcomes that are visible across the development lifecycle. The outcomes focus on risk control, developer productivity, and organizational repeatability rather than on tool-specific implementation details.
- Consistent baseline enforcement: A repository root AGENTS.md establishes minimum mandatory checks and severity mappings so every change receives consistent baseline scrutiny regardless of which team owns a subdirectory.
- Scoped specialization: Nested AGENTS.md files allow teams to strengthen rules for sensitive areas (for example, authentication, cryptography, or infra provisioning) without creating global friction in lower-risk code paths.
- Clear reviewer ownership: Rules are mapped to owner teams or individuals so P0/P1 findings automatically route to the correct reviewers and avoid ambiguous escalation paths.
- Branch protection and gating: Critical findings (P0) become blocking conditions on protected branches while P1 findings raise high-priority issues that require timely remediation but do not necessarily block merges.
- Reduced noise and higher signal: Rule scopes, allowlists, and targeted exclusions reduce false positives; planned rule tuning prevents unattended noise growth.
- Auditable decisions and change history: Changes to AGENTS.md are reviewed like code, versioned on the default branch, and linked to review findings so auditors can trace why a rule was added or relaxed.
- Automated remediation with controlled guardrails: Use of automated helpers (for example an instruction such as @codex fix the P1 issue) is permitted for low-risk fixes, while human signoff is enforced for P0-level remediation.
How Root and Nested AGENTS.md Files Inherit and Specialize Rules
AGENTS.md functions as a declarative place to document and persist the repository’s review rules. The most common and practical deployment pattern is a repository-level (root) AGENTS.md that defines the baseline policy and nested AGENTS.md files in subdirectories that refine that baseline. The following paragraphs describe the recommended inheritance semantics, typical merging strategies, and governance guardrails to prevent accidental weakening of critical rules.
Inheritance semantics (conceptual)
Treat the root AGENTS.md as the canonical baseline. It should define mandatory checks, severity mappings (for example which classes of findings are P0 vs P1), and organization-wide constraints that cannot be relaxed by lower-level policies. Nested AGENTS.md files should be allowed to do three things:
- Declare additional rules that apply only to their directory tree.
- Tighten severity for specific rule classes within their scope (for example raising certain security checks from P1 to P0 for a crypto module).
- Introduce scoped allowlists or exceptions for generated code and third-party vendored directories, documented with rationale and a planned expiration where appropriate.
In practical terms, inheritance should behave like a union of rules with clear precedence: when a nested file defines a rule identical to a root-level rule, the nested definition takes precedence for files under that directory. When a nested file omits a rule, the root-level rule continues to apply. Governance must prevent nested files from silently disabling root-level P0 protections.
Precedence and merge model
| Precedence | Scope | Effect |
|---|---|---|
| 1 — File-level/nested AGENTS.md | Directory tree where AGENTS.md sits | Overrides or extends root rules for files in that tree |
| 2 — Root AGENTS.md | Entire repository default | Baseline rules applied unless overridden by nested files |
| 3 — Organization-level policy (if available) | Multiple repositories | Should set minimum constraints that repositories cannot lower |
Operationally, implement a deterministic merge: start from the root file, then walk up the directory path from the file to the repository root, applying nested overrides as you encounter AGENTS.md files. Avoid ambiguous merge strategies; explicit override semantics are easier to reason about during audit.
Practical patterns for specialization
Below are common patterns teams use to specialize rules in nested directories. Use them as templates rather than prescriptive schemas; the exact text structure of AGENTS.md may vary between environments.
- Hardening sensitive modules: A nested AGENTS.md raises severity for a misconfiguration or insecure API usage category from P1 to P0, ensuring branch protection blocks merges until a human review resolves the issue.
- Exempting generated code: A nested file explicitly marks a generated directory as exempt from stylistic or complexity checks while still surfacing security concerns. Exemptions must include a justification and a maintainer-signed approval.
- Per-package reviewer routing: Directory-level AGENTS.md maps rule categories to specific maintainer teams or individuals so critical P0 findings are routed to the right on-call or gatekeeper.
- Temporary experimental rules: Nested AGENTS.md can enable a trial rule for a limited scope with a stated sunset date and required monitoring, enabling incremental rollouts without repository-wide disruption.
Example (illustrative) AGENTS.md fragments
## Code Review Rules
# Root-level rules: baseline checks for the repository
- require-security-analysis: true
- severity-mapping:
insecure-credentials -> P0
weak-crypto -> P0
missing-logging -> P1
- reviewers-on-p0: ["team-security"]
# Nested directory example: /auth-service/AGENTS.md
## Code Review Rules
# Augment root rules within /auth-service
- inherit: true
- tighten:
weak-crypto -> P0
- allowlist:
- path: vendor/
reason: third-party vendored libs; periodic audit required
The snippet is conceptual: use a structured, human-readable “Code Review Rules” section in each AGENTS.md and keep rationale and owner information alongside each rule. Store AGENTS.md in the same repository so changes are tracked like code.
Governance guardrails to prevent policy weakening
Because nested AGENTS.md can relax rules, governance must enforce guardrails:
- Prohibit nested files from lowering the severity of root-level P0 rules; treat P0 as non-overridable by default.
- Require that any exception or allowlist entry includes a justification, expiration, and an owning person or team.
- Subject AGENTS.md changes to a review process that includes security and platform reviewers for policy-relevant edits.
- Maintain a changelog or commit message convention that references the governing ticket for auditability.
Operational checklist for initial rollout
Before deploying nested specialization broadly, complete this checklist to make adoption predictable and auditable:
- Publish a root AGENTS.md with baseline rules and owner contacts on the default branch.
- Map directories to owner teams using CODEOWNERS or an equivalent so reviewer routing is automatic.
- Document non-overridable rules (especially P0) and the process to request exceptions.
- Enable branch protection mappings that translate P0 findings into required status checks and required reviewers.
- Announce the rollout and run a pilot in a small set of directories to validate the inheritance behavior and false-positive rate.
Adopt a cadence for reviewing AGENTS.md files regularly: a periodic audit prevents drift between the documented policy and actual team practices.
Designing high-signal P0/P1 rules
P0 and P1 findings should represent the small fraction of changes that require immediate human attention. High-signal rules minimize noisy alerts so on-call engineers and reviewers can treat each finding as actionable: they contain deterministic evidence, point to the exact offending diff or file, identify an owner, and include a clear remediation path. Aim for rules that are narrow in scope, reproducible across runs, and easy to verify by a reviewer without additional investigation.
Practical rule design balances sensitivity and specificity: sensitivity ensures you catch critical regressions, specificity minimizes false positives. Design rules to report only when the change materially increases risk or violates explicit policy. For example, a P0 rule should not fire on a comment change; it should fire when a secret is actually added to a committed configuration file, when a runtime flag disables an authentication check, or when infrastructure code tightens network exposure for production services.
The “## Code Review Rules” section in AGENTS.md
Use the repository’s AGENTS.md to declare centralized, human-readable rules under a dedicated header titled “## Code Review Rules”. Each rule entry should include a stable rule identifier, severity level, a plain-language rationale, narrow file/path scope, the concrete condition that triggers the finding, required owner/team, recommended remediation steps, and an enforcement action (audit-only, required fix, CI-failure). Keep the content concise so reviewers and audit logs can display the text verbatim.
## Code Review Rules
- id: backend-secrets-in-committed-config
severity: P0
title: "Committed secret in configuration file"
rationale: "Committed secrets in repository configurations lead to credential leakage if not rotated."
scope:
paths:
- "services/*/config/**"
- "infra/**/secrets.yml"
condition:
- file_contains_regex: "(?i)access_key|secret|private_key"
- diff_adds: true
owners:
- team: platform-security
enforcement: require-fix
remediation: "Remove the secret from code, move it to a secrets manager, rotate keys, and reference the secret by identifier."
That example shows the minimum fields that drive downstream automation and human workflows. The AGENTS.md format should be consistent across root and nested files so the agent can merge rules predictably.
Rule examples and patterns by domain
Below are illustrative patterns for backend, frontend, data, infrastructure, and security rules. Treat these as templates for specifying deterministic evidence and scoped matches rather than as prescriptive syntax for any tooling API.
-
Backend (P0 / P1)
- Rule: Disabling authentication checks in a request handler. Evidence: diff removes or bypasses authentication middleware, or conditionally returns user context without authentication. Owner: backend-service owners. Enforcement: P0, require-fix.
- Rule: Database credential hard-coded in source. Evidence: new file additions or modifications matching credential patterns inside application code. Owner: database-owners. Enforcement: P0/P1 depending on environment.
-
Frontend
- Rule: Insecure inclusion of third-party scripts on production pages. Evidence: changes to HTML/templates adding external
- Rule: Insecure inclusion of third-party scripts on production pages. Evidence: changes to HTML/templates adding external
