AI Coding Agent File Editing: Why I Never Let Mine Touch sed
DEV Community

AI Coding Agent File Editing: Why I Never Let Mine Touch sed

I'm building an AI coding agent - an LLM that writes and edits code on its own. The obvious way to let it edit files is to hand it a shell: sed, echo >, cat. I tried it. It's a disaster.

This is a first-principles guide to the structured editor tool I built instead - and why an AI agent should never edit code with raw shell commands.

Why raw shell editing breaks an AI agent

Three problems, all fatal:

  • It's blunt. sed -i 's/old/new/' changes every match, everywhere, silently. The model meant one line; it hit five.
  • It's token-expensive. To edit safely the agent runs cat on the whole file, burning thousands of tokens just to see it.
  • It's unreviewable. A stream of echo >> and sed is impossible to audit. You can't tell what the agent actually changed.

The fix isn't a smarter prompt. It's a better tool. The tool is the product.

A structured editor tool: four precise actions

Instead of a shell, give the agent four narrow file actions:

  • open_file - read a file with line numbers
  • create_file - write a new file
  • str_replace - replace one exact piece of text
  • undo_edit - revert the last change

Each one is deterministic and reviewable. No sed, no surprises.

The one rule that makes AI code editing safe: fail loud

str_replace is where all the safety lives. The rule: find the target text, and act only if it appears exactly once.

  • 0 matches → return an error ("not found"). Don't touch the file.
  • 2+ matches → return an error ("appears 3 times - add more context"). Don't guess which one.
  • exactly 1 → replace it, save, confirm.

Why so strict? If the model says "replace x = 1" and that text is in five places, editing the wrong one silently corrupts the code - and nobody notices until much later. Forcing uniqueness makes every edit unambiguous. The tool refusing is a feature, not a bug.

open_file returns line numbers - on purpose

When the agent reads a file, each line comes back numbered:

12: def handler(request):
13: return None

That's not decoration. It's how the model points at exactly where it wants to edit - "line 13" - instead of guessing. Context in, precision out.

Undo is a snapshot, not a reverse

Here's the mistake I made first. I thought undo_edit meant "run the replacement backwards" - swap new back to old. It doesn't work: the new text might appear elsewhere, and you don't have the originals during an undo.

The right model: before every edit, save a copy of the whole file. Undo just writes that copy back. You don't reconstruct the past - you keep a photo of it.

Once you save that snapshot inside str_replace, undo_edit becomes three lines: look up the saved copy, write it back, done.

The takeaway

An AI agent given a bare shell edits code like a bull in a china shop. The same agent, given a structured editor - windowed reads, unique-match replaces, snapshot undo - edits like a careful engineer. Same model. Different tool. The tool layer is the product.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.