Skip to content
mentoor

Topic 1 of 4 · 4 min read

Planning a migration with an agent

Goal: Produce a migration plan with an inventory, a per-file recipe and a batch order that an agent can execute.

Plan a migration before the agent touches a file: count every call site, read the upgrade guide, and write one recipe that says exactly how a single file changes. Agents are good at following a clear recipe. They are poor at inventing one halfway through 400 files.

Check yourself · 1 of 3

You are about to migrate 300 files to a new API with an agent. What should you build first?

Choose one answer

Why it matters

A migration is a change you repeat many times: a framework upgrade, a renamed API, a new logging library. If you start with "migrate us to v5", the agent will make up rules as it goes, and each file may follow a different rule. Google's papers on LLM-driven migrations describe the same shape: find the places to change, make the edits, then validate them automatically.

How to do it

  1. Inventory first. Use a search tool, not the model's memory. grep, ripgrep or ast-grep give a real list with counts. Save it to a file such as migration/inventory.txt. An agent reading "all the usages" will miss some; a search will not.
  2. Read the upgrade guide yourself. Take the official changelog and migration notes for both versions. Note breaking changes, removed APIs and changed defaults. Defaults are where silent behaviour changes hide.
  3. Write a per-file recipe. Three to eight rules, each with a before and an after, plus "leave alone" cases and a "stop and ask" case. Put it in a file the agent reads at the start of each run.
  4. Group and order the files. Group by module or by how hard the case is. Do leaf modules first, shared libraries last, or the reverse if your tests demand it. State the order.
  5. Define done. Name the check that proves a file is migrated: type check passes, the old import no longer appears, its tests pass.

Before and after

Weak task: "Upgrade our HTTP client library to the new version and fix everything."

Better task: "Read migration/RECIPE.md and migration/inventory.txt. Migrate only the files in batch 1. Do not edit tests. Stop and list any call site the recipe does not cover."

What goes wrong

  • The agent builds its own inventory. It finds the easy sites and reports success. Give it the list and ask it to tick items off.
  • The recipe has no "leave alone" rules. The agent then rewrites generated files, vendored code or tests that must stay as they are.
  • Nobody reads the upgrade guide. A changed default (timeouts, null handling, sort order) passes the type check and fails in production.
  • No stop rule. Without "ask when unsure", an agent will guess on the odd case and the guess looks like the rest.

Examples

A recipe fragment

## Rule 3: replace `request.get(url, cb)`
Before: `request.get(url, (err, res) => {...})`
After:  `const res = await http.get(url)` inside an `async` function
Leave alone: files under `src/generated/` and `vendor/`
Stop and ask: any call that passes a third argument

The stop rule turns the odd case into a question instead of a guess.

An inventory with counts

rg -l "from 'request'" src | sort > migration/inventory.txt
wc -l migration/inventory.txt

The count (say 212 files) is the number you later check against. If the final search shows anything above zero, the migration is not finished.

Key points

  • Build the inventory with a search tool and save it to a file; do not ask the model to remember every call site.
  • Read the upgrade guide yourself and list breaking changes and changed defaults before any edit.
  • Write a per-file recipe with before and after examples, leave-alone cases and a stop-and-ask rule.
  • Order the batches on purpose, and name the check that proves each file is done.
  • A plan the agent can tick off item by item is easier to review than a vague goal.

Common mistakes

  • Asking the agent to find all usages from memory instead of running a search.
  • Skipping the official migration notes and trusting the model's knowledge of the old and new API.
  • Writing a recipe with only the happy-path rule and no leave-alone or stop-and-ask cases.
  • Starting edits before deciding what proves a file is done.

Check yourself · 2 of 3

What belongs in a per-file migration recipe?

Choose one answer

Check yourself · 3 of 3

Why read the upgrade guide yourself before the agent starts?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic