---
title: "Large-scale refactoring with shadow testing"
date: "2026-11"
category: "AI Engineering"
tags: ["AI Engineering"]
description: "As AI gets more capable, the bottleneck in building software is moving from generation to verification..."
source: "https://enriquemark.com/en/posts/refactoring-with-shadow-testing/"
---

As AI gets more capable, the bottleneck in building software is moving from generation to verification. I think it's essential to look at how the very top, frontier teams are changing the way they develop, and the most central example is naturally the production practice at Anthropic (Ant for short from here on). Recently they [migrated their production API from Python to Rust](https://www.anthropic.com/research/migrating-api-to-rust), and what I paid most attention to is how they make sure "trust" gets verified.

Real business logic is pretty complex. We have all kinds of tests, but enumerating the combinations that happen in practice costs far too much. And a lot of business logic isn't that easy to formalize. Sometimes the behavioral tests pass, the tests really are green, the requests succeed, and yet because the languages differ, Rust behaves a little differently from Python in subtle ways. Those differences may only show up under specific combinations, which regular tests have a hard time covering. If one of them shows up in production, it can cause errors nobody saw coming.

A good option here is "shadow testing": compare the results of running the old and new code, look for differences, and find out where exactly they happen. Once you know the differences and can locate them, you can tell whether the business logic stayed the same before and after the refactor, and you know what to fix. [Scientist](https://github.blog/developer-skills/application-development/scientist/) calls this kind of testing a scientific experiment. (The library itself only compares, it doesn't locate. Pinning down where a difference actually happens needs extra design.) It really does work like a science experiment: you set up a control group and an experimental group, then watch the results of the whole system and compare the differences, both in behavior and in performance. Where it differs from a real experiment is that our goal runs the other way. Instead of using differences to find a mechanism, in a refactor the differences (mainly behavioral ones, of course; we obviously want to keep better performance) are exactly what we want to get rid of.

Shadow testing has clear limits too. First, if the code under test has side effects, that directly pollutes the input data and the results become useless. Second, it needs a lot of real data to observe and compare, so it may not suit a brand-new system. Then again, if you've chosen to refactor, you most likely have some data.

Back to Ant's migration. Their situation is different from what typical shadow-testing tools assume. Those tools usually assume a request can safely run twice, or is deterministic, so they run the old code once in production and then the new code once. Their API doesn't have that property. Take billing and usage limits: you obviously can't let both runs do the same thing, or customers get charged twice. And the service itself is Claude, which runs on an LLM, so it's even further from deterministic. The same input won't produce the same output every time. That second one is especially obvious. Even if you send the same request twice, the responses will most likely differ, which means every comparison would get flagged as a problem.

So Ant's approach is replay. Inference doesn't get triggered twice. They cut at the model output, box off the nondeterministic part, and compare how everything else is handled. That "everything else" is highly deterministic in itself: validation before the model call, parsing and assembling the output after it. Apart from inference, which is highly nondeterministic, the rest is just code running, so any difference there reflects a difference in the code. In testing this refactor, their goal stayed very clear the whole time: control the variables, then watch the data and look for differences. Everything serves that goal.

Stopping there isn't enough, though. Even once you find the differences, you still have to fix them and bring the system back in line with how it behaved before the refactor. This is where agents come in. What's needed is a verification loop where differences trigger code changes. But you can't throw the raw difference data at an agent. The context would blow up, and there's a privacy problem too. What they did was group and categorize it with scripts first. Only when a new case shows up does it notify an agent: it goes to a coordinating agent, and also gets dropped into their Slack channel so engineers can follow along. If we read this through Poteto's earlier framework, a difference is the outer loop's trigger signal, which reaches the coordination layer and starts the inner loop's development cycle.

Ant has loops too, mainly to serve fast development feedback, and they're also split into inner and outer. (This is easy to mix up with Poteto's. Think of them as divisions at two different levels: one is the signal-triggered loop, the other is the internal development loop.) The inner loop handles fast development and checking, letting agents iterate and fail in a safe, controlled environment. It also pins down the nondeterministic model step, using a script to simulate model replies, then runs the experimental and control groups and compares the results. A difference triggers a fix loop: reproduce the difference first, then fix its cause, and finally add a regression test. Once that passes, open a PR.

At this point it switches to the outer loop, which is also where the human checkpoint sits. The upside of putting the checkpoint here is that what you get has already been through many rounds of iteration. Engineers don't have to focus on trivial details. Their precious attention only goes to checking whether the agent's solution is actually appropriate, because a solution that passes the validator isn't necessarily right in practice. The engineer's job here is to make fuzzy decisions in the real context, in other words, to make judgment calls. Only after making sure nothing's wrong does the fix really enter the outer loop, which is the shadow testing deployed in real production that I described earlier.

There's also a memory loop. Lessons from the inner loop's heavy iteration, along with the precious results of human review, get organized and stored here, and every later check and regression run draws on them. This part is a matter of context management. Beyond that there are the tests that failed before and after the refactor, patterns enforced with lint, and a rule that if the original Python version gets changed, the change has to show up in the Rust version too, or else an exemption has to be explicitly requested. While the refactor was going on, the old Python codebase itself kept getting iterated and upgraded. The two had to move in parallel, which means they had to stay aligned the whole time.

Looking at it as a whole, the process isn't complicated. From start to finish it has only two guiding ideas: loops and verification. Loops depend on signals, and verification is the act of finding signals. So the two form a bigger loop that keeps going. Experimental comparison provides difference signals, signals trigger the development loop, and after development the code goes back into the production experiment to keep looking for possible signals, until it's good enough to route most production requests to it. Then the whole refactor is declared done.

As AI gets stronger, the ability to build loops will decide the final output. And engineers, like the wizards who used to hand-write assembly, will be positioned more around designing processes, understanding the business, and making decisions and judgments. Process is pretty critical in all this. A good process works on development, and on verification just as much. Above the small processes are bigger processes, until they cover the whole end-to-end process.

---

**Translation note.** I wrote this in Chinese. This English version is an LLM translation, so the wording is not mine even though the thinking is. Original: [基于影子测试的大规模重构](</zh-hant/posts/refactoring-with-shadow-testing/>).
