Topic 3 of 4 · 4 min read
Batching a migration across many files
Goal: Design a batch plan with checks between batches so one bad batch cannot spread across the codebase.
Run a migration in small batches with a check after each one, so a bad rule is caught in 20 files and not in 2,000. The recipe is the same in every batch. What changes is how much you risk before you look.
Check yourself · 1 of 3
Why run a pilot batch before the full migration?
Why it matters
An agent applies a rule fast and with confidence. If the rule is slightly wrong, it will be wrong in every file it touches. Google's own work on large migrations pairs the model's edits with automatic validation for that reason. Small batches also match good review practice: Google's engineering guide describes small changes as faster to review and easier to roll back.
How to do it
- Pick the batch unit. A module, a directory or N files of the same kind. Aim for a diff a person can skim in one sitting.
- Start with a pilot batch. Choose a few files that cover the common case and one odd one. Review them closely. Fix the recipe, not the files, when you find a problem.
- Gate every batch. Type check, build and tests for the touched modules must pass before the next batch starts. Have the agent stop on the first failure.
- Fan out only after the pilot. Independent batches can run in parallel: separate git worktrees or branches, or headless runs of a CLI agent in a loop. Each run gets the recipe, its own file list and a narrow scope.
- Keep a ledger. A file such as
migration/progress.mdlists each batch, its status and notes. A fresh agent session can pick up from it. - One batch, one commit or PR. A failed batch is then a revert, not an investigation.
while read -r batch; do
claude -p "Follow migration/RECIPE.md for the files in $batch only. Stop on any failure." \
&& npm run typecheck && npm test -- "$batch" || { echo "stopped at $batch"; break; }
done < migration/batches.txt
Before and after
Weak: one overnight run over the whole repository, one huge pull request in the morning.
Better: a 10-file pilot, review, a recipe fix, then batches of about 30 files, each gated and merged on its own.
What goes wrong
- Fan-out before the pilot. Parallel runs multiply a recipe bug.
- Batches that overlap. Two agents editing the same shared file cause conflicts and lost changes.
- No stop on failure. The loop keeps going and buries the first error under later ones.
- Fixing files by hand and not the recipe. The next batch repeats the same mistake.
Examples
A batch plan
Batch 0 (pilot): 10 files from `billing/`, includes 1 odd case. Review by hand.
Batch 1: `billing/` remaining, 28 files. Gate: typecheck + billing tests.
Batch 2: `accounts/`, 35 files. Gate: typecheck + accounts tests.
Batch 3: `shared/`, 14 files, last because everything imports it.
Stop rule: any red gate halts the run and is logged in progress.md.
A bad rule caught early
The pilot shows the agent changing timeout: 30 to timeout: 30000 in places that were already in milliseconds. The fix goes into the recipe ("check the unit before converting"), and the pilot is redone. Had this run across 2,000 files, the change would have landed in every one of them.
Key points
- Small batches limit how much a wrong rule can damage before someone looks.
- Run a pilot batch first and fix the recipe, not individual files, when it shows problems.
- Gate each batch on type check, build and tests, and stop the run at the first failure.
- Fan out in parallel only for independent batches, each in its own branch or worktree.
- Keep a progress ledger and one commit or pull request per batch so any batch can be reverted alone.
Common mistakes
- Starting the full run before a pilot batch has been reviewed.
- Letting parallel agents edit overlapping files or the same shared module.
- Continuing past a failing check so later batches hide the first error.
- Fixing problems in the output by hand without updating the recipe.
Check yourself · 2 of 3
What should happen when a batch fails its type check or tests?
Check yourself · 3 of 3
When is running migration batches in parallel reasonable?
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
- Small CLs (Google Engineering Practices) (opens in a new tab)ArticleFree
Why small, self-contained changes are reviewed better; use it to size migration batches.