Reviewing and Securing AI-Written Code
Agents produce code fast, which means they produce mistakes fast too. The pro discipline is treating every agent diff as untrusted until reviewed, and building review into the workflow so volume never outruns judgment. You are the lead engineer accountable for what merges, no matter who typed it.
Step 1: Know what agents get wrong
- Subtle logic errors that pass a happy-path test but fail on edge cases.
- Invented APIs or dependencies that do not exist (hallucinated imports).
- Secrets hardcoded, or input that reaches a query without sanitizing.
- Over-broad changes that touch files the task never needed.
Step 2: Use an agent to review the agent
A dedicated reviewer subagent with a fresh context, separate from the one that wrote the code, catches a surprising amount. It has no attachment to the implementation and reads the diff cold. Pair it with a human on anything that touches auth, money, or data.
Step 3: Gate the risky paths
Combine the tools from this level: a Stop hook that runs the test suite and a security linter, a reviewer subagent on every PR, and a human approval required before merge to main. Defense in depth means no single failure ships a bug.
Trust the agent to do the work. Trust the review, not the agent, to decide what ships.