Topic 4 of 4 · 4 min read
Verifying migrated code at scale
Goal: Choose verification steps that catch missed call sites and silent behaviour changes after a migration.
Verify a migration with several cheap checks that each catch a different failure: the compiler for broken code, tests for changed behaviour, a search for leftovers, and a sampled read of the diff. No single check is enough, and a green build alone proves little.
Check yourself · 1 of 3
After a migration, what is the most direct check for missed call sites?
Why it matters
An agent can produce code that compiles and still miss a call site, change a default or "fix" a failing test by editing it. At scale, nobody reads every line, so the checks have to do the work. Google's migration papers describe automatic validation as part of the pipeline, with humans reviewing the results.
How to do it
- Search for leftovers. Rerun the inventory search. The number of old-API matches should be zero, or each remaining one should be on a written exception list.
- Type check and build the whole repository, not only the touched modules. Shared types break callers far from the edit.
- Run the full test suite, and compare the list of changed test files with the plan. A migration that edits many tests deserves a close look: the agent may have changed the expectation, not the code.
- Sample the diff. Read a random 10 percent plus every file the agent flagged, every file over a size limit, and every file with an unusual pattern. Use a script to list "files whose diff does not match the recipe shape".
- Compare behaviour before and after. Snapshots of API responses, rendered output, query plans or a benchmark on both versions. This catches changed defaults that tests do not cover.
- Roll out carefully. Staging first, then a small share of traffic, with a fast way back.
# leftovers: expect no output
rg "from 'request'" src
# files where tests were edited by the migration
git diff --stat main -- '**/*.test.ts'
Before and after
Weak: "Build passes, merge." Better: leftovers search is empty, the full suite is green with no unexplained test edits, 30 sampled files match the recipe, and a before-and-after comparison of 50 recorded API responses shows no differences.
What goes wrong
- Trusting the type check. Runtime behaviour, such as a changed default, is invisible to it.
- Counting green tests as proof. The tests may not cover the changed path, or may have been edited.
- Sampling only easy files. Pick samples by risk and by randomness.
- Leftover sites in strings or config. Dynamic imports, YAML and docs can hold the old name; search them too.
Examples
A leftovers check
rg -n "from 'request'|require\\('request'\\)" src scripts config docs
test "$(rg -c "from 'request'" src | wc -l)" -eq 0 && echo clean
Searching scripts, config and docs as well as src finds the old name in places the type checker never looks.
Catching a silent default
After the upgrade, the new client retries three times by default and the old one never retried. Types and tests pass. A comparison of recorded requests before and after shows a payment call now sent twice on timeout. The fix is to set retries to zero for that call and add a test for it.
Key points
- Rerun the original inventory search after the migration; the leftover count should be zero or explained.
- Type checks catch broken code but not changed behaviour, so add tests and before-and-after comparisons.
- Treat edited test files in a migration as a warning sign and read those diffs.
- Sample the diff by risk and at random, and use a script to flag files that do not match the recipe shape.
- Roll out gradually with a fast way back, because some behaviour changes only show in real use.
Common mistakes
- Treating a passing build as proof that the migration is complete.
- Not searching strings, config files and docs for the old API name.
- Accepting many edited tests without checking whether the expectations were weakened.
- Sampling only the files that are easy to read.
Check yourself · 2 of 3
Types and tests pass, but a library default changed silently. What catches it?
Check yourself · 3 of 3
A migration edited 80 test files. What is the right reaction?
Practice items, not counted towards your mastery.
Start here
- Migrating Code At Scale With LLMs At Google (opens in a new tab)PaperFree
Google's report on LLM-driven migrations: a plan, per-file edits and automatic validation.
- How is Google using AI for internal code migrations? (opens in a new tab)PaperFree
An earlier Google write-up on where AI helps in migrations and where humans still review.
- OpenRewrite documentation (opens in a new tab)DocsFree
Recipe-based, deterministic refactoring for Java and more; a model for how a recipe should look.
More for this topic
- Exploring Generative AI (martinfowler.com) (opens in a new tab)ArticleFree
Field notes on where AI coding help works and where checks must catch it.