Blog 7 min read

AI legacy code rewrite: decide first, then prove the behaviour holds

Before an AI legacy code rewrite, choose between full rewrite, strangler fig or no change, then build the tests and shadow runs that prove nothing broke.

By Ayena TCHIKPE, founder and software engineer Published

In this article
  1. What AI changed in code migration, and what it did not
  2. Refactor vs rewrite with AI agents: a decision table
  3. Go/no-go checklist before you spend tokens
  4. Build the safety net, then rewrite slice by slice
  5. How we approach legacy code with AI

In short

  • AI made writing the new code cheap. Proving it behaves like the old code costs what it always did, so budget for the proof first.
  • Choose between a full rewrite, a slice-by-slice strangler fig and leaving the code alone before you spend a single token. The decision table below helps you choose.
  • Build the safety net before the agent writes anything. Characterization tests, golden outputs, replayed traffic and a shadow run turn "looks right" into a pass or fail.
  • Ship in small slices behind a flag, with a rollback you have tested. We advise against a big-bang AI rewrite of any system whose behaviour nobody can describe in tests.

Your legacy system works, nobody fully understands it, and an AI legacy code rewrite now looks cheap enough to try. The risk is a new codebase that compiles, passes the few tests you have, and quietly drops the business rules your customers rely on. This article gives you a decision table, a go/no-go checklist and a step plan to rewrite legacy code with AI only when you can prove the result behaves like the original.

Fortune reported on 30 October 2026 that Cisco president Jeetu Patel gave engineer Padmanabhan Krishnamurthy "unlimited tokens" with the goal of "refactoring 98% of Cisco's legacy code within 24 months". The article names no tools and says nothing about how the output will be checked.

What AI changed in code migration, and what it did not

AI changed the price of producing code. It did not change the price of knowing that code is right.

In its experience report on internal migrations, Google says that for one migration "80% of the code modifications in the landed CLs were fully AI-authored", with total time "reduced by an estimated 50% as reported by the engineers". But every change was validated automatically, mostly by "building the changed files and running their unit tests", humans still reviewed everything, and on a second migration "the bottleneck in the process was the speed at which engineers could review the changes". The time savings are the engineers' estimates, not a comparative study.

A preprint posted on arXiv on 29 October 2026, When Modernization Breaks Working Code, asked five models to modernise 100 WordPress 4.0 PHP files to PHP 8.3 while preserving behaviour, one file at a time, with no project context and no execution feedback. The result: "Untouched, 83 files already pass every check; the best LLM leaves only 59." In files that needed no change at all, the models broke 36 to 60 percent. The setting is deliberately narrow, but the lesson is direct: without a feedback loop, the agent damages code that was fine, and doing nothing was the better baseline.

83 → 59Files passing every behaviour check before and after the best model's "modernize this" pass, with no context and no test feedback.Mahmud et al., arXiv 2610.40269, 29 October 2026

Refactor vs rewrite with AI agents: a decision table

There are three honest options: leave the code alone, replace it slice by slice behind a router (the strangler fig), or rewrite it in one go. Martin Fowler says of the full replacement plan: "we've seen this simple-sounding plan go down in flames most of the time."

What you see in your system Our recommendation Why
It works, changes are rare, the pain is mostly aesthetic Leave it, add tests around what you touch The arXiv study shows a rewrite can lose behaviour you had for free.
Clear entry points (HTTP API, queue, batch job), active changes, one painful area Strangler fig, slice by slice Each slice can be compared with the old one on the same inputs and rolled back alone.
Runtime or framework is end of life, but behaviour must stay identical Feature-exact migration, slice by slice Same inputs, same outputs, so every slice gets a pass or fail. Change features later.
Small, well-tested component with few integrations Full AI rewrite is reasonable Your existing tests already define the behaviour; the agent works against them.
No tests, no one can explain the rules, money or safety at stake No rewrite yet. First write characterization tests Without a definition of correct, nobody can say the new code is right.
You want new behaviour and a new stack at the same time Split it in two projects Mixing both makes every difference look like a feature or a bug at will.

