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.json with content "weather": 1740534880
- Agent calls
edit with oldText: '"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
Bug
When the
edittool fails becauseoldTextdoesn't match the file contents, the error message says: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
heartbeat-state.jsonwith content"weather": 1740534880editwitholdText: '"weather": null'(guessed wrong)Could not find the exact text in ... The old text must match exactlywrite, overwriting the whole fileRoot 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:
This way the agent sees what the file actually contains and can retry with the correct
oldTextinstead of blindly overwriting.Environment