"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/
Python
fix(browser): revert rooted-grep to single multi-line execute (Codex round 2)
apirenob26708d62b1c
diff --git a/browser/agent-harness/cli_anything/browser/tests/test_domshell_backend.py b/browser/agent-harness/cli_anything/browser/tests/test_domshell_backend.pyindex 275269013..f0660d67e 100644--- a/browser/agent-harness/cli_anything/browser/tests/test_domshell_backend.py+++ b/browser/agent-harness/cli_anything/browser/tests/test_domshell_backend.py@@ -26,57 +26,53 @@ def test_grep_unrooted_produces_single_grep_call(mock_call):@patch.object(backend, "_call_execute", new_callable=AsyncMock)-def test_grep_rooted_produces_three_calls_in_order(mock_call):- """Rooted grep dispatches cd → grep → cd-back, in that order."""+def test_grep_rooted_emits_single_multiline_call(mock_call):+ """Rooted grep is ONE multi-line ``cd / grep / cd back`` execute call.++ Each ``_call_execute`` in non-daemon mode opens a fresh MCP session+ that lands in its own DOMShell 2.x lane, so splitting cd / grep /+ restore across separate calls would lose the cwd between them. The+ three lines must travel in one ``domshell_execute`` call to share+ a lane.+ """mock_call.return_value = {}backend.grep("Login", path="/main", prev="/")assert mock_call.call_args_list == [- call("cd /main", False),- call("grep Login", False),- call("cd /", False),+ call("cd /main\ngrep Login\ncd /", False),]@patch.object(backend, "_call_execute", new_callable=AsyncMock)-def test_grep_rooted_restores_cwd_when_grep_raises(mock_call):- """If grep errors mid-flight, the trailing cd-back still runs (try/finally)."""- mock_call.side_effect = [- {}, # cd /main → success- RuntimeError("grep blew up"), # grep → raise- {}, # cd / (restore) — required- ]-- with pytest.raises(RuntimeError, match="grep blew up"):- backend.grep("Login", path="/main", prev="/")-- assert mock_call.call_count == 3- assert mock_call.call_args_list[-1] == call("cd /", False)-+def test_grep_rooted_uses_single_call_for_lane_isolation(mock_call):+ """Documents the lane-isolation contract: ONE call, not three.[email protected](backend, "_call_execute", new_callable=AsyncMock)-def test_grep_rooted_short_circuits_when_cd_errors(mock_call):- """If the initial cd reports an error, grep is skipped and no restore is sent."""- mock_call.return_value = {"isError": True, "error": "no such path"}+ The trailing ``cd prev`` is delivered as the final line of the same+ multi-line command and runs even if ``grep`` errors, per DOMShell's+ documented continue-on-error semantics+ (`apireno/DOMShell#46 <https://github.com/apireno/DOMShell/issues/46>`_).+ """+ mock_call.return_value = {}- result = backend.grep("Login", path="/missing", prev="/")+ backend.grep("Login", path="/main", prev="/")- # Only the initial cd ran; grep + restore were skipped.- assert mock_call.call_args_list == [call("cd /missing", False)]- assert result == {"isError": True, "error": "no such path"}+ assert mock_call.call_count == 1@patch.object(backend, "_call_execute", new_callable=AsyncMock)def test_grep_rooted_quotes_path_with_spaces(mock_call):- """Paths with whitespace are shell-quoted before interpolation."""+ """Paths with whitespace are shell-quoted inside the multi-line command."""mock_call.return_value = {}backend.grep("Login", path="/path with spaces", prev="/")- cd_cmd = mock_call.call_args_list[0].args[0]- # shlex.quote single-quotes anything containing whitespace.- assert cd_cmd == "cd '/path with spaces'"+ cmd = mock_call.call_args.args[0]+ # shlex.quote single-quotes anything containing whitespace; the rest+ # of the multi-line layout (grep + cd back) stays intact.+ assert cmd.startswith("cd '/path with spaces'\n")+ assert "\ngrep Login\n" in cmd+ assert cmd.endswith("\ncd /")@patch.object(backend, "_call_execute", new_callable=AsyncMock)diff --git a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.pyindex 3946c116d..9247225b5 100644--- a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py+++ b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py@@ -156,30 +156,6 @@ def _assert_single_line(field: str, value: str) -> None:)-def _is_error(result: Any) -> bool:- """Best-effort check that a ``domshell_execute`` result represents an error.-- Inspects ``isError`` if the MCP SDK populated it; otherwise scans the- concatenated text content for a leading "error". Robust to both the- raw ``CallToolResult`` object and the dict shapes used in unit tests.- """- if hasattr(result, "isError") and result.isError:- return True- if isinstance(result, dict):- if result.get("isError"):- return True- if "error" in result:- return True- text = ""- content = getattr(result, "content", None)- if content:- for c in content:- piece = getattr(c, "text", None)- if piece:- text += piece- return text.strip().lower().startswith("error")--async def _call_execute(command: str, use_daemon: bool = False) -> Any:"""Run a DOMShell command via the single `domshell_execute` MCP tool.@@ -361,12 +337,15 @@ def grep("""Search for pattern in the accessibility tree.When ``path`` is provided and is not ``/``, the search is rooted at that- path: ``cd`` into it, ``grep``, then ``cd`` back to ``prev``. The three- steps run as separate ``domshell_execute`` calls with the restore in a- ``finally`` block, so the cwd is restored even if ``grep`` errors- mid-flight. (A single multi-line call would be one round-trip instead of- three, but it relies on DOMShell's currently-undocumented continue-past-- error behavior — see PR review.)+ path: ``cd`` into it, ``grep``, then ``cd`` back to ``prev`` — sent as one+ multi-line ``domshell_execute`` call so all three lines share an MCP+ session (and therefore a DOMShell lane / cwd). Each ``_call_execute`` in+ non-daemon mode opens a fresh stdio session that lands in its own+ DOMShell 2.x lane, so splitting cd/grep/restore across separate calls+ would lose the cwd between them. The trailing ``cd prev`` is delivered as+ the final line of the same command and runs even if ``grep`` errors —+ DOMShell's multi-line splitter continues past errors (see+ `apireno/DOMShell#46 <https://github.com/apireno/DOMShell/issues/46>`_).``path``, ``prev``, and ``use_daemon`` are keyword-only to prevent silentbreakage of callers written against the pre-migration positional@@ -389,17 +368,12 @@ def grep(>>> grep("Login", path="/main"){"matches": ["/main/button[0]"]}"""+ _assert_single_line("pattern", pattern)if path and path != "/":_assert_single_line("path", path)_assert_single_line("prev", prev)- _assert_single_line("pattern", pattern)- cd_result = asyncio.run(_call_execute(f"cd {_q(path)}", use_daemon))- if _is_error(cd_result):- return cd_result- try:- return asyncio.run(_call_execute(f"grep {_q(pattern)}", use_daemon))- finally:- asyncio.run(_call_execute(f"cd {_q(prev)}", use_daemon))+ command = f"cd {_q(path)}\ngrep {_q(pattern)}\ncd {_q(prev)}"+ return asyncio.run(_call_execute(command, use_daemon))return asyncio.run(_call_execute(f"grep {_q(pattern)}", use_daemon))