AI Code Review and Auto-Fix for Pull Requests: A Technical Practice

    This article explores how to automate the post-submission stage of the pull request lifecycle using a multi-agent pipeline. It breaks the workflow into three single-purpose agents—review, fix, and approval—and explains how they form a closed loop: parallel diff scanning with context expansion, false-positive suppression through independent verification, severity grading and deduplication, minimal and rollback-friendly patches on a separate branch, and rule-based auto-approval with default-deny, sensitive-path fallback, and full audit trails. It also covers human-in-the-loop boundaries, common anti-patterns, a four-step rollout plan, and the metrics used to measure effectiveness, such as review adoption rate, missed critical findings, average PR wait time, auto-approval ratio, and rollback rate.

    1. The Problem: When Code Generation Speeds Up, Delivery Becomes the Bottleneck

    Over the past year, the capability boundary of AI coding tools has been pushed forward rapidly. A pattern widely observed across the industry is that models can now handle most of the code writing in a repository—breaking down requirements into function implementations, filling in unit tests, and carrying out refactoring migrations—at a speed far beyond manual typing. Yet one easily overlooked fact remains: writing code has never been the whole of delivery.

    A pull request (PR) goes through several stages between submission and merge: review comment generation, comment response, a second review round, approval, and finally merge and release. In the era of manual coding, the time spent on these stages was masked by the long process of writing code itself. Once generation is compressed to a matter of minutes, however, waiting for review, going back and forth for one more revision, and finding someone to click approve turns into the most visible time sink in the entire pipeline.

    The typical scenario is familiar to nearly every developer: you submit a PR and immediately switch to the next task; halfway through, review comments come back, forcing you to break your current context, reload the code you were just working on, make the changes, push, and wait for another review round to run; if the reviewer happens to be in a meeting, you also have to ping the group chat to chase the approval. Context-switching cost, waiting cost, and coordination cost add up, and often drain more energy than writing the code did.

    As a result, the industry is shifting attention from “generating code” to “delivering code,” exploring automation to take over the stretch of road after a PR is submitted. From a general technical perspective, this article abstracts a practical solution: multiple single-responsibility agents collaborate to discover problems, fix code, and apply rule-based approval, while keeping humans in control at key decision points.

    2. Overall Architecture: Three Roles, One Closed Loop

    A mature automated PR pipeline typically consists of three types of roles, with clear responsibility boundaries and no overreach:

    • Review agent (finds problems): scans the diff and surrounding context in parallel and produces a structured list of findings;
    • Fix agent (fixes problems): reads the review conclusions, makes the minimal necessary changes on a separate branch, and pushes them;
    • Approval agent (applies rules): decides whether the current PR meets the conditions for automatic release, based on the gate rules the team has explicitly declared.

    The three are chained into a closed loop: review finds problems → fix and submit a new version → trigger a re-review → once findings are cleared, move to rule evaluation → if conditions are met, auto-approve and enter the merge queue. Throughout the process, humans hold three keys: the authority to define rules, the authority to take over exceptions manually, and final control over the merge strategy.

    The value of this split lies in independent enablement and gradual rollout. A team can enable fix capability in only some repositories and first observe review accuracy; it can also open auto-approval only to low-risk repositories. The finer the capability granularity, the lower the resistance to adoption.

    3. Review Agent: From “Nitpicking Format” to “Finding Real Problems”

    The easiest way for automated review to fail is to produce a large volume of low-value comments, which desensitizes developers to them. To make it genuinely trusted, engineering design is needed across four dimensions.

    3.1 Parallel Scanning and Context Expansion

    A single sequential scan tends to miss cross-file impact. A more robust approach is to have multiple check units work in parallel: one focuses on the logical correctness of the current diff, one traces back the contracts of callers, and one checks the consistency of configuration and constants. The parallel results are then aggregated and cross-validated.

    3.2 False-Positive Suppression: The Verification Step Cannot Be Skipped

    A suspected issue needs to go through an independent verification stage, where another judgment unit tries to “rebut” the conclusion: Can a real triggering path be constructed? Is it already covered by an upstream check? Could it only occur with a test double? Only issues that cannot be rebutted make it into the final list. This step is the main source of review credibility.

    3.3 Grading and Deduplication

    A three-level classification is recommended, with clear semantics that make it easy to drive follow-up actions:

    • Critical: correctness defects that should be fixed before merge, such as inverted boundary conditions, unhandled null values, or broken retry logic;
    • Advisory: minor issues such as readability, naming, and local structure that do not block merge;
    • Existing: problems that already existed in the repository and were not introduced by this change, recorded separately and scheduled separately.

    Deduplication should aggregate by “same root cause” rather than “same line of text,” to avoid repeatedly reporting the same defect in multiple locations.

    3.4 Depth Tiers and Externalized Rules

    Review depth can be divided into three tiers—fast, standard, and deep—configured according to each repository’s risk level. The default policy should focus on defects that affect production and not get tangled up in formatting preferences or coverage numbers. If a team needs to customize focus areas, it can place a review instruction file in the repository root, using natural language to describe “what must be checked and what to ignore,” decoupling rules from implementation.

    3.5 Output Format: Explainable Inline Comments

    Each comment should contain four elements: what the problem is, under what conditions it is triggered, how it is suggested to be fixed, and the basis and verification method for the judgment. Inline comments should be anchored to a specific line of code with an expandable evidence chain, so developers can quickly decide whether to “accept, rebut, or escalate to a human.”

    A boundary that needs to be made explicit: the review agent only performs static reading. It does not build or run code, and it has no authority to approve or block a PR. Its output is opinions, not verdicts.

    4. Fix Agent: Minimal Changes and Rollback-Friendliness

    The thing to fear most in the fix stage is “refactoring while you’re at it.” Recommended engineering constraints:

    • Make only the minimal necessary changes for confirmed issues, without touching unrelated code along the way;
    • Commit on a separate branch, preserving the original branch and commit history for easy comparison and rollback;
    • Map each change description one-to-one with the review comments, forming a traceable mapping;
    • If the same issue still fails re-review after two consecutive fix rounds, automatically escalate to a human to avoid an infinite loop.

    A schematic flow constraint can be expressed like this:

    1. for round in 1..max_rounds:
    2. findings = review(pr_head)
    3. blocking = [f for f in findings if f.level == "important"]
    4. if not blocking:
    5. return gate_decision(pr_head)
    6. patch = fix(blocking) # minimal necessary changes only
    7. pr_head = push(patch, branch=fix_branch)
    8. return escalate_to_human(pr_head) # exceeded the round limit

    5. Approval Agent: Writing “Can This Merge?” as Explicit Rules

    Auto-approval is the most sensitive link and must be built on the premise of “rules first.” Rules should be text that is public to the team, reviewable, and versionable—not implicit judgment hidden inside the implementation. Common dimensions include:

    • Change scope: only low-risk paths such as documentation, tests, and configuration;
    • Review conclusion: no unresolved critical findings;
    • Quality gates: all automated checks pass and coverage is not below the agreed threshold;
    • Change size: diff lines and file count within thresholds;
    • Author and path constraints: additional conditions such as non-core modules and non-first-time contributors.

    A schematic rule expression:

    1. auto_approve:
    2. require:
    3. - no_open_important_findings
    4. - all_required_checks_passed
    5. - changed_files_under: 20
    6. deny_paths:
    7. - "auth/**"
    8. - "billing/**"
    9. fallback: human_review

    Three practice principles: default deny, and release only when all conditions are satisfied; a hit on a sensitive path goes straight to a human; all auto-approvals are logged, including the rule version and the basis for the decision, for later auditing and traceability.

    6. Human-Machine Collaboration Boundaries and Common Anti-Patterns

    Automation is not a replacement for reviewers; it moves human attention away from mechanical labor. Anti-patterns to watch out for include: rules that are too loose and let risk spill over; review comments that are not explainable, causing developers to either blindly follow or completely ignore them; fix rounds with no upper limit, wasting resources; and auto-approval without auditing, making problems impossible to attribute.

    A reasonable division of labor is: the machine handles broad coverage and repetitive work, while humans handle architectural judgment, business semantics, and exception handling.

    7. Rollout Roadmap and Measuring Effectiveness

    A four-step progression is recommended: first enable read-only review in low-risk repositories and calibrate the false-positive rate; then enable auto-fix for advisory-level issues; then pilot rule-based approval in a small scope; and finally gradually expand repository coverage while tightening the rules. Metrics should include review comment adoption rate, missed critical findings, average PR wait time, auto-approval ratio, and rollback rate.

    8. Conclusion

    When code generation is no longer the bottleneck, automating the delivery pipeline becomes the new efficiency lever. Multi-agent collaboration across review, fix, and rule-based approval is essentially introducing a pipeline that is “explainable, gradual, and auditable” into software engineering. If an organization already has general cloud infrastructure such as a container platform, message queue, logging service, and monitoring and alerting, the pipeline can be deployed as a continuously running job, funneling review conclusions and approval decisions into a unified logging and alerting system to form a closed-loop, observable delivery capability.