nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] diff_edit validator accepts a multi-block diff whose later block depends on an earlier one, then execute fails

Open addyCooks opened this issue 2 weeks ago • 2 comments

Description

diff_edit checks every SEARCH block against the original file, but applies the blocks one after another. If block 2 searches for text that block 1 produces or removes, the validator says the diff is valid and execute then throws. The file stays unchanged.

Environment

  • OS: Windows 11 Home 10.0.26300
  • Node version: v22.22.3
  • Nanocoder version: 1.32.0 (built from main at 5df35b3a)
  • Provider: Not provider-specific
  • Model: Not model-specific

Steps to Reproduce

  1. Create chain.txt containing one\ntwo\n and read it with read_file.

  2. Call diff_edit with this diff:

     <<<<<<< SEARCH
     one
     =======
     two
     >>>>>>> REPLACE
     <<<<<<< SEARCH
     two
     =======
     THREE
     >>>>>>> REPLACE
    

Expected Behavior

Either the validator rejects the diff with a clear message, or the blocks are applied in order.

Actual Behavior

The validator returns valid, then execute throws.

Logs/Screenshots

validator: {valid: true}
execute:   Search block 2 could not be applied cleanly after earlier edits.

(X=1 -> Y=2 followed by Y=2 -> X=1 fails the same way.)

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs with the latest version of nanocoder (main)

Suggested fix:

  • source/tools/file-ops/diff-edit.tsx (validateBlocks, using countOccurrences at line 141): check each block against the content after the previous blocks were applied, so validator and execute agree. Return the block number in the error.
  • Add a test for a chained diff and a swap.

addyCooks avatar Nov 16 '26 09:11 addyCooks

I am working on this with AI assistance. The tool currently requires distinct original-file ranges (including duplicate-search rejection), and its preview/visibility checks use those original ranges. I plan to preserve that contract and have validation also dry-run the ordered replacements using the same helper as execution, so a later block made ambiguous or removed by an earlier block is rejected before approval, with the block number. I will add regression cases for the reported chain, a swap, and an overlapping edit that removes a later search, check that failed edits leave the file unchanged, and retain successful independent multi-block coverage.

aliberk1 avatar Nov 16 '26 09:11 aliberk1

Hi @aliberk1, I’m assigning this one to you. I’ll review your PR this time. Going forward, please comment on the issue first and wait until you’re assigned before you start working on it. That way we avoid duplicate work and keep things fair for everyone. Thanks for contributing!

addyCooks avatar Nov 16 '26 10:11 addyCooks