I first noticed this problem a few times in Claude Code.
Sometimes an edit would fail with: "Error editing file: String to replace was not found".
With Claude models, it was relatively rare. Claude Code's harness is tuned around Claude models, so they generally follow its editing workflow quite well. But when I tried other models through OpenRouter, especially models like MiniMax, I started seeing this failure much more often.
Later, while using Pi coding agent with open-weight models, the reason became much clearer. Pi's default edit_tool relies on exact text replacement: "oldText → newText". The oldText must match the current file exactly. Even a small difference in whitespace or indentation can cause: "Could not find edits[0]".
Then the agent has to: "read file again → find exact text → retry".
The deeper issue is stale file state. The model may remember version A, while previous edits, formatters, lint fixes, or editor automation have already changed the file to version B. Now the exact-string match fails.
But Codex takes a different approach with apply_patch.
Instead of reproducing one exact old string, the model describes the change as a patch:
*** Begin Patch
*** Update File: app.py
@@
- print("hello")
- print("hello world") *** End Patch
The patch uses smaller surrounding context and is more tolerant of minor changes. That made me wonder: "Can I bring the same editing approach to Pi?".
So I started looking through existing Pi packages.
Some solutions basically brought Codex Native into Pi, which also meant installing Codex.
Another package had a pretty good custom implementation, but it only supported GPT-family models.
I checked its GitHub repo hoping to extend it, but development had been inactive for around two months and newer PRs were no longer being processed.
So I finished the missing part myself.
I built an apply_patch tool for Pi that can also work with other model families.
If you're looking for something similar, here it is:
https://pi.dev/packages/@paulpham157/apply-patch
My takeaway from all of this:
- Coding agents are not only about model intelligence.
- The editing primitive matters too.
- A strong model with a brittle edit tool can still fail on simple changes.
- A better harness can make weaker models much more reliable.
- Sometimes improving an agent doesn't require a better model.
- It just needs a better tool.
- Tool is a small part in the harness.


Top comments (0)