AI-Generated Code at Over 90%: Building a Layered Review Defense
As AI code generation accelerates, review processes designed for line-by-line human authorship come under pressure. This article presents a three-layer review system for AI-generated code: pre-review verification with inspectable evidence, diff review in an independent context, and rollback as a fallback. It compares how well each layer covers common risk types, discusses the limits of the mechanism, and suggests a practical adoption sequence.
1. Background: The Review Target Has Changed
As the share of AI-generated code passes 90%, code generation speed has increased sharply, and traditional review practices are starting to feel the strain. In the past, developers wrote code line by line, and reviewers could keep pace with changes. Now a single merge request may contain a large number of tool-generated changes. The cost of manual line-by-line review rises sharply, and the review can easily become a formality.
The risk types in generated code differ from those in human-written code. The main categories include:
- Logic deviation: The code compiles, but it misunderstands business rules, such as incorrect boundary-condition handling or missing state transitions.
- Security risks: Generated code may concatenate unvalidated input or introduce endpoints with incomplete permission checks.
- Architecture drift: Changes may bypass established layering conventions, access the data layer directly, or duplicate existing utility functions.
- Missing tests: Generators often focus only on the main flow, leaving exception branches and regression scenarios uncovered.
- Over-modification: To complete a small requirement, a tool may also change unrelated files and expand the scope of the change.
These risks are difficult to cover with a single method. The review system therefore needs a layered design, with each layer responsible for finding different types of problems.
2. Layer 1: Verify Before Anyone Else Reviews
Most problems can actually be found before a merge request is submitted. The core principle of this layer is: do not accept verbal claims such as “done”; require the person executing the work to provide evidence that can be checked.
Common verification methods include:
- Run tests: Execute unit tests and integration tests, and record the number of passes and failing cases.
- Static checks: Run code-style and type checks, and record the number of warnings.
- Build verification: Run a full build and confirm that the artifact can be generated normally.
- UI comparison: For changes involving the interface, capture screenshots of the running result, compare them item by item with the design draft, and record differences.
The verification process can be written as explicit gate rules, for example:
verify_pipeline:step 1: run static checks and save the output logstep 2: run unit tests and record pass and fail countsstep 3: run the build and record the exit statusstep 4: if UI changes are involved, capture screenshots, compare with the design draft, and record differencesgate: if any step fails, block the review request
The key is to “show the output.” If the executor can provide only a conclusion, the reviewer cannot judge whether the conclusion is reliable. Requiring logs and screenshots makes verification results traceable and also makes later retrospectives easier.
3. Layer 2: Diff Review with an Independent Context
After verification is complete, the next step is code review. The key at this layer is reviewer independence. If the reviewer shares the same conversation history as the generator, the reviewer is likely to carry over assumptions from the generation process and miss the same errors.
A reviewer using a fresh context receives only three types of input: the code diff, the requirement description, and the team’s coding conventions. It does not know the reasoning process used during generation, so it is more likely to find places that “look reasonable but are actually wrong.”
The following example code shows this organization:
def review_diff(diff: str, requirement: str, conventions: str) -> list:# The review context contains only the diff, requirement, and conventions; no generation history is included.context = {"diff": diff,"requirement": requirement,"conventions": conventions,}findings = reviewer.run(context)# Keep only medium- and high-severity issues that require human handling.return [f for f in findings if f.severity in ("high", "medium")]
To improve review quality, require the reviewer to explain the basis for each finding in the output, for example by pointing to the specific file, line number, and violated rule. Conclusions without supporting evidence should be marked as low-confidence to avoid false positives interfering with developers’ judgment.
4. Layer 3: Rollback as the Fallback
No matter how complete the first two layers are, they cannot guarantee zero defects. The review system must therefore include a reversible fallback mechanism. Building rollback capability usually revolves around the following points:
- Small commits: The smaller the scope of a single change, the lower the cost of locating a problem and reverting it.
- Feature flags: New features are off by default and are gradually rolled out through configuration. If an anomaly occurs, they can be disabled quickly.
- Version tags: Each release is tagged with an identifiable version label, ensuring that the system can be rolled back to a clear historical state.
- Automatic rollback triggers: Relying on log services and monitoring alerts, set thresholds for metrics such as error rate and latency. When a threshold is exceeded, trigger automatic or semi-automatic rollback.
- Reversible data migrations: For changes involving data-structure changes, prepare both forward and backward migration scripts, and complete backups before the change.
The value of the rollback mechanism is that it limits the scope of loss. Even if a defect slips through the first two layers, it will affect only a limited number of users and can be recovered in a relatively short time.
5. Coverage Comparison Across the Three Layers
The table below compares, in a qualitative way, how well each layer covers common risks, helping teams decide where to invest resources.
| Risk type | Verification execution | Independent diff review | Rollback fallback |
|---|---|---|---|
| Functional logic errors | Medium (depends on test quality) | High | Low |
| Security risks | Low | Medium | Low |
| Architecture drift | Low | High | Low |
| Insufficient test coverage | High (can be found through coverage) | Medium | Low |
| Unforeseen production issues | Low | Low | High |
As the table shows, no single layer can cover all risks. Verification is good at finding problems that can be detected automatically. Review is good at finding problems at the structure and intent level. Rollback is responsible for controlling the impact scope of production issues.
6. Limitations of the Mechanism
This system has clear boundaries, and teams need to understand these limitations when implementing it.
First, the reviewer and the generator may share the same knowledge blind spots. Even with an independent context, the model’s own training bias may still cause the same type of error to be ignored repeatedly. Independent review therefore cannot replace the judgment of domain experts.
Second, the effectiveness of verification depends on the tests themselves. If test coverage is low, or assertions are too loose, the verification layer will produce a false “pass” conclusion.
Third, problems such as performance bottlenecks, concurrency races, and resource leaks usually cannot be found from code diffs alone. They require specialized methods such as stress testing and performance profiling.
Fourth, critical paths involving money, permissions, and data deletion should have manual approval nodes and should not rely entirely on automated gates.
7. Adoption Recommendations
When introducing this mechanism, teams can proceed in the following order:
- First, solidify verification gates: Make static checks, tests, and builds mandatory steps before merging, and require verification output to be submitted. This step involves the smallest change and shows results relatively quickly.
- Then introduce independent review: Prepare a standardized input template for review, clarify how the diff, requirement, and conventions are organized, and periodically sample and review the accuracy of review conclusions.
- Finally, improve rollback capability: Map out the release and rollback processes for key services, set monitoring metrics and trigger conditions, and confirm through drills that the rollback path is usable.
- Keep review records: Archive verification output, review comments, and final handling results for periodic retrospectives. Identify high-frequency defect types and use them to optimize gate rules and review templates.
Following this order allows teams to gradually build review capability that matches code generation speed without adding too much burden.
