From 7cb21b84f1b03a8e2f24027f2abb9f789a67fc4c Mon Sep 17 00:00:00 2001 From: Patrick Buckley Date: Sun, 29 Mar 2026 15:32:04 -0700 Subject: [PATCH] fix: detect binary files in read_file instead of silent corruption (#227) read_file silently converted null bytes to spaces, showing corrupted content with no warning. Now samples the first 8KB for null bytes and returns a clear error directing the user to bash for binary inspection. --- turnstone/core/session.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/turnstone/core/session.py b/turnstone/core/session.py index bbcdf434..81cfc85a 100644 --- a/turnstone/core/session.py +++ b/turnstone/core/session.py @@ -4188,6 +4188,16 @@ class ChatSession: return self._exec_read_image(call_id, path, resolved) try: + with open(resolved, "rb") as fb: + raw = fb.read(8192) # sample first 8KB for binary detection + if b"\x00" in raw: + self._read_files.discard(resolved) + msg = ( + f"Error: {path} appears to be a binary file " + "(contains null bytes). Use bash to inspect binary files." + ) + self._report_tool_result(call_id, "read_file", msg, is_error=True) + return call_id, msg with open(resolved) as f: all_lines = f.readlines() except FileNotFoundError: