{"slug": "how-to-verify-ai-generated-code-before-merge-five-checks-outside-the-agent-s", "title": "How to verify AI-generated code before merge: five checks outside the agent's loop", "summary": "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.", "body_md": "# How to verify AI-generated code before merge: five checks outside the agent's loop\n\nPicture 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.\"\n\nThe slowdown is still there, and the test can no longer catch it.\n\nOutcomes like this are why maintainers should confirm an agent's diff before merge.\n\nIn [__Stack Overflow's 2026 survey__](https://survey.stackoverflow.co/2026/ai/data), 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__](https://dora.dev/research/2025/dora-report/) found that AI adoption now improves delivery throughput but still harms delivery stability.\n\nIn 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.\n\nTL;DR: Merging AI dependency vulnerability fixes\n\n- **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.\n- **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.\n- **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.\n- **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.\n- **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.\n\n## What does an agent actually check before it says \"done\"?\n\nAn agent's \"done\" is a summary of the commands it chose to run in its own environment and the output it read back.\n\nIf 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.\n\nThe problem comes from where the check happens: inside the session that produced the code, reported by the tool that wrote it.\n\nDevelopers 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.\n\nThese are self-reported answers about AI output that developers read themselves.\n\nSo an agent's \"done\" reports what the agent chose to check. Until something outside the session confirms it, it remains a claim.\n\n## Why don't passing tests prove AI-generated code is right?\n\nA 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.\n\nSo before merge, a pass should carry weight only when a deterministic check that the agent cannot edit or skip agrees with it.\n\nIn 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.\n\nIn [__our re-read of the VulnBump benchmark__](https://blog.codacy.com/should-an-ai-agent-fix-your-vulnerable-dependencies), 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.\n\nIn those 70 cases, the agent's report said the fix worked, and only the independent rescan said otherwise.\n\n## How do you verify AI-generated code before merging?\n\nYou 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.\n\n### Check 1: Re-run the tests and build in CI from a clean checkout\n\nThis check catches code that passed only in the agent's session.\n\nA 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.\n\nThe agent's report can't show what it didn't run or what existed only on its side.\n\nRun 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.\"\n\nA pass in the agent's own terminal can inform review, but it doesn't satisfy the gate.\n\n### Check 2: Flag test changes in agent pull requests\n\nThis 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:\n\n- Deleted or skipped tests\n- Removed assertions\n- Loosened thresholds\n- Changed expected values\n\nIf the agent changed the test, its pass measures agreement with its own change.\n\nMany 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.\n\nHere is an example for a repository with Python and TypeScript tests:\n\n# Route every test change to a named reviewer\n\n/tests/        @example-org/test-reviewers\n\ntest_*.py      @example-org/test-reviewers\n\n*.test.ts      @example-org/test-reviewers\n\n### Check 3: Gate on diff coverage for the changed lines\n\nDiff 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.\n\nTo 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.\n\n**Note:** diff coverage counts executed lines, not tested requirements. A line can run without anything checking its result. [__Code coverage and test coverage__](https://blog.codacy.com/code-coverage-vs-test-coverage) covers the difference.\n\nCoverage 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.\n\n### Check 4: Run AI-generated code through the same gate as human code\n\nA 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.\n\nIts rules cover static analysis, secrets detection and dependency scanning, plus SAST (static application security testing), which scans source code for vulnerable patterns.\n\nOur 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%.\n\nThose 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.\n\nThe 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__](https://blog.codacy.com/how-to-keep-your-ai-generated-code-secure).\n\nCan 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.\n\nA 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.\n\n### Check 5: Compare the diff against the task\n\nSomeone 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.\n\nFor 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.\n\nSmall agent tasks make that comparison practical, and these [__pull request best practices__](https://blog.codacy.com/pull-request-best-practices) cover how to scope them. The same size limits apply to agent work.\n\nIn our VulnBump re-read, we identified an unaided agent made unrelated dependency bumps in 34 of 307 attempts, so scope drift is measurable.\n\nOf course, at scale, a person can't be the only judge of intent, or PR review becomes a bottleneck. [Verity](https://verity.md/), Codacy's pair programmer for coding agents, automates the first pass of this check.\n\n[__Verity runs inside the agent's loop__](https://blog.codacy.com/how-codacy-dogfoods-verity-with-its-own-ai-agents) 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.\n\n## How do you check AI-generated code without reading every line?\n\nHere is a hypothetical walkthrough of a PR for an agent in which the agent is asked to add rate limiting to an API endpoint.\n\n**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.\n\n**In this pull request:** the diff raises a timeout in an existing test. Code owners add the test reviewer, whose approval is now required.\n\n**In this pull request:** the new middleware's error path never ran. The gate fails, and the agent adds a test.\n\n**In this pull request:** the gate flags an API key hardcoded in a new config file.\n\n**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.\n\n- The agent finishes, runs the tests in its session, and opens a PR reporting that all tests pass.\n- CI checks out the branch fresh, builds it and runs the full suite, and the result posts as a required status check (check 1).\n- The diff raises a timeout in an existing test. Code owners add the test reviewer, whose approval is now required (check 2).\n- 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.\n- The quality and security gate (check 4) flags an API key hardcoded in a new config file.\n- 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.\n\nWithout 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.\n\nThe reviewer's attention goes to checks 2 and 5, the judgments a rule can't make. A [__code review checklist__](https://blog.codacy.com/code-review-checklist) helps keep that human review consistent across reviewers.\n\n## Where Codacy fits in verifying agent pull requests\n\nCodacy runs the checks behind gates 3 and 4 on its own infrastructure, outside the agent's loop.\n\nIt 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.\n\nAround that core, it also:\n\n- Reports coverage and diff coverage per PR from test reports uploaded by CI, so a [__diff coverage threshold__](https://blog.codacy.com/diff-coverage) can block a merge\n- 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\n- Applies gate policies across repositories, with a dashboard that shows which repositories have merge gates and coverage reporting and which are missing them\n- Connects to GitHub, GitLab or Bitbucket in the cloud, with no pipeline to configure\n\nFor 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.\n\n## Turning an agent's \"done\" into merge evidence\n\nAn 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.\n\nThe [__case for independent quality gates for coding agents__](https://blog.codacy.com/why-coding-agents-need-independent-quality-gates) develops that argument further. For what it means for review, see [__why AI-generated code needs verification over approval__](https://blog.codacy.com/code-review-is-dead-why-ai-generated-code-needs-verification-not-human-approval).\n\n**Agents report done. Your gate confirms it.**\n\nCodacy 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.\n\n## FAQs on merging AI-generated code at scale\n\n### How to measure code coverage?\n\nCode 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.\n\nDiff 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.\n\nA threshold counts executed lines, not tested requirements, which is the [__difference between code coverage and test coverage__](https://blog.codacy.com/code-coverage-vs-test-coverage).\n\n### Can AI-generated code follow team standards?\n\nAI-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.\n\nA 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__](https://blog.codacy.com/how-to-keep-your-ai-generated-code-secure) follows the same pattern for the security rules\n\n### How can reviews catch bugs faster?\n\nReviews 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.\n\nThat leaves reviewers to read test changes and the intent mismatches Verity escalates.", "url": "https://wpnews.pro/news/how-to-verify-ai-generated-code-before-merge-five-checks-outside-the-agent-s", "canonical_source": "https://blog.codacy.com/verify-ai-generated-code-before-merge", "published_at": "2026-11-24 08:19:39+00:00", "updated_at": "2026-11-24 08:45:16.100037+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "ai-safety"], "entities": ["Codacy", "Claude Code", "Stack Overflow", "DORA", "VulnBump"], "also_reported_by": [], "takeaway": "Gate agent-written diffs on deterministic checks the agent cannot edit — a clean-checkout CI run, a diff coverage threshold, and static analysis, secrets and dependency scanning — because an agent's own passing tests only prove agreement with tests it may have changed.", "recommendation": {"advice": "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.", "playbook": "coding-agents", "playbook_url": "https://wpnews.pro/playbooks/coding-agents"}, "alternates": {"html": "https://wpnews.pro/news/how-to-verify-ai-generated-code-before-merge-five-checks-outside-the-agent-s", "markdown": "https://wpnews.pro/news/how-to-verify-ai-generated-code-before-merge-five-checks-outside-the-agent-s.md", "text": "https://wpnews.pro/news/how-to-verify-ai-generated-code-before-merge-five-checks-outside-the-agent-s.txt", "jsonld": "https://wpnews.pro/news/how-to-verify-ai-generated-code-before-merge-five-checks-outside-the-agent-s.jsonld"}}