TLDR overview
- To verify AI-generated code, define what the change must do, review the diff, run independent automated checks, test behavior against the requirements, pair contextual review with merge controls your repository enforces, and keep a record of every result.
- Apply the same risk-based standard to every change, so a change to access control gets specialist review whether an agent or an engineer wrote it.
- Each check covers some classes of problem and misses others, so this guide states what each one can and cannot tell you.
- SonarQube sets the standard with pull request analysis and a quality gate, and Gitar enforces it at merge speed with contextual review and fixes inside the pull request.
AI coding agents allow your team to ship far more change than ever before, and much of that code is of high quality. What remains is deciding, quickly and consistently, which pull requests are ready to merge. This guide gives software developers, tech leads, and platform engineering teams an ordered workflow for that decision and a checklist you can copy into your own repositories.
How do I verify AI-generated code before merging it?
Verify AI-generated code by reviewing each pull request against requirements you documented before the work began. Rely on checks that run independently of the agent that wrote the code, and merge only when your repository enforces those checks. Run the steps in this order:
- Define what the change must do, what it may touch, and how risky it is.
- Review the diff for unexpected files, removed protections, invented APIs, and new dependencies.
- Run independent automated checks: static analysis, type checks, secrets detection, and dependency vulnerability checks.
- Test behavior against the requirements, including negative paths, in an isolated environment.
- Combine contextual review of intent and logic with merge controls your code host enforces.
- Fix findings, rerun every affected check, and keep the evidence.
Hold every change to the same standard, and scale the scrutiny to the risk of the change. A one-line copy fix and a change to how your service checks permissions deserve different levels of review, and the author of the change has no bearing on which level applies.
The checks in this workflow fall into two kinds of verification, deterministic and agentic. Checks such as static analysis and type checks return the same verdict every time they run on the same code, which makes their results repeatable and auditable. Agentic review reasons about intent, business logic, and context that fixed rules cannot express. Each kind catches problems the other misses, so a sound workflow uses both kinds, plus tests and human judgment for changes that carry real risk.
Define what the change must do before you verify it
Write down the acceptance criteria, the files and services the change may touch, and the failure cases it must handle, such as an unauthorized caller or a malformed request. Put them in the ticket or pull request description so reviewers and review tools can check against them. Then assign a risk level, and route changes to authorization, payments, data handling, infrastructure, dependencies, or the verification setup itself to a named approver.
What this step cannot tell you: A failure case nobody writes down never becomes a test.
What checks should you run on an AI-generated pull request?
Read the diff for changes outside the agreed scope, removed validation or tests, calls to APIs that do not exist, and dependencies the task did not need. Then run checks that do not rely on the agent's judgment: static analysis, type checks, secrets detection, and dependency vulnerability checks. Run them on every pull request, and earlier in the agent's session where your tooling allows.
What these checks cannot tell you: They confirm the code follows known-safe patterns, and they will usually pass a valid-looking logic flaw such as a missing ownership check.
Test behavior against the requirements you wrote
Write or review tests from the acceptance criteria, covering unit, integration, and relevant end-to-end paths, plus a negative test for each failure case. Tests an agent writes alongside its code often share its assumptions, and high coverage can still miss whether an unauthorized user is turned away. Run tests in an isolated environment with no path to production data or credentials.
What testing cannot tell you: Tests cover only the paths they exercise, so load and environment-specific issues can still surface in production.
Combine contextual review with enforceable merge controls
A reviewer, supported by AI review at agent volume, checks intent, logic, security-sensitive paths, and architectural boundaries. AI review adds a second perspective, so pair it with deterministic checks and keep human approval for high-risk changes. A failing check blocks a merge only when your code host marks it as required, so require your checks and approvals on protected branches, and keep agents from approving their own pull requests.
What this step cannot tell you: Merge controls enforce only the checks you configured.
Fix findings, rerun checks, and retain the evidence
Rerun the full pipeline after every fix, and reach green by fixing the code rather than deleting tests, lowering thresholds, or adding suppressions. Record accepted risks as exceptions with an owner and review date. Keep the commit SHA, tool and ruleset versions, test results, quality gate decision, and approvals with the pull request.
What this step cannot tell you: Evidence records what your team checked, and nothing about the checks it never ran.
Pre-merge AI code verification checklist
Set your own owners and blocking thresholds based on risk.
- Are the requirements and failure cases written down?
- Does the diff stay in scope, with no protections or tests removed?
- Did static analysis, secrets detection, and dependency checks pass?
- Do tests cover the requirements and failure cases?
- Has a reviewer checked intent, logic, and security?
- Are the checks that matter required before merge, and rerun on the final commit?
How SonarQube, Sonar Vortex, and Gitar support this workflow
Sonar covers the analysis, quality gate, and pull request review parts of this workflow. Your test frameworks, runtime testing, code host settings, and human approvers stay in your own toolchain. Developers who verify their code with SonarQube are 44% less likely to report outages due to AI (Sonar State of Code Developer Survey).
SonarQube sets the standard. SonarQube Cloud and SonarQube Server analyze every pull request against the quality profiles your organization defines, with static analysis for code quality and code security, secrets detection, and coverage imported from your test tools. SonarQube reports only the issues the pull request introduces, decorates the pull request in GitHub, GitLab, Bitbucket, and Azure DevOps, and evaluates your quality gate for a consistent pass or fail on new code, with an industry-leading low false positive rate. When your code host requires the quality gate status, a failing gate blocks the merge. SonarQube Advanced Security extends the analysis to dependencies, covering known vulnerabilities, malicious packages, and license compliance.
Gitar enforces it at merge speed. Gitar, Sonar's code verification agent, reviews every pull request in context for logic, intent, and security, flags only the issues worth fixing, and can commit fixes under rules your team writes in plain language, iterating until CI passes. When CI fails, Gitar diagnoses the root cause and separates real breakage from flaky or infrastructure noise before it acts. Blocking merges, auto-apply, auto-approve, and auto-merge are opt-in and depend on your plan and code host, and Gitar blocks through each host's native review mechanism, so your branch protection settings decide whether a block holds a merge. On the Enterprise plan, Functional Validation checks whether a pull request implements what its linked issue asked for, which ties contextual review back to the criteria you wrote in the first step. Gitar is available on its own or alongside SonarQube.
SonarQube brings deterministic analysis and Gitar brings agentic review to the pull request, so each change gets both kinds of verification. Because both run independently of the agent that wrote the code, the two together give your team a zero-trust, multilayered verification standard at the point of merge.
Sonar Vortex moves verification into the agentic loop. Sonar Vortex works inside supported coding agents, including Claude Code, Codex, GitHub Copilot CLI, Cursor, and Antigravity. It gives the agent a map of your code and your standards before it writes, and checks each change against the same standard your quality gate applies at the pull request, so fewer issues reach review in the first place. Vortex builds on your CI analysis, so each project and branch needs to be analyzed in CI first.
Next steps
- Explore Sonar's AI code verification approach to see how SonarQube holds agent-generated code and the rest of your code to one standard.
- What is zero-trust verification in software delivery? explains the model behind this workflow.
- Set up pull request analysis in SonarQube Cloud to decorate pull requests and evaluate your quality gate on every change.
- Install the SonarQube CLI to analyze local changes and connect SonarQube to your coding agent.
- Get started with Gitar to add contextual review and CI fixes to your pull requests.
