nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] string_replace counts matches with split(), so overlapping matches are not seen as ambiguous

Open addyCooks opened this issue 2 weeks ago • 1 comments

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

  1. Create a.txt containing aaa and read it with read_file.
  2. Call string_replace with {"path": "a.txt", "old_str": "aa", "new_str": "B"}.
  3. Create b.txt containing x\nx\nx\n, read it, and call string_replace with {"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) and source/tools/file-ops/diff-edit.tsx:141 (countOccurrences).
  • Add tests for aaa / aa and a repeated-line case.

addyCooks avatar Nov 16 '26 09:11 addyCooks

assign this to me @addyCooks

Vishesh-Verma-07 avatar Nov 16 '26 09:11 Vishesh-Verma-07