Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 64 additions & 50 deletions libs/deepagents/deepagents/backends/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,12 @@ async def agrep(
return self._coerce_grep_result(await self.default.agrep(pattern, path, glob))

def glob(self, pattern: str, path: str | None = None) -> GlobResult:
"""Find files matching a glob pattern, routing by path prefix."""
results: list[FileInfo] = []
"""Find files matching a glob pattern, routing by path prefix.

Routes to backends based on path: a specific route searches one backend,
`"/"` or `None` searches all backends, otherwise searches the
Comment thread
mdrxy marked this conversation as resolved.
Resolved
default backend.
"""
if path is not None:
backend, backend_path, route_prefix = _route_for_path(
default=self.default,
Expand All @@ -438,34 +441,40 @@ def glob(self, pattern: str, path: str | None = None) -> GlobResult:
truncated=_glob_truncated(glob_result),
)

# Path doesn't match any specific route - search default backend AND all routed backends
truncated = False
default_result = self.default.glob(pattern, path)
# A backend error must not be swallowed as a partial success (mirrors the
# grep merge path); surface it instead of returning default-only matches.
if isinstance(default_result, GlobResult) and default_result.error:
return default_result
default_matches = default_result.matches if isinstance(default_result, GlobResult) else default_result
results.extend(default_matches or [])
truncated = truncated or _glob_truncated(default_result)

for route_prefix, backend in self.routes.items():
route_pattern = _strip_route_from_pattern(pattern, route_prefix)
sub_result = backend.glob(route_pattern, "/")
if isinstance(sub_result, GlobResult) and sub_result.error:
return sub_result
sub_matches = sub_result.matches if isinstance(sub_result, GlobResult) else sub_result
results.extend(_remap_file_info_path(fi, route_prefix) for fi in (sub_matches or []))
truncated = truncated or _glob_truncated(sub_result)

# Deterministic ordering
results.sort(key=lambda x: x.get("path", ""))
return GlobResult(matches=results, truncated=truncated)
# If path is None or "/", search default and all routed backends and merge.
# Otherwise, search only the default backend.
if path is None or path == "/":
results: list[FileInfo] = []
truncated = False
default_result = self.default.glob(pattern, path)
# A backend error must not be swallowed as a partial success (mirrors the
# grep merge path); surface it instead of returning default-only matches.
if isinstance(default_result, GlobResult) and default_result.error:
return default_result
default_matches = default_result.matches if isinstance(default_result, GlobResult) else default_result
results.extend(default_matches or [])
truncated = truncated or _glob_truncated(default_result)

for route_prefix, backend in self.routes.items():
route_pattern = _strip_route_from_pattern(pattern, route_prefix)
sub_result = backend.glob(route_pattern, "/")
Comment thread
mdrxy marked this conversation as resolved.
Resolved
if isinstance(sub_result, GlobResult) and sub_result.error:
return sub_result
sub_matches = sub_result.matches if isinstance(sub_result, GlobResult) else sub_result
results.extend(_remap_file_info_path(fi, route_prefix) for fi in (sub_matches or []))
truncated = truncated or _glob_truncated(sub_result)

# Deterministic ordering
results.sort(key=lambda x: x.get("path", ""))
return GlobResult(matches=results, truncated=truncated)

return self.default.glob(pattern, path)

async def aglob(self, pattern: str, path: str | None = None) -> GlobResult:
"""Async version of glob."""
results: list[FileInfo] = []
"""Async version of glob.

See `glob()` for detailed documentation on routing behavior and parameters.
"""
if path is not None:
backend, backend_path, route_prefix = _route_for_path(
default=self.default,
Expand All @@ -482,29 +491,34 @@ async def aglob(self, pattern: str, path: str | None = None) -> GlobResult:
truncated=_glob_truncated(glob_result),
)

# Path doesn't match any specific route - search default backend AND all routed backends
truncated = False
default_result = await self.default.aglob(pattern, path)
# A backend error must not be swallowed as a partial success (mirrors the
# grep merge path); surface it instead of returning default-only matches.
if isinstance(default_result, GlobResult) and default_result.error:
return default_result
default_matches = default_result.matches if isinstance(default_result, GlobResult) else default_result
results.extend(default_matches or [])
truncated = truncated or _glob_truncated(default_result)

for route_prefix, backend in self.routes.items():
route_pattern = _strip_route_from_pattern(pattern, route_prefix)
sub_result = await backend.aglob(route_pattern, "/")
if isinstance(sub_result, GlobResult) and sub_result.error:
return sub_result
sub_matches = sub_result.matches if isinstance(sub_result, GlobResult) else sub_result
results.extend(_remap_file_info_path(fi, route_prefix) for fi in (sub_matches or []))
truncated = truncated or _glob_truncated(sub_result)

# Deterministic ordering
results.sort(key=lambda x: x.get("path", ""))
return GlobResult(matches=results, truncated=truncated)
# If path is None or "/", search default and all routed backends and merge.
# Otherwise, search only the default backend.
if path is None or path == "/":
results: list[FileInfo] = []
truncated = False
default_result = await self.default.aglob(pattern, path)
# A backend error must not be swallowed as a partial success (mirrors the
# grep merge path); surface it instead of returning default-only matches.
if isinstance(default_result, GlobResult) and default_result.error:
return default_result
default_matches = default_result.matches if isinstance(default_result, GlobResult) else default_result
results.extend(default_matches or [])
truncated = truncated or _glob_truncated(default_result)

for route_prefix, backend in self.routes.items():
route_pattern = _strip_route_from_pattern(pattern, route_prefix)
sub_result = await backend.aglob(route_pattern, "/")
if isinstance(sub_result, GlobResult) and sub_result.error:
return sub_result
sub_matches = sub_result.matches if isinstance(sub_result, GlobResult) else sub_result
results.extend(_remap_file_info_path(fi, route_prefix) for fi in (sub_matches or []))
truncated = truncated or _glob_truncated(sub_result)

# Deterministic ordering
results.sort(key=lambda x: x.get("path", ""))
return GlobResult(matches=results, truncated=truncated)

return await self.default.aglob(pattern, path)

def write(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,29 @@ def test_composite_backend_grep_path_isolation():
assert not any("/memories/" in p for p in match_paths), f"grep path=/tools should not return /memories results, but got: {match_paths}"


def test_composite_backend_glob_path_isolation():
"""Test that glob with path=/tools doesn't return results from /memories."""
mem_store = InMemoryStore()

state = StoreBackend(store=mem_store, namespace=lambda _rt: ("default",))
store_be = StoreBackend(store=mem_store, namespace=lambda _rt: ("filesystem",))

comp = CompositeBackend(default=state, routes={"/memories/": store_be})

comp.write("/tools/hammer.md", "tool for nailing")
comp.write("/notes/other.md", "unrelated note")
comp.write("/memories/secret.md", "private memory")

result = comp.glob("*.md", path="/tools")
matches = result.matches
match_paths = [m["path"] for m in matches] if matches is not None else []

# Only /tools files: excludes routed backend (/memories) and other default dirs (/notes)
assert match_paths == ["/tools/hammer.md"]
assert "/memories/secret.md" not in match_paths
assert "/notes/other.md" not in match_paths


def test_composite_grep_and_glob_propagate_truncated(monkeypatch: pytest.MonkeyPatch):
"""A truncated result from a routed/default backend must surface through the composite."""
mem_store = InMemoryStore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,29 @@ async def test_composite_backend_multiple_routes_async():
assert "persistent memory" in updated_content.file_data["content"]


async def test_composite_backend_aglob_path_isolation():
"""Test that aglob with path=/tools doesn't return results from /memories."""
mem_store = InMemoryStore()

state = StoreBackend(store=mem_store, namespace=lambda _rt: ("default",))
store_be = StoreBackend(store=mem_store, namespace=lambda _rt: ("filesystem",))

comp = CompositeBackend(default=state, routes={"/memories/": store_be})

await comp.awrite("/tools/hammer.md", "tool for nailing")
await comp.awrite("/notes/other.md", "unrelated note")
await comp.awrite("/memories/secret.md", "private memory")

result = await comp.aglob("*.md", path="/tools")
matches = result.matches
match_paths = [m["path"] for m in matches] if matches is not None else []

# Only /tools files: excludes routed backend (/memories) and other default dirs (/notes)
assert match_paths == ["/tools/hammer.md"]
assert "/memories/secret.md" not in match_paths
assert "/notes/other.md" not in match_paths


async def test_composite_backend_als_nested_directories_async(tmp_path: Path):
"""Test async ls operations with nested directories."""
root = tmp_path
Expand Down
Loading