[Bug] diff_edit validator accepts a multi-block diff whose later block depends on an earlier one, then execute fails
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
-
Create
chain.txtcontainingone\ntwo\nand read it withread_file. -
Call
diff_editwith 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, usingcountOccurrencesat 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.
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.
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!