diff --git a/nanobot/agent/tools/filesystem.py b/nanobot/agent/tools/filesystem.py index 77b3a926e..dd6c1b87b 100644 --- a/nanobot/agent/tools/filesystem.py +++ b/nanobot/agent/tools/filesystem.py @@ -1174,11 +1174,11 @@ class ListDirTool(_FsTool): if recursive: for item in sorted(dp.rglob("*")): - if any(p in self._IGNORE_DIRS for p in item.parts): + rel = item.relative_to(dp) + if any(p in self._IGNORE_DIRS for p in rel.parts): continue total += 1 if len(items) < cap: - rel = item.relative_to(dp) items.append(f"{rel}/" if item.is_dir() else str(rel)) else: for item in sorted(dp.iterdir()): diff --git a/tests/tools/test_filesystem_tools.py b/tests/tools/test_filesystem_tools.py index 263d5e8f2..ea71f3009 100644 --- a/tests/tools/test_filesystem_tools.py +++ b/tests/tools/test_filesystem_tools.py @@ -327,6 +327,24 @@ class TestListDirTool: assert ".git" not in result assert "node_modules" not in result + @pytest.mark.asyncio + @pytest.mark.parametrize("relative_root", ["build", "build/project"]) + async def test_recursive_ignores_only_descendants(self, tool, tmp_path, relative_root): + root = tmp_path / relative_root + (root / "src").mkdir(parents=True) + (root / "src" / "main.py").write_text("pass") + (root / "README.md").write_text("hi") + (root / ".git").mkdir() + (root / ".git" / "config").write_text("ignored") + (root / "src" / "node_modules").mkdir() + (root / "src" / "node_modules" / "package.json").write_text("{}") + + result = await tool.execute(path=str(root), recursive=True) + + assert set(result.replace("\\", "/").splitlines()) == { + "README.md", "src/", "src/main.py", + } + @pytest.mark.asyncio async def test_max_entries_truncation(self, tool, tmp_path): for i in range(10):