Refactoring With Coding Agents

Refactoring With Coding Agents

Refactoring suits agents well and can do serious damage unconstrained. Mechanical vs judgment refactors, rule-plus-stop-list sweeps, verified increments, and escalation.

Refactoring is a task class well suited to agents, and one where an unconstrained sweep can do serious damage. It scores high on three properties that make a task easier to delegate to an agent and is behavior-preserving by definition, which makes "did behavior change" the central verification question. The discipline is separating mechanical refactors (execute) from judgment ones (propose), specifying sweeps as a rule plus an explicit stop-list, running large migrations in verified increments rather than one big diff, keeping refactor commits separate from behavior changes, and escalating to a human the moment the sweep reveals a decision the rule cannot make.

Why refactoring suits agents

Refactoring scores high on three properties that make a task easier to delegate to an agent. It is precisely specifiable — the transformation can be stated in advance. It is mechanically verifiable — a passing test suite that covers the affected behavior answers the question that matters. And it is pattern-following — renames, extractions, and API swaps are repetitive, well-defined transformations; that models handle them well because they have seen many examples is a working hypothesis, not a documented rule.

The reason the verification is so clean is the definition itself. Refactoring, in Martin Fowler's canonical sense, is a change made to the internal structure of software to make it easier to understand and cheaper to modify, without changing its observable behavior (Fowler, Refactoring; definition at refactoring.com, as of October 2026). Behavior-preserving means there is a central acceptance question — did behavior change — and it is answerable by tests that cover that behavior rather than by reading. That is a far more tractable review than "is this new feature correct," where the answer lives in judgment.

The same properties that make refactoring safe to delegate also make overreach visible. Because the transformation is fully described, any diff beyond it is by definition suspect. You do not have to wonder whether an edit belongs; the rule already said what belongs, and everything else is a flag. That legibility is what makes agents both effective and, unconstrained, dangerous here.

Mechanical versus judgment refactors

The delegation line runs between two kinds of refactor, and confusing them is a source of bad outcomes.

Refactor kindExamplesThe agent's role
MechanicalRenames, API-call swaps, pattern upgrades, dead-code removalExecute the fully described transformation
JudgmentRe-architecting a module, choosing new boundaries, consolidating near-duplicatesPropose options; the human decides

A mechanical refactor is one whose transformation is fully described before it starts — you can write down the rule, and executing it faithfully is the whole job. That is squarely agent territory. A judgment refactor is one where the hard part is the decision: where a boundary should fall, whether two similar things are really the same thing, how a module should be shaped. The edit is easy; deciding what edit to make is the work, and the work is not mechanical.

Agents execute mechanical refactors and, at most, propose options for judgment ones — sketching two possible module boundaries for you to choose between, not choosing. The failure mode is letting an agent make an architectural decision because it produced confident code for one option; a plausible re-architecture you did not decide on is a decision made by default. Deciding stays human; the agent's speed applies to execution, not to judgment.

Constraining the sweep

A mechanical refactor delegated well is specified as a rule plus an explicit stop-list — what to change, and what to leave even though it looks improvable. An illustrative example, using a hypothetical in-house legacyHttp wrapper:

code
Rule: Replace every call to the deprecated `legacyHttp.get(url, cb)` with
      `httpClient.get(url)` returning a promise; update call sites to await.

Stop-list — do NOT, even if it looks better:
  - rename any variables or functions
  - "clean up" adjacent code, reorder imports, or reformat untouched lines
  - change error handling shape (leave existing try/catch as-is)
  - touch files that do not call legacyHttp.get

The stop-list is not optional politeness; it is the load-bearing part. Drive-by improvement is a failure mode to plan for in agent refactors — asked to swap an API call, an unconstrained agent may also rename a variable, reorder some imports, and "tidy" a nearby function, edits that can each look locally reasonable (a working hypothesis about agent behavior, not a documented rule). The diff-minimality check applies with full force: every line beyond the stated transformation is a flag, not a bonus, and the correct response to scope creep is to reject or re-scope it, not to absorb it. Guardrail tests pinned before the sweep make the behavior-preservation claim checkable instead of merely asserted — the mechanics of pinning them are their own topic, but their presence is the precondition that lets you verify a sweep at all.

Increments over big bangs

Large migrations tempt a single sweeping run, and the temptation is worth resisting even when the agent could technically produce the whole thing at once. A 400-file diff nobody can actually review is a process failure whether or not it is correct — "correct" is a claim you cannot verify at that size, and an unverifiable success is indistinguishable from a hidden failure.

The discipline is to slice: break the migration by module or by pattern instance, and verify each slice green before starting the next. Each slice lands as its own commit, so a slice that breaks something rolls back cheaply instead of poisoning the whole migration, and a bisect through the history lands on a small, comprehensible change rather than the entire sweep. The unit of work is what you can review and verify, not what the agent can generate in one pass.

Refactor commits stay separate from behavior-change commits, and this separation earns its keep at review and at debugging time. A commit labeled as a pure rename that also changes a condition has destroyed the reviewer's shortcut — the whole value of "this is behavior-preserving" is that it can be reviewed lightly, and mixing a behavior change in silently revokes that. Keeping them apart means that when a bisect lands on a refactor commit, the result points to a refactor that broke its behavior-preserving promise, and you can check that commit against the rule it was supposed to follow instead of separating an intended behavior change from an accidental one.

When the refactor reveals a decision

A refactor plan is a hypothesis, and sweeps run into things the rule did not anticipate — an inconsistency the transformation cannot resolve, a call site that does not fit the pattern, a case the spec never named. These are escalation points, not places for the agent to improvise a resolution. An agent that "handles" an ambiguity by guessing has quietly made a judgment call inside what was supposed to be a mechanical sweep.

The habit is pause-and-decide: the agent reports the ambiguity and stops, the human amends the rule to cover the new case, and the sweep resumes under the updated rule. This keeps the decision where it belongs and keeps the sweep mechanical on both sides of the pause.

Budgeting for these amendments is realism, not a sign the plan failed. A migration that runs start to finish with no human decision deserves a second look: it may have been simple, or it may be hiding guesses it made. Expecting to stop, decide, and resume a few times is what separates a controlled sweep from a fast one you will be unwinding later.

Project Inquiry