nanocoder
nanocoder copied to clipboard
[Bug] string_replace counts matches with split(), so overlapping matches are not seen as ambiguous
Description
string_replace (and diff_edit) count occurrences with content.split(search).length - 1. split does not count overlapping matches, so a search string that overlaps itself looks unique and the first match is replaced without a warning.
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
a.txtcontainingaaaand read it withread_file. - Call
string_replacewith{"path": "a.txt", "old_str": "aa", "new_str": "B"}. - Create
b.txtcontainingx\nx\nx\n, read it, and callstring_replacewith{"path": "b.txt", "old_str": "x\nx", "new_str": "Y"}.
Expected Behavior
Both calls are rejected as ambiguous ("aa" is at index 0 and 1; "x\nx" is at lines 1-2 and 2-3).
Actual Behavior
Both validators return valid. The first result is Ba, the second Y\nx\n.
Logs/Screenshots
validator: {valid: true} (for "aa" in "aaa")
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:
- Count with a loop:
idx = content.indexOf(search, idx + 1)until-1. - Places to change:
source/tools/file-ops/string-replace.tsx(the counting near line 107 and the validator at line 297) andsource/tools/file-ops/diff-edit.tsx:141(countOccurrences). - Add tests for
aaa/aaand a repeated-line case.
assign this to me @addyCooks