Skip to content

fix(tools): don't false-flag CJK files as binary in read_file - #80186

Open
fanxiong wants to merge 1 commit into
NousResearch:mainfrom
fanxiong:fix/readfile-cjk-binary-guard
Open

fix(tools): don't false-flag CJK files as binary in read_file#80186
fanxiong wants to merge 1 commit into
NousResearch:mainfrom
fanxiong:fix/readfile-cjk-binary-guard

Conversation

@fanxiong

@fanxiong fanxiong commented Aug 6, 2026

Copy link
Copy Markdown

问题

read_file 会把 CJK 密集的文本文件误判为二进制并拒绝读取(报 "Binary file - cannot display as text")。

_is_likely_binaryhead -c 1000 采样前 1000 字节。当截断点正好落在一个多字节 UTF-8 字符中间时,终端以 errors="replace" 解码,半个字符变成尾部 U+FFFD;而该函数把样本里出现的任何 U+FFFD 都当作"存在不可解码字节"的证据。中文文本的非 ASCII 字节占比高达 73%~98%,第 1000 字节落在字符中间的概率极高——实测这些文件几乎 100% 被误判。

修复

U+FFFD 出现在样本末尾head -c 1000 的截断伪影,不是二进制证据;真正的乱码(不可解码字节)会出现在样本中部。因此改为扫描 content_sample[:-1](排除尾部截断伪影),其余判定逻辑不变:

  • 样本中部的 U+FFFD → 仍判二进制(read→edit→write 破坏原始字节的保护不降级)
  • 仅尾部的 U+FFFD(截断伪影)→ 按文本正常读取
  • 控制字符比例判定、扩展名判定均不受影响

测试

  • 新增回归测试 test_trailing_replacement_char_is_truncation_artifact
    • 尾部 U+FFFD(截断伪影)→ 不判二进制
    • 中部 U+FFFD(真乱码)→ 仍判二进制
  • 本地 scripts/run_tests.sh tests/tools/test_file_operations.py47 passed, 0 failed

_is_likely_binary samples the first 1000 bytes via head -c 1000. When the
byte cut lands mid-way through a multi-byte UTF-8 char, the terminal's
errors=replace decode turns the partial char into a trailing U+FFFD, which
the guard treated as evidence of binary content. Every CJK-heavy file
(73-98% non-ASCII bytes) tripped it, so read_file refused to read them.

Scan everything except the sample tail: genuine undecodable bytes still
produce U+FFFD mid-sample; only the head -c truncation artifact sits at
the very end.

Regression test added: trailing U+FFFD not flagged, mid-sample U+FFFD
still flagged as binary.
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists tool/file File tools (read, write, patch, search) duplicate This issue or pull request already exists labels Aug 6, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Duplicate of #79408. Both PRs apply the same trailing-U+FFFD exemption for a byte-truncated UTF-8 sample; #79408 is the earlier open implementation.

@kyssta-exe

Copy link
Copy Markdown
Contributor

Triage note: this appears to duplicate three other PRs that fix the same issue — UTF-8 files whose 1000-byte read_file sample lands mid-multibyte-char (trailing U+FFFD) get misclassified as binary. Same files (tools/file_operations.py + tests), same fix. The cluster: #80188, #80250, #80261. Recommend picking one to champion and closing the rest as duplicates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists tool/file File tools (read, write, patch, search) type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants