Evaluating AI Agent Reliability in Code Refactoring Tasks
This article explains how to assess whether an AI agent can handle large-scale code refactoring reliably. It argues that "did it pass on the first run" should be demoted to an observation metric, while "did it stop honestly at a verifiable boundary" should become a hard requirement. The article covers how to define verifiable tasks with four constraint types, how to separate completion claims from validation evidence, how to spot scattered edits and enforce a change budget, why question quality is itself a capability, how to use separate logic/regression and visual/interaction probes, a suggested evaluation workflow, and how to embed agents into daily team practice.
Large-scale code refactoring rarely fails because a model cannot write a single function. It fails on cross-module constraints, build consistency, and regression validation. A pattern that keeps recurring in these discussions can be abstracted into one class of engineering problem: the plan has already been broken down to an executable granularity, yet the agent uses the environment as an excuse to skip validation, or keeps making scattered edits on top of a wrong assumption until the change surface spins out of control. To evaluate this kind of capability, “did it pass on the first attempt” should be demoted to an observation metric, and “did it stop honestly at a verifiable boundary” should be promoted to a hard requirement.
Define a Verifiable Task, Not a Demonstrable One
A multi-step refactoring task should carry at least four kinds of constraints.
The first is the goal constraint: which behaviors must change and which must not, described in terms of observable results rather than open-ended phrasing such as “optimize this” or “fix the build.”
The second is the validation constraint: what build, unit test, integration check, or manual spot check corresponds to each step, with a rule that failure blocks progress to the next step.
The third is the set of invariants: which public interfaces, data formats, concurrency assumptions, error codes, and performance budgets must remain unchanged.
The fourth is the rollback point: the diff at the end of each step should be independently reviewable, and failure should return the code to the last state that passed validation.
A detailed plan does not automatically produce reliable execution. The value of a detailed plan is that it turns “what to do next and how to prove it is correct” into gates. If the assistant keeps pushing forward while a gate has not passed, the more detailed the plan, the larger the downstream deviation, because errors amplify along subsequent steps.
Separate Completion Claims from Evidence
The engineering default should be that an “it’s done” without attached validation evidence is only a status claim. Minimum evidence includes whether the build actually ran, whether the key error in a failure log was explained, which relevant tests were executed, and whether the results matched expectations. Cleaning build artifacts only, retrying only, or editing comments only does not count as validation.
A common misattribution is to describe a logic error as a cache, environment, or toolchain problem. Environment problems do exist, but the order of judgment should be fixed: first look at the symbol, type, dependency, and test assertion that the error message points to; failures that reproduce consistently in a clean build should be treated as code defects first; only when the same correct code behaves differently in a clean environment versus an incremental one should cache or incremental compilation be listed as a hypothesis, and that hypothesis must be proven with a controlled experiment rather than used to explain every failure.
The controlled experiment can be simple: fix the same commit, then run both an incremental build and a build after a full clean. If both produce the same failure signature, the conclusion “the cache is preventing it from working” is ruled out. If the assistant skips the comparison and immediately asks a human for direction, it has not completed even the minimal diagnosis.
Detect Scattered Edits and Set a Change Budget
“Change one line here, one line there” until something accidentally passes is a typical unauditable modification. The harm is not that one attempt happened to compile, but that:
- The edits are disconnected from the root cause, so a reviewer cannot read the design intent from the diff.
- Tests cover only the path that was hit by accident, while adjacent behavior is silently broken.
- Later steps inherit an unstable patch, and the cost of localization grows exponentially.
- Large amounts of unrelated formatting or renaming drown the real logic changes.
A practical convergence rule is to give every step a change budget and a stop condition. For example: if two consecutive builds fail and the new edits do not explain the same error signature, stop and reread the plan and the failure point instead of expanding the edit surface; if a single step’s diff exceeds the agreed module boundary, submit a review note first and then decide whether to split the step. A budget does not limit creativity; it forces hypothesis updates to happen before more code is changed.
A schematic gate can be written as a checklist rather than bound to one specific set of commands:
- Are the directories this step may modify and the directories it must not modify clearly stated?
- When the build fails, does the assistant cite a specific error location rather than a vague conclusion about the environment?
- Were the relevant tests executed? If not, the step’s status is incomplete.
- Does the diff contain only the changes required to complete this step?
- If the assistant asks a human, does the question narrow the range of uncertainty rather than handing the whole diagnostic responsibility back?
Question Quality Is Itself a Capability
A mature executor asks questions when information is insufficient, but a good question is different from shifting blame. A good question comes with evidence of what was already tried: which assertion failed, which two modules have conflicting assumptions, which plan step is inconsistent with the existing code. Saying only “there’s an environment problem, what’s next” without reproduction or elimination does not reduce uncertainty.
By the same token, not asking questions is not a sign of greater strength. If the requirement is ambiguous, silently picking one interpretation and rolling out a large change costs more afterwards. When evaluating, you can record the number of questions, whether the questions were answerable, and whether the answers actually changed the assumptions. A few clarifications in the middle followed by passing the gates is usually better than either never validating at all or unbounded trial and error.
Use Multiple Probes to Map Capability Boundaries
Code refactoring covers only one dimension: modifying an existing system to a specification. To judge whether an assistant also understands commonsense constraints and interaction results, you need separate, non-substitutable probes, and each probe must have a decidable standard.
Logic and regression probes look at whether, given a plan, the assistant can keep interfaces unchanged, keep existing tests passing, and add new tests that can fail in order to lock in the new behavior. Visual or interaction probes look at spatial relationships, playability, and readability, for example whether object spacing makes the target action physically feasible, whether key elements are visible, whether layers are aligned, and whether the training process actually converges to a repeatable policy. Merely drawing the elements, or animation that looks busy, does not count as passing. Style or effects mentioned in the reasoning process must be judged by the final observable result; when the verbal plan and the finished quality disagree, that should be recorded as hallucination, not as a plus.
Do not merge different probes into a single overall score. Passing one complex refactoring does not prove interaction commonsense is sound; a good-looking demo does not prove the ability to maintain a large codebase. Recording failure modes separately is more useful than chasing one vague “strong or weak” verdict.
A Suggested Evaluation Workflow
Pick a real refactoring with dependencies between steps, not a single-file example. A human first freezes the plan and the acceptance criteria, then the assistant executes on the same code baseline with restrictions on skipping steps. For each step, collect: elapsed time, the moment completion was claimed, the moment validation first actually passed, edit size, whether the failure signature changed, and whether files outside the plan were introduced.
When comparing runs, compare process quality only, not slogans. Passing on the first attempt with a clean diff shows the plan and the model are well matched; multiple failures that converge on the same root cause may still be usable; declaring completion very early and then expanding the edit surface on the same error should be judged not deliverable, even if someone finishes it manually.
Human intervention needs a protocol. Answering clarification questions is allowed; reading the error for the assistant or pointing directly at the line to change is not, otherwise you are measuring the human’s debugging ability. If the assistant repeatedly attributes code errors to the environment, the protocol should require a reproducible experiment; if it cannot produce one, terminate the run to avoid hours of ineffective rewriting.
Applying This to Team Practice
When embedding an assistant in daily development, treat it by default as a collaborator that accelerates drafts and retrieval, not as an unreviewed committer. On the repository side, keep small commits, reproducible builds and tests, and readable failure messages. On the task side, insist on “one step, one proof”: a step that has not passed validation is not merged and is not used as a premise for the next step. On the review side, look first at whether the diff is explainable, and only then at speed.
If cloud-based build, log, and artifact management is used to retain each attempt, store only the inputs, logs, and diffs relevant to reproduction, and avoid sending secrets or production data into the conversation context. Checks that need to run for a long time belong in continuous integration rather than in the conversation transcript, so that “did it really compile and pass tests” does not depend on the assistant’s self-report.
The conclusion is direct: whether an agent can carry out step-by-step refactoring is not measured by how fast it declares two steps complete, but by whether it attributes the first failure correctly, narrows the change, and revalidates. Writing gates, change budgets, and separate probes into the evaluation is what lets a team distinguish reliable engineering collaboration from uncontrollable trial and error.
