From f57eba2b07a75c206023772f1a4a7d9061f1018b Mon Sep 17 00:00:00 2001 From: FanouZeng-TT <18280587072@163.com> Date: Sun, 20 Sep 2026 12:02:14 +0800 Subject: [PATCH] fix(tools): keep read_file progressing when a single line exceeds the output budget ReadFileTool stops at the first numbered line that does not fit the 128 KiB budget. When that line is the first one, no line is appended and the tool returns an empty page that reports lines 2-1 and repeats the same offset, so the caller can never reach the rest of the file. A long line further down has the same effect for everything after it. Emit a bounded prefix of that line and advance the continuation offset past it, so every read returns content and makes progress. --- nanobot/agent/tools/filesystem.py | 5 +++++ tests/tools/test_filesystem_tools.py | 32 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/nanobot/agent/tools/filesystem.py b/nanobot/agent/tools/filesystem.py index 0b9469549..6fe5a43bb 100644 --- a/nanobot/agent/tools/filesystem.py +++ b/nanobot/agent/tools/filesystem.py @@ -403,6 +403,11 @@ class ReadFileTool(_FsTool): for line in numbered: chars += len(line) + 1 if chars > self._MAX_CHARS: + if not trimmed: + # Do not leave the reader at the same offset when a + # single numbered line exceeds the entire budget. + # Return a bounded prefix and advance past the line. + trimmed.append(line[: self._MAX_CHARS]) break trimmed.append(line) end = start + len(trimmed) diff --git a/tests/tools/test_filesystem_tools.py b/tests/tools/test_filesystem_tools.py index 13d065558..a82094d7a 100644 --- a/tests/tools/test_filesystem_tools.py +++ b/tests/tools/test_filesystem_tools.py @@ -98,6 +98,38 @@ class TestReadFileTool: assert len(result) <= ReadFileTool._MAX_CHARS + 500 # small margin for footer assert "Use offset=" in result + @pytest.mark.asyncio + async def test_first_line_longer_than_budget_returns_prefix(self, tool, tmp_path): + """A single line over the whole budget must still yield content.""" + f = tmp_path / "minified.txt" + f.write_text("y" * (ReadFileTool._MAX_CHARS * 2) + "\nsecond line\n", encoding="utf-8") + + result = await tool.execute(path=str(f)) + + assert "1| " in result + assert "second line" not in result + assert len(result) <= ReadFileTool._MAX_CHARS + 500 + assert "Use offset=2 to continue" in result + + @pytest.mark.asyncio + async def test_long_middle_line_advances_to_following_content(self, tool, tmp_path): + f = tmp_path / "bundle.txt" + f.write_text( + "first\n" + "z" * (ReadFileTool._MAX_CHARS * 2) + "\nlast\n", + encoding="utf-8", + ) + + first = await tool.execute(path=str(f)) + assert "Use offset=2 to continue" in first + + second = await tool.execute(path=str(f), offset=2) + assert "2| " in second + assert len(second) <= ReadFileTool._MAX_CHARS + 500 + assert "Use offset=3 to continue" in second + + third = await tool.execute(path=str(f), offset=3) + assert "3| last" in third + @pytest.mark.asyncio async def test_oversized_file_is_rejected_before_read(self, tool, tmp_path, monkeypatch): f = tmp_path / "huge.txt"