The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(search): keep partial-result warnings across multi-root searches
Teknium37555b53a9b1
diff --git a/tests/tools/test_search_multi_root_warnings.py b/tests/tools/test_search_multi_root_warnings.pynew file mode 100644index 00000000000..7028c52f1ba--- /dev/null+++ b/tests/tools/test_search_multi_root_warnings.py@@ -0,0 +1,59 @@+"""Live regression: a multi-root search keeps every root's partial-result warning.++``path="a,b"`` searches each existing root and merges the results. The merge used+to drop each root's skipped-unreadable-path warning (content mode) or overwrite the+traversal's warning with its own "searched N that exist" note (filename mode), so a+search that silently skipped a mode-000 directory came back looking complete.+"""++import json+import os+import shutil++import pytest++from tools.file_operations import ShellFileOperations+import tools.file_tools # registers search_files+from tools.registry import registry+++def two_roots(tmp_path, monkeypatch):+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))+ roots, locked = [], []+ for name in ("alpha", "beta"):+ root = tmp_path / name+ (root / "locked").mkdir(parents=True)+ (root / f"{name}.py").write_text("needle\n")+ (root / "locked" / "secret.py").write_text("needle\n")+ os.chmod(root / "locked", 0o000)+ roots.append(root)+ locked.append(root / "locked")+ yield roots, locked+ for path in locked:+ os.chmod(path, 0o755)++[email protected]("posix")[email protected](hasattr(os, "geteuid") and os.geteuid() == 0, reason="root reads chmod 000 dirs")[email protected]("engine", ["rg", "grep"])[email protected]("target", ["content", "files"])+def test_multi_root_search_reports_every_skipped_path(two_roots, monkeypatch, target, engine):+ if engine == "rg" and shutil.which("rg") is None:+ pytest.skip("ripgrep not installed")+ if engine == "grep":+ monkeypatch.setattr(ShellFileOperations, "_has_command", lambda self, command: command != "rg")+ roots, locked = two_roots+ pattern = "needle" if target == "content" else "*.py"+ handler = registry.get_entry("search_files").handler+ out = json.loads(handler({"pattern": pattern, "target": target, "path": ",".join(map(str, roots))},+ task_id=f"multi-root-{target}-{engine}"))++ assert "error" not in out, out+ found = out.get("files") or [m["path"] for m in out.get("matches", [])]+ assert sorted(found) == sorted(str(root / f"{root.name}.py") for root in roots)+ warning = out.get("warning", "")+ assert "Skipped 2 unreadable path(s)" in warning, warning+ assert all(str(path) in warning for path in locked), warning+ assert "Results may be incomplete" in warning+ assert warning.rstrip().endswith("searched 2 that exist"), warningdiff --git a/tools/file_operations.py b/tools/file_operations.pyindex f94340c535e..23d7cd0e44c 100644--- a/tools/file_operations.py+++ b/tools/file_operations.py@@ -1710,8 +1710,9 @@ class ShellFileOperations(LintMixin, SearchMixin, FileOperations):exclusions = self._macos_search_exclusions(path)if exclusions and not result.error:skipped = ", ".join(item.split("/")[-1] for item in exclusions)- result.warning = (+ protected = ("Skipped macOS protected folders during broad search to avoid "f"an unattended privacy prompt: {skipped}. Search a protected ""folder directly when access is intentional.")+ result.warning = f"{result.warning} {protected}" if result.warning else protectedreturn resultdiff --git a/tools/file_operations_search.py b/tools/file_operations_search.pyindex 46bd4c8a56e..c0f014a9b52 100644--- a/tools/file_operations_search.py+++ b/tools/file_operations_search.py@@ -243,6 +243,33 @@ def _prepend_skipped_paths_warning(diagnostics: str, existing: Optional[str]) ->_PERMISSION_SEARCH_HINT = "Nothing matched in the readable part of the search scope."+_SKIPPED_WARNING_RE = re.compile(+ r"Skipped (?P<count>\d+) unreadable path\(s\) with restricted permissions: (?P<shown>.*?)"+ r"(?: \(\+\d+ more\))?\. Results may be incomplete; fix permissions or run as a user that "+ r"can read them to include them\.")+++def _merge_child_warnings(warnings: list[Optional[str]], has_results: bool) -> list[str]:+ """Combine per-root search warnings into one list: skipped-path sentences collapse+ into a single sentence with the summed count, the rest are deduplicated in order.+ The nothing-matched hint is dropped once any root produced results."""+ total, shown, rest = 0, [], []+ for warning in filter(None, warnings):+ for match in _SKIPPED_WARNING_RE.finditer(warning):+ total += int(match.group("count"))+ shown.extend(p for p in match.group("shown").split(", ") if p and p not in shown)+ for piece in _SKIPPED_WARNING_RE.sub("\0", warning).split("\0"):+ piece = piece.strip()+ if has_results:+ piece = piece.replace(_PERMISSION_SEARCH_HINT, "").strip()+ if piece and piece not in rest:+ rest.append(piece)+ if not total:+ return rest+ kept = shown[:3]+ sentence = _skipped_unreadable_warning(kept, max(total - len(kept), 0))+ return [sentence, *rest]+_FIND_MODIFIED_ORDER_ERROR = ("Exact modification-time order requires GNU find with "@@ -787,12 +814,14 @@ class SearchMixin:# One global traversal across roots so modified ordering and pagination# are exact; root admission wraps the actual rg/find invocation.merged = self._search_files(pattern, existing, limit, offset, order)+ child_warnings = [merged.warning]else:- merged = SearchResult()+ merged, child_warnings = SearchResult(), []for root in existing:sub = self._search_content(pattern, root, file_glob, limit, offset, output_mode, context)if sub.error:return sub+ child_warnings.append(sub.warning)merged.matches.extend(sub.matches)merged.files.extend(sub.files)merged.counts.update(sub.counts)@@ -805,7 +834,8 @@ class SearchMixin:note += "; skipped missing: " + ", ".join(missing[:3])if len(missing) > 3:note += f" (+{len(missing) - 3} more)"- warning_parts = [note]+ has_results = bool(merged.total_count or merged.matches or merged.files or merged.counts)+ warning_parts = [*_merge_child_warnings(child_warnings, has_results), note]if not merged.error:protected_paths = [absolute for _r, _rel, absolute in self._effective_macos_search_exclusions(existing)]if protected_paths: