read tool fails with "readline was closed" on files in a legacy encoding (windows-1251)
Component
CLI / TUI
Description
The read tool reads ordinary text through TextStream.withFallback: an optimistic UTF-8 stream, and on InvalidUtf8Error a retry over openDecoded (packages/opencode/src/kilocode/text-stream.ts), which decodes the whole file with the detected encoding and returns Readable.from([text]) - one synchronous chunk. collect in tool/read.ts iterates that stream with readline to the end of the file (it always reads to the end, to count the lines). Under Bun, readline closes before for await takes the last lines, and the iteration rejects with ERR_USE_AFTER_CLOSE "readline was closed". The read fails, so files in a legacy encoding cannot be read at all unless the byte limit cuts the read before EOF. UTF-8 files are not affected (they never take the fallback path).
Kilo version
7.8.8 (same code on main, 2026-10-13)
Steps to reproduce
- Create a text file in windows-1251 with Cyrillic text, a few thousand lines (e.g. Delphi/Pascal sources are often in this encoding).
- Ask the agent to read it (any offset/limit).
- The tool returns
Error: readline was closed. In a real project 9 of 11 read calls on such files failed; agents fell back to shell scripts with guessed encodings.
Driving the same pipeline in a test (KiloReadObject + withFallback + readline to EOF): 0 of 10 reads succeed with Readable.from([text]); with an async source in 64 KB slices all succeed with the right line count.
Operating System
Windows 11
Terminal
VS Code extension / CLI daemon
Related: #5522 (same error message, closed with the legacy extension)