Sub topic 13
Planning and Tracking a Large Migration
This chapter assumes you can read and write code, use Git branches and commits, run a test suite and a linter, and have used a coding agent. It also assumes you know the earlier ideas in this tutorial: scoped tasks, characterization tests, small steps, and diff review.
Why large migrations need a plan
A migration moves code from one library, framework, or API to another. Examples are a library upgrade, a switch from one HTTP client to another, or a rename that touches hundreds of files. One agent session cannot hold all of that. The context window fills up, and the agent loses track of what it already changed.
The fix is to keep the plan and the progress outside the conversation, in files you can commit. Any new session can then read them and continue.
Step 1: Inventory the work
Before editing, find every place the old API is used. Use a deterministic search rather than the agent's memory.
grep -rn "getUser(" src/ --include="*.ts" | cut -d: -f1 | sort | uniq -c | sort -rnThis prints each file with its number of call sites, largest first. Save the result. It gives you the size of the job and shows which files are riskiest.
Step 2: Split into batches
Group the files into batches that can be reviewed and reverted independently. Good batch boundaries are:
- One module or directory per batch.
- Low-risk files first, so you learn the pattern cheaply.
- Shared code, such as utilities and types, early, because other batches depend on it.
Keep each batch small enough to review in one sitting.
Step 3: Write a tracking file
Create a checklist in the repository, for example MIGRATION.md. It is the agent's memory between sessions.
# Migration: getUser -> fetchUser
Rule: replace getUser(...) with fetchUser(...). Do not change return handling in this migration.
Verify each batch with: npm test && npm run lint && npx tsc --noEmit
## Batches
- [x] 1. src/utils (4 files) - commit a1b2c3d
- [x] 2. src/api (7 files) - commit d4e5f6a
- [ ] 3. src/components/profile (12 files)
- [ ] 4. src/pages (20 files)
## Notes
- src/legacy/auth.ts uses a custom wrapper. Handle by hand.Each session starts with a prompt such as: "Read MIGRATION.md. Do only the next unchecked batch. Run the verify command, then tick the box and add the commit hash."
The loop
Keep the old and new paths working
During a migration, the codebase is in a mixed state. Old and new code must run side by side, or the main branch breaks between batches. Common ways to do this:
- Add a thin adapter so old call sites keep working while new ones use the new API.
- Put the new behavior behind a feature flag, so you can switch it off without a revert.
- Migrate callers first, and remove the old implementation last.
Removing old code is the final batch. Do it only after nothing references it, which a search confirms.
Where agents go wrong on long jobs
- Drift between batches. Later batches use a slightly different pattern. State the rule once in
MIGRATION.mdand point to a finished batch as the model. - Quietly widening scope. The agent fixes unrelated problems it notices. Say "only the listed files" in every prompt.
- False progress. The agent ticks a box that is not done. Check the box against a search that shows zero remaining old calls in that batch.
Exercise
Why store the migration plan and progress in a committed file instead of only in the chat, and what should the agent's prompt at the start of each new session tell it to do?
Show answer
A committed file survives across sessions and context limits, so any new session can see what is done and what remains. The prompt should tell the agent to read the tracking file, work only on the next unchecked batch, run the verification command, and then update the file with the result and commit hash.