MCPcopy Create free account
hub / github.com/agentforce314/clawcodex / _looks_like_binary

Function _looks_like_binary

src/command_system/input_processing.py:229–260  ·  view source on GitHub ↗

Return True if the first ``_BINARY_SNIFF_BYTES`` bytes contain a NUL AND the file is not BOM-prefixed text. Defense-in-depth check for files whose extension isn't in ``_AT_MENTION_BINARY_EXTENSIONS`` — e.g. a misnamed ``.txt`` that's actually a tarball, or any extension we haven't e

(path: str)

Source from the content-addressed store, hash-verified

227
228
229def _looks_like_binary(path: str) -> bool:
230 """Return True if the first ``_BINARY_SNIFF_BYTES`` bytes contain a NUL
231 AND the file is not BOM-prefixed text.
232
233 Defense-in-depth check for files whose extension isn't in
234 ``_AT_MENTION_BINARY_EXTENSIONS`` — e.g. a misnamed ``.txt`` that's
235 actually a tarball, or any extension we haven't enumerated. NUL bytes
236 do not appear in well-formed utf-8 text, so their presence in a sniff
237 window is a high-precision signal.
238
239 Exception: UTF-16 / UTF-32 text files contain NUL bytes for every
240 ASCII char and would trip the naive NUL sniffer. A BOM prefix
241 (``\\xff\\xfe`` LE / ``\\xfe\\xff`` BE / UTF-32 variants) tells us
242 the file is real text that just needs a different decoder; we return
243 False so the text-read branch can decode it properly via
244 ``_read_text_with_encoding``. Without that paired fix this BOM
245 short-circuit would let UTF-16 mojibake flow into a system-reminder
246 (the exact failure mode the binary branch was added to prevent).
247
248 Conservative on read errors (treats unreadable files as "not binary"
249 so the existing text-read path can surface the OSError its caller
250 already handles).
251 """
252 try:
253 with open(path, "rb") as fh:
254 chunk = fh.read(_BINARY_SNIFF_BYTES)
255 except OSError:
256 return False
257 if _detect_bom_encoding(chunk) is not None:
258 # Encoded text — handled by the text branch with the right codec.
259 return False
260 return b"\x00" in chunk
261
262
263# If a decoded file has more than this fraction of U+FFFD replacement

Callers 1

expand_at_mentionsFunction · 0.85

Calls 2

_detect_bom_encodingFunction · 0.85
readMethod · 0.45

Tested by

no test coverage detected