Topic 2 of 4 · 4 min read
Choosing codemods or agents for an upgrade
Goal: Choose between a deterministic codemod and agent edits for each part of a migration and justify the choice.
Use a deterministic codemod for changes that follow a rule, and use an agent for changes that need judgement; most real migrations need both. A codemod is a program that rewrites code through its syntax tree. OpenRewrite does this for Java and other languages, and jscodeshift does it for JavaScript and TypeScript. Both are repeatable: the same input always gives the same output.
Check yourself · 1 of 3
Which change is best handled by a deterministic codemod?
Why it matters
An agent can be asked to rename a function in 300 files. It will probably get 295 right and quietly miss five, and a second run may give a different result. A codemod gets all 300 the same way, fast and for free. But a codemod cannot decide what to do when the old code has no direct replacement. That is where an agent earns its cost.
How to choose
Split the migration into change types and ask three questions for each:
- Is there a rule that never needs context? Renaming an import, changing a method name, swapping argument order. Use a codemod, or an existing recipe. Check first whether OpenRewrite or the library's own authors ship one.
- Is the output checked mechanically? If a compiler or a test will catch a wrong edit, an agent is safer to use.
- Does it need to read surrounding code to decide? Replacing a deprecated API whose new version needs a different call structure, or tests that must be rewritten by intent. Use an agent, in small batches.
A good order: run the codemod first for all mechanical changes, commit it alone, then give the agent only the remainder. The agent can even write the codemod: ask it to produce a jscodeshift transform, test it on fixtures, and then run the transform yourself.
Before and after
Weak: "Upgrade every file to the new Router API."
Better:
Step 1 codemod (jscodeshift): rename useHistory -> useNavigate, 214 call sites
Step 2 agent: history.push(x) -> navigate(x) when x is a string;
when state or replace options are passed, list the file for a human
Step 3 grep: zero "useHistory" left
What goes wrong
- An agent doing a rule-based job. Costly, slow and inconsistent, and the misses are hard to see.
- A codemod doing a judgement job. It applies a rule where the context is different and creates subtle bugs, such as a changed default.
- Untested codemods. A transform with no fixtures can change code you did not mean to touch.
- Mixing both in one commit. The reviewer cannot tell mechanical lines from judgement lines, so review gets weaker.
Examples
A split of one upgrade
Upgrading a Java service from one framework major version to the next, the team ran the published OpenRewrite recipe for renamed classes and imports. It touched hundreds of files in one commit. An agent then handled the 12 places where a removed configuration option needed a new design, each in its own PR.
The agent writes the tool
// transform.ts, written by the agent, tested on 6 fixture files
export default function transform(file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.Identifier, { name: 'useHistory' })
.replaceWith(j.identifier('useNavigate'))
.toSource();
}
The human reviews the transform and the fixtures once, instead of reviewing 214 edits.
Key points
- A codemod gives the same output every time, so use it for any change that follows a rule.
- Use an agent where the right edit depends on the surrounding code, and keep its batches small.
- Check for an existing recipe or codemod from the library's authors before writing anything.
- Run the codemod first, commit it alone, then give the agent only what remains.
- An agent can write and test a codemod on fixtures, which gives you speed and repeatability.
Common mistakes
- Giving an agent a pure rename job across hundreds of files and trusting the count.
- Using a codemod on cases where the same old code means different things in different places.
- Putting mechanical and judgement changes in the same commit.
- Running a codemod with no fixtures and no tests of its own.
Check yourself · 2 of 3
In a mixed migration, what is a good order of work?
Check yourself · 3 of 3
A teammate asks an agent to write a jscodeshift transform. What should you ask for before running it on the repo?
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.