Sub topic 13
Planning and tracking a large migration
A large migration is a change too big for one prompt, one context window, or one pull request. Examples are moving from one HTTP client library to another, upgrading a framework across hundreds of files, or replacing a deprecated internal API. This chapter covers how to plan such a change, split it into batches, and track progress so that you and the agent always know what is done and what is left.
It assumes you have read the earlier chapters on scoping a task, planning before editing, committing small steps, and combining the agent with codemods. You should be comfortable with Git branches, a test suite, and a linter.
Why large migrations need their own plan
A single refactoring fits in one session. A migration does not, for four reasons:
- The agent forgets between sessions. A new session starts without the previous session's reasoning. Anything not written to a file in the repository is lost.
- Scale hides mistakes. An error rate of 2% is invisible across 10 files and certain across 500.
- Reviewers have limits. A 6,000-line diff does not get reviewed carefully. It gets approved.
- The codebase keeps moving. Other people merge changes while you migrate. Old-style code can appear after you removed it.
The answer to all four is the same. Turn the migration into a measurable, repeatable process, and keep its state in the repository, not in anyone's memory.
Step 1: Build an inventory
Before asking the agent to change anything, find out how big the job is. Use deterministic tools for counting. A text search or a structural search gives exact numbers, and the agent's estimate does not.
Record at least:
- how many files and call sites use the old API;
- which patterns occur (for example,
getUser(id)andgetUser(id, { cache: false })); - which modules have weak or no test coverage;
- which usages look unusual, such as wrappers, dynamic calls, or code generated from templates.
The unusual cases matter most. A mechanical rename handles 90% of sites. The remaining 10% cost most of the effort, and they are where an agent is most likely to guess.
Step 2: Choose a slicing strategy
A batch is the unit you transform, verify, review, and commit together. Pick a slicing strategy that makes each batch independently correct.
| Strategy | Batch looks like | Works well when | Watch out for |
|---|---|---|---|
| By directory or module | All files in `src/billing/` | Modules have clear boundaries | Shared utilities used by every module |
| By pattern | All calls of the form `getUser(id)` | A codemod can handle each pattern | Patterns that overlap |
| By risk | Low-risk leaf modules first, core last | Test coverage varies | Pressure to skip the core at the end |
| By owner | Each team's code in its own pull request | Many teams share the repository | Slow coordination |
Start with a pilot batch of one small, well-tested module. The pilot tests your process: whether the prompt works, whether the verification commands catch problems, and whether the diff is reviewable. Fixing the process on 5 files is far cheaper than on 500.
Step 3: Keep a plan file in the repository
Create a file such as MIGRATION.md on the migration branch. It serves as shared memory for you and for every agent session. Start each session by telling the agent to read it and end each session by having the agent update it.
# Migration: getUser -> fetchUser
Goal: no remaining calls to getUser in src/. Behavior of public endpoints unchanged.
Verify every batch with:
npm test && npx tsc --noEmit && npm run lint && ./migration-status.sh
Rules:
- One batch per commit. Do not touch files outside the batch.
- Do not edit tests to make them pass. Report failing tests to me.
- If a call site does not match a known pattern, add it to "Open questions" and skip it.
## Batches
- [x] 1. src/billing/ (pilot, 6 files)
- [x] 2. src/orders/ (14 files)
- [ ] 3. src/accounts/ (22 files)
- [ ] 4. src/admin/ (31 files, low test coverage, add characterization tests first)
## Open questions
- src/accounts/legacy.ts:88 passes getUser as a callback. Rewrite needs a decision.
## Decisions
- fetchUser returns a Promise. Call sites that were synchronous are wrapped in async functions, not changed to use .then.The file has four parts, and each has a job:
- The goal and the verification command define done. The command is the same one every time.
- The rules repeat constraints that would otherwise fade from the agent's attention over a long session.
- The batch checklist shows progress and tells a new session where to continue.
- The open questions and decisions stop the agent from re-deciding the same question differently in each batch. Consistency across batches is a common weak point.
Step 4: Track progress with a number
A checklist records what you intended. A metric records what is true. Write a short script that counts remaining uses of the old API, and run it after every batch.
#!/usr/bin/env bash
# migration-status.sh: count source files that still use the old or the new API.
set -euo pipefail
count_files() {
{ grep -rlE "$1" src --include='*.ts' || true; } | wc -l | tr -d ' '
}
old=$(count_files 'getUser\(')
new=$(count_files 'fetchUser\(')
budget=$(cat .migration-budget)
echo "files using old API: $old (budget: $budget)"
echo "files using new API: $new"
if [ "$old" -gt "$budget" ]; then
echo "Old API usage grew beyond the budget." >&2
exit 1
fiThe file .migration-budget holds a single number, the highest count of old-API files you allow. Lower it by hand in each batch commit. Run the script in continuous integration (CI). This is a ratchet: the number can only go down, so other developers cannot reintroduce the old API while the migration is in progress.
The || true inside the braces matters. grep exits with status 1 when it finds nothing, and with set -e and pipefail that would stop the script exactly when the migration is finished.
Step 5: Run the same loop for every batch
Each batch follows the same sequence:
- Tell the agent which batch to do and to read
MIGRATION.md. - Apply the mechanical part with a codemod where one exists. Let the agent handle the cases the codemod does not match.
- Run the verification command from the plan file. Make the agent report the actual output, not a summary of it.
- Review the diff for this batch only, as you would review a pull request.
- Commit, tick the checklist, and lower the budget in the same commit.
- Start a fresh session for the next batch if the context has grown long.
Step 6 is worth the habit. Long sessions accumulate stale assumptions, and a fresh session that reads the plan file starts from the recorded state instead.
Handling code that cannot move yet
Some call sites cannot be migrated in this pass, for example code that depends on behavior the new API lacks. Do not let the agent force them. Choose one of these options for each:
- Adapter. Wrap the new API in a function with the old signature so the old call sites keep working. Remove the adapter later.
- Skip and record. List the site under "Open questions" and exclude it from the batch.
- Split the work. Make a separate task with its own tests and its own plan.
An adapter lets old and new code coexist, which is also the basis of the gradual rollout covered in the next chapter.
Common problems
- Drift between batches. Batch 3 uses a different error-handling style from batch 1. Prevent it by recording decisions in the plan file and pointing the agent at a finished batch as the reference example.
- Green tests, wrong behavior. In low-coverage modules, passing tests prove little. Add characterization tests before migrating those modules, and list them as their own batch step.
- Batch creep. The agent "helpfully" fixes unrelated issues. Check each diff for files outside the batch list, and keep the rule "do not touch files outside the batch" in the plan.
- A stale plan. If the checklist and the metric disagree, trust the metric and correct the checklist.
Summary
A large migration is a sequence of small, verified batches governed by a plan file and a metric. You inventory first, slice by a strategy that keeps batches independent, pilot on a small module, and repeat one loop. The agent does the work inside each batch. The repository stores the state, and you decide whether a batch is correct.
Exercise
Your migration plan file lists 5 batches, all ticked. Your status script still reports 11 files using the old API. Which do you trust, and what do you do next?
Show answer
Trust the script, because it measures the code and the checklist only records intent. Look at the 11 files the script finds. They are either call sites in a pattern that no batch covered, files added by other developers after the batch was done, or sites the agent skipped. Add them to the plan as new batches or open questions, correct the checklist, and keep the budget in CI so the count cannot rise again.
Exercise
Why does the plan file include a "Decisions" section, and what goes wrong without it?
Show answer
The agent starts each session without memory of earlier sessions. Without recorded decisions, it can solve the same problem a different way in each batch, such as using async/await in one batch and .then in another. The codebase ends up inconsistent, and the differences are hard to spot in per-batch review.