[Bug] string_replace changes non-UTF-8 bytes on lines it never touched (Latin-1 é becomes U+FFFD)
Description
string_replace decodes the whole file as UTF-8 and writes the whole file back as UTF-8. In a file that isn't valid UTF-8 (Latin-1, Windows-1252, Shift-JIS, ...), every byte that doesn't decode is turned into U+FFFD (EF BF BD). That includes bytes on lines the edit never touched. The edit reports success, so nothing tells the user that the rest of the file changed.
Legacy-encoded files still turn up in real repos: old source files with accented comments, .properties files, fixtures and test data. Changing one line damages the rest of the file, and only git can bring the original bytes back.
Environment
- OS: macOS (Darwin 25.6.0, x86_64)
- Node version: 22.23.3
- Nanocoder version: main at
ef4e79ec, and v1.31.0 - Provider: a local OpenAI-compatible test server with scripted responses
- Model: n/a (scripted)
Steps to Reproduce
- In a scratch folder, create a Latin-1 file:
printf 'caf\351\nold\n' > legacy.txt(line 1 iscaféin Latin-1, a singleE9byte). - Run
nanocoder --trust-directory --mode auto-accept --plain run "rename old to new"and have the model callread_fileonlegacy.txt, thenstring_replacewithold_str: "old"andnew_str: "new". - Compare the bytes:
xxd -p legacy.txt.
Expected Behavior
Only the replaced text changes. Line 1 keeps its original bytes, or the tool refuses to edit a file it can't round-trip and says why.
Actual Behavior
before: 63 61 66 e9 0a 6f 6c 64 0a caf\xE9 / old
after: 63 61 66 ef bf bd 0a 6e 65 77 0a caf� / new
Line 1 was never part of the edit, but its E9 byte became EF BF BD. The tool still reported success and the run exited 0.
Control: the same text saved as UTF-8 (caf\303\251\nold\n) keeps line 1 byte for byte (63 61 66 c3 a9 0a 6e 65 77 0a).
Same result on main ef4e79ec and on v1.31.0.
Logs/Screenshots
What I think is going on:
- The file cache reads the file and decodes it with
buffer.toString('utf-8')(source/utils/file-cache.ts:172). That decode is lossy: invalid bytes become U+FFFD. string_replacetakes that decoded content from the cache (source/tools/file-ops/string-replace.tsx:86), applies the replacement, and writes the whole string back withwriteFile(absPath, newContent, 'utf-8')(source/tools/file-ops/string-replace.tsx:107).
diff_edit follows the same pattern (source/tools/file-ops/diff-edit.tsx:274 and :280), so it probably has the same problem. I've only tested string_replace.
Additional Context
Proposed fix (happy to adjust): before writing, check that the original bytes survive a UTF-8 round trip. If they don't, refuse the edit with a clear error (something like "legacy.txt is not valid UTF-8; string_replace would change bytes outside the edit"), rather than writing a damaged file. Refusing is the smallest safe change. Editing the raw bytes, or decoding as latin1 and writing back as latin1, would let the edit go through, but it's more work and harder to get right for multi-byte encodings. Either way, files that are valid UTF-8 behave exactly as today.
For tests: a Latin-1 file is either left byte for byte unchanged outside the edit or rejected with the error, and a UTF-8 file with non-ASCII text still edits normally. The same tests would apply to diff_edit if you want both covered in one change.
- [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
@addyCooks could you take a look when you have a moment? I'd like to work on this one, so please assign it to me if that works for you.
Thanks @diyaavirmani, this is a clear report and I can confirm it on current main: for a Latin-1, Windows-1252 or Shift-JIS file, both string_replace and diff_edit rewrite bytes outside the edit as U+FFFD (the Latin-1 e9 becomes ef bf bd) and still report success. A UTF-8 file with accents edits normally.
Your proposal to refuse the edit with a clear error is the right first step (it matches how PDF/DOCX edits are refused). I'm assigning this to you. Please cover both tools in the same change, with tests for a Latin-1 file (refused, bytes unchanged) and a UTF-8 file with non-ASCII text (still edits normally).
Note: there are already two open or closed PRs for this (#1858, #1859) from a contributor who wasn't assigned. Please don't base yours on them.
@insuffer, thanks for the interest, but please ask for assignment on the issue and wait before opening a PR. I'm closing #1859 as a duplicate of the assigned work.
@addyCooks the fix for this is up in #1866 (all checks green, no conflicts; #1867 tracks the related formatter follow-up). Would you be able to take a look when you have a moment? Thanks!