Your team writes more code than it did a year ago. For more and more teams, most of it is drafted by agents, and every line of it still has to be correct, consistent and readable by the next person. Review does not scale with that. If a human has to catch every force unwrap, every stray print, every view model that grew a network call, the reviewer becomes the bottleneck, and then the reviewer starts skimming.
The way out is not “review harder”. It is to make sure that by the time a human opens a pull request, everything a machine could have caught is already caught. Everything that catches a mistake before a human does is a guardrail. Stack them in the right order and you get a pyramid. The examples are Swift; the layers are the same in any stack.
🔺 The pyramid
You know the testing pyramid: many fast unit tests at the bottom, a few slow UI tests at the top. Guardrails work the same way. The bottom layers are cheap, fast and deterministic, and they should carry most of the weight. The top layers need judgment, cost attention and can miss, so they should only see what is left.
Solid layers are deterministic: they behave the same way every time, for a person and for an agent. Dashed layers depend on someone, or something, paying attention; hooks are the exception.
One rule runs the whole thing: push every rule as low as it will go. A lower layer is cheaper, faster and certain. A rule in the compiler cannot be forgotten. A rule in a skill can be skipped on a bad day. A rule that only lives in a reviewer’s head gets enforced when that reviewer is awake.
This mattered before AI. It matters much more now, for three reasons.
- Agents produce volume. Several small PRs a day per engineer is common once background agents run. The top of the pyramid cannot grow with that; the bottom can.
- Tool output is the best prompt an agent gets.
force_unwrapping violation at ArticleView.swift:42is a precise, unambiguous instruction. The agent reads it and fixes it, without a human in the loop. - Deterministic beats persuasive. “Never force unwrap” in an instruction file is a request. A lint rule set to error is a wall.
Here is each layer, bottom up, with what belongs there and a snippet you can copy.
🧱 1. Compiler and types
The base, and the strongest layer you have: if the mistake does not compile, nobody has to remember anything. Whenever a review comment can be turned into a type, turn it into a type.
Agents rarely make typos. What they do make is a plausible change to one code path that forgets the others. A type that makes the wrong state impossible catches exactly that.
// Before: three properties that can contradict each other.
// An agent adds a retry, sets isLoading = true and forgets to clear error:
// the screen shows a spinner on top of an error message.
@Observable final class ArticleListViewModel {
var isLoading = false
var articles: [Article] = []
var error: Error?
}
// After: the screen is in exactly one state, and the view has to handle each one
enum ArticleListState {
case loading
case loaded([Article])
case failed(Error)
}
@Observable final class ArticleListViewModel {
var state: ArticleListState = .loading
}
The same idea works over and over:
- A type for each ID, not a bare
String. The agent cannot pass a user ID where an article ID belongs. - An enum, not a string. The set of values is closed, and a new one shows up in the diff.
- A non-optional property, not a nil check at every call site.
private, not a comment saying “don’t call this”.- An exhaustive
switchwithoutdefault. A new case breaks the build everywhere it needs handling.
Then make the compiler strict. Agents are very good at producing code that builds with seven warnings, and a warning nobody is forced to fix is just noise. Every stack has the same two switches:
| Treat warnings as errors | Stricter checks | |
|---|---|---|
| Swift | SWIFT_TREAT_WARNINGS_AS_ERRORS = YES | Swift 6 language mode: data races become compile errors |
| Kotlin | allWarningsAsErrors = true | Explicit API mode for shared modules |
| TypeScript | --max-warnings 0 in ESLint | "strict": true, noUncheckedIndexedAccess |
🧹 2. Formatter and linter
Everything about how code looks, and every pattern a tool can recognize, lives here. No human should ever type “please sort the imports” into a review box again.
SwiftFormat fixes layout automatically. Commit the config, so every machine and every agent formats the same way:
# .swiftformat
--swiftversion 6.0
--indent 4
--maxwidth 120
--self remove
--importgrouping testable-last
--enable isEmpty
SwiftLint catches patterns. Start with what agents keep doing: a debug print left behind, a force unwrap because it was the shortest way to make it compile, UserDefaults.standard because the agent did not know about your wrapper. Opt-in rules cover the generic ones, custom regex rules cover your own conventions:
# .swiftlint.yml
opt_in_rules:
- force_unwrapping
- empty_count
- first_where
custom_rules:
no_print:
name: "No print"
regex: '\bprint\('
message: "Use Logger instead of print."
severity: error
no_direct_user_defaults:
name: "No direct UserDefaults"
regex: 'UserDefaults\.standard'
message: "Go through SettingsStore, not UserDefaults directly."
severity: error
excluded: "Sources/Storage/.*"
Run it with swiftlint --strict in CI, so warnings fail too. A rule that only warns is a rule that gets ignored, by people and by agents.
Unused code needs a check of its own, and it earns its place with agents in particular: an agent tries one approach, abandons it, and leaves the helper behind. SwiftLint can catch that too, with analyzer rules that read the compiler log:
# .swiftlint.yml
analyzer_rules:
- unused_declaration
- unused_import
Run swiftlint analyze --strict --compiler-log-path build.log in CI, after the build. It catches those leftovers before they become someone else’s “is this used anywhere?”.
Not on iOS? The same three jobs exist in every stack:
| Formatter | Linter | Unused code | |
|---|---|---|---|
| Swift | SwiftFormat | SwiftLint | swiftlint analyze |
| Kotlin | ktfmt or ktlint | detekt, Android Lint | detekt, Android Lint |
| TypeScript | Prettier | ESLint | knip |
When you switch on a new rule, the existing codebase will light up with hundreds of violations. Do not fix them in one giant PR. Record a baseline so only new violations fail, and put the old ones in the bucket for the 1% migrator to clean up a module at a time.
🚦 3. CI and Danger
Some rules are not about a single file. They are about the pull request as a whole: its size, its description, which files changed together. A linter cannot see those. Danger can. It also comes as Danger JS and danger-kotlin, so the checks below translate line by line.
// Dangerfile.swift
import Danger
import Foundation
let danger = Danger()
let pr = danger.github.pullRequest
let changed = danger.git.modifiedFiles + danger.git.createdFiles
// A PR an agent opened still has to be small enough to read.
if (pr.additions ?? 0) + (pr.deletions ?? 0) > 400 {
fail("Over 400 changed lines. Split it so a reviewer can actually read it.")
}
// Every PR links its ticket (Jira-style keys; adjust to your tracker).
if pr.body?.range(of: #"[A-Z]+-\d+"#, options: .regularExpression) == nil {
fail("Add the ticket link to the PR description.")
}
// Source changed, tests did not. Adjust the paths.
let touchedSources = changed.contains { $0.hasPrefix("Sources/") }
let touchedTests = changed.contains { $0.hasPrefix("Tests/") }
if touchedSources && !touchedTests {
warn("Sources changed without a test change. Intentional?")
}
The size check is the important one in an agent world. A 2,000-line PR does not get reviewed, it gets approved. Danger makes “small” a rule instead of a hope.
🤖 4. Agent instructions: hooks, CLAUDE.md, skills
This is the first layer written for the agent rather than for the code, and it has three very different parts.
Hooks are deterministic, so start there. They do not ask the agent to remember the bottom of the pyramid; they run it inside the agent’s loop. Format every Swift file right after the agent edits it, and refuse to let it finish while SwiftLint complains:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "f=$(jq -r '.tool_input.file_path'); case \"$f\" in *.swift) swiftformat \"$f\" ;; esac"
}
]
}
],
"Stop": [
{
"hooks": [
{ "type": "command", "command": "jq -e '.stop_hook_active' >/dev/null && exit 0; swiftlint lint --strict --quiet 1>&2 || exit 2" }
]
}
]
}
}
That goes in .claude/settings.json, committed to the repo. On another stack, swap in your own formatter and linter; the shape stays the same. The agent now finds out about a lint error in seconds, on its own machine, instead of twenty minutes later in CI. The stop_hook_active check lets the agent stop on its second try, so a lint error it cannot fix does not trap it in a loop.
CLAUDE.md is a map, not a rulebook. Keep it short: what the project is, how to build and test, where things live, and that the checks must pass before it is done. Do not copy lint rules into it. Every rule you write in prose is one the agent can miss, and one more line pushing your real instructions out of its attention.
## Before you finish
Run `swiftformat .`, `swiftlint --strict` and the unit tests.
Fix everything they report. Do not disable a rule to make it pass.
Skills hold what only judgment can catch. Where business logic goes. Which existing service to reuse instead of writing a new one. How a screen in your app is put together. No tool can prove these, so a skill is the best you can do: make the agent reliably think of it.
---
name: feature-architecture
description: Use when adding or changing a screen, view model or service in a feature module.
---
- Views render state and forward user actions. Nothing else.
- View models own state and call services. No URLSession, no UserDefaults.
- Before adding a new service, search Sources/Services for an existing one.
- New screens get a view model test for every user action.
Skills are the softest layer the agent sees, so treat them like code. Review changes to them, and back each one with a small eval built from real PRs where the mistake happened. A skill without an eval is a hope.
The workflow is a guardrail too. Most of the layers above check the code. This one checks what the agent does before anyone else sees the code. It still belongs to this layer, because it is written for the agent. An agent that opens a pull request the moment the build is green hands its unfinished work to CI and to a reviewer. Write down what “ready for a PR” means, as a skill the agent runs every time:
---
name: create-pr
description: Use before opening a pull request.
---
1. Build and run the unit tests. Fix until everything is green.
2. Run the UI tests for every screen you touched.
3. Review your own changes (git diff main...) in a fresh subagent that did
not write the code: bugs, edge cases, missing tests, the rules from the
feature skills. Fix what it finds, or explain in the PR why not.
4. Open the PR as a draft: what changed, why, how it was tested,
and what was not tested.
The local review in step 3 is the cheapest review you will ever get. It runs before CI, before a reviewer is assigned, and it uses a fresh context on purpose: the agent that wrote the code is the worst one to judge it, because it already believes it is right.
Then push the most important step down a layer. A skill can be skipped, a hook cannot. This one refuses gh pr create while the unit tests fail:
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"timeout": 600,
"command": "jq -r '.tool_input.command' | grep -q 'gh pr create' || exit 0; make test-unit 1>&2 || exit 2"
}
]
}
]
Replace make test-unit with your own unit test command. Keep the slow UI suites in the skill and in CI. The hook is for what is fast enough to run every time.
👀 5. Review: AI first, human last
The top of the pyramid is the most expensive guardrail you have, and the one that gets tired.
So let an AI reviewer go first. In my experience it is one of the most valuable guardrails in the whole pyramid, because it is the only automated layer that reads the change the way a reviewer does: as a whole, against the ticket, with judgment.
An AI review bot runs on every pull request, in CI, and leaves inline comments like a colleague would. It is not the same as the local review from the create-pr skill. The local review is the author checking their own work. The PR review is independent: it runs on the final diff, it runs the same way for every PR, and it reviews the PRs humans write too.
What makes it useful rather than noisy:
- Give it the same context the agents have. CLAUDE.md, the feature skills, the architecture rules. A reviewer that does not know your conventions can only comment on generic things, and generic things belong in the linter.
- Point it at what tools cannot see. Logic bugs, missed edge cases, error handling, a missing test for a new branch, a change that does not match the ticket, a new service that duplicates an existing one.
- Tell it to stay quiet when unsure. Ask for high-confidence findings only, ranked by severity, and no style comments. A bot that flags everything gets ignored within a week.
- Make the author answer it. Every comment is either fixed or answered before a human is asked to review. When the author is an agent, it reads the comments and fixes them itself, so the first round of review costs nobody’s time.
Then a human reads what is left. If the pyramid is doing its job, that human is not reading about formatting, naming or force unwraps, and not about the obvious bug the bot already caught. They are reading the question only a person can answer: is this the right change?
Fixing the code is only half of answering a comment. Every review comment, from the bot or from a human, is also a bug report for the pyramid: something got past the lower layers. Ask which layer should have caught it. A type or a lint rule? A skill that is wrong, or one the agent never loaded? A step missing from the create-pr workflow? An eval that would have shown the skill does not fire? Fix that layer too, and the same comment never comes back. Mining comments every week catches the ones that slip through anyway.
⚠️ Common mistakes
- Rules in the wrong layer. Formatting rules in CLAUDE.md, architecture rules in a regex. The first is weaker than it should be, the second is noisy and wrong.
- Warnings. A pyramid of warnings is a pyramid of suggestions. Make it fail.
- Treating a long AI review as normal. When the pyramid works, the bot finds a few things per PR, not thirty. If it finds many real problems, a lower layer failed: the agent did not load the skill, the skill is wrong, or the
create-prreview was skipped. Fix that layer. If most of its comments get dismissed, the bot is the problem, so tune it. - Only in CI. If the agent learns about a lint error after pushing, every mistake costs a CI run and a fix-up commit. Run the bottom layers locally, through hooks.
- A huge instruction file. Three hundred lines of CLAUDE.md is a sign that rules belong lower down, or in skills that only load when relevant.
- Skills without evals. A skill that does not fire looks exactly like a skill that works, until you check. An eval checks both: given a real task from a past PR, does the agent load the skill, and does the result follow it? Run the evals whenever a skill or the model changes.
🛠️ Set it up
You probably own most of this already. In order:
- Turn up the compiler. Strictest language mode and warnings as errors in CI.
- Commit the formatter and linter configs. Run both in CI so warnings fail. Record a baseline for existing violations.
- Add an unused code check to CI (
swiftlint analyzeon iOS). - Add a Dangerfile with PR size, ticket link and tests-changed checks.
- Commit the hooks in
.claude/settings.json, so every agent formats as it goes, cannot finish with lint errors, and cannot open a PR with failing tests. - Trim CLAUDE.md to a map: build, test, structure, “the checks must pass”.
- Write a
create-prskill: tests, UI tests for touched screens, and a local review in a fresh subagent before every PR. - Write skills only for judgment calls, each with an eval.
- Add an AI review bot to every PR, with the same skills as the agents, and require its comments to be resolved before a human reviews.
- Treat every review comment as a bug in a lower layer, and schedule the weekly review mining for the ones that keep coming back.
📌 Summary
- The problem: agents write more code than humans can review line by line, and review is the only guardrail that gets tired.
- The pyramid: compiler and types, formatter and linter, CI and Danger, agent instructions, and review on top. The lower the layer, the cheaper and more certain it is.
- The rule: push every rule as low as it will go. Types over lint rules, lint rules over skills, skills over review comments.
- The workflow: before a PR, the agent runs the tests and reviews its own diff with fresh eyes. A hook makes the tests non-optional.
- AI review: a bot reviews every PR with your skills, the author resolves its comments, and only then does a human look.
- Every comment is a bug: each review comment points at a layer that let it through, whether a rule, a skill, the workflow or a missing eval. Fix the code and that layer.
- The AI twist: tool output is the best feedback an agent can get, and hooks bring it into the agent’s loop. A human only reads what no machine could judge.