Evaluating Code Fix Capabilities of AI Coding Models: A Practical Framework

    This article breaks down how to evaluate the code-fixing capabilities of AI coding models on legacy codebases. It explains why fixing a bug is not a single-point action, defines three evaluation layers (symptom removal, semantic fidelity, and maintainability), and presents a reproducible test design covering defect categories, context supply, a weighted scorecard, regression validation, and repeated sampling. It also analyzes four common anti-patterns of overly aggressive fixes, shows how to encode engineering constraints into prompts, and offers practical recommendations for safe adoption.

    1. Background: From “Fixing It” to “Fixing It Safely”

    In real engineering work, fixing a defect is never a single-point action. Business code often evolves over a long time, forming “legacy code” that is structurally complex, highly coupled, and poorly commented—what developers commonly call “spaghetti code.” This kind of code is characterized by many branches, many implicit conventions, data fields reused across multiple places, and inconsistent exception-handling paths.

    Therefore, evaluating whether an AI coding tool is usable cannot rely only on whether it “made the error disappear.” It must be assessed against three layers:

    1. Symptom removal: the target defect no longer reproduces;
    2. Semantic fidelity: the original business semantics, data structures, and boundary conditions are not broken;
    3. Maintainability: the change scope is controlled, the style is consistent, and the change can be reviewed and rolled back.

    In a recent round of comparative testing, several mainstream AI coding tools were placed into the same set of legacy-project defects, and clear tiers emerged: some tools achieved a perfect score; some successfully removed the defect but did so too aggressively, directly altering the structures or write paths that carry business data and causing hidden damage at the data layer; and some chose the most intrusive option among multiple candidate solutions. This shows that the real dividing line in defect-fixing capability is the judgment to make the “minimal necessary change.”

    2. Evaluation Design: Making Results Reproducible

    For an evaluation to be meaningful, variables must be fixed first. It is recommended to build the test set across the following five dimensions.

    2.1 Defect Type Stratification

    • Null and boundary cases: empty collections, zero values, out-of-bounds access, time zone and precision issues;
    • Concurrency and ordering cases: race conditions, duplicate consumption, missing idempotency;
    • Data compatibility cases: field semantic changes, serialization formats, historical data compatibility;
    • Dependency and configuration cases: environment differences, timeout and retry strategies;
    • Readability defects: misuse caused by ambiguous naming.

    2.2 Context Supply Method

    Provide three types of context for the same defect to observe how much the model depends on information:

    1. A. Only the error stack and a single function
    2. B. The function + its direct callers
    3. C. The function + the call chain + data table schema description + summary of historical commits

    In practice, the more complete the context, the lower the probability of aggressive rewriting; but overly long context also introduces noise and needs to be trimmed.

    2.3 Scorecard (Suggested Weights)

    Dimension Weight Criteria
    Defect removed 30% Target test case passes
    Data semantic fidelity 30% Field meaning and written content unchanged
    Change minimality 20% Lines changed, files affected
    Style consistency 10% Naming, exception handling, logging conventions
    Explainability 10% Whether root cause and risks are stated

    2.4 Regression Validation

    Three layers of validation must be included: unit tests, contract tests (input and output parameters of interfaces unchanged), and data comparison (field-by-field alignment of persisted results before and after the change). Without data comparison, it is impossible to detect cases that are “fixed on the surface but broken underneath.”

    2.5 Repeated Sampling

    Sample the same prompt multiple times and measure the pass rate and the aggressive-rewrite rate. A single run is too coincidental to serve as a conclusion.

    3. Failure Mode Analysis: Why Aggressive Implementations Are Dangerous

    The cases in testing that were “changed but performed poorly” can usually be grouped into four anti-patterns.

    Anti-Pattern 1: Changing the Data Structure Instead of the Logic

    The original logic used a compact structure to carry multiple fields. The model considered it “hard to read,” so it reordered fields or split the structure, causing historical data deserialization to fail.

    1. # Original: field order is strongly tied to historical data
    2. record = (user_id, amount, status, created_at)
    3. # Aggressive rewrite: semantics look clearer, but compatibility is broken
    4. record = {"id": user_id, "amount": amount, "created": created_at, "state": status}

    Anti-Pattern 2: Avoiding Exceptions by Clearing Data

    When encountering an error caused by dirty data, directly filtering out or discarding the anomalous records makes the defect disappear but the data missing, which shifts the problem downstream.

    Anti-Pattern 3: Bundling a Fix Inside a Large-Scale Refactor

    A single change spans dozens of files, so reviewers cannot locate the real fix, and the rollback cost is extremely high.

    Anti-Pattern 4: Swallowing Exceptions

    Changing a thrown exception into a silent return of a default value causes no alerts in the short term but leads to hard-to-diagnose data deviations over the long term.

    4. Prompt Engineering: Writing Constraints into the Input

    To reliably obtain the “minimal necessary change,” it is recommended to explicitly declare engineering constraints in the prompt, for example:

    1. You are a senior maintainer. Please follow these rules:
    2. 1. Only modify code directly related to the defect; do not refactor unrelated parts;
    3. 2. Do not change data structures, field order, serialization formats, or persisted content;
    4. 3. Keep the existing exception-handling and logging style;
    5. 4. Output: root cause analysis / change list / risk points / validation cases;
    6. 5. If a data structure change is unavoidable, stop first and explain alternatives.

    Rule 5 is especially critical: it gives the model a “safe exit” and prevents it from choosing a destructive approach just to complete the task.

    5. Engineering Recommendations

    • Build a defect sample library: desensitize real production issues and turn them into regression cases;
    • Introduce a change budget: limit the number of files and lines changed in a single change; exceeding the limit requires manual confirmation;
    • Enforce data comparison: add before-and-after result alignment checks to the pipeline;
    • Divide work between humans and models: the model handles localization and suggestions, while merging is reviewed by humans;
    • Observability as a fallback: use logging services and monitoring alerts to set thresholds on key metrics, and roll back immediately on abnormal fluctuations.

    If a company chooses cloud hosting, it can run evaluation tasks on a container platform, store samples in object storage, decouple task scheduling with a message queue, and use the general engineering capabilities of platforms such as Baidu AI Cloud to complete pipeline orchestration and observability.

    6. Conclusion

    The gap among AI coding tools in defect fixing is essentially not about “whether they can write code,” but about “whether they understand the implicit contracts of a legacy system.” Evaluation should place data semantic fidelity and change minimality on equal footing with defect removal. For teams, the truly actionable approach is to build a closed loop with a stratified defect set, a scorecard, regression, and data comparison, so that model output is controllable, verifiable, and rollback-ready.