Topic 1 of 4 · 4 min read
Planning an agent-run migration
Goal: Break a codebase-wide migration into a pilot and batches, each with a check that proves the batch is done.
A migration run by agents is a pilot first, then batches, and every batch ends with a check that proves it is done. Never start with "migrate the whole repo".
Check yourself · 1 of 3
What should you do before asking an agent to migrate a large codebase?
Why it matters
A change across hundreds of files is too big for one prompt and too big to review in one pass. An agent such as Claude Code, Codex or Cursor works well on a bounded slice with a clear target. It works badly on a vague, huge one: it drifts, skips files and reports success it has not earned. Planning turns one scary change into many small, checkable ones.
How to do it
- Inventory. Count what must change before you ask for anything. A search such as
grep -rl "moment(" src | wc -lgives the real size. Save the file list. It is your denominator. - Catalogue the patterns. Open ten or fifteen files and write down the distinct shapes of the old code. For example:
moment()with no args,moment(x).format(...), andmoment.duration. Each shape gets one written "after" form. Edge shapes go in a list marked "ask a human". - Pilot. Pick three to five files that cover different shapes. Run the agent on them, with the catalogue in the prompt. Read every line of the result. Fix the catalogue where the agent was confused, not just the code.
- Size the batches. Group by directory or owner so a batch has one reviewer and one test target. A batch of 20 to 40 files is common, but pick a size you can verify in minutes.
- Write the done-check per batch. Each batch has a command that fails if the work is not done: the tests for that folder, the type check, and a search that returns zero old patterns.
Before and after
Weak plan: "Migrate from moment to date-fns across the app. Be careful."
Better plan:
Goal: remove moment from src/ (412 files import it)
Pilot: src/billing/invoice.ts, src/utils/dates.ts, src/auth/session.ts
Batch 1: src/billing (31 files) check: npm test -- billing && tsc --noEmit && grep -r "from 'moment'" src/billing (expect 0)
Batch 2: src/auth (18 files) same checks for auth
Rule: unknown pattern -> stop and list it, do not guess
What goes wrong
- No inventory. Nobody knows if 90 percent or 60 percent is done.
- One giant batch. The diff is unreviewable, and one bad pattern is copied into every file.
- Check written after the work. The agent then grades itself with a check it designed to pass.
- Mixed goals. A "while you are there" cleanup hides behaviour changes inside a mechanical diff.
Examples
A pilot that changed the plan
The pilot on 4 files showed that the agent turned moment().add(1, 'month') into addDays(new Date(), 30). That is a behaviour change at month ends. The team added a catalogue line: month arithmetic uses addMonths. Only then did batch 1 start.
A done-check as a script
set -e
npm test -- billing
npx tsc --noEmit
test "$(grep -rl "from 'moment'" src/billing | wc -l)" -eq 0
The batch is done only when all three lines exit with 0.
Key points
- Count the files and patterns first; the inventory is the denominator for progress.
- Write a catalogue of old shapes, each with one approved new form and a list of cases to hand to a human.
- Pilot on a few varied files and fix the catalogue, not just the code, before scaling up.
- Size each batch so one person can verify it in minutes, grouped by folder or owner.
- Define the done-check before the work starts, and make it a command that can fail.
Common mistakes
- Asking one agent session to migrate the whole repository in a single prompt.
- Skipping the inventory, so nobody can say how much remains.
- Letting the agent write its own pass criteria after it has done the work.
- Allowing refactors or style fixes inside the migration batches.
Check yourself · 2 of 3
Why run a pilot on three to five varied files before the main batches?
Check yourself · 3 of 3
Which done-check is best for a batch that removes an old library from one folder?
Practice items, not counted towards your mastery.
Start here
- Accelerating code migrations with AI (Google Research) (opens in a new tab)ArticleFree
A large engineering organisation describes combining AI edits with automated validation for code migrations.
- OpenRewrite documentation (opens in a new tab)DocsFree
How recipes make deterministic, repeatable code changes for Java and other languages.
- jscodeshift: a JavaScript codemod toolkit (opens in a new tab)ToolFree
A codemod toolkit that rewrites JavaScript and TypeScript through the syntax tree.