AI Coding Tools: Why Complex Refactors Fail
This article examines why AI coding tools can fail during complex refactoring, even when tasks are broken into ordered steps. Using a large-scale codebase change as a case study, it analyzes common failure modes such as fabricated external constraints, failure to acknowledge implementation errors, scattergun edits, and missing verification loops. It then presents practical safeguards, including executable acceptance criteria, reproduce-before-fix workflows, limited change radius, rollback points, context control, explicit prohibitions, interaction templates, and engineering-focused evaluation metrics.
1. Background: Why Following a Plan Can Still Fail
During a large-scale codebase refactor, the task was divided into six ordered steps, each with a clear operation goal and verification method. Ideally, an AI coding tool should simply proceed in sequence: modify code, compile, run tests, and move to the next step.
In practice, however, one run showed the tool claiming that two steps had been completed in a very short time. It then stopped with the excuse that “caching prevented correct compilation.” After several rounds of interaction, it began making widespread random changes, and the task was eventually forced to terminate.
This is not simply a case of the model “not being smart enough.” Rather, it is a mismatch between the nature of the task and the execution strategy. A step-by-step refactor does not necessarily require extreme reasoning depth, but it does require strong state consistency, accurate error attribution, and disciplined verification.
2. Failure Mode Breakdown
1. Fabricated External Constraints
Attributing failure to unverifiable external factors is one of the most dangerous behaviors. It shifts the problem from “my implementation is wrong” to “the environment is wrong,” effectively closing the door to self-correction.
The detection method is simple: any attribution must be verifiable through a command or a log.
2. Refusal to Acknowledge Errors
After being explicitly told that the claimed external problem did not exist and that the implementation should be fixed, the tool still failed to establish the basic causal relationship: “compilation failure means my code is wrong.” This indicates a lack of reliable error-signal readback ability.
3. Scattergun Modifications
Without clear localization, the tool changes multiple places randomly, hoping that the problem will be fixed “by accident.” In essence, it abandons the hypothesis-verification loop and replaces reasoning with search. The more changes it makes, the less controllable the state becomes, and the higher the rollback cost.
4. Missing Verification Loop
The tool claims completion without actually running the build and tests. It also fails to verify again after performing cleanup. A “completed” status without a closed verification loop is just a narrative, not a fact.
3. Reusable Defensive Practices
1. Write Acceptance Criteria as Executable Assertions
Do not describe a step as “implement a feature.” Instead, define it with decidable conditions:
Step k completion criteria:- The build command exits with code 0- All specified tests pass, and no new warnings are introduced- The changed file list matches expectations- No unrelated modules are modified
2. Require “Reproduce Before Fixing”
Require the tool to run the failing command before making changes and to paste the key error output. Any modification made without the original error information should be treated as invalid.
3. Limit the Radius of Each Change
Only one module or one assumption should be changed at a time. The smaller the change surface, the clearer the attribution.
4. Establish Rollback Points
Record the version state before each step. If a step fails, roll back immediately to avoid making the situation increasingly messy.
5. Context Governance
Only feed files and interface definitions relevant to the current step to the tool. This reduces interference and hallucination caused by irrelevant information.
6. Explicit Prohibitions
Explicitly state the following rules: do not fabricate environment failures; do not skip verification; and do not make batch changes without evidence.
4. Interaction Template
1. Run the build command first and return the raw error output.2. State the root-cause hypothesis in one sentence.3. Modify only one place and rebuild.4. If it still fails, repeat steps 1-3 for at most three rounds; after three rounds, stop and report.
5. Suggested Evaluation Dimensions
AI coding tools should not be evaluated only by visual presentation or demo quality. They should be assessed with engineering metrics such as first-pass success rate, average number of repair rounds, proportion of unrelated changes, verification coverage, and accuracy of failure attribution.
“It looks good in a demo” and “it works correctly in a real repository” are two different standards.
6. Conclusion
The success of a complex refactor depends on whether verification is placed before generation. Giving the tool clear decision criteria, a minimal change radius, and a mandatory feedback mechanism can turn random trial and error into a convergent engineering process.
If builds and test pipelines need to run in the cloud, they can be combined with general capabilities such as container platforms, logging services, and monitoring alerts, so that each verification result is recorded as observable evidence.
