Skip to content
Empryo

ast_edit: no more search and replace roulette

Edits by symbol name, so there's no search string to miss. Started as my Master's thesis.

ast_edit: no more search and replace roulette

You ask an AI agent to change one line. It asks the editor to find a string and replace it. The file uses a tab where the model typed two spaces, so the search finds nothing. The agent re-reads the file, rebuilds the string and tries again. Three tool calls and a few thousand tokens for a one-line change. 😅

The model understood the code. It just had to reproduce the exact bytes of the file, and language models are bad at that.

I build Empryo alone and unfunded. The idea behind this tool is older than the product.

What a string edit asks of the modelLink to this section

  • The exact bytes. Tabs or spaces, Windows or Unix line endings, a trailing space the model never saw. One mismatch and the search comes back empty.
  • A unique match. return null; can appear five times in a file. The model has to add surrounding lines until the search string is unique, and every extra line is another chance for a typo.
  • Valid syntax afterwards. The replacement can land in the middle of an expression or drop a brace, and nobody notices until the typechecker runs.

Naming the symbol insteadLink to this section

Empryo's ast_edit has no search string. In TypeScript and JavaScript files, the agent names the symbol by kind and name, and the tool changes the parsed syntax tree:

ts
ast_edit({
  path: "src/auth.ts",
  action: "set_body",
  target: "function",
  name: "validateToken",
  newCode: "return verify(token, key);",
})

No whitespace to copy and no line number to go stale. The tool finds validateToken however the file was reformatted. The Genome, Empryo's live index of the project, already knows each symbol's kind and location, so the agent skips the extra read to find the target.

Changing a return type is one operation:

ts
ast_edit({
  path: "src/api.ts",
  action: "set_return_type",
  target: "function",
  name: "fetchUser",
  value: "Promise<User>",
})

Multi-step changes run as one batch. Here the function becomes async, gets a parameter and imports User. If any step fails, none apply, so the file is never half-edited:

ts
ast_edit({
  path: "src/api.ts",
  operations: [
    { action: "set_async", target: "function", name: "fetchUser", value: "true" },
    { action: "add_parameter", target: "function", name: "fetchUser", value: "cache: boolean" },
    { action: "add_named_import", value: "./types", newCode: "User" },
  ],
})

The errors are written for a model to act on. Ask for a symbol that does not exist and the error lists every symbol of that kind in the file, so the retry is a lookup instead of another guess.

Late in a sessionLink to this section

The more the agent changes a file, the less its memory of that file matches the disk, and the more string searches miss. Names don't drift. validateToken is still validateToken after forty edits, a reformat and a reshuffle of the imports.

An agent that trusts its edits also stops re-reading files to check them. That is where most of the savings come from.

It started as a thesisLink to this section

The approach comes from my Master's thesis, Typed vs Untyped Programming Languages, written with Artur Matusiak at Axis Communications in 2022. We wanted to migrate JavaScript projects to TypeScript automatically. Regular expressions and then eval() broke on real code, and eval() was a security risk too. Reading the file as one long string failed because a function body spans many lines. What worked was parsing the code into an abstract syntax tree, reading each variable's type with ts-morph, writing the type into the tree and printing it back as TypeScript. No string manipulation anywhere.

ast_edit gives that idea to an agent.