LLM Code Evaluation: Why Complex Refactoring Keeps Failing

    This article examines why LLMs often fail at complex refactoring tasks despite producing solid execution plans. It breaks down four common failure modes—misattributing code errors to the environment, skipping minimal verification loops, shotgun debugging, and long-horizon consistency decay—and argues that planning, code generation, and execution stability must be evaluated separately. A three-layer evaluation matrix and practical engineering recommendations are included to help teams turn fast-looking output into genuinely usable results.

    1. Background: Plan Quality Is Not Execution Quality

    In real engineering work, a common approach to a large change in a medium-sized, complex project is to first have an AI coding tool produce a detailed execution plan: break the work into several major steps, and for each step specify which files to change, what to change, and how to verify it. These plans often look flawless.

    But hand the same codebase and the same plan to different models, and the results can vary dramatically. Some models pass on the first attempt with comparable implementation quality. Some proactively ask about ambiguous points, clarify, and then execute. Others start fast, claim two steps done within minutes, then get stuck on reasons like “environment/cache anomaly” that do not actually exist, repeatedly ask for instructions, and finally fall into shotgun-style edits—one line here, one line there—still failing to compile and pass tests after hours and thousands of changes.

    The core conclusion this exposes: plan generation, code generation, and long-horizon execution stability are three distinct capabilities that must be evaluated separately.

    2. Failure Mode Breakdown: Four Typical Breakdowns

    2.1 Misattribution (Treating Code Errors as Environment Problems)

    Models tend to explain compilation failures as external factors such as caches, dependencies, or toolchains. The real cause is usually incorrect code. Misattribution stops the model from self-checking and pushes it to ask a human for help, turning a task that could close the loop automatically into manual fallback.

    2.2 No Minimal Verification Loop

    The correct approach is “change one step, compile once, run tests once,” rolling back or fixing on failure. The failure pattern is: after performing a cleanup action, the model moves to the next step without recompiling, so errors keep accumulating.

    2.3 Shotgun Programming

    Without locating the root cause, the model randomly changes multiple places, and each change introduces new uncertainty. The number of changes is negatively correlated with success rate—the more it changes, the messier it gets.

    2.4 Long-Horizon Consistency Decay

    The later the step, the more likely the model forgets constraints (for example, “tests must pass before continuing”), leading to misalignment between the action layer and the structural layer, layout errors, and states that fail to converge.

    3. Building a Reusable Evaluation Matrix

    Instead of only asking “does it run,” split evaluation into three layers:

    • Plan layer: Are steps verifiable, is the dependency order correct, and are there rollback points?
    • Execution layer: Does it follow the minimal loop, can it diagnose compilation/test failures itself, and does it ask when there is no basis rather than fabricating reasons?
    • Artifact layer: Functional correctness, edge-case handling, maintainability, and, for visualization/interaction tasks, common-sense soundness (for example, spacing of physical ornaments, playability, and interface readability).

    Illustrative verification constraints can be written as pseudocode that forces the model to comply:

    1. for step in plan.steps:
    2. apply(step.patch)
    3. build = run_build()
    4. if build.failed:
    5. locate_root_cause(build.log) # attributing to "environment anomaly" is forbidden
    6. fix_minimally()
    7. assert run_build().success
    8. assert run_tests().success # must not proceed to the next step unless it passes

    4. Engineering Implementation Recommendations

    1. Externalize verification: Let the pipeline handle compilation and testing, have the model submit only patches, and return failure logs in a structured form to reduce room for “fabricating reasons.”
    2. Limit the scope of each change: Commit in small steps that can be rolled back, avoiding thousands of changes at once.
    3. Enforce root-cause localization: Require the model to state a hypothesis for the failure and how it will verify it before making changes.
    4. Supply context in layers: Keep global constraints (coding standards, directory responsibilities, interface contracts) resident, and retrieve local details on demand to mitigate long-horizon forgetting.
    5. Move human-AI collaboration points earlier: Concentrate ambiguous points that need clarification in the planning phase instead of repeatedly interrupting mid-execution.
    6. List visualization and common-sense checks separately: For tasks involving rendering, layout, or physical effects, add automated or manual acceptance items for playability and readability.

    5. Conclusion

    Evaluating LLM code capabilities cannot rely only on demo effects and generation speed. What truly determines adoption cost is long-horizon execution stability, honesty in self-checking, and recovery ability after failure. Building the verification loop into the process, shrinking change granularity, and making root-cause localization a hard constraint are what turn “looks fast” into “actually usable.”