anomalyco / anomalyco/opencode
`edit` tool: failed oldString match returns no context — models abandon the edit instead of retrying
@kitlangton is already working on this.
Since Aug 16, 2026.
- Dominant language
- TypeScript
- Stars
- 209k
- Forks
- 27.5k
- PR merge metrics
- PR metrics pending
Description
Problem
When every replacer strategy fails to match oldString, the edit tool throws:
"Could not find oldString in the file. It must match exactly, including whitespace, indentation, and line endings."
This error tells the model that it failed but shows it nothing to correct against. In practice (observed across SWE-bench runs with mid-tier models), the model's oldString is usually close-but-wrong — a reflowed comment line, drifted indentation — and after this error the model gives up after a single attempt: no re-read, no retry, a silent no-op on the task.
Concrete trace (SWE-bench django__django-10914): the model built an oldString whose comment wrapped onto different lines than the file; all replacer strategies missed; the model received the bare error and abandoned the edit — the session ended seconds later with no change made.
Proposal (two small changes)
edit.ts— when no replacer matches, include the closest-matching region of the file (line-numbered) in the error, so the model can copy the real text verbatim into a correctedoldString:
Could not find oldString in the file. It must match exactly...
The closest matching region in the file is shown below with line numbers.
Re-read it, correct your oldString to match this text EXACTLY, and retry:
329: # The numeric mode to set newly-uploaded files to. ...
330: # you'd pass directly to os.chmod ...
331: FILE_UPLOAD_PERMISSIONS = None
The region finder is a simple trimmed-line window scorer (exact-line matches weighted over substantial partials, with a minimum-signal threshold so unrelated files produce no hint). No new dependencies.
edit.txt(tool description) — tell the model the failure error includes the closest region, and to retry with corrected text rather than abandon after one failed edit.
Effect observed
A/B on the same tasks/model: a task where the model previously quit with an empty diff produced a real, on-target edit once the hint + retry guidance were in place (patch applied cleanly, zero regressions in the surrounding suite).
Patch
Fix + 3 unit tests (reproducing the near-miss above, asserting the hint fires, and asserting no false hint on unrelated content) are ready as a PR; all 27 existing edit tests pass unchanged.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.