You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] diff_edit echoes back the entire file the model just edited #795
The diff_edit tool suffers from a "full file echo" token waste bug. When an agent uses diff_edit to change a few lines in a large file, the tool successfully applies the diff but then returns the entire modified file content, prefixed with line numbers, back to the agent as the tool result. This wastes LLM generation and evaluation tokens (since context window billing is cumulative) and degrades harness efficiency on long-running tasks. This is similar to the reported issues for write_file (#762) and string_replace (#763).
Open nanocoder in a directory with a large file (e.g., 2000 lines).
Prompt the agent to make a small, one-line change to the file using the diff_edit tool.
Observe the output of the tool call in the context window (or in the exported chat). The tool returns the entire file content with line numbers prepended.
Expected Behavior
The diff_edit tool should return a brief confirmation summary (e.g., Successfully applied X diff blocks.), and perhaps a tiny snippet of the lines immediately surrounding the diff, minimizing token consumption.
Actual Behavior
The tool returns the entire updated file contents prefixed with line numbers (\n\nUpdated file contents:\n...), which can consume tens of thousands of tokens per edit on large files.
Additional Context
I have searched existing issues to ensure this is not a duplicate (only #762 for write_file and #763 for string_replace exist, nothing covers diff_edit).
I can reproduce this issue consistently
This issue occurs with the latest version of nanocoder
Implemented this in #798. The PR now returns bounded changed-region context instead of the full edited file, keeps accurate gap markers, preserves a summary for every changed region under the 4,000-character cap, and includes regression tests for large, separated, out-of-order, and oversized multi-block edits. Local validation passed all 6,258 tests plus lint, types, formatting, Knip, and the dependency audit.
Confirming this with exact numbers, since #784 is about to land the same fix for string_replace and leaves this one as the last uncapped file echo in the codebase.
The code is source/tools/file-ops/diff-edit.tsx:160:
letfileContext="\n\nUpdated file contents:\n";
Byte-identical in shape to what write_file had before #762 and what string_replace has until #784 merges. diffEditTool is registered at source/tools/index.ts:25, so it is live and models can call it.
Measured impact, from running the equivalent one-line edit against this repo's own source/app/App.tsx (792 lines) through the tool layer:
whole-file echo: roughly 8,495 tokens per edit
bounded 20-line window: roughly 329 tokens
So about a 96% cut, and it repeats on every edit and is re-sent on every later step of the agent loop.
Worth waiting for #784 to merge before picking this up, because it gives you a direct template: it adds formatUpdatedFileContext() with a STRING_REPLACE_CONTEXT_LINES window, omitted-range markers, and boundary clamping. The cleanest version of this fix is probably to lift that helper somewhere shared and have both editors call it, rather than copying it, so the two file editors cannot drift apart again.
One thing to carry over: #784 also fixed a latent bug in the same area where a match starting mid-line reported line 0, because the old scan only compared offsets at line starts. Worth checking whether diff_edit has the same flaw before assuming its line numbers are trustworthy.
Activity
RealBhupesh commentedon Aug 5, 2026
I’d like to take this one. I confirmed it is currently unassigned and found no active PR covering
diff_edit’s full-file echo.Implementation plan:
I’ll keep the change scoped to #795 and open it as ready for review once the behavior and token reduction are verified.
RealBhupesh commentedon Aug 5, 2026
Implemented this in #798. The PR now returns bounded changed-region context instead of the full edited file, keeps accurate gap markers, preserves a summary for every changed region under the 4,000-character cap, and includes regression tests for large, separated, out-of-order, and oversized multi-block edits. Local validation passed all 6,258 tests plus lint, types, formatting, Knip, and the dependency audit.
will-lamerton commentedon Aug 9, 2026
Confirming this with exact numbers, since #784 is about to land the same fix for
string_replaceand leaves this one as the last uncapped file echo in the codebase.The code is
source/tools/file-ops/diff-edit.tsx:160:Byte-identical in shape to what
write_filehad before #762 and whatstring_replacehas until #784 merges.diffEditToolis registered atsource/tools/index.ts:25, so it is live and models can call it.Measured impact, from running the equivalent one-line edit against this repo's own
source/app/App.tsx(792 lines) through the tool layer:So about a 96% cut, and it repeats on every edit and is re-sent on every later step of the agent loop.
Worth waiting for #784 to merge before picking this up, because it gives you a direct template: it adds
formatUpdatedFileContext()with aSTRING_REPLACE_CONTEXT_LINESwindow, omitted-range markers, and boundary clamping. The cleanest version of this fix is probably to lift that helper somewhere shared and have both editors call it, rather than copying it, so the two file editors cannot drift apart again.One thing to carry over: #784 also fixed a latent bug in the same area where a match starting mid-line reported
line 0, because the old scan only compared offsets at line starts. Worth checking whetherdiff_edithas the same flaw before assuming its line numbers are trustworthy.Adding this to the #772 tracker.