如何除錯 AI 智能體的錯誤:審查機器生成 PR 的框架
Debugging an AI Agent's Mistakes: A Framework for Reviewing Machine-Written PRs
文章提出一套審查 AI 智能體生成 PR 的框架,協助開發者在合併前核對需求、檢查實作及找出潛在錯誤。框架建議先訂明驗收標準,再檢查完整變更差異、測試是否有效,以及 API、依賴項和安全風險。文章亦提出由自動化檢查、獨立審查到人工批准的流程,並按改動風險調整審查深度。
Debugging an AI Agent's Mistakes: A Framework for Reviewing Machine-Written PRs
AI coding agents are changing how developers write, test, and maintain software. From generating entire functions to fixing bugs and opening pull requests (PRs), these tools can accelerate development and reduce repetitive work. However, faster code generation does not automatically mean better software.
An AI agent can produce code that compiles successfully, passes a test suite, and looks perfectly reasonable while still introducing subtle bugs. It might misunderstand a requirement, overlook an edge case, use an incorrect API, or change existing functionality without realizing the consequences.
The challenge for developers is no longer simply identifying mistakes in manually written code. It is learning how to systematically evaluate machine-generated changes before they reach production.
This guide presents a practical framework for reviewing AI-generated pull requests, identifying common agent mistakes, debugging failures, and building a reliable human-in-the-loop review process.
1. Why AI-Generated Pull Requests Need a Different Review Approach
Traditional code reviews often involve understanding another developer's reasoning, checking implementation details, and discussing potential improvements.
AI-generated pull requests introduce a slightly different challenge: the code may look convincing even when the underlying assumptions are incorrect.
Consider a developer who asks an AI coding agent to implement an API endpoint that retrieves customer information.
The agent might successfully create the endpoint, add a database query, and write a test confirming that the expected customer record is returned.
However, it might overlook authorization checks. An authenticated user could potentially retrieve another customer's information simply by changing an identifier in the request.
The implementation may work exactly as demonstrated by its tests while still containing a serious security vulnerability.
Several recurring problems make AI-generated PRs particularly important to review carefully:
- Incorrect assumptions: The agent interprets requirements differently from what the developer intended.
- Incomplete testing: Tests cover expected scenarios but overlook important failure conditions.
- Unnecessary complexity: The agent introduces additional abstractions, dependencies, or helper functions without a clear benefit.
- Regression risks: Existing functionality breaks because the agent fails to understand how different components interact.
- Security weaknesses: Authentication, authorization, input validation, or secret handling is implemented incorrectly.
- Misleading confidence: The agent reports success without providing sufficient evidence that the implementation meets every requirement.
The solution is not to avoid AI coding agents. Instead, developers need a repeatable process that evaluates the code independently of the agent's explanation.
2. Start With Requirements Before Examining the Code
One of the most effective ways to identify AI mistakes is to establish what the code should accomplish before reading the implementation.
If reviewers begin with the generated code, they may unconsciously evaluate it against the agent's interpretation rather than the original requirements.
Start by translating the task into explicit acceptance criteria.
For example, suppose the task is to implement a password reset endpoint.
The acceptance criteria might include:
- Accept a valid email address.
- Return a generic response regardless of whether the account exists.
- Generate a time-limited reset token.
- Prevent expired or previously used tokens from being accepted.
- Avoid exposing reset tokens in application logs.
- Add tests for successful resets and relevant failure scenarios.
- Preserve the existing authentication behavior.
These requirements give reviewers a concrete basis for evaluating the PR.
Build a requirements-to-tests mapping
For each acceptance criterion, identify the implementation and test that demonstrate the expected behavior.
| Requirement | What to inspect | Evidence required |
|---|---|---|
| Valid input | Request validation | Invalid inputs are rejected safely |
| Generic response | Response handling | Existing and nonexistent accounts receive equivalent public responses |
| Token expiration | Token validation | Expired tokens cannot reset passwords |
| Single use | Token lifecycle | A consumed token cannot be reused |
| Secret protection | Logging and storage | Sensitive token values are not exposed |
| Regression prevention | Existing authentication code | Relevant existing tests still pass |
This approach makes missing functionality easier to detect than relying on a PR description or a summary produced by the coding agent.
Rule of thumb: Review the intended behavior first, then determine whether the implementation actually delivers it.
3. Inspect the Diff for Unexpected Changes
AI agents sometimes make changes beyond the requested task. A small bug fix might unexpectedly include a refactor, a dependency upgrade, or a modification to a shared utility.
Every additional change creates another opportunity for unintended behavior.
Before reviewing individual functions, inspect the scope of the pull request.
Useful Git commands include:
# Summarize changed files and their sizes
git diff --stat main...HEAD
# Review the complete change
git diff main...HEAD
# Review filenames and change types
git diff --name-status main...HEAD
Adjust main if your repository uses a different base branch.
Look for files that do not appear necessary to fulfill the original request. Pay particular attention to configuration files, authentication middleware, database migrations, dependency manifests, and CI workflows.
For example, a PR intended to fix a validation error might also modify an existing permission check.
That unrelated change should be investigated before approval.
Questions to ask during scope review
- Does every modified file contribute to the requested behavior?
- Were existing safeguards removed or weakened?
- Did the agent introduce unnecessary dependencies?
- Were configuration values changed without explanation?
- Are there incomplete implementations, hard-coded values, or unexplained comments?
- Can the change be divided into smaller, easier-to-review PRs?
Unexpected changes are not automatically wrong, but they should have a clear justification.
4. Review Tests Before Trusting the Implementation
A green test suite is useful evidence, but it does not guarantee correctness.
AI agents can generate tests that reflect the same incorrect assumptions as their implementations. They can also accidentally weaken existing tests while attempting to resolve failures.
For this reason, reviewing test changes should be an independent step in the PR process.
Look for tests that have become less meaningful
Suppose the original test verifies a precise result:
expect(calculateDiscount(100, 10)).toBe(90);
The agent changes it to:
expect(calculateDiscount(100, 10)).toBeTruthy();
The revised assertion is much weaker. It could pass for many incorrect outputs.
A reviewer should investigate why the original assertion changed and restore meaningful expectations unless there is a documented reason to revise them.
Also check whether the agent has:
- Deleted existing regression tests.
- Marked failing tests as skipped.
- Removed important assertions.
- Added mocks that bypass the behavior being tested.
- Tested only successful requests.
- Changed coverage thresholds or test execution settings.
Use negative tests to expose hidden mistakes
Negative tests examine what happens when inputs, dependencies, or operating conditions are invalid.
For a payment API, relevant scenarios might include:
- Missing payment details.
- Invalid authentication credentials.
- Duplicate payment requests.
- Network timeouts.
- A declined transaction.
- An unexpected response from the payment provider.
For an AI-generated authentication change, test expired tokens, insufficient permissions, and malformed requests.
The objective is to determine whether the implementation handles failure conditions correctly, not merely whether the expected demonstration works.
Verify that tests actually detect the bug
When practical, introduce a controlled defect in a local test branch or temporarily revert the relevant fix. Confirm that the appropriate test fails.
This technique, sometimes called mutation testing or negative verification depending on the method used, helps determine whether the test meaningfully exercises the behavior.
Do not modify the production branch to perform this experiment.
A test that passes regardless of whether the fix exists offers little evidence that the underlying problem has been resolved.
5. Debug the Agent's Reasoning by Tracing Data Flow
When an AI-generated PR behaves incorrectly, resist the temptation to ask the agent to rewrite the entire implementation immediately.
First, identify where the observed behavior diverges from the expected behavior.
A structured debugging process makes it easier to locate the actual defect.
Step 1: Reproduce the problem
Create the smallest input or scenario that reliably demonstrates the issue.
For example, if an API returns incorrect results for empty search queries, reproduce the behavior with an empty string rather than testing a large collection of unrelated inputs.
Step 2: Identify the expected and actual results
Document the discrepancy clearly.
Expected:
An empty search query returns a validation error.
Actual:
The API returns every record in the database.
This description narrows the investigation to input validation and query construction.
Step 3: Trace the input through the implementation
Follow the data from the incoming request through validation, transformation, business logic, database access, and response generation.
Check whether the input is validated, whether defaults are applied correctly, and whether unexpected values reach sensitive operations.
Step 4: Inspect the relevant assumptions
Ask why the agent implemented the behavior this way.
It may have assumed that empty strings are equivalent to missing values, that a helper function performs validation automatically, or that a database query safely handles empty filters.
Verify these assumptions against the actual code and documentation.
Step 5: Create a targeted regression test
Once the defect is understood, add a test that reproduces the original failure and confirms the intended behavior.
Step 6: Rerun the relevant checks
Run the targeted test, related tests, and the project's standard validation suite. Inspect the final diff to confirm that the fix did not introduce unrelated changes.
This process turns debugging into an evidence-driven investigation rather than a sequence of increasingly broad AI prompts.
6. Watch for Hallucinated APIs and Incorrect Dependencies
AI agents can generate plausible-looking function names, configuration options, and library methods that do not exist or behave differently from what the agent expects.
For example, an agent might call a method with an unsupported parameter or rely on an API behavior from a different library version.
These errors can be difficult to spot when the surrounding code looks familiar.
Review unfamiliar imports, method calls, and configuration options against the actual documentation for the dependency version used by the project.
Check the following:
- Does the referenced function or method exist?
- Is the argument format correct?
- Does the installed version support the feature?
- Are exceptions handled as documented?
- Has the agent introduced an unnecessary package?
- Are dependencies pinned or constrained appropriately?
- Could a new package introduce security or maintenance risks?
Do not assume that a dependency is legitimate simply because the generated code imports it successfully in the agent's environment.
A package may be missing from the repository, available only in a different environment, or incorrectly named.
For critical dependencies, inspect the package's official documentation and release information before approving the change.
7. Treat Security-Sensitive Changes as High Risk
Security problems deserve additional scrutiny because a small implementation mistake can expose sensitive data or allow unauthorized operations.
AI-generated code should receive particularly careful review when it touches authentication, authorization, payment processing, personal information, infrastructure, or production credentials.
Common security mistakes to investigate
Missing authorization checks
An endpoint may authenticate users without verifying whether they are permitted to access the requested resource.
Insufficient input validation
Unexpected values may reach database queries, shell commands, file operations, or other sensitive functionality.
Information leakage
Debug logs, error messages, or API responses may expose credentials, personal information, or internal system details.
Unsafe execution
Untrusted model output or user input may be passed into shell commands, dynamic code execution, or other privileged operations without appropriate safeguards.
Overly broad permissions
An agent may grant an application, workflow, or service more permissions than it needs.
Weak error handling
Broad exception handlers may conceal security failures or incorrectly report that sensitive operations succeeded.
For every security-sensitive change, verify both the normal execution path and the failure paths.
Run appropriate static analysis, dependency checks, secret scanning, and security tests where available. Automated tools can identify common issues, but they do not replace a reviewer who understands the application's security requirements.
8. Use a Structured AI-Assisted Review Workflow
AI can help review code, but the review process should not depend entirely on the same agent that generated the implementation.
A practical workflow separates code generation, automated checks, independent review, and final approval.
Phase 1: Understand the task
Write down the intended behavior, acceptance criteria, and risk level.
Identify which components should change and which existing behaviors must remain intact.
Phase 2: Generate and inspect the implementation
Ask the coding agent to make a focused change.
Before opening the PR, review the generated diff and remove unrelated modifications. The person submitting the PR should understand the implementation well enough to explain its important decisions.
Phase 3: Run automated checks
Execute the project's relevant checks, including:
- Unit and integration tests.
- Type checking and compilation.
- Linting and formatting.
- Static analysis.
- Dependency and security scanning.
- Build and deployment validation where applicable.
Confirm that the checks actually ran against the intended branch and that failures have not been hidden by configuration changes.
Phase 4: Perform an independent review
Use a separate reviewer, which may be a human or an additional AI tool, to inspect the change for logic errors, missing edge cases, security weaknesses, and unnecessary complexity.
A useful review prompt is:
Review this pull request against the original requirements.
Do not assume the implementation is correct because the
tests pass or the PR description claims success.
Identify:
1. Functional bugs and incorrect assumptions.
2. Missing edge-case and failure-path tests.
3. Regressions or unrelated changes.
4. Security and authorization weaknesses.
5. Incorrect API usage or dependency assumptions.
6. Performance and maintainability concerns.
For each finding, provide:
- The affected file and relevant code.
- The expected behavior.
- The actual or potentially incorrect behavior.
- A concrete reproduction or supporting evidence.
- A recommended fix.
Do not change the code. Report findings for human verification.
A second AI reviewer can provide another perspective, but its findings must also be verified. Two agents can share the same blind spot.
Phase 5: Make a human approval decision
The final reviewer should confirm that the requirements are met, tests are meaningful, security-sensitive behavior is correct, and the changes are appropriate for the intended release.
AI can assist with analysis and repetitive checks, but merge approval should remain with an accountable person.
9. Build a Practical PR Review Checklist
A consistent checklist reduces the chance that reviewers overlook important details during busy development cycles.
Use the following checklist before merging an AI-generated pull request.
| Review category | Verification question |
|---|---|
| Requirements | Does the implementation satisfy every acceptance criterion? |
| Scope | Are all changed files necessary for the requested task? |
| Logic | Have assumptions, boundary conditions, and failure paths been checked? |
| Tests | Do tests verify meaningful outcomes rather than merely successful execution? |
| Regressions | Do relevant existing tests still pass? |
| Dependencies | Are new libraries, methods, and configuration options valid? |
| Security | Are authentication, authorization, validation, and secrets handled correctly? |
| Performance | Are queries, retries, resource usage, and external API calls reasonable? |
| Maintainability | Does the implementation follow established project conventions? |
| Observability | Are useful errors logged without exposing sensitive information? |
| Deployment | Are migrations, configuration changes, and rollback requirements understood? |
| Ownership | Can the responsible developer explain the code and justify the final decision? |
Not every change requires the same level of scrutiny.
A documentation correction may need only a lightweight review. A database migration or authentication change deserves deeper inspection, targeted testing, and appropriate specialist review.
The objective is to scale review effort according to risk rather than apply the same process to every PR.
10. Improve the Process When the Same Mistakes Keep Returning
Repeated AI-generated mistakes often reveal weaknesses in the development workflow itself.
If several PRs introduce duplicate utilities, the agent may need clearer instructions about reusing existing code.
If tests repeatedly miss boundary conditions, the team's task templates may need explicit requirements for negative testing.
If security checks are frequently overlooked, automated validation should run before the PR becomes eligible for approval.
Useful improvements include:
- Maintaining repository-specific instructions for coding agents.
- Providing examples of preferred architectural patterns.
- Defining acceptance criteria before code generation begins.
- Requiring targeted tests for bug fixes.
- Automatically detecting changes to sensitive configuration and CI files.
- Enforcing minimum security and quality checks in CI.
- Recording recurring review findings and addressing their root causes.
The most effective improvement is often to prevent a recurring mistake rather than rely on reviewers to catch it indefinitely.
Teams adopting AI-assisted development should also evaluate how decisions are made when automated tools provide incomplete or conflicting recommendations. Understanding the capabilities and limitations of AI-supported decision-making can help teams establish appropriate review controls.
For professionals who want to explore this area further, Edstellar's Decision Making with AI Training provides a relevant learning resource for developing a more informed approach to AI-supported decisions.
Conclusion
AI coding agents can accelerate software development, but generating code is only one part of building reliable software.
The real challenge is determining whether the generated implementation meets the original requirements, handles unexpected situations, preserves existing functionality, and operates safely in production.
A dependable review process begins with explicit acceptance criteria, examines the complete diff, challenges the tests, verifies unfamiliar APIs, investigates security-sensitive changes, and requires independent evidence before approval.
The most important principle is simple: Treat an AI-generated pull request as a proposed implementation, not proof that the problem has been solved.
When developers combine automated checks, systematic debugging, risk-based reviews, and human accountability, they can benefit from AI-assisted development without sacrificing software quality or reliability.
Disclosure: This article was created with the assistance of AI and should be reviewed for technical accuracy before publication.
來源:DEV Community · dev.to