← Back to home

How To Implement Continuous Refactoring With AI Agents

Deliberate refactoring as an enabler for autonomous refactoring.

· Mart van der Jagt

Why Continuous Refactoring matters more with AI

AI Agents struggle with writing good software in bad codebases. Continuous refactoring is the practice that structurally addresses this challenge. The discipline is not new. Kent Beck and Martin Fowler championed it as an ongoing practice long before microservices. What changes with AI is the value of consistency and the cost of maintaining it.

A codebase is the largest and most persistent context an AI agent reads. Consistent patterns produce consistent output; inconsistent patterns amplify into low-quality output. Drift in an AI-driven codebase accumulates faster than under human-only construction. More code is written, while supervision decreases.

The cost side moves the other way. Refactoring used to require sustained human attention, which made it expensive enough to defer. Most of it is pattern recognition: drift from a template, repeated structures, deviations from convention. Agents do this kind of work well, and they do it continuously and cheaply. The historical excuse for letting drift compound is gone.

Together, the two shifts raise the value and the feasibility of continuous refactoring at the same time. This optional discipline is now ready to become the operational baseline.

What is Continuous Refactoring?

Continuous refactoring under AI splits into two distinct workstreams.

Deliberate refactoring is human-led structural work that makes the codebase AI-friendly. It produces the conditions autonomous refactoring needs to operate cheaply, captured in the prerequisites below. The substance is platform-agnostic; the .NET examples in this piece are not.

Autonomous refactoring is agent-led ongoing work. It is an umbrella term for targeting drift: standards drift, dependency drift, architectural drift, contract drift, security findings. All of it is deviation from intended state. It exists in two forms, both repurposed from established practice: codified standards checks and opportunistic refactoring. The first are automated checks that are put in place to verify against rules that are declared in advance, whereas the second verifies opportunistically based on the AI’s ‘gut feeling’.

Prerequisites: build the AI-friendly codebase first

Autonomous refactoring is only economical on a codebase that has been made AI-friendly. Elsewise the agent surfaces more drift than the team can remediate; autonomous refactoring degrades into manual effort with extra steps.

The hard prerequisites are the deliberate-refactoring outputs themselves:

  1. Small, well-bounded services. Microservices, well-bounded modular monoliths or nanoservices.
  2. A standardized solution structure that the agent can navigate without per-repository context.
  3. Infrastructure as code, so environment differences can be reasoned about declaratively.
  4. Cross-cutting concerns centralized in versioned shared packages, so corrections propagate through dependency updates rather than per-repository edits.
  5. Documentation as code, so specifications and contracts live next to the code they describe.
  6. A simple, consolidated tech stack, so proven patterns and fixes scale across the codebase.

The soft prerequisites are organizational. Refactoring is ongoing work that doesn’t immediately translate to feature delivery. The organization must be ready to embrace continuous refactoring as part of the organizational culture. Most organizations adopting AI agents do not have these prerequisites established yet. Going straight to autonomous refactoring without the deliberate work is the dominant failure mode and the most expensive one.

How to implement autonomous refactoring

Autonomous refactoring runs as three practices, distinguished by what triggers them and what reference they compare the repository against.

Codified standards checks verify the repository against the template: version-controlled rules declared in advance. If the template says cross-cutting concerns must come from a specific shared package within a declared version range, the check fails when a repository diverges. The reference is fixed and explicit. Practical examples: project structure conforms to the template, required IaC and pipeline files are present and valid, contract definitions match the deployed surface, dependency versions sit within declared bounds.

Opportunistic refactoring checks the repository against tacit standards: best practices an experienced engineer would apply but that are not codified anywhere. The agent’s judgment, informed by its training and any in-repo guidance, substitutes for the engineer’s instinct. The reference is informal. Practical examples: naming inconsistencies the template does not specify, duplicated structures that could be consolidated, performance and security patterns the agent recognizes but the template has not yet captured.

These first two run in both modes. As quality gates, they block pull requests when drift is detected. As continuous scanning, they surface drift between pull requests on a schedule.

Template update fan-out propagates a template change to every consuming repository. Triggered by a new template version release rather than by repository drift. This is the practice that closes the loop when a standards gap or repeated opportunistic finding gets codified into the template and must reach the entire estate.

The workflow all three follow

  1. Trigger. A standards check fails, an opportunistic finding is filed, or a new template version is published.
  2. Create a story. The agent files an issue per repository with the finding (or template diff) and a proposed correction.
  3. Review the recommendation. Mechanical findings auto-merge under policy. Non-mechanical findings route to a human reviewer.
  4. Plan the fix. Repository-level findings go in the repository. Findings that reveal a template gap go in the template, where they trigger the fan-out cycle.

Practices all three share

Continuous refactoring in relation to nanoservices

Look at the deliberate-refactoring list: smaller services, simple stack, IaC, documentation as code, centralized cross-cutting concerns in versioned packages, standardized solution structure. That list is the nanoservice template, item for item. The relationship is not that they share prerequisites; it is identical work.

Deliberate refactoring is the path to the nanoservice template. It is how you arrive at a place where nanoservices are operationally feasible.

Autonomous refactoring is the maintenance regime that keeps the template’s invariants alive. Without it, hundreds of nanoservices accumulate divergence over many regenerations, and contract-only verification stops working because the underlying assumptions about template uniformity no longer hold.

Continuous refactoring is therefore both the precondition and the maintenance regime for nanoservices.

It is useful without nanoservices. Any codebase benefits from deliberate work toward AI-friendliness, and any sufficiently consistent codebase benefits from autonomous maintenance. Nanoservices is the limiting case where the discipline stops being optional.

What continuous refactoring depends on

Continuous refactoring is not all of engineering. The discipline depends on the following four practices being executed well. Continuous refactoring integrates with each, whereas each of the below is a topic in its own right.

Architectural decisions. Where boundaries fall, what gets codified into the template, when a repository should be rewritten rather than maintained: these are human judgments that feed the discipline. Continuous refactoring makes them data-driven. Repeated opportunistic findings signal a codification candidate. Persistent post-correction drift signals a rewrite candidate. Recurring boundary violations signal a decomposition problem. Architecture review owns the decisions on a regular cadence; continuous refactoring owns the evidence that informs them.

Verification. Autonomous refactoring is only as reliable as the contract and test layer it runs against. Behaviors not expressed in tests can be silently broken by a regeneration. Treat the test suite as load-bearing infrastructure: invest in contract tests at service boundaries, in mutation testing as a check on the tests themselves, and in production observability as the catch-net when the first two miss. The discipline does not eliminate the verification problem; it makes the verification layer first-class.

Data migration. When a correction moves data between aggregates or services, the code regenerates cheaply and the data work does not. Handle code and data as separate units. The agent produces a regeneration PR; the migration is a parallel human-reviewed task with its own runbook. Migration patterns such as zero-downtime cutovers, dual-write windows, and idempotent backfills can be codified in the template so the pattern is consistent. The cutover decision and execution stay human.

Cross-system coordination. Changes that span repositories owned by different teams need sequencing the template cannot fully express. The discipline handles per-repository work autonomously, but coordinated cutovers, such as contract-breaking changes between producer and consumer or dependency upgrades that must align across services, stay human. Contract versioning is the lever: version the producer’s contract first, let each consumer’s agent catch up against the new version on its own timeline, and treat the strategic alignment as architecture work, not refactoring work.

Conclusion

Continuous refactoring stops being cleanup and becomes the operating discipline of agentic engineering. It splits into two workstreams that must run separately. Deliberate refactoring is a prerequisite for enabling the use of AI agents. Autonomous refactoring prevents drift once these prerequisites have been met.

The three practices of autonomous refactoring provide a closed loop. Repeated opportunistic findings across repositories signal a missing template rule. Publishing the new template version then propagates it through fan-out. The set of codified standards grows over time as best practices stabilize.

Frequently Asked Questions

What is continuous refactoring in the context of AI agents?

Continuous refactoring is the operating discipline that keeps an AI-driven codebase consistent over time. Under AI it splits into two workstreams: deliberate refactoring, which is human-led structural work that makes the codebase AI-friendly, and autonomous refactoring, which is agent-led ongoing work that targets drift across standards, dependencies, architecture, contracts, and security.

What is the difference between deliberate and autonomous refactoring?

Deliberate refactoring is the human-led work of getting a codebase into a state where agents can operate cheaply: small bounded services, a standardized solution structure, infrastructure as code, centralized cross-cutting concerns, documentation as code, and a consolidated tech stack. Autonomous refactoring is the agent-led work that keeps the codebase in that state once it has been reached.

Why does continuous refactoring matter more with AI?

AI amplifies inconsistency. A codebase is the largest and most persistent context an agent reads; consistent patterns produce consistent output, inconsistent patterns produce low-quality output. More code is written while supervision decreases, so drift accumulates faster than under human-only construction. At the same time, the cost of maintenance work has collapsed: agents handle pattern recognition continuously and cheaply, so the historical excuse for deferring refactoring is gone.

What are the prerequisites for autonomous refactoring?

Autonomous refactoring is only economical on a codebase that has been made AI-friendly. The hard prerequisites are the deliberate-refactoring outputs themselves: small bounded services, a standardized solution structure, infrastructure as code, centralized cross-cutting concerns in versioned shared packages, documentation as code, and a simple consolidated tech stack. Without them, the agent surfaces more drift than the team can remediate and the practice degrades into manual effort with extra steps.

How do codified standards checks differ from opportunistic refactoring?

Codified standards checks verify the repository against an explicit, version-controlled template; the reference is fixed and declared in advance. Opportunistic refactoring checks the repository against tacit standards an experienced engineer would apply but that are not codified anywhere; the agent’s judgment substitutes for the engineer’s instinct. Repeated opportunistic findings are the signal that a tacit standard should be codified into the template.

Does continuous refactoring require nanoservices?

No. Any codebase benefits from deliberate work toward AI-friendliness, and any sufficiently consistent codebase benefits from autonomous maintenance. Nanoservices are the limiting case where the discipline stops being optional: at that granularity, contract-only verification depends on template uniformity holding, and autonomous refactoring is what keeps that uniformity alive across many regenerations.

Can AI agents handle data migrations as part of refactoring?

No. When a correction moves data between aggregates or services, the code regenerates cheaply and the data work does not. Treat code and data as separate units: the agent produces a regeneration pull request, while the migration is a parallel human-reviewed task with its own runbook. Patterns such as zero-downtime cutovers, dual-write windows, and idempotent backfills can be codified in the template, but the cutover decision and execution stay human.

What is template update fan-out?

Template update fan-out is the practice of propagating a template change to every consuming repository, triggered by a new template version release rather than by repository drift. It is how a standards gap or repeated opportunistic finding, once codified into the template, reaches the entire estate. Changes propagate to canary repositories first; full fan-out continues only if the canaries hold.