cd /news/ai-agents/how-to-verify-ai-generated-code-befo… · home › topics › ai-agents › article
[ARTICLE · art-175277] src=blog.codacy.com ↗ pub= topic=ai-agents verified=true sentiment=· neutral

How to verify AI-generated code before merge: five checks outside the agent's loop

Codacy published a five-check merge process for verifying AI-generated code outside the agent's own loop, citing Stack Overflow's 2026 survey in which 48% of developers said they trust AI output when they can easily verify it and only 6.6% extend that trust to important work decisions. The checks require CI to re-run tests and builds from a clean checkout, route test changes to a named reviewer, gate on diff coverage rather than overall coverage, apply the same static analysis, secrets detection and dependency scanning as human code, and have a person compare the diff with the ticket. Codacy also reported that across 307 attempts in its re-read of the VulnBump benchmark, an unaided Claude Code agent claimed a fix that an independent rescan still flagged.

recommendation

Require CI to re-run tests and builds from a clean checkout, send test edits to a named reviewer, and gate on diff coverage so agent diffs are validated by checks the agent cannot edit or skip.

more in playbook: Working with AI coding agents
by read12 min views2 publishedNov 24, 2026
How to verify AI-generated code before merge: five checks outside the agent's loop
Image: Blog (auto-discovered)

Picture an agent asked to speed up a checkout endpoint. Its new query is faster on most carts, but slower on large ones, so a timing test fails. The agent raises the test's time limit until it passes, then reports "Done, all tests pass."

The slowdown is still there, and the test can no longer catch it.

Outcomes like this are why maintainers should confirm an agent's diff before merge.

In Stack Overflow's 2026 survey, 48% of developers said they trust AI output when they can easily verify it. Only 6.6% extend that trust to important work decisions. DORA's 2025 report found that AI adoption now improves delivery throughput but still harms delivery stability.

In this article, we'll cover what an agent actually checks, why passing tests fall short, and five merge checks that don't depend on its report.

TL;DR: Merging AI dependency vulnerability fixes

  • Check 1: CI re-runs the tests and build from a clean checkout, exposing what the agent's session hid, such as tests that never ran. A required status check blocks the merge until it passes.
  • Check 2: Test changes go to a named reviewer, because a pass measured against a test the agent edited only proves agreement with its own change. A code owner rule makes that person's approval required before merge.
  • Check 3: A diff coverage gate requires the lines the agent changed to be tested. Overall coverage hides untested new code, because one pull request barely moves a repository-wide percentage.
  • Check 4: Agent code passes the same gate as human code: static analysis, secrets detection, and dependency scanning against the team's own rules. It catches violations the agent's summary won't mention.
  • Check 5: Someone compares the diff with the ticket, because a pull request can pass the other four checks and still solve a different problem. An automated system inside the loop does the first pass, and a person reviews what it escalates.

What does an agent actually check before it says "done"? #

An agent's "done" is a summary of the commands it chose to run in its own environment and the output it read back.

If it ran only the tests near the files it changed, the summary says the tests pass. If it edited a test earlier in the session, the pass is measured against the edited version. The maintainer sees the summary, not the run. The problem comes from where the check happens: inside the session that produced the code, reported by the tool that wrote it.

Developers already treat AI output as unfinished work. In the same 2026 survey, only 9.6% use an AI answer as-is. 76.5% run it locally first, and 63.9% compare it with the codebase.

These are self-reported answers about AI output that developers read themselves.

So an agent's "done" reports what the agent chose to check. Until something outside the session confirms it, it remains a claim.

Why don't passing tests prove AI-generated code is right? #

A passing test suite shows that the code agrees with the tests. When one agent writes the code, writes or edits the tests, and reports the result, both sides of that agreement come from the same reading of the task. A wrong reading can pass cleanly.

So before merge, a pass should carry weight only when a deterministic check that the agent cannot edit or skip agrees with it.

In this context, deterministic means the check gives the same result for the same code, whoever triggers it. Examples include a CI job on a clean checkout, a coverage threshold, and a static analysis rule.

In our re-read of the VulnBump benchmark, agents upgraded vulnerable dependencies and an independent rescan judged each fix. Across 307 attempts, an unaided Claude Code agent claimed a fix that the rescan still flagged 70 times. With scanner data, that happened twice on this task.

In those 70 cases, the agent's report said the fix worked, and only the independent rescan said otherwise.

How do you verify AI-generated code before merging? #

You verify AI-generated code by running five checks before merge whose results don't depend on the agent's report. Four of them can be automated. The fifth is partly automated and ends with a person who knows what the task was.

Check 1: Re-run the tests and build in CI from a clean checkout

This check catches code that passed only in the agent's session.

A subset of tests that never ran, a dependency installed only on the agent's machine, or a file that never got committed are some examples that come to mind.

The agent's report can't show what it didn't run or what existed only on its side.

Run the full test suite and the build in CI from a fresh checkout of the pull request branch. Make the job a required status check on the default branch. On GitHub, this is the branch protection setting "Require status checks to pass before merging."

A pass in the agent's own terminal can inform review, but it doesn't satisfy the gate.

Check 2: Flag test changes in agent pull requests

This check flags test changes and routes them to a person, because they are the most direct way an agent's pass can mislead. For example:

  • Deleted or skipped tests
  • Removed assertions
  • Loosened thresholds
  • Changed expected values

If the agent changed the test, its pass measures agreement with its own change. Many test changes are legitimate, such as new tests for new behavior. The check exists so that a person looks at them. A CODEOWNERS file assigns paths to reviewers, and branch protection can require their approval.

Here is an example for a repository with Python and TypeScript tests:

/tests/        @example-org/test-reviewers

test_*.py      @example-org/test-reviewers

*.test.ts      @example-org/test-reviewers

Check 3: Gate on diff coverage for the changed lines

Diff coverage is the share of lines changed in a pull request that the tests actually executed. It catches new code that no test touches. Overall coverage hides this, because one pull request barely moves a repository-wide percentage.

To measure code coverage, run the test suite with your language's coverage tool, which records which lines execute during tests. Then divide covered lines by executable lines. Diff coverage applies the same calculation to the changed lines only.

Note: diff coverage counts executed lines, not tested requirements. A line can run without anything checking its result. Code coverage and test coverage covers the difference.

Coverage has one limit: a test can run a line without checking it, as heavy mocking does by executing code without its real dependencies. Checks 2 and 5 cover what coverage can't.

Check 4: Run AI-generated code through the same gate as human code

A quality and security gate is a set of rules a tool enforces on every pull request, using your team's own standards. It catches violations an agent's summary won't mention, because the agent reports only on the checks it chose to run.

Its rules cover static analysis, secrets detection and dependency scanning, plus SAST (static application security testing), which scans source code for vulnerable patterns.

Our own data shows how often agent code trips a gate: from October 1st to November 9th 2026, 12.5% of agent-opened pull requests in repositories with a Codacy gate failed it. For human-opened pull requests, the rate was 8.5%.

Those are 1,355 of 10,876 agent pull requests and 32,325 of 380,124 human ones. A failed gate flags something to review, not a confirmed defect.

The VulnBump re-read shows a related problem. An unaided agent introduced new vulnerabilities in 33 of 307 upgrade attempts, and the benchmark's pass does not count them against the fix. A merge gate that fails on new vulnerabilities catches them.

Can AI-generated code follow team standards? It can, when the standards exist as rules a tool enforces on every PR and not only as instructions in the agent's prompt.

A prompt instruction is guidance the agent weighs against everything else in its context. A rule in the merge gate gives the same answer on every run. Agent PRs get no exception.

Check 5: Compare the diff against the task

Someone has to compare the diff with the ticket or acceptance criteria, because deterministic gates can't tell whether the code does what was asked. A PR can pass the four checks above and still solve a different problem from the one assigned.

For this check, look for files touched outside the task's scope, changes nobody asked for, and tests that encode a different reading of the requirement. Small agent tasks make that comparison practical, and these pull request best practices cover how to scope them. The same size limits apply to agent work.

In our VulnBump re-read, we identified an unaided agent made unrelated dependency bumps in 34 of 307 attempts, so scope drift is measurable.

Of course, at scale, a person can't be the only judge of intent, or PR review becomes a bottleneck. Verity, Codacy's pair programmer for coding agents, automates the first pass of this check.

Verity runs inside the agent's loop and checks each turn against the task. It hands intent mismatches back to the agent to fix before a pull request exists, and passes it into a human review after two self-corrections fail.

How do you check AI-generated code without reading every line? #

Here is a hypothetical walkthrough of a PR for an agent in which the agent is asked to add rate limiting to an API endpoint.

In this pull request: CI checks out the branch fresh, builds it and runs the full suite. The result posts as a required status check.

In this pull request: the diff raises a timeout in an existing test. Code owners add the test reviewer, whose approval is now required.

In this pull request: the new middleware's error path never ran. The gate fails, and the agent adds a test.

In this pull request: the gate flags an API key hardcoded in a new config file.

In this pull request: Verity flags an unrelated change to a retry policy in another client. It still stands after two self-correction passes, so a person reviews. The reviewer asks for the change in a separate pull request.

  • The agent finishes, runs the tests in its session, and opens a PR reporting that all tests pass.
  • CI checks out the branch fresh, builds it and runs the full suite, and the result posts as a required status check (check 1).
  • The diff raises a timeout in an existing test. Code owners add the test reviewer, whose approval is now required (check 2).
  • The coverage report shows the new middleware's error path never ran. The diff coverage gate fails (check 3), and the agent adds a test.
  • The quality and security gate (check 4) flags an API key hardcoded in a new config file.
  • Verity flags an unrelated change to a retry policy in another client (check 5). After two self-correction passes, it still stands, so Verity asks a person to review. The reviewer asks for the change in a separate pull request.

Without the gates, each of those problems would have reached the default branch behind a green "done." That is what failure looks like when the checks are missing: nothing visibly breaks at merge time.

The reviewer's attention goes to checks 2 and 5, the judgments a rule can't make. A code review checklist helps keep that human review consistent across reviewers.

Where Codacy fits in verifying agent pull requests #

Codacy runs the checks behind gates 3 and 4 on its own infrastructure, outside the agent's loop.

It analyzes every pull request for quality issues, security vulnerabilities, exposed secrets, and vulnerable dependencies, then reports a status to the Git provider that a required status check can enforce.

Around that core, it also:

  • Reports coverage and diff coverage per PR from test reports uploaded by CI, so a diff coverage threshold can block a merge
  • Lets the same rules run in the IDE and the terminal, so an agent can check its work against the team's gate before it reports done
  • Applies gate policies across repositories, with a dashboard that shows which repositories have merge gates and coverage reporting and which are missing them
  • Connects to GitHub, GitLab or Bitbucket in the cloud, with no pipeline to configure

For check 5, Verity covers the intent side: it works inside the agent's loop, next to the platform's deterministic gates and not in place of them, and a person still reviews what it escalates.

Turning an agent's "done" into merge evidence #

An agent's "done" stays a claim until something outside its session confirms it. The five checks do that through a clean-checkout CI run, a named reviewer for test changes, a diff coverage gate, the team's own quality and security gate, and a comparison of the diff with the ticket.

The case for independent quality gates for coding agents develops that argument further. For what it means for review, see why AI-generated code needs verification over approval.

Agents report done. Your gate confirms it.

Codacy runs static analysis, secrets detection, and dependency scanning on every pull request, with merge gates and no CI pipeline to configure. Agent code then passes the same gate as human code.

FAQs on merging AI-generated code at scale #

How to measure code coverage?

Code coverage is measured by running the test suite with your language's coverage tool, which records which lines execute during the tests, and then dividing covered lines by executable lines.

Diff coverage applies the same calculation to only the lines changed in a PR, which makes it more useful as a merge gate for agent-written code.

A threshold counts executed lines, not tested requirements, which is the difference between code coverage and test coverage.

Can AI-generated code follow team standards?

AI-generated code can follow team standards when those standards exist as rules a tool enforces on every pull request, not only as instructions in the agent's prompt.

A prompt instruction is guidance the agent weighs against everything else in its context. A rule in the merge gate returns the same answer on every run, and agent pull requests get no exception. Securing AI-generated code follows the same pattern for the security rules

How can reviews catch bugs faster?

Reviews catch bugs faster when automated merge gates handle the checks a rule can make, so reviewers spend their time on judgments a rule can't. Clean-checkout CI, a diff coverage threshold, and a quality and security gate can all run before a person opens the pull request.

That leaves reviewers to read test changes and the intent mismatches Verity escalates.

── more in #ai-agents 4 stories · sorted by recency
── more on @codacy 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/how-to-verify-ai-gen…] indexed:0 read:12min 2026-11-24 · —