The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(search): harden bounded fallback handling
Royalaid6d9022a55e4f
diff --git a/tests/tools/test_search_budget_truncation.py b/tests/tools/test_search_budget_truncation.pyindex 76dd9abdd14..16741d7d099 100644--- a/tests/tools/test_search_budget_truncation.py+++ b/tests/tools/test_search_budget_truncation.py@@ -182,6 +182,48 @@ def test_find_modified_capability_failure_is_actionable_without_retry():assert len(env.find_commands) == 1+ ("order", "output"),+ [+ (+ "discovery",+ "/narrow/a.py\n/narrow/b.py\n/narrow/c.py\n/narrow/d.py\n",+ ),+ (+ "modified",+ "40 /narrow/a.py\n30 /narrow/b.py\n20 /narrow/c.py\n10 /narrow/d.py\n",+ ),+ ],+)+def test_find_sigpipe_is_benign_only_after_fetch_limit_rows(order, output):+ result = ShellFileOperations(FindRecordingEnvironment(output, code=141))._search_files(+ "*.py", "/narrow", limit=2, offset=1, order=order+ )++ assert result.error is None+ assert result.files == ["/narrow/b.py", "/narrow/c.py"]+ assert result.truncated is True+++ ("order", "output", "error_fragment"),+ [+ ("discovery", "/narrow/partial.py\n", "bounded find traversal"),+ ("modified", "10 /narrow/partial.py\n", "modification-time"),+ ],+)+def test_find_sigpipe_with_fewer_than_fetch_limit_rows_fails_closed(+ order, output, error_fragment+):+ result = ShellFileOperations(FindRecordingEnvironment(output, code=141))._search_files(+ "*.py", "/narrow", limit=2, offset=1, order=order+ )++ assert error_fragment in (result.error or "")+ assert result.files == []+ assert result.total_count == 0++@pytest.mark.parametrize(("order", "output", "error_fragment"),[diff --git a/tests/tools/test_search_files_engine_selection.py b/tests/tools/test_search_files_engine_selection.pyindex 84bd74f045c..0e4a13e9ebb 100644--- a/tests/tools/test_search_files_engine_selection.py+++ b/tests/tools/test_search_files_engine_selection.py@@ -79,7 +79,10 @@ def test_modified_zero_match_exit_one_is_valid_without_capability_error():assert len(env.rg_commands) == 1[email protected]("version", ["ripgrep 13.0.0\n", "ripgrep unknown\n"])+ "version",+ ["ripgrep 13.0.0\n", "ripgrep unknown\n", "ripgrep 14 garbage\n"],+)def test_modified_requires_parseable_ripgrep_14_before_search(version):env = RecordingEnvironment()@@ -103,6 +106,25 @@ def test_modified_requires_parseable_ripgrep_14_before_search(version):assert len([c for c in env.commands if "--version" in c]) == 1+def test_modified_accepts_complete_ripgrep_semver_with_revision_text():+ env = RecordingEnvironment()+ original_execute = env.execute++ def execute(command, **kwargs):+ if "--version" in command:+ env.commands.append(command)+ return {"output": "ripgrep 14.1.1 (rev abc123)\n", "returncode": 0}+ return original_execute(command, **kwargs)++ env.execute = execute+ result = ShellFileOperations(env).search(+ "*.py", path="/repo", target="files", order="modified"+ )++ assert result.error is None+ assert len(env.rg_commands) == 1++def test_empty_discovery_output_is_zero_matches_without_retry():env = RecordingEnvironment(rg_output="", rg_code=0)ops = ShellFileOperations(env)@@ -339,6 +361,37 @@ def test_modified_multi_path_search_preserves_exact_order_request():assert "'/two'" in env.rg_commands[0]+def test_comma_delimited_file_roots_preserve_internal_spaces_in_one_search():+ env = RecordingEnvironment(rg_output="C:/root one/a.py\nC:/root two/b.py\n")+ combined = "C:/root one, C:/root two"++ path_checks = 0++ def execute(command, **kwargs):+ nonlocal path_checks+ env.commands.append(command)+ if command.startswith("test -e "):+ path_checks += 1+ output = "not_found\n" if path_checks == 1 else "exists\n"+ return {"output": output, "returncode": 0}+ if command.startswith("command -v rg"):+ return {"output": "/usr/bin/rg\n", "returncode": 0}+ if "--files" in command:+ return {"output": env.rg_output, "returncode": 0}+ return {"output": "", "returncode": 1}++ env.execute = execute+ result = ShellFileOperations(env).search(+ "*.py", path=combined, target="files"+ )++ assert result.error is None+ assert result.files == ["C:/root one/a.py", "C:/root two/b.py"]+ assert len(env.rg_commands) == 1+ assert "'C:/root one' 'C:/root two'" in env.rg_commands[0]+ assert "path contained 2 entries" in (result.warning or "")++def test_multi_path_modified_capability_error_propagates():env = RecordingEnvironment()diff --git a/tools/file_operations.py b/tools/file_operations.pyindex 6618cde4899..7a9ec3a0321 100644--- a/tools/file_operations.py+++ b/tools/file_operations.py@@ -1106,7 +1106,11 @@ class ShellFileOperations(FileOperations):return self._rg_modified_capability[executable]quoted = self._quote_executable(executable)result = self._exec(f"{quoted} --version", timeout=10)- match = re.search(r"(?im)^ripgrep\s+(\d+)(?:\.|\s|$)", result.stdout or "")+ match = re.search(+ r"(?im)^ripgrep\s+(\d+)\.\d+\.\d+"+ r"(?:[-+][0-9A-Za-z.-]+)?(?:\s|$)",+ result.stdout or "",+ )if result.exit_code == 0 and match and int(match.group(1)) >= 14:error = Noneelse:@@ -3497,12 +3501,17 @@ class ShellFileOperations(FileOperations):"""Recover a not-found ``path`` that is really several paths in one string.Production trajectories show models passing "dir1 dir2 dir3" (or- comma-separated lists) as ``path``. Split on whitespace/commas; when- at least one candidate exists and at least two candidates were given,- search every existing path, merge results, and note skipped parts.- Returns None when this doesn't look like a multi-path string.+ comma-separated lists) as ``path``. Commas explicitly delimit paths and+ therefore preserve internal spaces; without commas, retain the legacy+ whitespace-separated recovery. When at least one candidate exists and+ at least two candidates were given, search every existing path, merge+ results, and note skipped parts. Returns None when this doesn't look+ like a multi-path string."""- parts = [p for chunk in path.split(",") for p in chunk.split() if p.strip()]+ if "," in path:+ parts = [part.strip() for part in path.split(",") if part.strip()]+ else:+ parts = path.split()if len(parts) < 2:return Noneexisting, missing = [], []@@ -3740,14 +3749,11 @@ class ShellFileOperations(FileOperations):result = self._exec(cmd, timeout=60)stdout, limit_reason = _search_stdout_and_limit(result)- if order == "modified" and result.exit_code not in {0, 124}:- return SearchResult(error=(- "Exact modification-time order requires GNU find with "- "-printf support; install ripgrep 14+ or use order='discovery'."- ))- if order == "discovery" and result.exit_code not in {0, 124}:- return SearchResult(error="File search failed while running bounded find traversal.")+ # Parse before classifying exit 141: with pipefail, a bounded producer+ # can receive SIGPIPE when head intentionally closes after fetch_limit+ # rows. It is benign only when the parsed payload proves that bound was+ # reached; a shorter payload remains a hard failure.raw_files: List[str] = []for line in stdout.splitlines():if order == "modified":@@ -3757,6 +3763,15 @@ class ShellFileOperations(FileOperations):raw_files.append(parts[1])elif line:raw_files.append(line)+ bounded_sigpipe = result.exit_code == 141 and len(raw_files) >= fetch_limit++ if order == "modified" and result.exit_code not in {0, 124} and not bounded_sigpipe:+ return SearchResult(error=(+ "Exact modification-time order requires GNU find with "+ "-printf support; install ripgrep 14+ or use order='discovery'."+ ))+ if order == "discovery" and result.exit_code not in {0, 124} and not bounded_sigpipe:+ return SearchResult(error="File search failed while running bounded find traversal.")from tools.environments.local import LocalEnvironment, _IS_WINDOWS, _msys_to_windows_pathif _IS_WINDOWS and isinstance(self.env, LocalEnvironment):