edit_file and apply_patch corrupt every non-UTF-8 byte in the file
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 77/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
Research direction
Start with src/main/tools/files.ts around edit_file, apply_patch.ts at the cited update paths, and write_file's overwrite path; inspect planPatch and the existing unit tests. Add a Windows-1252 fixture to verify non-UTF-8 bytes are refused without rewriting the file, then run the relevant tests. Done when all three write paths reject bytes that fail a UTF-8 round-trip.
Written by the indexing model from the issue text.
Description
Found in the 2026-10-07 code review and confirmed with a probe against the real tools.
Problem
edit_file (src/main/tools/files.ts:246-253) and apply_patch (apply_patch.ts:203,267) decode the whole file with readFile(file, 'utf8') and write it back as UTF-8. isBinaryFile (text_files.ts) only checks for NUL bytes, so Windows-1252, Latin-1 and Shift-JIS files pass. Every byte that isn't valid UTF-8 becomes EF BF BD, anywhere in the file, not just in the edited region. The approval diff compares the decoded strings, so it shows only the edited hunk.
Scenario
A legacy .properties file contains café in Windows-1252. The agent edits an unrelated line. The user approves a clean-looking diff, and é is replaced. The probe caf\xe9\n… came out containing ef bf bd.
Fix
Refuse to edit when the bytes don't round-trip:
const bytes = await readFile(file);
const before = bytes.toString('utf8');
if (!Buffer.from(before, 'utf8').equals(bytes))
throw new ToolError(`${input.path} is not UTF-8; editing it would rewrite other bytes.`);
Add the same check in planPatch for updates and in write_file when it overwrites an existing file. Optionally mention the encoding in read_file's result. Add unit tests with a Windows-1252 fixture.
- Dominant language
- TypeScript
- Stars
- 2
- Forks
- 2
- Avg merge
- 5h 28m
- Merged PRs (30d)
- 24
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PierrunoYT/patch
-
enhancement platform: windows priority: low severity: low
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PierrunoYT/patch#198 ·
Maintainers usually reply within 1 day
-
priority: medium security severity: low
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
PierrunoYT/patch#66 ·
Maintainers usually reply within 1 day
-
bug platform: macos priority: low severity: low tests
Difficulty 3/5 1-2 days Newbie friendliness 56/100
PierrunoYT/patch#218 · 1 comment ·
Maintainers usually reply within 1 day
-
enhancement priority: low security severity: low
Difficulty 4/5 3-5 days Newbie friendliness 55/100
PierrunoYT/patch#211 · 2 comments ·
Maintainers usually reply within 1 day
-
enhancement platform: windows priority: low security severity: low
Difficulty 4/5 3-5 days Newbie friendliness 55/100
PierrunoYT/patch#207 ·
Maintainers usually reply within 1 day
All issues in PierrunoYT/patch
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 84/100
answerLoops/answerLoops#345 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 82/100
siyuan-note/siyuan#20313 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
LanternOps/breeze#8254 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 1-3 hours Newbie friendliness 82/100
gofish-graphics/gofish-graphics#1084 ·
Maintainers usually reply within 1 day