"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/
Python
fix(browser): nav URL parsing + session-required on rooted wrappers (PR #308 round 7.2)
apireno28f19a4025c1
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 bb9fbcbf5..241d4c80d 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@@ -275,6 +275,96 @@ def test_grep_rooted_anchor_failure_short_circuits(mock_call):assert "error" in result+# ── Absolute split-and-check requires session in non-daemon mode ─────+#+# Without a session (and without daemon mode), each `_call_execute` lands+# in a fresh DOMShell lane — the anchor cd's cwd doesn't carry over to+# the operation, producing wrong-scope output. Raise up front to surface+# the misuse instead of silently producing wrong results. (Mirrors+# `type_text`'s round-6.1 guard; propagated to ls/cat/click/grep in+# round 7.2 per Codex P2 #2.)+++def test_ls_absolute_without_session_raises_in_non_daemon():+ with pytest.raises(ValueError, match="session"):+ backend.ls("/main", session=None)++[email protected](backend, "_call_execute", new_callable=AsyncMock)+def test_ls_absolute_with_daemon_no_session_works(mock_call):+ """Daemon mode shares lane via the persistent connection — no+ session required."""+ mock_call.return_value = _make_result("[lane: 1]")+ backend.ls("/main", use_daemon=True, session=None)+ # 3-call split-and-check still happens.+ assert mock_call.call_count == 3++[email protected](backend, "_call_execute", new_callable=AsyncMock)+def test_ls_relative_without_session_works(mock_call):+ """Relative path runs as a single call — no anchor, no lane drift+ risk, so session=None is fine in non-daemon mode."""+ mock_call.return_value = _make_result("[lane: 1]")+ backend.ls("main", session=None)+ assert mock_call.call_count == 1+++def test_cat_absolute_without_session_raises_in_non_daemon():+ with pytest.raises(ValueError, match="session"):+ backend.cat("/main/btn", session=None)++[email protected](backend, "_call_execute", new_callable=AsyncMock)+def test_cat_relative_without_session_works(mock_call):+ mock_call.return_value = _make_result("[lane: 1]")+ backend.cat("main/btn", session=None)+ assert mock_call.call_count == 1+++def test_click_absolute_without_session_raises_in_non_daemon():+ with pytest.raises(ValueError, match="session"):+ backend.click("/main/button[0]", session=None)++[email protected](backend, "_call_execute", new_callable=AsyncMock)+def test_click_relative_without_session_works(mock_call):+ mock_call.return_value = _make_result("[lane: 1]")+ backend.click("button[0]", session=None)+ assert mock_call.call_count == 1+++def test_grep_rooted_absolute_without_session_raises_in_non_daemon():+ """Rooted grep with absolute path requires session for lane consistency."""+ with pytest.raises(ValueError, match="session"):+ backend.grep("Login", path="/main", prev="/", session=None)+++def test_grep_rooted_relative_without_session_raises_in_non_daemon():+ """Rooted grep with RELATIVE path also requires session — both+ branches issue 3 separate calls and depend on shared lane state.+ """+ with pytest.raises(ValueError, match="session"):+ backend.grep("Login", path="dialog", prev="/main", session=None)++[email protected](backend, "_call_execute", new_callable=AsyncMock)+def test_grep_rooted_relative_with_daemon_no_session_works(mock_call):+ """Daemon mode shares lane via the persistent connection — no+ session required even for relative-rooted grep."""+ mock_call.return_value = _make_result("[lane: 1]")+ backend.grep("Login", path="dialog", prev="/main", use_daemon=True, session=None)+ # 3-call split-and-check still happens.+ assert mock_call.call_count == 3++[email protected](backend, "_call_execute", new_callable=AsyncMock)+def test_grep_unrooted_without_session_works(mock_call):+ """Unrooted grep is a single call — no session required."""+ mock_call.return_value = _make_result("[lane: 1]")+ backend.grep("Login", session=None)+ assert mock_call.call_count == 1++# ── _parse_execute_result: dict shapes per command ───────────────────## DOMShell 2.x returns text content; the CLI was written for the@@ -325,16 +415,87 @@ def test_parse_execute_result_error_returns_error_dict():assert parsed["output"] == parsed["error"]-def test_parse_execute_result_default_for_other_commands():- """cat/click/focus/type/open/refresh/back/forward all fall through- to the generic ``{"output": text}`` shape."""- for cmd in ("cat", "click", "focus", "type", "open", "refresh", "back", "forward"):+def test_parse_execute_result_default_for_non_nav_commands():+ """cat/click/focus/type/refresh fall through to the generic+ ``{"output": text}`` shape (nav commands get their own branch —+ see test_parse_execute_result_nav_*)."""+ for cmd in ("cat", "click", "focus", "type", "refresh"):parsed = backend._parse_execute_result(_make_result("done\n[lane: 1]"), cmd,)assert parsed == {"output": "done"}+# ── Nav commands (back/forward/navigate/open): URL/title extraction ──+++def test_parse_execute_result_nav_extracts_url_and_title():+ """back/forward/navigate/open extract `url` and `title` so+ page.go_back/go_forward's `"url" in result` guards fire and+ session.set_url updates correctly. (Codex P2 #1.)+ """+ fixture = _make_result(+ "✓ Navigated back\n"+ "URL: https://example.com/page\n"+ "Title: Example Page\n"+ "[lane: 7]"+ )+ for cmd in ("back", "forward", "navigate", "open"):+ parsed = backend._parse_execute_result(fixture, cmd)+ assert parsed["url"] == "https://example.com/page"+ assert parsed["title"] == "Example Page"+ # The raw output is preserved alongside.+ assert "✓ Navigated back" in parsed["output"]+++def test_parse_execute_result_nav_omits_url_when_missing():+ """Malformed/early response without URL line → `output` only, no crash."""+ parsed = backend._parse_execute_result(_make_result("✓ done\n[lane: 1]"), "back")+ assert parsed == {"output": "✓ done"}+ assert "url" not in parsed+ assert "title" not in parsed+++def test_parse_execute_result_nav_extracts_url_without_title():+ """A response with URL but no Title line still extracts the URL."""+ parsed = backend._parse_execute_result(+ _make_result("URL: https://example.com/x\n[lane: 1]"), "open",+ )+ assert parsed["url"] == "https://example.com/x"+ assert "title" not in parsed+++def test_parse_execute_result_nav_title_handles_trailing_whitespace():+ """Title regex strips trailing newline/whitespace."""+ parsed = backend._parse_execute_result(+ _make_result(+ "URL: https://example.com\n"+ "Title: Spaced Title \n"+ "[lane: 1]"+ ),+ "open",+ )+ assert parsed["title"] == "Spaced Title"++[email protected](backend, "_call_execute", new_callable=AsyncMock)+def test_back_returns_dict_with_url_key(mock_call):+ """End-to-end: backend.back() returns a dict with `url` so+ page.go_back's guard fires. (The Codex P2 #1 regression case.)+ """+ sess = _make_session(working_dir="/")+ mock_call.return_value = _make_result(+ "✓ Navigated back\n"+ "URL: https://previous.com\n"+ "Title: Previous\n"+ "[lane: 1]"+ )+ result = backend.back(session=sess)+ assert "url" in result+ assert result["url"] == "https://previous.com"+ assert result["title"] == "Previous"++# ── End-to-end: wrappers return parsed dicts (not CallToolResult) ────@@ -473,13 +634,14 @@ def test_grep_rooted_emits_three_call_sequence(mock_call):``session.domshell_lane_id`` — see round-4 lane-persistence tests."""mock_call.return_value = _make_result("[lane: 1]")+ sess = _make_session(working_dir="/")- backend.grep("Login", path="/main", prev="/")+ backend.grep("Login", path="/main", prev="/", session=sess)assert mock_call.call_args_list == [- call("cd %here%/main", False, session=None),- call("grep Login", False, session=None),- call("cd %here%", False, session=None),+ call("cd %here%/main", False, session=sess),+ call("grep Login", False, session=sess),+ call("cd %here%", False, session=sess),]@@ -490,14 +652,15 @@ def test_grep_rooted_quotes_path_with_spaces(mock_call):Upstream-smoked: DOMShell's `cd` accepts the quoted form cleanly."""mock_call.return_value = _make_result("[lane: 1]")+ sess = _make_session(working_dir="/")- backend.grep("Login", path="/path with spaces", prev="/")+ backend.grep("Login", path="/path with spaces", prev="/", session=sess)# Three-call sequence — quoting applies to the anchor (first call).assert mock_call.call_args_list == [- call("cd '%here%/path with spaces'", False, session=None),- call("grep Login", False, session=None),- call("cd %here%", False, session=None),+ call("cd '%here%/path with spaces'", False, session=sess),+ call("grep Login", False, session=sess),+ call("cd %here%", False, session=sess),]diff --git a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.pyindex cb493b1bc..8543bf3da 100644--- a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py+++ b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py@@ -290,6 +290,36 @@ def _anchor_path_cmd(deeper: str = "") -> str:return f"cd {_here_path(deeper)}"+def _require_session_for_split_check(+ wrapper: str, session: Any, use_daemon: bool,+) -> None:+ """Raise if a split-and-check wrapper would lose lane consistency.++ Split-and-check patterns (anchor → op → restore as separate+ ``_call_execute`` calls) need all three calls in the same DOMShell+ lane. In daemon mode the persistent connection guarantees that+ naturally; otherwise the wrapper needs ``session.domshell_lane_id``+ to forward via ``group_id`` on each call. Without either, the+ anchor lands in lane A and the operation lands in lane B — same+ lane-isolation regression as round-5's first failure mode.++ The general rule: any wrapper that issues multiple+ ``_call_execute`` calls whose correctness depends on shared lane+ state requires a session in non-daemon mode. Mirrors the round-6.1+ guard in ``type_text``; applied to ``ls`` / ``cat`` / ``click``+ absolute paths and ``grep`` rooted paths (both absolute and+ relative) in round 7.2. Centralizes the contract so future+ multi-call wrappers can call into the same check.+ """+ if session is None and not use_daemon:+ raise ValueError(+ f"{wrapper}: a session argument is required in non-daemon "+ "mode for rooted paths so the anchor cd and the operation "+ "share a DOMShell lane. Either pass a Session, use an "+ "unrooted form, or enable daemon mode."+ )++# CSI (ANSI) escape sequences DOMShell wraps error text in (typically red).# Strip them before prefix-matching "error" so an ANSI-coloured error line# isn't misread as success.@@ -404,9 +434,26 @@ def _parse_execute_result(result: Any, command: str) -> dict:matches = [ln for ln in text.splitlines() if ln.strip()]return {"matches": matches, "raw": text}- # cd / cat / click / focus / type / open / refresh / back / forward- # all funnel through the CLI's generic dict pretty-printer; an- # ``{"output": text}`` shape is enough.+ if command in ("back", "forward", "navigate", "open"):+ # Navigation commands typically respond with a `URL: <url>`+ # (and often `Title: <title>`) line — extract both so+ # `page.go_back` / `page.go_forward`'s `"url" in result` guards+ # actually fire and `session.set_url` updates correctly. (Round+ # 7's parser made these guards permanently False; this is the+ # Codex P2 #1 fix.) The raw `output` is preserved alongside so+ # CLI display paths still work.+ nav: dict = {"output": text}+ url_match = re.search(r"URL:\s+(\S+)", text)+ if url_match:+ nav["url"] = url_match.group(1)+ title_match = re.search(r"Title:\s+(.+?)(?:\r?\n|$)", text)+ if title_match:+ nav["title"] = title_match.group(1).strip()+ return nav++ # cd / cat / click / focus / type / refresh all funnel through the+ # CLI's generic dict pretty-printer; an ``{"output": text}`` shape+ # is enough.return {"output": text}@@ -600,6 +647,7 @@ def ls(path: str = "/", use_daemon: bool = False, *, session: Any = None) -> dic"""translated, is_absolute = _translate_path(path)if is_absolute:+ _require_session_for_split_check("ls", session, use_daemon)# Split-and-check: the anchor's success is load-bearing — if# cd fails, ls would run in the wrong cwd and produce# wrong-target results. Three separate _call_execute calls so@@ -689,6 +737,7 @@ def cat(path: str, use_daemon: bool = False, *, session: Any = None) -> dict:"Use `ls` to list the root's children, or pass a specific name.")if is_absolute:+ _require_session_for_split_check("cat", session, use_daemon)# Split-and-check: anchor at tab root, halt if anchor fails,# otherwise run relative cat, restore. Anchor success is# load-bearing — without it cat resolves the relative path@@ -771,6 +820,12 @@ def grep(# useful error regardless of which branch consumes it._assert_single_line("prev", prev)+ # Both rooted branches (absolute and relative) issue 3 separate+ # `_call_execute` calls — anchor cd, grep, restore cd — and depend on+ # all three landing in the same DOMShell lane. Check session here so+ # the requirement applies uniformly.+ _require_session_for_split_check("grep", session, use_daemon)+# Split-and-check. Anchor success is load-bearing: if the cd# to the rooting path fails, grep would search against the wrong# cwd and produce wrong-scope matches.@@ -820,6 +875,7 @@ def click(path: str, use_daemon: bool = False, *, session: Any = None) -> dict:"click: an element name is required — cannot click the tab root.")if is_absolute:+ _require_session_for_split_check("click", session, use_daemon)# Split-and-check: anchor at tab root, halt if anchor fails,# otherwise click the relative path, restore. Anchor success is# load-bearing — clicking the wrong element if cwd has drifted@@ -851,22 +907,22 @@ def open_url(url: str, use_daemon: bool = False, *, session: Any = None) -> dictsession: Harness session whose DOMShell lane id is reused / updatedReturns:- Dict shaped ``{"output": str}`` on success, or- ``{"error": str, "output": str}`` on failure.+ Dict shaped ``{"output": str, "url": str, "title": str}`` on+ success, or ``{"error": str, "output": str}`` on failure.- Note no ``"url"`` key. ``page.open_page`` already calls- ``session.set_url(url)`` from the input arg unconditionally, so- this isn't a regression for ``open_url`` callers. But the same- absence applies to ``back()`` / ``forward()`` — ``page.go_back``- and ``page.go_forward`` guard their ``session.set_url`` updates- on ``"url" in result``, and that guard now always evaluates- False (it was already False against the pre-parse- ``CallToolResult`` shape, so no behavior change vs main, but- worth knowing for future parser refinement).+ ``url`` and ``title`` are extracted from the DOMShell response+ text via regex (lines starting ``URL:`` / ``Title:``) — they're+ present when DOMShell emits those lines, omitted otherwise.+ ``page.open_page`` doesn't depend on ``url`` (it always calls+ ``session.set_url(url)`` from the input arg), but the same+ parser is used by ``back`` / ``forward`` whose ``page``-layer+ callers DO depend on ``"url" in result``.Example:>>> open_url("https://example.com")- {"output": "✓ Opened https://example.com\\n[lane: 1]"}+ {"output": "✓ Opened\\nURL: https://example.com\\nTitle: Example Domain\\n[lane: 1]",+ "url": "https://example.com",+ "title": "Example Domain"}"""result = asyncio.run(_call_execute(f"open {_q(url)}", use_daemon, session=session)@@ -896,15 +952,13 @@ def back(use_daemon: bool = False, *, session: Any = None) -> dict:session: Harness session whose DOMShell lane id is reused / updatedReturns:- Dict shaped ``{"output": str}`` on success, or- ``{"error": str, "output": str}`` on failure.-- Note no ``"url"`` key. ``page.go_back`` guards its- ``session.set_url`` update on ``"url" in result`` and that- guard now always evaluates False — ``session.current_url`` is- not updated from a back navigation. (Same end behavior as- against the pre-parse ``CallToolResult`` shape, but worth- knowing for future parser refinement.)+ Dict shaped ``{"output": str, "url": str, "title": str}`` on+ success, or ``{"error": str, "output": str}`` on failure. ``url``+ and ``title`` are extracted from the response text via regex+ (lines starting ``URL:`` / ``Title:``) and may be omitted if+ the response shape changes upstream — ``page.go_back`` guards+ its ``session.set_url`` update on ``"url" in result``, so a+ missing URL silently skips the update."""result = asyncio.run(_call_execute("back", use_daemon, session=session))return _parse_execute_result(result, "back")@@ -918,15 +972,13 @@ def forward(use_daemon: bool = False, *, session: Any = None) -> dict:session: Harness session whose DOMShell lane id is reused / updatedReturns:- Dict shaped ``{"output": str}`` on success, or- ``{"error": str, "output": str}`` on failure.-- Note no ``"url"`` key. ``page.go_forward`` guards its- ``session.set_url`` update on ``"url" in result`` and that- guard now always evaluates False — ``session.current_url`` is- not updated from a forward navigation. (Same end behavior as- against the pre-parse ``CallToolResult`` shape, but worth- knowing for future parser refinement.)+ Dict shaped ``{"output": str, "url": str, "title": str}`` on+ success, or ``{"error": str, "output": str}`` on failure. ``url``+ and ``title`` are extracted from the response text via regex+ (lines starting ``URL:`` / ``Title:``) and may be omitted if+ the response shape changes upstream — ``page.go_forward`` guards+ its ``session.set_url`` update on ``"url" in result``, so a+ missing URL silently skips the update."""result = asyncio.run(_call_execute("forward", use_daemon, session=session))return _parse_execute_result(result, "forward")