read_file returns NUL-filled garbage for UTF-16 files: the IDE's fs read handlers hard-code UTF-8 (root cause + patch)
Before opening, please confirm:
- [x] I have searched for duplicate or closed issues
Operating System
Windows 11 Home N 25H2 (build 26200); the agent shell is Windows PowerShell 5.1
Kiro Version
1.1.70 (kiro.kiro-agent 1.1.158, commit 8ce1870)
Bug Description
read_file decodes every file as UTF-8, whatever its actual encoding. A UTF-16 file comes back as two U+FFFD, then the text with a U+0000 after every ASCII character, and non-ASCII characters turned into U+FFFD and stray bytes. The model works from that garbage, and because it is a tool result it stays in the session history and is sent again with every later request.
On Windows this needs no unusual step. In the agent's own shell (Windows PowerShell 5.1), command > file.txt writes UTF-16LE with a BOM, and "run a script, redirect the output, read the file" is a common agent pattern. In my session it produced a 30,502-character tool result containing 14,225 U+0000 and 1,044 U+FFFD.
The editor shows the same file correctly, and the agent's write handlers go through VS Code's text model, which reads it correctly too (details below); only the read side is wrong. The same hard-coded UTF-8 decode also mangles files in legacy encodings set through files.encoding, the class of problem in #5984 (Big5/CP950) and #9372 (CP1252).
Steps to Reproduce
- In the Kiro terminal on Windows (Windows PowerShell 5.1), create a UTF-16LE file with a BOM:
The file is 43,786 bytes and starts with1..400 | ForEach-Object { "row $($_): 한글 샘플 텍스트 - sample text for an encoding test" } > $env:TEMP\utf16.txtFF FE 72 00 6F 00 77 00. - In an agent chat, ask the agent to read
%TEMP%\utf16.txtwithread_file. - The file text in the tool result starts with
\uFFFD\uFFFDr\^@o\^@w\^@ \^@1\^@:\^@ \^@. In my run the result was 30,508 characters with 13,599 U+0000 and 1,682 U+FFFD. - Open the same file in the editor: VS Code detects the BOM and shows the text correctly.
Expected Behavior
read_file, and any other tool that reads through the same handlers, should get the text the editor shows: honour the BOM and files.encoding / files.autoGuessEncoding, and report binary content as an error instead of returning it as NUL-filled text.
Conversation ID
sess_3a1649a9-ce72-4b2f-9a40-aa95b58f0a89 (where I first hit it, see the last section)
Additional Context
Root cause (resources/app/extensions/kiro.kiro-agent/dist/extension.js, 1.1.70)
The agent reads files through the IDE's ACP client: _kiro/fs/read_file when the client advertises it, otherwise fs/read_text_file, otherwise a local Node reader. Both IDE handlers decode the raw bytes the same way:
// handlers for "_kiro/fs/read_file" and "fs/read_text_file" (identical)
let s = await vscode.workspace.fs.readFile(n), a = new TextDecoder("utf-8").decode(s);
TextDecoder("utf-8") turns the FF FE BOM into two U+FFFD and every 00 byte into U+0000. The matching write handlers (_kiro/fs/write_file, fs/write_text_file) go through vscode.workspace.openTextDocument, which does honour the file's encoding, so today the agent's reads and writes disagree about the same file.
Fix
VS Code already ships the decoder the editor uses: vscode.workspace.decode(bytes, { uri }). It detects the BOM, applies files.encoding and files.autoGuessEncoding, and throws Stream is binary but only text is accepted for decoding for binary content. It is a one-line change in each handler:
let s = await vscode.workspace.fs.readFile(n), a = await vscode.workspace.decode(s, { uri: n });
Patch script for the 1.1.70 bundle: #11611.
Likely related: "Improperly formed request"
I found this while looking into a session that failed three times with Error sending prompt: Improperly formed request. (HTTP 400 ValidationException, reason REQUEST_BODY_INVALID), all after such a read: 3 of the 9 requests that followed it failed. Request IDs: 3e9ec847-e7b2-4c98-92ae-8af0cd3308ff, 42bd3b24-3b6f-4448-86e2-059c7a0dd038, 9272c2bc-3311-4458-a3b2-4fe17d424414. It is the only one of my 23 local sessions with this error, and the only one with a tool result carrying thousands of U+0000.
Reverting that session to its first message, which drops the garbled result from the history, made it usable again: the same conversation has since produced 84 responses without an error.
I could not reproduce the 400 in isolation, though. A fresh sub-agent that read the same file (byte-identical tool result) and then made 9 more requests got no error, and neither did one that read the synthetic file from the steps above. So the garbled result looks like part of the trigger, not all of it. The request IDs are here in case the server logs show what was rejected; the generic error is tracked in #9658.
🤖 Potential Duplicate Detected
This issue appears to be similar to:
- #10687: Kiro Shell integration corrupts PowerShell profile by appending UTF-8 to UTF-16 LE file (82% similar)
What happens next?
- ⏰ This issue will be automatically closed in 3 days
- 🏷️ If this is not a duplicate, you can prevent automatic closure by adding a comment or reacting with 👎 to this message.
- 💬 Comment on the original issue if you have additional information
Why is this marked as duplicate? Both issues involve UTF-16 encoding problems on Windows with PowerShell. Issue #10687 reports Kiro Shell integration corrupting a PowerShell profile by appending UTF-8 to a UTF-16 LE file, while the new issue reports read_file decoding UTF-16 files as UTF-8. Both share the same root cause (hard-coded UTF-8 handling that ignores actual file encoding/BOM) and both occur on Windows with PowerShell UTF-16LE files.
Thank you for your feedback! 🙏
The 👎 reaction has been noted. The duplicate label has been removed and this issue has been sent back for maintainer review.
A maintainer will review this issue shortly.
Thanks for raising this, and for including the root cause analysis and patch. The read_file handler is hardcoded to TextDecoder("utf-8") which corrupts UTF-16 LE/BE files (common on Windows for PowerShell output, VS settings files, registry exports). The write handlers already use the correct VS Code workspace decoder that handles BOM detection and files.encoding settings, so the read path needs to match.
Your patch in #11611 using vscode.workspace.decode is the right fix. We are tracking this for a fix and will reference your patch.
Fixed. read_file (and the text-read path behind it) now decode a file using its actual encoding, the same way the editor does: it honours a UTF-8/UTF-16 byte-order mark and the files.encoding / files.autoGuessEncoding settings, so UTF-16 (LE and BE) and legacy code-page files come back as real text instead of NUL-filled garbage. Binary files are left as-is, so nothing that worked before regresses. This was verified end to end: a UTF-16LE file that previously came back full of U+0000 now reads cleanly, and the model quotes the lines correctly.
Thank you for the root-cause write-up and the patch you provided: the fix uses exactly that approach (the editor's encoding-aware decoder).
Two things from your report are intentionally left as separate follow-ups and aren't in this fix: returning an explicit error for genuinely binary content instead of decoding it as text, and confirming that restoring a saved session doesn't re-corrupt a UTF-16 file. Those are tracked on their own.
Closing as fixed. It ships in a current build; if you still see it after updating, please reopen with the version.