Skip to content

edit tool: error message should include file contents on mismatch #27082

@ctclostio

Description

@ctclostio

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

  1. Have a JSON state file like heartbeat-state.json with content "weather": 1740534880
  2. Agent calls edit with oldText: '"weather": null' (guessed wrong)
  3. Error: Could not find the exact text in ... The old text must match exactly
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleMarked as stale due to inactivity

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions