A coding agent edits a file by sending the old text and the new text. The tool checks that the old text is in the file, swaps it for the new text and writes the result. If the old text is not found, the edit fails and the model tries again.
That works until the model copies the old text with the wrong indentation. The lines are right, the code is right, and the match fails on leading spaces. Vesper's README lists it as one of two mistakes that small local models make. Vesper, an MIT licensed coding agent, deals with it in its edit_file tool. This is how the tool handles it and where the approach stops working.
The order of attempts
The tool tries these in order.
- If the old text is not in the file, it checks whether the model pasted the line-number prefixes that
read_fileshows, and strips them. - It counts exact matches. One match, or several with
replace_allset, and it replaces them. More than one withoutreplace_allfails with the count and a request for more surrounding lines. - With no exact match, it looks for the same lines with indentation and trailing spaces ignored.
- If that finds nothing, the error names the file line most similar to the first line of the old text, found with
difflib.get_close_matches, so the model can see what it probably meant.
Step 3 is where an edit can land in the wrong place, so it is the one worth looking at.
One match or nothing
The fallback compares whole lines after stripping each one:
want = [line.strip() for line in old.strip("\n").splitlines()]
lines = text.splitlines(keepends=True)
...
return [(starts[i], starts[i + len(want)]) for i in range(len(lines) - len(want) + 1)
if all(lines[i + j].strip() == want[j] for j in range(len(want)))]
Then the caller applies a rule. Zero blocks is an error, more than one is an error, exactly one is accepted. A log() call that appears in two branches stays a failure:
text = "if a:\n log()\nif b:\n log()\n"
_find_block(text, " log()\n") # two blocks, so the edit is refused
Ignoring indentation makes the match looser, so it can hit places an exact match would not. Requiring a single hit is what keeps that from turning into a guess. When the edit is refused, the model is told how many places matched and to add surrounding lines.
Re-indenting the new text
Accepting the match is half the job. The model's new text carries the same wrong indentation as its old text, so pasting it in would damage the file. _fit_block compares the first indented line of what it found in the file with the first indented line the model gave, and shifts every line of the new text by the difference.
have = next((_indent(line) for line in found.splitlines() if line.strip()), "")
gave = next((_indent(line) for line in old.splitlines() if line.strip()), "")
...
if have.startswith(gave):
lines = [have[len(gave):] + line if line.strip() else line for line in lines]
elif gave.startswith(have):
extra = gave[len(have):]
lines = [line.removeprefix(extra) for line in lines]
It also keeps the file's line endings: if the block it replaces uses \r\n, so does the new text.
I ran it on a loop where the model dropped four spaces from every line:
old = "for x in items:\n out += x\n"
new = "for x in items:\n out += x * 2\n"
# in the file the loop sits at 4 spaces and its body at 8
# result: ' for x in items:\n out += x * 2\n'
The block replaced is the right one, and the new body sits at the file's indentation.
The tool's result text says matched ignoring indentation, so the model knows the edit was not exact. The edit then goes through the same approval step as any other, with a diff, so a person sees what landed.
What this doesn't fix
The shift is one constant, not a per-line mapping. That fails in two cases, and I confirmed both by running the functions.
- The model collapses nested levels, for example 2 and 4 spaces where the file has 4 and 8. The match is still correct, because the stripped lines are equal, but the inner lines come out at 6 spaces instead of 8. In my run a three-level block came back at 4, 6 and 8 spaces where the file has 4, 8 and 12.
- The file uses tabs and the model wrote spaces. Neither indentation is a prefix of the other, so no shift happens and the new lines keep the model's spaces inside a file that uses tabs.
In both cases the approval diff shows the problem, but in Python the first one can change what the code means.
The fallback also matches whole lines only. If the old text is a fragment from the middle of a line and does not match exactly, there is no fuzzy match: the edit fails and the model gets the closest line as a hint.
Why not match fuzzily on everything
A fuzzy match that picks the most similar block would accept many more model mistakes. It would also sometimes edit a place the model never meant, and an agent that does that once loses trust fast. The choice here is to accept one narrow class of mistake, wrong leading and trailing whitespace, and to refuse anything ambiguous. The model gets a clear error and corrects itself. The cost is one extra round trip.
If you build something similar
- Make the loose match require exactly one hit, and say how many hits there were when it fails.
- Tell the model when an edit matched loosely, so it learns from the result text.
- Keep the user's approval step on loose matches too. A diff is the last check.
- Re-indent the replacement by the same offset you matched with, and know that a single offset cannot repair a block whose inner levels are wrong.
The code is in src/vesper/workspace.py of https://github.com/Kernel-Hunter/vesper (MIT). If you find an edit it gets wrong, a small file that shows it is the most useful thing to send.
Top comments (1)
For the collapsed-levels case, the match already hands you a per-line table: line i of the model's old text pairs with line i of the file block, so you can map each model indent to a file indent (2→4, 4→8, 6→12) and apply that map to the new text. The same map covers the tabs case, since it can send four spaces to a tab. If one model indent maps to two different file indents, the model's nesting disagrees with the file and refusing is the safer call. What's left is a deeper level that the old text never showed; extending by the file's step and saying so in the result text seems reasonable.