Topic 2 of 4 · 4 min read
Codemods versus agent edits
Goal: Choose a codemod, agent edits or a mix for a described change and justify it by volume and variation between call sites.
Use a deterministic codemod when call sites follow one pattern, use agent edits when each site needs judgement, and often let the agent write the codemod rather than make every edit. The choice depends on two numbers: how many sites there are, and how much they differ from each other.
Check yourself · 1 of 3
A rename of one import appears identically in 600 files. What is the best tool?
Why it matters
A codemod is a small program that rewrites code by its syntax tree. Tools such as jscodeshift, ast-grep and OpenRewrite do this. It gives the same result on every file and runs in seconds. An agent editing 500 files one by one is slower, costs more, and may treat two identical sites in two different ways. On the other hand, a codemod cannot decide what to do with a call that was written in an unusual way.
How to decide
- High volume, low variation: codemod. Renaming an import, swapping
foo(a, b)forfoo({ a, b }), changing a config key. Write the rule once. - Low volume, high variation: agent edits. Twelve call sites that each wrap errors differently need reading and judgement.
- High volume, some variation: both. Run a codemod for the common shape, then give the agent the leftovers. The codemod should print the files it skipped.
- Tiny and mechanical: search and replace. A plain
sedor editor replace with a reviewed regex is fine when the text is unique.
Have the agent write the codemod
Asking a model to write a 40-line codemod is a small, checkable task. You review one script instead of 500 diffs. Give it three or four real before and after pairs, and ask for tests on those pairs plus a dry-run mode. Run it on one directory, read the diff, then widen.
Before and after
Weak: "Change every fetchUser(id, true) call to fetchUser(id, { includeTeams: true })" given to an agent across 800 files.
Better: ask the agent to write an ast-grep rule for that one shape, run it with --dry-run, and send only the files the rule could not match back to the agent.
ast-grep run --pattern 'fetchUser($ID, true)' --rewrite 'fetchUser($ID, { includeTeams: true })' --lang ts src
What goes wrong
- Regex on code. A text replace changes strings, comments and similar names. Prefer a syntax-aware tool.
- Trusting the codemod blindly. It can be wrong in the same way everywhere. Review the first diffs and run the tests.
- Using the agent for everything. Cost and inconsistency grow with file count.
- No list of skipped files. Sites the codemod did not match then stay old without anyone noticing.
Examples
A jscodeshift transform
module.exports = function (file, api) {
const j = api.jscodeshift;
return j(file.source)
.find(j.ImportDeclaration, { source: { value: 'request' } })
.forEach((p) => { p.node.source.value = 'node-fetch'; })
.toSource();
};
Run it with --dry --print on one folder first. It changes only import declarations, never a string that merely contains the word.
Choosing for three changes
- Rename an import in 600 files: codemod.
- Replace a custom retry wrapper in 9 files, each with different error handling: agent edits, reviewed one by one.
- Replace a call in 400 files where 30 pass extra options: codemod for the 370, agent for the 30 it skipped.
Key points
- Volume and variation between call sites decide the tool: many similar sites suit a codemod, few different ones suit agent edits.
- A codemod gives the same result on every file, which agent edits cannot promise.
- Letting the agent write the codemod turns hundreds of edits into one script you can review.
- Mixed plans work well: the codemod handles the common shape and prints what it skipped, and the agent handles the rest.
- Prefer syntax-aware tools to raw regex when code, strings and comments could look alike.
Common mistakes
- Giving an agent a purely mechanical change across hundreds of files and accepting uneven results.
- Using a text regex where the same characters appear in comments and strings.
- Running a new codemod on the whole repository before checking its output on a small folder.
- Writing a codemod with no report of the files it did not match.
Check yourself · 2 of 3
Twelve call sites each handle errors differently. Which approach fits?
Check yourself · 3 of 3
What is a good use of an agent when a codemod is the right tool?
Practice items, not counted towards your mastery.
Start here
- Migrating Code At Scale With LLMs At Google (opens in a new tab)PaperFree
Google's report on LLM-driven migrations: a plan, per-file edits and automatic validation.
- How is Google using AI for internal code migrations? (opens in a new tab)PaperFree
An earlier Google write-up on where AI helps in migrations and where humans still review.
- OpenRewrite documentation (opens in a new tab)DocsFree
Recipe-based, deterministic refactoring for Java and more; a model for how a recipe should look.
More for this topic
- jscodeshift: a JavaScript codemod toolkit (opens in a new tab)ToolFree
The standard way to write an AST-based codemod for JavaScript and TypeScript.
- ast-grep (opens in a new tab)ToolFree
Structural search and rewrite across many languages, with a pattern syntax that is quick to learn.