Skip to content

Your lightweight open-source personal agent.

fix(tools): support recursive glob filters in file searches

n1uz1104917aaec8b
diff --git a/nanobot/agent/tools/search.py b/nanobot/agent/tools/search.py
index a74d85760..57b20332f 100644
--- a/nanobot/agent/tools/search.py
+++ b/nanobot/agent/tools/search.py
@@ -107,6 +107,22 @@ def _match_glob(rel_path: str, name: str, pattern: str) -> bool:
if not normalized:
return False
if "/" in normalized or normalized.startswith("**"):
+ pattern_parts = PurePosixPath(normalized).parts
+ if "**" in pattern_parts:
+ path_parts = PurePosixPath(rel_path).parts
+ # Keep relative patterns suffix-matched, as with PurePath.match.
+ matched = [True] * (len(path_parts) + 1)
+ for part in pattern_parts:
+ if part == "**":
+ # A globstar consumes zero or more complete path segments.
+ for index in range(1, len(matched)):
+ matched[index] = matched[index] or matched[index - 1]
+ else:
+ matched = [False] + [
+ matched[index] and fnmatch.fnmatchcase(path_part, part)
+ for index, path_part in enumerate(path_parts)
+ ]
+ return matched[-1]
return PurePosixPath(rel_path).match(normalized)
return fnmatch.fnmatch(name, normalized)
diff --git a/tests/tools/test_search_tools.py b/tests/tools/test_search_tools.py
index b39f0c4f0..eeedc958f 100644
--- a/tests/tools/test_search_tools.py
+++ b/tests/tools/test_search_tools.py
@@ -61,6 +61,38 @@ async def test_find_files_filters_by_query_glob_and_type(tmp_path: Path) -> None
assert result.splitlines() == ["src/settings_view.tsx"]
[email protected]("tool_name", ["find_files", "grep"])
+ ("glob", "expected"),
+ [
+ ("**/*.py", ["main.py", "src/api.py", "src/nested/deep/worker.py"]),
+ ("src/**/*.py", ["src/api.py", "src/nested/deep/worker.py"]),
+ ("src/**", ["src/api.py", "src/nested/deep/worker.py"]),
+ ("src/*.py", ["src/api.py"]),
+ ("src/**/deep/*.py", ["src/nested/deep/worker.py"]),
+ (r"src\**\*.py", ["src/api.py", "src/nested/deep/worker.py"]),
+ ],
+)
+async def test_search_recursive_glob_matches_zero_or_more_directories(
+ tmp_path: Path, tool_name: str, glob: str, expected: list[str]
+) -> None:
+ for name in ["main.py", "src/api.py", "src/nested/deep/worker.py", "notes.md"]:
+ path = tmp_path / name
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text("needle\n", encoding="utf-8")
+
+ if tool_name == "find_files":
+ result = await FindFilesTool(workspace=tmp_path, allowed_dir=tmp_path).execute(
+ path=".", glob=glob,
+ )
+ else:
+ result = await GrepTool(workspace=tmp_path, allowed_dir=tmp_path).execute(
+ pattern="needle", path=".", glob=glob, output_mode="files_with_matches",
+ )
+
+ assert sorted(result.splitlines()) == expected
+
+
@pytest.mark.asyncio
async def test_find_files_can_include_directories(tmp_path: Path) -> None:
(tmp_path / "src" / "settings").mkdir(parents=True)