apply_patch fails on UTF-8 BOM files
Open 💬 2 comments Opened Jun 12, 2026 by wangyauneli
Description
The \^Gpply_patch\ tool fails to match context when the target file has a UTF-8 BOM (bytes \EF BB BF\). It returns \Failed to find context\ without indicating the BOM is the cause, which is misleading and causes the agent to retry endlessly.
Steps to reproduce
- Create a JS file with UTF-8 BOM encoding (e.g. saved by Windows Notepad or WeChat DevTools)
- Ask the agent to modify a line in that file using \^Gpply_patch\
- The tool reports \Failed to find context\ even though the context lines are present in the file
Expected behavior
The tool should detect and strip BOM before matching context, then preserve the original encoding when writing back.
Impact
Any .js / .json / other files saved with BOM by Windows editors cannot be patched, causing the agent to waste many retry attempts without understanding the root cause. This is a common scenario on Windows since Notepad adds BOM by default.
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