-
-
Notifications
You must be signed in to change notification settings - Fork 69.4k
tools.edit is brittle: exact-match replacement fails frequently; need patch/fuzzy/anchor modes #42221
Copy link
Copy link
Open
Description
Problem
tools.edit frequently fails with messages like:
edit failed: Could not find the exact text in . The old text must match exactly including all whitespace and newlines.
This happens across many repos/files (JS/CSS/MD), especially when:
- The target file is large and changes frequently.
- The replacement involves template strings/backticks (JS) or compact CSS formatting.
- Another agent/process has already made small formatting changes (whitespace/newlines), making the "oldText" no longer an exact match.
In practice this makes edit unreliable for real-world iterative work, even when the intended change is correct.
Steps to Reproduce
- Use
tools.editto replace a multi-line snippet in a JS or CSS file. - Make any small formatting change to the file (indentation, whitespace, line wrapping, quotes), or have another agent update the file.
- Retry the same
tools.editwith the previousoldText. - Observe that
editfails because exact text is not found.
Expected
tools.edit should support safer, more robust editing modes beyond exact-match replacement, e.g.:
- Regex / pattern-based replacement
- Anchor-based replacement (match around a stable marker, then replace a range)
- Fuzzy matching with a similarity threshold (diff-match-patch style)
- A
tools.patchcapability that applies unified diffs (git-apply like), with clear conflict reporting
Actual
Only exact-match replacement is supported. Minor formatting changes cause hard failure.
Why this matters
This is a systemic reliability issue: agents often work on the same files iteratively, and exact-match text replacement is brittle. It leads to repeated failures and forces manual workarounds.
Suggested approach
- Keep current exact-match behavior as default (safe), but add explicit opt-in modes:
mode: "regex" | "anchor" | "fuzzy" | "patch"
- For
patchmode, allow passing a unified diff and return a structured result:- applied hunks / rejected hunks / context mismatch details
- For
fuzzymode, require an explicit threshold and a max replacement span to reduce risk.
Environment
- OpenClaw: 2026.3.8
- macOS: 26.4 (arm64)
- Repro seen with JS/CSS in active projects (e.g., dispatch-board.js, base.css), but occurs broadly.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Type
Fields
Give feedbackNo fields configured for issues without a type.