Skip to content

The agent that grows with you

fix(search): unify fallback scans and reject partial errors

Royalaid06086b95e159
diff --git a/tests/tools/test_search_budget_truncation.py b/tests/tools/test_search_budget_truncation.py
index 98094e31181..d14a54e89ef 100644
--- a/tests/tools/test_search_budget_truncation.py
+++ b/tests/tools/test_search_budget_truncation.py
@@ -102,6 +102,21 @@ class FindRecordingEnvironment:
]
+class MultiRootFindEnvironment(FindRecordingEnvironment):
+ def execute(self, command, **kwargs):
+ self.commands.append((command, kwargs))
+ if command.startswith("test -e "):
+ output = "not_found\n" if "'/one/.hidden /two/.cache'" in command else "exists\n"
+ return {"output": output, "returncode": 0}
+ if command.startswith("command -v find"):
+ return {"output": "yes\n", "returncode": 0}
+ if command.startswith("command -v rg"):
+ return {"output": "", "returncode": 1}
+ if "find " in command:
+ return {"output": self.output, "returncode": self.code}
+ return {"output": "", "returncode": 1}
+
+
def test_find_discovery_is_one_unsorted_pruned_bounded_scan():
env = FindRecordingEnvironment("/narrow/a.py\n/narrow/b.py\n/narrow/c.py\n/narrow/d.py\n")
result = ShellFileOperations(env)._search_files(
@@ -132,6 +147,32 @@ def test_find_modified_is_one_exact_scan_without_bsd_retry():
assert "head -n 3" in command
+def test_no_rg_multi_root_modified_is_one_globally_sorted_scan():
+ env = MultiRootFindEnvironment(
+ "30 /two/.cache/new.py\n10 /one/.hidden/old.py\n"
+ )
+
+ result = ShellFileOperations(env).search(
+ "*.py",
+ path="/one/.hidden /two/.cache",
+ target="files",
+ order="modified",
+ limit=1,
+ )
+
+ assert result.error is None
+ assert result.files == ["/two/.cache/new.py"]
+ assert result.truncated is True
+ assert len(env.find_commands) == 1
+ command, kwargs = env.find_commands[0]
+ assert "find '/one/.hidden' '/two/.cache'" in command
+ assert "sort -rn" in command
+ assert "head -n 2" in command
+ assert "! -path '/one/.hidden'" in command
+ assert "! -path '/two/.cache'" in command
+ assert kwargs["timeout"] <= 60
+
+
def test_find_modified_capability_failure_is_actionable_without_retry():
env = FindRecordingEnvironment("", code=1)
result = ShellFileOperations(env)._search_files(
diff --git a/tests/tools/test_search_files_engine_selection.py b/tests/tools/test_search_files_engine_selection.py
index 4b96f2cb61f..9d3fa262d96 100644
--- a/tests/tools/test_search_files_engine_selection.py
+++ b/tests/tools/test_search_files_engine_selection.py
@@ -127,6 +127,19 @@ def test_modified_capability_failure_is_actionable_and_not_downgraded():
assert "ripgrep" in result.error
[email protected]("order", ["discovery", "modified"])
+def test_rg_partial_output_with_error_exit_fails_closed(order):
+ env = RecordingEnvironment(rg_output="/repo/partial.py\n", rg_code=2)
+
+ result = ShellFileOperations(env).search(
+ "*.py", path="/repo", target="files", order=order
+ )
+
+ assert result.error is not None
+ assert result.files == []
+ assert len(env.rg_commands) == 1
+
+
def test_invalid_direct_file_order_returns_structured_error():
env = RecordingEnvironment()
ops = ShellFileOperations(env)
diff --git a/tools/file_operations.py b/tools/file_operations.py
index 41118f17b53..6a08d8f4670 100644
--- a/tools/file_operations.py
+++ b/tools/file_operations.py
@@ -3496,23 +3496,7 @@ class ShellFileOperations(FileOperations):
rg_executable=resolved,
)
else:
- # find cannot accept the same cross-platform guarantees as rg;
- # preserve per-root fallback scans, but never swallow an error
- # and apply the caller's page once to the combined result.
- collected: List[str] = []
- any_truncated = False
- fetch = offset + limit + 1
- for root in existing:
- sub = self._search_files(pattern, root, fetch, 0, order)
- if sub.error:
- return sub
- collected.extend(sub.files)
- any_truncated = any_truncated or sub.truncated
- merged = SearchResult(
- files=collected[offset:offset + limit],
- total_count=len(collected),
- truncated=any_truncated or len(collected) > offset + limit,
- )
+ merged = self._search_files(pattern, existing, limit, offset, order)
else:
merged = SearchResult()
for root in existing:
@@ -3641,7 +3625,7 @@ class ShellFileOperations(FileOperations):
anchor = os.path.splitdrive(root)[0] + os.sep if os.path.splitdrive(root)[0] else os.path.abspath(os.sep)
return root == home or common == root or root == os.path.normcase(anchor)
- def _search_files(self, pattern: str, path: str, limit: int, offset: int,
+ def _search_files(self, pattern: str, path: str | List[str], limit: int, offset: int,
order: str = "discovery") -> SearchResult:
"""Search for files by name pattern (glob-like)."""
# Auto-prepend **/ for recursive search if not already present
@@ -3660,7 +3644,8 @@ class ShellFileOperations(FileOperations):
# A local find traversal rooted at/above the user's home or at a
# filesystem root can consume minutes and prompt on protected paths.
# Refuse before invoking find. Controller paths never classify remotes.
- if self._is_broad_local_search_root(path):
+ roots = [path] if isinstance(path, str) else path
+ if any(self._is_broad_local_search_root(root) for root in roots):
return SearchResult(error=(
"Broad local file search without ripgrep is disabled because "
"find cannot keep this traversal safely bounded. Install "
@@ -3677,13 +3662,15 @@ class ShellFileOperations(FileOperations):
# Prune hidden descendant directories while still allowing an
# explicitly selected hidden root. Hidden files are excluded too,
# matching rg's default semantics.
- q_path = self._escape_shell_arg(path)
+ q_roots = [self._escape_shell_arg(root) for root in roots]
+ root_exemptions = "".join(f" ! -path {root}" for root in q_roots)
hidden_prune = (
- f" \\( -type d -name '.*' ! -path {q_path} \\) -prune -o"
+ f" \\( -type d -name '.*'{root_exemptions} \\) -prune -o"
)
protected_paths = [
- os.path.normpath(os.path.join(path, item))
- for item in self._macos_search_exclusions(path)
+ os.path.normpath(os.path.join(root, item))
+ for root in roots
+ for item in self._macos_search_exclusions(root)
]
protected_prune = ""
if protected_paths:
@@ -3694,7 +3681,7 @@ class ShellFileOperations(FileOperations):
fetch_limit = offset + limit + 1
base = (
- f"find {q_path}{protected_prune}{hidden_prune} -type f "
+ f"find {' '.join(q_roots)}{protected_prune}{hidden_prune} -type f "
f"! -name '.*' -name {self._escape_shell_arg(search_pattern)}"
)
if order == "modified":
@@ -3785,7 +3772,7 @@ class ShellFileOperations(FileOperations):
stdout, limit_reason = _search_stdout_and_limit(result)
all_files = [f for f in stdout.splitlines() if f]
- if result.exit_code not in {0, 1, 124} and not all_files:
+ if result.exit_code not in {0, 1, 124}:
if order == "modified":
return SearchResult(error=(
"Exact modification-time order failed; ripgrep 14+ is "