#18742: feat(edit): return current file content on match failure
agents
stale
size: XS
Cluster:
Tool Execution and Error Handling
## Summary
When the Edit tool fails because `oldText` doesn't match the file content, the agent currently has to make a separate `Read` call to see the current state before retrying. This wastes tokens and context window.
**This PR wraps the Edit tool's `execute` method to return the current file content alongside the error message when a text mismatch occurs.** The agent can immediately see what changed and retry — no extra Read needed.
## Changes
- `src/agents/pi-tools.read.ts`: Wrap `createSandboxedEditTool` execute to catch "Could not find the exact text" errors and append current file content to the error response.
## Impact
- Saves 1 tool call (Read) per failed Edit retry
- Reduces token usage and context window consumption
- No behavior change for successful edits or non-mismatch errors
Closes #18132
Most Similar PRs
#21695: fix(edit): include line numbers in duplicate match errors
by lbo728 · 2026-02-20
66.3%
#23381: fix(tools): allow empty newText in edit tool for text deletion
by SidQin-cyber · 2026-02-22
55.2%
#13282: fix(agents): instruct agent not to retry lost tool results
by thebtf · 2026-02-10
54.1%
#18466: fix: suppress recoverable mutating tool errors when agent already r...
by stijnhoste · 2026-02-16
53.7%
#10189: fix: resolve file_path param in tool display for read/write tools
by Yida-Dev · 2026-02-06
53.3%
#12231: fix(read): add directory check before reading file
by higginsvott · 2026-02-09
53.3%
#18022: feat(tools): Structured Config Editor for JSON/YAML/TOML
by stakeswky · 2026-02-16
53.3%
#16966: fix: strip tool_use blocks from aborted/errored assistant messages
by StressTestor · 2026-02-15
53.2%
#22778: fix: allow empty string for edit tool newText parameter
by miloudbelarebia · 2026-02-21
52.8%
#9701: feat: handle Signal message edits with [edited] marker (#9656)
by divol89 · 2026-02-05
52.6%