Sub topic 12
Combining the Agent with Codemods and Structural Search
This chapter assumes you can read TypeScript or Python, use Git branches and commits, and run a test suite and linter from the command line. It also assumes you have used a coding agent before.
Why pair the agent with a deterministic tool
A coding agent edits files by predicting text. That works well for changes that need judgment. For a mechanical change, such as renaming a function at 200 call sites, it is a poor fit. The agent may skip a file, edit one call differently from the others, or alter unrelated lines.
A codemod is a script that rewrites code by a fixed rule. Structural search matches code by its syntax tree (the parsed shape of the code), not by its raw characters. A tool that does both applies the same rule everywhere and gives the same result on every run.
The split of work is simple:
- The deterministic tool performs the repetitive edit.
- The agent writes or refines the rule, runs the tool, and checks the result.
- You review the diff and the test results.
Text search versus structural search
A plain text search for getUser( also matches comments, strings, and unrelated functions that share the name. A structural search matches only real function calls, whatever their formatting or number of arguments.
| Approach | Matches | Risk |
|---|---|---|
| Find and replace | Any text that looks right | Edits comments, strings, and look-alike names |
| Structural search (ast-grep) | Code with the matching syntax shape | Rule may be too broad or too narrow |
| Agent edits by hand | Whatever the agent notices | Missed files, inconsistent edits |
A worked example with ast-grep
ast-grep is a command-line tool for structural search and rewrite. In a pattern, $$$ARGS matches zero or more arguments and captures them so the rewrite can reuse them.
Suppose getUser was renamed to fetchUser and every TypeScript call must follow. First, preview the rewrites without changing any file:
ast-grep run --pattern 'getUser($$$ARGS)' --rewrite 'fetchUser($$$ARGS)' --lang ts src/Read the preview. Then apply it:
ast-grep run --pattern 'getUser($$$ARGS)' --rewrite 'fetchUser($$$ARGS)' --lang ts --update-all src/Because the rule is applied by a tool, every call is rewritten in the same way, and arguments are copied unchanged.
A workflow that keeps you in control
Ask the agent to follow these steps:
- Search first and report how many matches the pattern finds. Compare that number to your own estimate.
- Preview the rewrite and show a sample of the changes.
- Apply the rule only to the directories in scope.
- Run the tests, type checker, and linter.
- Commit the result as its own checkpoint.
A match count far from your estimate is an early warning that the pattern is too broad or too narrow.
Common errors
- Pattern too broad.
getUser($$$ARGS)also matches a method with the same name on an unrelated object. Check the preview for matches outside the code you meant to change. - Pattern too narrow. A call written as
api.getUser(id)may not match a pattern written for a bare function call. Search for the old name afterward to find leftovers. - Wrong language flag. The
--langvalue must match the files. A mismatch gives zero matches or parse errors. - Skipping the preview. Applying with
--update-allstraight away leaves you to find mistakes in a large diff. - Mixing mechanical and manual edits. Commit the codemod output alone. Fix the cases it could not handle in a separate commit, so the reviewer can trust the first one.
Exercise
You ask an agent to rename getUser to fetchUser across src/. ast-grep reports 212 matches, but a text search for getUser( finds 230 lines. Name two likely explanations and one check for each.
Show answer
First, the extra lines may be comments, strings, or documentation, which structural search correctly ignores. Check by listing the unmatched lines and confirming they are not code. Second, some calls may have a shape the pattern misses, such as api.getUser(id) or a call in a file type outside --lang ts. Check by searching for the old name after the rewrite and reading each remaining hit.