Skip to content

[Bug] diff_edit echoes back the entire file the model just edited #795

Closed

Description

@akramcodez

Description

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).

Environment

  • OS: linux
  • Node version: (run node --version)
  • Nanocoder version: 1.29.0
  • Provider: (e.g., Ollama, OpenRouter, OpenAI-compatible API)
  • Model: (e.g., llama3.2, claude-3.5-sonnet)

Steps to Reproduce

  1. Open nanocoder in a directory with a large file (e.g., 2000 lines).
  2. Prompt the agent to make a small, one-line change to the file using the diff_edit tool.
  3. 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

Activity

  1. added
    bug
    performance
    architecture
    on Aug 5, 2026
  2. RealBhupesh commented on Aug 5, 2026

    @RealBhupesh
    Contributor

    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:

    • replace the full modified-file response with a concise success summary and bounded context around the applied diff blocks;
    • preserve enough line context for the model to verify the edit without returning unrelated file contents;
    • add regression coverage using a large file and multiple diff blocks;
    • include the required patch changeset and run the focused plus full validation suites.

    I’ll keep the change scoped to #795 and open it as ready for review once the behavior and token reduction are verified.

  3. RealBhupesh commented on Aug 5, 2026

    @RealBhupesh
    Contributor

    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.

  4. will-lamerton commented on Aug 9, 2026

    @will-lamerton
    Member

    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:

    let fileContext = "\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.

    Adding this to the #772 tracker.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    architecturebugperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions