From d25985be0b7631e54acb1c6dfb9f500b3eb094d3 Mon Sep 17 00:00:00 2001 From: Xubin Ren Date: Tue, 24 Mar 2026 03:45:16 +0000 Subject: [PATCH] fix(filesystem): clarify optional tool argument handling Keep the mypy-friendly optional execute signatures while returning clearer errors for missing arguments and locking that behavior with regression tests. Made-with: Cursor --- nanobot/agent/tools/filesystem.py | 4 ++-- tests/test_filesystem_tools.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/nanobot/agent/tools/filesystem.py b/nanobot/agent/tools/filesystem.py index a967073..da7778d 100644 --- a/nanobot/agent/tools/filesystem.py +++ b/nanobot/agent/tools/filesystem.py @@ -96,7 +96,7 @@ class ReadFileTool(_FsTool): async def execute(self, path: str | None = None, offset: int = 1, limit: int | None = None, **kwargs: Any) -> Any: try: if not path: - return f"Error: File not found: {path}" + return "Error reading file: Unknown path" fp = self._resolve(path) if not fp.exists(): return f"Error: File not found: {path}" @@ -264,7 +264,7 @@ class EditFileTool(_FsTool): if old_text is None: raise ValueError("Unknown old_text") if new_text is None: - raise ValueError("Unknown next_text") + raise ValueError("Unknown new_text") fp = self._resolve(path) if not fp.exists(): diff --git a/tests/test_filesystem_tools.py b/tests/test_filesystem_tools.py index 76d0a51..ca6629e 100644 --- a/tests/test_filesystem_tools.py +++ b/tests/test_filesystem_tools.py @@ -77,6 +77,11 @@ class TestReadFileTool: assert "Error" in result assert "not found" in result + @pytest.mark.asyncio + async def test_missing_path_returns_clear_error(self, tool): + result = await tool.execute() + assert result == "Error reading file: Unknown path" + @pytest.mark.asyncio async def test_char_budget_trims(self, tool, tmp_path): """When the selected slice exceeds _MAX_CHARS the output is trimmed.""" @@ -200,6 +205,13 @@ class TestEditFileTool: assert "Error" in result assert "not found" in result + @pytest.mark.asyncio + async def test_missing_new_text_returns_clear_error(self, tool, tmp_path): + f = tmp_path / "a.py" + f.write_text("hello", encoding="utf-8") + result = await tool.execute(path=str(f), old_text="hello") + assert result == "Error editing file: Unknown new_text" + # --------------------------------------------------------------------------- # ListDirTool @@ -265,6 +277,11 @@ class TestListDirTool: assert "Error" in result assert "not found" in result + @pytest.mark.asyncio + async def test_missing_path_returns_clear_error(self, tool): + result = await tool.execute() + assert result == "Error listing directory: Unknown path" + # --------------------------------------------------------------------------- # Workspace restriction + extra_allowed_dirs