Skip to content

fix(file-reads): read UTF-16 text files in read_file, summarize_file, and grep_code - #122

Merged
spelech merged 1 commit into
mainfrom
fix/utf16-file-reads
Nov 12, 2026
Merged

spelech merged 1 commit into
mainfrom
fix/utf16-file-reads

Conversation

@spelech

@spelech spelech commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Summary

Follow-up to #118. UTF-16 files are now indexed, but the read paths still failed for them:

  • read_file → Cannot read binary file: StoredProcedures/.../GetLatestAssistantPublishedVersion.sql: is_binary_file treated any NUL byte as binary, and UTF-16 text has a NUL in every other byte.
  • summarize_file → File not found on disk or storage (it reads through read_file).
  • grep_code skipped the file (same binary check), and local storage, topology snippets, and ADR parsing read files as UTF-8.

Change

  • app/services/text_decoding.py: is_binary_sample / is_binary_file — NUL bytes mean binary only when the bytes are not UTF-16/UTF-32 text (BOM, or the UTF-16 zero pattern). A UTF-8 BOM with NUL bytes stays binary.
  • FileReaderService.read_file / is_binary_file, code_grep_scanner, local_storage (read and re-index), topology/helpers snippets, and adr parsing read with read_source_text (encoding detection, no NUL).

Tests

  • New tests/test_utf16_file_reads.py: UTF-16 LE/BE (with and without BOM), UTF-32, and UTF-8 samples are text; PNG-like data and UTF-8-BOM-with-NUL are binary; read_file returns the decoded lines of a UTF-16 stored procedure; real binary files are still refused.
  • Local: new tests, tests/test_text_decoding_and_nul_safety.py, and the style guardrails pass. (tests/test_file_reader.py::test_read_text_file_full_and_line_slicing fails on Windows on main too: write_text writes CRLF, so size_bytes differs; CI runs on Linux.)
  • Found with ContextCortex v2.23.0 on a real repository: the UTF-16 stored procedure was the top search_code hit, but read_file and summarize_file failed on it.

🤖 Generated with Claude Code

… and grep_code

After #118, UTF-16 files were indexed, but read_file treated them as binary
("Cannot read binary file") because any NUL byte meant binary, and
summarize_file then reported "File not found". grep_code, local storage,
topology snippets, and ADR parsing also read them as UTF-8.

- text_decoding: is_binary_sample/is_binary_file treat NUL bytes as binary
  only when the bytes are not UTF-16/UTF-32 text (BOM or UTF-16 zero pattern).
- FileReaderService, the grep scanner, local storage, topology snippets, and
  ADR parsing read files with read_source_text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@spelech
spelech merged commit 9426590 into main Nov 12, 2026
15 checks passed
@spelech
spelech deleted the fix/utf16-file-reads branch November 12, 2026 19:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant