Skip to content
mentoor

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?

Choose one answer

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) for foo({ 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 sed or 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?

Choose one answer

Check yourself · 3 of 3

What is a good use of an agent when a codemod is the right tool?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic