Skip to main content

Anatomy of AI Code Review Workflows

Beyond /review: gates, subagents, hooks, and PR-quality system.

Code Review

Beyond /review โ€” the full PR-quality system: gates, subagents, hooks, and metrics. AI + automation + humans, layered to catch issues early, enforce quality, and ship with confidence.


TL;DRโ€‹

  • A great review isn't a single /review call โ€” it's a layered quality system.
  • Six layers: Pre-PR ยท CI ยท AI Static Analysis ยท Human Review ยท Merge Guardrails ยท Post-Merge.
  • Use subagents in parallel (@code-reviewer, @security-auditor, @test-writer, @docs-writer) and merge their outputs into one consolidated review.
  • Hooks shift left: pre-commit, pre-push, pre-merge โ€” each one is a cheap insurance policy.
  • Measure what matters: cycle time, acceptance rate, defect escape rate, AI suggestion adoption.

Why this mattersโ€‹

A PR is the last cheap moment to catch a defect. Once it's in main, every bug costs 10โ€“100ร— more. The job of an AI code review workflow isn't to replace humans โ€” it's to make every gate fast, consistent, and unmissable so humans spend their time on judgment, not nits.

Bottom line: Quality is never an accident. It's the result of intelligent effort โ€” codified once, automated forever.


The Big Pictureโ€‹

Think in layers: Prevent โ†’ Detect โ†’ Explain โ†’ Validate โ†’ Approve โ†’ Learn.

๐Ÿ’ก Tip: Shift left as much as possible. Catch issues before human review.


Quality Gates Overviewโ€‹

GateWhenWho/WhatPurposeFail action
๐Ÿ’ป Pre-PR (Local)Before pushHooks + local scriptsCatch issues fast (lint, test, secrets)Block commit / push
๐Ÿ› ๏ธ CI / BuildOn PR pushCI pipelineBuild, test, typecheck, security scansBlock PR
๐Ÿค– AI Static AnalysisOn PRAI subagentsDeep code analysis, patterns, risksAdd review comments
๐Ÿ‘ฅ Human ReviewOn PRPeer reviewerContext, intent, design, trade-offsRequest changes
๐Ÿ”’ Merge GuardrailsPre-mergeChecks + HooksFinal sanity before mergeBlock merge
๐Ÿš€ Post-MergeAfter mergeCI + monitoringDeploy, smoke tests, alertingRollback / alert

๐Ÿ’ก Tip: Each gate has a specific job. Don't overload a single gate.


  • โœ… Pre-PR (Local) โ€” Pre-commit hooks: lint, format, tests, secret scan
  • โœ… CI / Build โ€” Typecheck, unit tests, coverage, build
  • โœ… AI Static Analysis โ€” @code-reviewer + @security-auditor
  • โœ… Human Review โ€” Context, product fit, design, readability
  • โœ… Merge Guardrails โ€” Status checks, approvals, branch protection
  • โœ… Post-Merge โ€” Deploy checks, smoke tests, alerting

๐Ÿ’ก Tip: Configure branch protection to enforce the gates โ€” not just describe them.


End-to-End PR Workflow (detailed)โ€‹

#StepKey actionsOutput
1Developer creates branchgit checkout -b feat/awesome; small, intent-clear commitsClean commits
2Pre-PR (local) โ€” shift leftPre-commit hooks: lint, format, tests, secrets; fix locallyClean local state
3Push & open PRPush branch; open PR with good title & description; add screenshots if neededPR opened with context
4CI / automated gatesBuild, typecheck, unit tests, coverage, security scans (SAST, deps); optional: e2e, perf, accessibilityGreen/Red status checks
5AI analysis (subagents)@code-reviewer (quality, bugs, improvements), @security-auditor (vulns, secrets), @test-writer (missing tests, edge cases), @docs-writer (doc changes)AI review comments
6Human reviewPeer reviews context & intent, design, trade-offs, readability; iterates with authorApproved or changes requested
7Merge guardrailsBranch protection: required checks, approvals (1โ€“2 reviewers), no stale branchesReady to merge
8Post-mergeDeploy / preview checks; smoke & integration tests; monitor and alert on failuresHealthy production

๐Ÿ’ก Tip: Automate what you can. Escalate to humans where consolidated judgment is needed.


Subagents in the Code Review Workflowโ€‹

Run subagents in parallel. Merge their outputs into one consolidated review.

SubagentFocusKey toolsOutput
๐Ÿค– @code-reviewerCode quality, bugs & edge cases, readability, performanceRead, Grep, BashReview report
๐Ÿ›ก๏ธ @security-auditorVulnerabilities, secrets, auth & access, dependenciesRead, Bash, WebRisk report
๐Ÿงช @test-writerMissing tests, edge cases, coverage gaps, test qualityRead, BashTests + plan
๐Ÿ“š @docs-writerAPI changes, README updates, changelogs, examplesRead, EditDocs diff

Example fan-outโ€‹

๐Ÿ’ก Tip: Each agent has a clear scope. Keep prompts focused; merge outputs into one PR comment to reduce noise.


Hooks That Power the Flowsโ€‹

EventRunsExample useFail action
Pre-commitBefore commitLint, format, tests, secret scanBlock commit
Pre-pushBefore pushRun fast tests, typecheckBlock push
Post-pushAfter pushNotify PR, run extra checksNon-blocking
Pre-PROn PR openLabel PR, size check, template checkAdd comment
Post-PROn PR updateRun AI agents, update reviewAdd comment
Pre-mergeBefore mergeFinal checks, approve gatesBlock merge
Post-mergeAfter mergeDeploy, smoke tests, monitoringAlert / rollback

๐Ÿ’ก Tip: Keep hooks fast (< 5s) and idempotent.

Sample .claude/settings.json hook blockโ€‹

{
"hooks": {
"PreToolUse": [
{ "matcher": "*", "hooks": [{ "type": "command", "command": "./scripts/pre-tool.sh" }] }
],
"PostToolUse": [
{ "matcher": "Write|Edit",
"hooks": [
{ "type": "command", "command": "npm run lint" },
{ "type": "command", "command": "npm test --silent" }
] }
],
"PreCommit": [
{ "matcher": "*",
"hooks": [
{ "type": "command", "command": "./scripts/secret-scan.sh" },
{ "type": "command", "command": "./scripts/format-check.sh" }
] }
]
}
}

Code Review Report Templateโ€‹

A consistent format = faster reviews + better decisions.

## Summary
Short summary of the PR.

## Risks & Issues

### ๐Ÿ”ด Critical
- SQL injection risk in `src/users.ts:42`

### ๐ŸŸก Medium
- Missing null check in `src/auth.ts:91`

### ๐ŸŸข Low
- Unused variable in `src/utils.ts:23`

## Missing Tests
- Login with invalid credentials
- Rate limiting on /login

## Suggestions (file:line)
- Parameterize queries in `src/users.ts:42`
- Add guard clause for null user in `src/auth.ts:91`
- Remove unused variable in `src/utils.ts:23`

Review checklist (used by every reviewer)โ€‹

  • โœ… Security & secrets
  • โœ… Correctness
  • โœ… Edge cases
  • โœ… Performance
  • โœ… Tests & coverage
  • โœ… Docs & changelog
  • โœ… Readability
  • โœ… Conventions

Decisionโ€‹

  • โœ… Approve
  • โœ๏ธ Request changes
  • โ›” Block

๐Ÿ’ก Tip: Codify the format inside agents/code-reviewer.yml so every AI review looks the same.


Branch Protection & Required Checksโ€‹

.github/branch-protection.yml:

main:
required_status_checks:
strict: true
contexts:
- build
- test
- lint
- code-reviewer
required_pull_request_reviews:
required_approving_review_count: 1
require_code_owner_reviews: true
enforce_admins: true
restrictions: {}

Checklist:

  • โœ… All status checks green
  • โœ… At least 1 approval
  • โœ… No stale reviews
  • โœ… No force push
  • โœ… Linear history
  • โœ… Signed commits (optional)

๐Ÿ’ก Tip: Automate enforcement. Don't rely on memory.


PR Template (starter)โ€‹

.github/pull_request_template.md:

## Summary
What and why?
-

## Testing
- [ ] Unit tests
- [ ] Integration tests
- [ ] Manual tests

## Screenshots / Demos (if UI)

## Checklist
- [ ] Code reviewed
- [ ] Tests added/updated
- [ ] Docs updated (if needed)
- [ ] Changelog updated
- [ ] No secrets

## Related Issue
Closes #123

๐Ÿ’ก Tip: A good template = fewer back-and-forths.


Metrics That Matterโ€‹

Track these weekly to optimize the system, not just individuals.

MetricTargetWhy
โฑ๏ธ PR cycle time< 24 hSpeed of delivery
โœ… PR acceptance rate> 85%Quality of work
๐Ÿ›ก๏ธ Defect escape rate< 5%Real-world quality
๐Ÿ—ฃ๏ธ Review turnaround< 6 hTeam responsiveness
๐Ÿค– AI suggestion adoption> 60%AI effectiveness
๐Ÿ” Rework rate< 15%Process health
๐Ÿ“ˆ Coverageโ‰ฅ 80%Test confidence

๐Ÿ’ก Tip: Review metrics weekly. Optimize the system, not just the people.


Step-by-Step Setupโ€‹

1. Set up branch protectionโ€‹

Apply .github/branch-protection.yml (above) to main and release/*.

2. Ship the AI review subagentsโ€‹

  • agents/code-reviewer.yml
  • agents/security-auditor.yml
  • agents/test-writer.yml
  • agents/docs-writer.yml

3. Add a /review slash command (fan-out)โ€‹

.claude/commands/review.md:

---
description: Full PR review โ€” fan-out to all reviewer subagents
usage: /review [--base <branch>] [--scope <path>]
arguments:
- name: base
default: main
permissions:
allow: ["Read", "Bash(git *)", "Bash(npm test)", "WebSearch"]
deny: ["Write", "Bash(rm -rf *)"]
---

# Instructions
Run the following subagents in parallel against the diff vs `{{base}}`:
1. `@code-reviewer` โ€” quality, correctness, edge cases
2. `@security-auditor` โ€” vulnerabilities, secrets
3. `@test-writer` โ€” missing tests, coverage gaps
4. `@docs-writer` โ€” docs & changelog impact

Merge their outputs into the Code Review Report Template:
- Summary
- Risks & Issues (Critical / Medium / Low)
- Missing Tests
- Suggestions (file:line)
- Decision

4. Configure pre-PR (local) hooksโ€‹

hooks/pre-commit.sh:

#!/usr/bin/env bash
set -e
./scripts/secret-scan.sh
npm run lint -- --quiet
npx prettier --check .
npm test --silent

Wire it into .claude/settings.json and package.json prepare.

5. Add CI gatesโ€‹

.github/workflows/ci.yml (sketch):

name: CI
on: [pull_request]
jobs:
build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run lint
- run: npm run typecheck
- run: npm test -- --coverage
- uses: github/codeql-action/analyze@v3

6. Add a GitHub Action that runs the AI fan-out on PRsโ€‹

.github/workflows/ai-review.yml (sketch):

name: AI Review
on: [pull_request]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: claude /review --base origin/${{ github.base_ref }}
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}

7. Monitor & alertโ€‹

Send post-merge metrics + failures to your existing dashboards (Datadog, Grafana, Slack).

8. Review weeklyโ€‹

Hold a 30-min "PR-quality retro" each week. Improve one thing.


Variants for Different Team Settingsโ€‹

๐Ÿ‘ค Solo developerโ€‹

  • Pre-commit + /review only.
  • Skip multi-reviewer requirement.
  • Auto-approve docs-only PRs.

๐Ÿ‘ฅ Small team (2โ€“10)โ€‹

  • Required: 1 human reviewer + AI fan-out.
  • Pre-commit + CI + /review PR comment.
  • Weekly PR-quality retro.

๐Ÿข Enterprise / regulatedโ€‹

  • Required: 2 reviewers (1 owner), AI fan-out, security scans, compliance check.
  • Mandatory: signed commits, SBOM, change ticket linkage.
  • Post-merge: canary, smoke tests, audit log.
  • Block on any ๐Ÿ”ด critical AI finding until acknowledged.

๐ŸŒ Open-source / communityโ€‹

  • Maintainers' bot runs /review automatically on new PRs.
  • Provide a contributor guide with PR template + DoD.
  • Triage label workflow + AI suggestion to assign reviewer.

Bottom line: Same gates, escalating strictness.


Common Patterns & Recipesโ€‹

Pattern 1 โ€” Parallel fan-out, single consolidated commentโ€‹

Pattern 2 โ€” Risk-tiered routingโ€‹

If diff_lines > 500 OR touches src/auth/** :
add reviewer @security-team
require 2 approvals

Pattern 3 โ€” Auto-fix small nitsโ€‹

"PostToolUse": [
{ "matcher": "Write|Edit",
"hooks": [{ "type": "command", "command": "npx prettier --write ." }] }
]

Pattern 4 โ€” Severity-based blockโ€‹

  • ๐Ÿ”ด Critical โ†’ block merge until acknowledged
  • ๐ŸŸก Medium โ†’ require explanation
  • ๐ŸŸข Low โ†’ suggestion only

Pattern 5 โ€” Self-review pre-pushโ€‹

Before pushing, /review --base main on YOUR local branch.
Fix what AI flags. Then push.

Pattern 6 โ€” Reviewer "explainer" modeโ€‹

For files you don't own, ask /explain <file> first.
Then review with context.

Pattern 7 โ€” Post-merge regression watchโ€‹

# scripts/post-merge.sh
npm run smoke || ./scripts/notify-slack.sh "๐Ÿ”ด Post-merge smoke failed"

Best Practices โœ…โ€‹

  • ๐Ÿ›ก๏ธ Shift left โ€” prevent issues at commit, not at merge.
  • โšก Automate everything that has a clear rule.
  • ๐Ÿค Keep humans for judgment โ€” design, trade-offs, intent.
  • ๐Ÿค– Run AI subagents in parallel โ€” and merge into one comment.
  • ๐Ÿ“ Standard report format โ€” same shape every time.
  • โฑ๏ธ Keep hooks fast (< 5s).
  • ๐Ÿ“ˆ Measure & iterate weekly.
  • ๐ŸŽฏ Optimize the system, not the individual.

Anti-Patterns to Avoid โŒโ€‹

  • ๐Ÿšซ Huge PRs (> 500 lines).
  • ๐Ÿšซ No tests / low coverage.
  • ๐Ÿšซ Skipping code review.
  • ๐Ÿšซ AI-only approval for risky changes.
  • ๐Ÿšซ No context in PR (bad title, no description).
  • ๐Ÿšซ Vague commit messages ("fix stuff").
  • ๐Ÿšซ Ignoring CI failures.
  • ๐Ÿšซ Secrets in code or logs.

โš ๏ธ Bad habits here cost real money and real trust.


Troubleshootingโ€‹

SymptomCauseFix
Reviews take daysNo SLA, no rotationSet a 24-h review SLA + reviewer rotation
AI comments are noiseVague subagent promptsTighten system_prompt + few-shots
Critical bugs slip throughNo @security-auditorAdd it to the fan-out
Reviewers fatigueHuge PRsCap PR size; require small batches
Same nits every PRNot auto-fixingAdd auto-format hook on save
Tests skippedNo coverage gateBlock merge below threshold
Post-merge failuresNo smoke testsAdd post-merge smoke + alert
Metrics flatNot reviewing weeklyAdd 30-min PR-quality retro

Quick-Start Checklistโ€‹

  • โœ… Branch protection on main
  • โœ… Pre-commit hooks (lint, format, tests, secret scan)
  • โœ… CI: typecheck, tests, coverage, security
  • โœ… /review fan-out (4 subagents)
  • โœ… Standard PR + review report templates
  • โœ… Weekly PR-quality retro
  • โœ… Dashboards for the 7 metrics

๐Ÿง  Mantra: Prevent early ยท Automate smartly ยท Augment humans ยท Measure everything.


Starter Configsโ€‹

agents/code-reviewer.ymlโ€‹

name: code-reviewer
description: Reviews PRs for quality, correctness, and readability.
model: claude-sonnet-4
tools: [Read, Bash, Grep]
permissions:
allow: ["Read", "Bash(npm test)"]
deny: ["WriteOutsideWorkspace"]
system_prompt: |
You are a strict senior reviewer. Follow CLAUDE.md.
Output the Code Review Report Template exactly.
Be concrete: every issue has a file:line and a fix.
triggers: ["review", "pr", "audit", "quality"]
timeout_ms: 120000
max_tokens: 8000

agents/security-auditor.ymlโ€‹

name: security-auditor
description: Finds vulnerabilities, secrets, auth/access issues.
model: claude-sonnet-4
tools: [Read, Bash, WebSearch]
permissions:
allow: ["Read", "WebSearch"]
deny: ["Write", "Bash(rm -rf *)"]
system_prompt: |
You are a security engineer. Focus on:
- OWASP Top 10
- Secrets in code/logs
- Authn/Authz issues
- Risky dependencies
Output severity (Critical/Medium/Low) + file:line + suggested fix.
triggers: ["security", "vulnerability", "secret", "audit"]

agents/test-writer.ymlโ€‹

name: test-writer
description: Finds missing tests and writes them.
model: claude-sonnet-4
tools: [Read, Bash, Edit]
permissions: { allow: ["Read", "Edit", "Bash(npm test)"] }
system_prompt: |
Identify missing tests and edge cases.
Propose specific tests (Given/When/Then).
Where useful, draft them inline.
triggers: ["test", "coverage", "missing tests"]

agents/docs-writer.ymlโ€‹

name: docs-writer
description: Suggests docs/changelog updates required by this PR.
model: claude-haiku-4
tools: [Read, Edit]
permissions: { allow: ["Read", "Edit"] }
system_prompt: |
Identify API or behavior changes that need:
- README updates
- API doc updates
- Changelog entries
Output a docs diff plan.
triggers: ["docs", "readme", "changelog"]

Glossaryโ€‹

  • Gate โ€” A check that must pass before code can progress.
  • Shift left โ€” Move quality checks earlier in the dev process.
  • Branch protection โ€” Repo rules that prevent unsafe merges.
  • SAST โ€” Static Application Security Testing.
  • SBOM โ€” Software Bill of Materials.
  • Defect escape rate โ€” % of bugs that reach production despite reviews.
  • AI suggestion adoption โ€” % of AI proposals accepted by reviewers.
  • Cycle time โ€” Time from PR open โ†’ merge.
  • Severity tiers โ€” Critical / Medium / Low classification.

Further Readingโ€‹

  • Search: "GitHub branch protection best practices"
  • Search: "code review metrics DORA"
  • Search: "OWASP Top 10 2025"
  • Search: "AI-assisted code review evidence research"

๐Ÿช™ Golden Rulesโ€‹

  • ๐Ÿ›ก๏ธ Small PRs.
  • ๐Ÿงน Clean main.
  • ๐Ÿงช Tests first.
  • ๐Ÿ”’ No secrets.
  • ๐Ÿš€ Ship safe.

Remember: "Quality is never an accident. It is always the result of intelligent effort." โ€” John Ruskin