Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(tools): make oversized line truncation explicit
chengyongrucab3a360ea51
diff --git a/nanobot/agent/tools/filesystem.py b/nanobot/agent/tools/filesystem.pyindex 6fe5a43bb..e81a07580 100644--- a/nanobot/agent/tools/filesystem.py+++ b/nanobot/agent/tools/filesystem.py@@ -396,23 +396,28 @@ class ReadFileTool(_FsTool):end = min(start + (limit or self._DEFAULT_LIMIT), total)numbered = [f"{start + i + 1}| {line}" for i, line in enumerate(all_lines[start:end])]result = "\n".join(numbered)+ line_truncated = Falseif len(result) > self._MAX_CHARS:trimmed: list[str] = []chars = 0for line in numbered:- chars += len(line) + 1- if chars > self._MAX_CHARS:+ extra = len(line) + (1 if trimmed else 0)+ if chars + extra > 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])+ line_truncated = Truebreaktrimmed.append(line)+ chars += extraend = start + len(trimmed)result = "\n".join(trimmed)+ if line_truncated:+ result += (+ f"\n\n(Line {offset} truncated; its remaining characters are not shown. "+ "Use exec with a targeted command to inspect the omitted content.)"+ )if end < total:result += f"\n\n(Showing lines {offset}-{end} of {total}. Use offset={end + 1} to continue.)"else:diff --git a/tests/tools/test_filesystem_tools.py b/tests/tools/test_filesystem_tools.pyindex a82094d7a..210edf467 100644--- a/tests/tools/test_filesystem_tools.py+++ b/tests/tools/test_filesystem_tools.py@@ -2,6 +2,7 @@import pytest+from nanobot.agent.tools.file_state import file_read_contextfrom nanobot.agent.tools.filesystem import (EditFileTool,ListDirTool,@@ -99,36 +100,66 @@ class TestReadFileTool: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."""+ @pytest.mark.parametrize("following", ["", "\nsecond line"])+ async def test_oversized_first_line_is_explicitly_truncated(self, tool, tmp_path, following):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+ original = "界" * (ReadFileTool._MAX_CHARS + 100) + "OMITTED" + following+ f.write_text(original, encoding="utf-8")++ with file_read_context("read-1", lambda: {}):+ first = await tool.execute(path=str(f), limit=1)++ assert first.startswith("1| 界")+ assert "OMITTED" not in first+ assert len(first) <= ReadFileTool._MAX_CHARS + 500+ assert "Line 1 truncated; its remaining characters are not shown" in first+ assert "Use exec" in first+ assert "column" not in tool.parameters["properties"]+ assert f.read_text(encoding="utf-8") == original++ with file_read_context("read-2", lambda: {"read-1": first}):+ repeated = await tool.execute(path=str(f), limit=1)+ assert "File unchanged" in repeated++ if following:+ assert "Use offset=2 to continue" in first+ with file_read_context("read-3", lambda: {"read-1": first}):+ second = await tool.execute(path=str(f), offset=2, limit=1)+ assert "2| second line" in second+ assert "End of file" in second+ else:+ assert "End of file" in first+ assert "Use offset=" not in first@pytest.mark.asyncioasync 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",- )+ f.write_text("first\n" + "z" * (ReadFileTool._MAX_CHARS * 2) + "\nlast\n")first = await tool.execute(path=str(f))assert "Use offset=2 to continue" in first+ assert "truncated" not in firstsecond = await tool.execute(path=str(f), offset=2)- assert "2| " in second+ assert second.startswith("2| z")assert len(second) <= ReadFileTool._MAX_CHARS + 500+ assert "Line 2 truncated" in secondassert "Use offset=3 to continue" in secondthird = await tool.execute(path=str(f), offset=3)assert "3| last" in third+ assert "End of file" in third++ @pytest.mark.asyncio+ async def test_line_exactly_fitting_budget_is_not_truncated(self, tool, tmp_path):+ f = tmp_path / "exact.txt"+ line = "x" * (ReadFileTool._MAX_CHARS - len("1| "))+ f.write_text(line + "\nlast")++ first = await tool.execute(path=str(f))+ assert first.split("\n\n")[0] == "1| " + line+ assert "truncated" not in first+ assert "Use offset=2 to continue" in first@pytest.mark.asyncioasync def test_oversized_file_is_rejected_before_read(self, tool, tmp_path, monkeypatch):