Skip to content

The agent that grows with you

fix(tools): keep the find lane's depth-0 match; exclude the root by identity

Brooklyn Nicholson9c84bde80281
diff --git a/tests/tools/test_file_operations.py b/tests/tools/test_file_operations.py
index 665e13bde36..0e23d864233 100644
--- a/tests/tools/test_file_operations.py
+++ b/tests/tools/test_file_operations.py
@@ -451,6 +451,22 @@ class TestSearchFilesIncludesDirectories:
assert str(visible_dir) in result.files
assert str(hidden_dir) not in result.files
+ def test_find_lane_never_returns_the_search_root_itself(self, tmp_path, monkeypatch):
+ """The root is excluded by path identity — not by ``-mindepth 1``, which
+ would also exclude the depth-0 operand match a symlinked root depends on
+ (#116270) — and a same-named descendant still matches (#54347)."""
+ root = tmp_path / "vault"
+ child = root / "vault"
+ child.mkdir(parents=True)
+
+ ops = ShellFileOperations(LocalEnvironment("/"))
+ monkeypatch.setattr(ops, "_has_command", lambda command: command == "find")
+ result = ops.search("vault", path=str(root), target="files")
+
+ assert result.error is None
+ assert str(child) in result.files
+ assert str(root) not in result.files
+
def test_target_files_paginates_combined_files_and_directories(self, tmp_path, monkeypatch):
"""Files and directories share one ordering: page slices must compose."""
root = tmp_path / "repo"
diff --git a/tools/file_operations_search.py b/tools/file_operations_search.py
index da87b4d181b..42492e20d68 100644
--- a/tools/file_operations_search.py
+++ b/tools/file_operations_search.py
@@ -252,6 +252,17 @@ def _posix_roots(roots: List[str]) -> bool:
not re.match(r"^[A-Za-z]:[\\/]", root) and "\\" not in root for root in roots)
+def _find_literal_path_expressions(roots: List[str]) -> List[str]:
+ """Escape each search root for find's ``-path`` test, which matches its PATTERN
+ (glob) against the path find echoes — the operand verbatim. The operand reaches
+ the shell already single-quoted; ``-path`` must compare literally, so ``*?[]``
+ in a root's own name need escaping (a directory literally named ``a[1]`` must
+ be excluded by identity, not treated as a character class). Backslashes are
+ left alone: find does not read them as escapes inside a bracket-free pattern,
+ and doubling them would break matching on backslash-shaped roots."""
+ return [re.sub(r"([*?\[\]])", r"\\\1", root) for root in roots]
+
+
class SearchMixin:
"""File-name and content search via rg with find/grep fallbacks. Requires
``_exec``, ``_has_command``, ``_expand_path``, ``_escape_shell_arg``,
@@ -830,9 +841,20 @@ class SearchMixin:
# indistinguishable from an empty directory - while ``rg --files`` followed the
# same argument (#116270). Following the operand inside the command is also what
# covers a link that only exists on the execution host (SSH/container), with no
- # probe of its own.
- base = (f"find -H {' '.join(q_roots)} -mindepth 1{protected_prune}{hidden_prune} "
- f"\\( -type f -o -type d \\) ! -name '.*' -name {self._escape_shell_arg(search_pattern)}")
+ # probe of its own. That contract forbids ``-mindepth 1`` here: with ``-H`` the
+ # followed operand IS depth 0, so it would exclude the root itself — the only
+ # match when the root is a symlink to a file. The root directory itself is
+ # excluded by path identity instead, inside the ``-type d`` arm (the ``-type f``
+ # arm must keep the depth-0 match). ``-path`` matching is literal against the
+ # path as find echoes it, which is the operand verbatim, so the operand needs
+ # its glob metacharacters escaped.
+ root_exemptions = "".join(
+ f" ! -path {self._escape_shell_arg(root)}"
+ for root in _find_literal_path_expressions(find_roots)
+ )
+ base = (f"find -H {' '.join(q_roots)}{protected_prune}{hidden_prune} "
+ f"\\( -type f -o \\( -type d{root_exemptions} \\) \\) "
+ f"! -name '.*' -name {self._escape_shell_arg(search_pattern)}")
if order == "modified":
cmd = "set -o pipefail; " + base + f" -printf '%T@ %p\\n' 2>/dev/null | sort -rn | head -n {fetch_limit}"
else: