The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tools): follow a symlinked root on the files lane
webtecnica9308a168c310
diff --git a/tests/tools/test_search_budget_truncation.py b/tests/tools/test_search_budget_truncation.pyindex b018b0d6ae2..9a0564286a5 100644--- a/tests/tools/test_search_budget_truncation.py+++ b/tests/tools/test_search_budget_truncation.py@@ -167,7 +167,9 @@ def test_no_rg_multi_root_modified_is_one_globally_sorted_scan():assert result.truncated is Trueassert len(env.find_commands) == 1command, kwargs = env.find_commands[0]- assert "find '/one/.hidden' '/two/.cache'" in command+ # ``-H``: the operands are stat'ed, so a symlinked root is walked instead of+ # matching nothing at all (#116270). The single globally-sorted scan is unchanged.+ assert "find -H '/one/.hidden' '/two/.cache'" in commandassert "sort -rn" in commandassert "head -n 2" in commandassert "! -path '/one/.hidden'" in commanddiff --git a/tests/tools/test_search_zero_match_and_multipath.py b/tests/tools/test_search_zero_match_and_multipath.pyindex 23e07447a83..00eb3236119 100644--- a/tests/tools/test_search_zero_match_and_multipath.py+++ b/tests/tools/test_search_zero_match_and_multipath.py@@ -263,3 +263,82 @@ class TestZeroMatchProbeEngineParity:pytest.skip("rg not installed")r = ops.search("TOKEN_ALPHA\\nother", path=str(proj / "proj"), target="content")assert "line-oriented" not in (r.warning or "")+++class TestSymlinkedRootOnTheFilesLane:+ """A symlinked root must be searched on the files lane, on every engine (#116270).++ ``find <link> -type f`` tests the link itself, so a symlinked root listed nothing at+ all - ``total_count: 0``, ``error=None``, no warning, indistinguishable from an empty+ directory - while ``rg --files`` followed the same argument. ``find -H`` follows the+ operand (and only the operand), so both engines answer the same thing, and the+ follow happens inside the command, on the host that owns the link.+ """++ @pytest.fixture+ def linked(self, tmp_path, monkeypatch):+ monkeypatch.setenv("HERMES_HOME", str(tmp_path / ".hermes"))+ target = tmp_path / "target"+ target.mkdir()+ (target / "real.md").write_text("TOKEN\n")+ empty = tmp_path / "empty"+ empty.mkdir()+ (tmp_path / "linkdir").symlink_to(target)+ (tmp_path / "link.md").symlink_to(target / "real.md")+ (tmp_path / "emptylink").symlink_to(empty)+ return tmp_path++ @staticmethod+ def _pin_engine(monkeypatch, ops, keep):+ """Force ``keep`` as the engine so a host that also has rg exercises find."""+ real = ops._has_command++ def only(cmd, _real=real, _keep=keep):+ if cmd in ("rg", "grep"):+ return _real(cmd) if cmd == _keep else False+ return _real(cmd)++ monkeypatch.setattr(ops, "_has_command", only)++ @pytest.mark.parametrize("engine", ["find", "rg"])+ @pytest.mark.parametrize("lane", ["linkdir", "link.md"])+ def test_symlinked_root_lists_the_target_files(self, linked, monkeypatch, engine, lane):+ from tools.file_tools import _get_file_ops++ ops = _get_file_ops(task_id=f"t-symlink-files-{engine}-{lane.replace('.', '-')}")+ if not ops._has_command(engine):+ pytest.skip(f"{engine} not installed")+ self._pin_engine(monkeypatch, ops, engine)+ r = ops.search("*.md", path=str(linked / lane), target="files")+ assert r.error is None, r.error+ assert r.total_count >= 1 and any(f.endswith(".md") for f in r.files), (+ f"a symlinked root ({lane}) listed nothing and said nothing on the {engine} "+ f"lane: total_count={r.total_count} files={r.files!r}")++ def test_plain_and_empty_roots_keep_their_answer(self, linked, monkeypatch):+ """Following the operand must not invent matches or move an ordinary root."""+ from tools.file_tools import _get_file_ops++ ops = _get_file_ops(task_id="t-symlink-files-guards")+ self._pin_engine(monkeypatch, ops, "find")+ plain = ops.search("*.md", path=str(linked / "target"), target="files")+ assert plain.error is None, plain.error+ assert plain.total_count >= 1 and any(f.endswith("real.md") for f in plain.files)+ for root in (str(linked / "empty"), str(linked / "emptylink")):+ nothing = ops.search("*.md", path=root, target="files")+ assert nothing.error is None and nothing.total_count == 0, (+ f"an empty root ({root}) answered {nothing.total_count} file(s): {nothing.files!r}")++ def test_a_symlinked_root_pointing_at_home_is_still_refused(self, tmp_path, monkeypatch):+ """The no-rg breadth guard must classify the link's target (#116270)."""+ import tools.file_operations as file_operations+ from tools.environments.local import LocalEnvironment+ from tools.file_operations import ShellFileOperations++ home = tmp_path / "home"+ home.mkdir()+ (tmp_path / "link-to-home").symlink_to(home)+ monkeypatch.setattr(file_operations, "_HOME", str(home))+ ops = ShellFileOperations(LocalEnvironment(str(tmp_path)))++ assert ops._is_broad_local_search_root(str(tmp_path / "link-to-home")) is Truediff --git a/tools/file_operations_search.py b/tools/file_operations_search.pyindex abbcfcda935..cf23e4f7285 100644--- a/tools/file_operations_search.py+++ b/tools/file_operations_search.py@@ -636,7 +636,11 @@ class SearchMixin:value = _msys_to_windows_path(value).replace("\\", "/")if not os.path.isabs(value):value = os.path.join(getattr(self.env, "cwd", None) or self.cwd, value)- return os.path.normcase(os.path.abspath(value))+ # Classify the linked target, not the link: ``find -H`` now follows an+ # operand symlink, so a link pointing at $HOME (or at the filesystem root)+ # must not slip a recursive find past this guard (#116270). Local-only by+ # the isinstance check above, so this resolves on the host that runs find.+ return os.path.normcase(os.path.realpath(value))from tools import file_operations as _fo # lazy: _HOME is monkeypatched thereroot = normalized(path)@@ -702,7 +706,14 @@ class SearchMixin:protected_paths = [absolute for _r, _rel, absolute in self._effective_macos_search_exclusions(roots)]protected_prune = f" {self._prune_expr(protected_paths)} -o" if protected_paths else ""fetch_limit = offset + limit + 1- base = (f"find {' '.join(q_roots)}{protected_prune}{hidden_prune} -type f "+ # ``-H`` follows a symlink handed in as an OPERAND, and only an operand: without+ # it ``find <link> -type f`` tests the link itself, so ``target="files"`` listed+ # nothing at all for a symlinked root - total_count: 0, no error, no warning,+ # 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)}{protected_prune}{hidden_prune} -type f "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}"