-
-
Notifications
You must be signed in to change notification settings - Fork 69.5k
edit tool: error message should include file contents on mismatch #27082
Description
Bug
When the edit tool fails because oldText doesn't match the file contents, the error message says:
Could not find the exact text in . The old text must match exactly including all whitespace and newlines.
This is unhelpful because the agent (LLM) has no way to know what the file actually contains — it may have been guessing or working from stale context. The agent often recovers by falling back to write (overwriting the whole file), which is a worse outcome than a targeted edit.
Reproduction
- Have a JSON state file like
heartbeat-state.jsonwith content"weather": 1740534880 - Agent calls
editwitholdText: '"weather": null'(guessed wrong) - Error:
Could not find the exact text in ... The old text must match exactly - Agent falls back to
write, overwriting the whole file
Root cause
The error in @mariozechner/pi-coding-agent/dist/core/tools/edit.js (line 77) doesn't include any hint about what the file actually contains.
Suggested fix
Include a snippet of the actual file content in the error so the agent can self-correct with a proper edit:
const snippet = normalizedContent.length <= 800
? normalizedContent
: normalizedContent.slice(0, 800) + "\n... (truncated)";
reject(new Error(
`Could not find the exact text in ${path}. The old text must match exactly including all whitespace and newlines.\nCurrent file contents:\n${snippet}`
));This way the agent sees what the file actually contains and can retry with the correct oldText instead of blindly overwriting.
Environment
- openclaw v2026.2.24 (pi-coding-agent dependency)
- Windows 11
- Node.js v25.2.1