From a721806a5091fa5db3e10da2bcf4fe2b906e2c94 Mon Sep 17 00:00:00 2001 From: chengyongru <2755839590@qq.com> Date: Wed, 23 Sep 2026 00:05:09 +0800 Subject: [PATCH] fix(files): treat BOM-only text as empty --- nanobot/agent/tools/filesystem.py | 3 +++ tests/tools/test_read_enhancements.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/nanobot/agent/tools/filesystem.py b/nanobot/agent/tools/filesystem.py index 6f680bb05..0b9469549 100644 --- a/nanobot/agent/tools/filesystem.py +++ b/nanobot/agent/tools/filesystem.py @@ -375,6 +375,9 @@ class ReadFileTool(_FsTool): "Only supported text files and images can be read." ) + if not text_content: + return f"(Empty file: {path})" + # Normalize CRLF -> LF before line-splitting. Primarily a Windows # concern (git checkouts with autocrlf, editors saving CRLF) but # applied on all platforms so downstream StrReplace/Grep behavior diff --git a/tests/tools/test_read_enhancements.py b/tests/tools/test_read_enhancements.py index b5fddefdb..5e3c91bbc 100644 --- a/tests/tools/test_read_enhancements.py +++ b/tests/tools/test_read_enhancements.py @@ -138,6 +138,16 @@ class TestReadDedup: assert "2| Second line" in result assert "\x00" not in result + @pytest.mark.parametrize("encoding", ["utf-8-sig", "utf-16", "utf-32"]) + @pytest.mark.asyncio + async def test_bom_only_unicode_text_is_empty_file(self, tool, tmp_path, encoding): + f = tmp_path / "empty.txt" + f.write_bytes("".encode(encoding)) + + result = await tool.execute(path=str(f)) + + assert result == f"(Empty file: {f})" + # --------------------------------------------------------------------------- # Cross-session isolation (issue #3571)