Go/no-go checklist before you spend tokens

Our rule: two or more "no" answers on the first five lines means you are not ready. Build the safety net first.

  1. We can name the behaviour that must not change: invoices, prices, permissions, reports, exports.
  2. Each item on that list has an automated check. A unit test, an API test or a golden file, not a person who "knows how it works".
  3. We can capture real inputs we are allowed to replay, with personal data masked.
  4. Outputs are deterministic, or can be made so, with dates, random IDs and ordering fixed.
  5. There is a seam to cut along where old and new code can receive the same input.
  6. We can route a share of traffic with a flag and send it back to the old path in minutes.
  7. Someone owns review, with a weekly cap so review does not become the bottleneck Google describes.

Build the safety net, then rewrite slice by slice

The agent needs a judge that says pass or fail, otherwise it will settle for code that looks right.

A characterization test records what the system does today, bugs included. On Hacker News, rstuart4133, who had a model translate a small accounting system to Rust, skipped the hard-to-compare PDF invoices, and the model moved and dropped lines until "the invoice didn't add up". Their fix was to make the PDF deterministic and compare it byte for byte. Do the same with every report, export and file your system produces.

In a shadow run, the old code still answers the user, a copy of each request goes to the new slice, and both results are compared and logged. GitHub's open-source library Scientist works this way: the old path is the "control", the new one the "candidate", and mismatches are recorded.

Diagram of a shadow run. A real or replayed request reaches a router behind a feature flag. The old system answers the user while a copy goes to the new AI written slice. Both outputs go to a compare step. If they are the same, more traffic moves to the new slice; if they differ, the team fixes the slice, adds a test and runs it again.
The new slice earns traffic only when its outputs match the old system on real inputs.
  1. Map the seams and pick a first slice: small, often used, easy to compare.
  2. Freeze behaviour with characterization tests and golden files, green against the old code.
  3. Capture masked real inputs and make outputs deterministic.
  4. Let the agent write the slice against those tests. No new features.
  5. Replay, then shadow until mismatches reach zero or are each explained and accepted in writing.
  6. Shift traffic behind a flag, 1%, then 10%, then all, with a rollback you have rehearsed.
  7. Switch off the old slice and delete its code. Only then improve the new one.

If agents run this work, the characterization suite must block them from calling a slice done. Our guardrails checklist for AI coding agents covers the hooks and CI gates that make it binding.

How we approach legacy code with AI

Our way of working on software starts with the business rules: we write down with you what the tool must do, and never do, in plain language, and every rule becomes a test before the code is written. For backends, our microservices work modernises an existing system piece by piece, without stopping what works, each service with a clear, tested contract. When AI agents do part of the writing, aycod, our open-source guardrail, refuses to let a task end, a commit go through or a push leave until lint, types and tests are green, so a characterization suite becomes a rule the agent cannot skip.

If you are weighing a rewrite, send us an email with what the system does, its stack, roughly how large it is and which tests exist today. We will tell you which row of the decision table you are in and what we would build first.


Sources (as of 8 November 2026): Fortune, Cisco is letting a 27-year-old refactor 98% of its legacy code with unlimited AI tokens (30 October 2026) · Mahmud et al., When Modernization Breaks Working Code: A Single-File Study of LLMs on Legacy PHP, arXiv 2610.40269 (29 October 2026) · Nikolov et al., How is Google using AI for internal code migrations?, arXiv 2501.06972 · Martin Fowler, Strangler Fig · GitHub, Scientist · Hacker News, Cisco gives engineer unlimited AI tokens to rewrite legacy code.

Go from reading to building.

You now know what matters. Tell us what you want to build: the assessment is free, with no commitment.

Get my free assessment

What we have built

Read next

  1. 8 November 2026 · 6 min read Rescue an AI-generated app or rebuild it? How we audit and decide
  2. 2 December 2026 · 6 min read LLM model deprecation migration: a one-week plan for your AI features
  3. 1 December 2026 · 6 min read MCP server security: how to vet the servers your AI agents install
← All articles