# Editing Files Reliably — Coding Agents & AI-Assisted Development

Source: https://www.geekswithgeeks.com/en/coding-agents/t-edit

> Compare whole-file rewrites, search/replace edits and diffs.

## Small, checkable edits beat rewrites

Editing is where agents most often go wrong, so the **edit format** matters. **Rewriting the whole file** is simple but slow, expensive and risks silently changing unrelated lines. **Search/replace edits** give the exact old text and the new text; the tool applies the change only if the old text appears **exactly once**, otherwise it returns a clear error (not found: re-read the file; ambiguous: add more context). **Unified diffs** (the `git diff` format) describe changes compactly but are sensitive to wrong line numbers and stale context. Whatever the format, good harnesses **verify edits applied**, show the resulting **diff**, keep edits **small and focused**, and **re-read** the file when an edit fails because it changed. Stale context (the file changed since the model last read it) is a common cause of failures.

## Exact-match search/replace, run

I ran this with plain Python 3 (standard library only), using a throwaway project created in a temporary folder. The tool refuses an edit whose old text appears twice (it asks for more context), refuses text that is not found (the file may have changed), and applies an edit only when the old text is unique.

```python
def apply_edit(source, old, new):
    """Exact search/replace edit: the old text must appear exactly once."""
    count = source.count(old)
    if count == 0:
        return None, "error: old text not found (the file may have changed; re-read it)"
    if count > 1:
        return None, f"error: old text appears {count} times; include more context to make it unique"
    return source.replace(old, new), "ok"

code = "def f(a):\n    return a * 2\n\ndef g(a):\n    return a * 2\n"
for old in ("    return a * 2\n", "return a * 3", "def f(a):\n    return a * 2\n"):
    result, status = apply_edit(code, old, "def f(a):\n    return a * 20\n")
    print(repr(old[:22]), "->", status)

```

Output:

```
'    return a * 2\n' -> error: old text appears 2 times; include more context to make it unique
'return a * 3' -> error: old text not found (the file may have changed; re-read it)
'def f(a):\n    return a' -> ok
```

## A unified diff, run

I ran this with plain Python 3 (standard library only), using a throwaway project created in a temporary folder. `difflib` produces the standard diff format for adding an early return: removed lines start with `-`, added lines with `+`, and unchanged context lines have no marker. This is what you review before accepting an agent's work.

```python
import difflib

before = """def total(items):
    return sum(items)
"""
after = """def total(items):
    if not items:
        return 0
    return sum(items)
"""
diff = difflib.unified_diff(before.splitlines(True), after.splitlines(True), "a/cart.py", "b/cart.py")
print("".join(diff))

```

Output:

```
--- a/cart.py
+++ b/cart.py
@@ -1,2 +1,4 @@
 def total(items):
+    if not items:
+        return 0
     return sum(items)
```

**Quiz:** Why does the edit tool require the old text to appear exactly once?

- [ ] It is a legal rule
- [ ] To make edits slower
- [ ] Because files can only contain one line
- [x] To avoid changing the wrong place and to detect stale file content

*Answer:* To avoid changing the wrong place and to detect stale file content. Ambiguity or absence signals the model is working from a wrong picture of the file.
