Skip to content

"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/

Python

fix(browser): PR #308 polish round — Copilot Med + Lows

apireno25c78b60a02f
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.py
index 38cea51d6..ce2e7dc7a 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
@@ -455,10 +455,29 @@ def test_type_text_relative_path_focus_failure_skips_type(mock_call):
assert result.content[0].text == "Error: focus: No such element"
-def test_type_text_raises_without_session():
- """The two halves must share a lane; require a session up front."""
+def test_type_text_raises_without_session_in_non_daemon_mode():
+ """Non-daemon mode: the two halves must share a lane via group_id,
+ which requires a session. Without one, raise.
+ """
with pytest.raises(ValueError, match="session"):
- backend.type_text("search_input", "text", session=None)
+ backend.type_text(
+ "search_input", "text", use_daemon=False, session=None,
+ )
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_type_text_allows_no_session_in_daemon_mode(mock_call):
+ """Daemon mode shares lane via the persistent connection — session
+ isn't required. Locks in the contract Copilot flagged.
+ """
+ mock_call.return_value = _make_result("✓\n[lane: 1]")
+
+ # No exception. Relative path → two calls (focus + type).
+ backend.type_text("search_input", "hello", use_daemon=True, session=None)
+
+ assert mock_call.call_count == 2
+ assert mock_call.call_args_list[0].args[0] == "focus search_input"
+ assert mock_call.call_args_list[1].args[0] == "type hello"
def test_type_text_raises_with_empty_path():
diff --git a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
index fcaf2f6d1..bab51b208 100644
--- a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
+++ b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
@@ -18,6 +18,7 @@ that single tool.
"""
import asyncio
+import logging
import os
import re
import shlex
@@ -27,6 +28,9 @@ from typing import Any, Optional
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
+
+log = logging.getLogger(__name__)
+
# DOMShell MCP server command
# The harness connects to a running DOMShell server via domshell-proxy (stdio bridge).
# Configure via environment variables:
@@ -85,7 +89,7 @@ def is_available() -> tuple[bool, str]:
Examples:
>>> is_available()
- (True, "DOMShell v2.0.0 is available")
+ (True, "DOMShell vX.Y.Z is available") # whatever npx resolves to
>>> is_available()
(False, "npx not found. Install Node.js from https://nodejs.org/")
"""
@@ -149,6 +153,12 @@ def _q(arg: str) -> str:
# domshell_execute reply. We parse it out and store it on the harness
# Session so subsequent calls can pass group_id=<id> and stay pinned to
# the same Chrome tab-group (i.e. same browser state).
+#
+# The marker format is part of the DOMShell 2.x wire contract — see the
+# upstream docs (https://github.com/apireno/DOMShell, search the README
+# for "Multi-line semantics" / "lane marker"). If DOMShell ever changes
+# the marker format (e.g. to JSON, or to [group: …]), this regex needs
+# to follow.
_LANE_LINE = re.compile(r"\[lane:\s*([^\]\s]+)\s*\]\s*$")
@@ -391,8 +401,13 @@ async def _call_execute(
)
_capture_lane(session, result)
return result
- except Exception:
- # Daemon died, fall back to spawning new server
+ except Exception as e:
+ # Daemon died — log diagnosability and fall back to spawning
+ # a fresh server below. Silent swallow was making the daemon
+ # failure mode invisible in user reports.
+ log.warning(
+ "DOMShell daemon call failed, respawning per-command: %s", e,
+ )
await _stop_daemon()
# Spawn new MCP server process
@@ -747,8 +762,8 @@ def type_text(
) -> dict:
"""Type text into an input element.
- Issued as two separate ``domshell_execute`` calls — ``focus``, check
- for error, then ``type`` only if ``focus`` succeeded. Both share the
+ Issued as separate ``domshell_execute`` calls — ``focus``, check for
+ error, then ``type`` only if ``focus`` succeeded. Both share the
persisted lane id (via ``session``), so the focus state from the
first call carries into the second.
@@ -760,6 +775,16 @@ def type_text(
keys into whatever was previously focused (potentially a password
field). Halting between focus and type prevents that.
+ Performance note: in non-daemon mode with an absolute path this
+ issues up to four ``_call_execute`` calls (anchor ``cd``, focus,
+ type, restore ``cd``), and each opens a fresh stdio MCP session —
+ so up to four ``npx`` server spawns per call. Relative paths use
+ two calls; daemon mode reuses one persistent connection for the
+ whole chain regardless of path form. The per-call overhead is
+ accepted to keep the wrapper-level error semantics simple; a future
+ refactor could share a single ``ClientSession`` across the chain
+ (post-merge follow-up).
+
Args:
path: Path to input element
text: Text to type
@@ -779,15 +804,17 @@ def type_text(
_assert_single_line("path", path)
_assert_single_line("text", text)
- # A session is required: the focus and type halves split across two
- # _call_execute calls, and without `session.domshell_lane_id` the
- # second call would land in a different DOMShell lane and miss the
- # focus state from the first.
- if session is None:
+ # In daemon mode both halves share the persistent `_daemon_session`
+ # naturally — same MCP session means same DOMShell lane, no
+ # group_id juggling needed. Only the non-daemon path requires
+ # `session.domshell_lane_id` because each `_call_execute` opens a
+ # fresh stdio session that would land in its own lane otherwise.
+ if session is None and not use_daemon:
raise ValueError(
- "type_text: a session argument is required so the focus and type "
- "calls share a DOMShell lane. Otherwise the focus state from the "
- "first call would not apply to the second."
+ "type_text: a session argument is required in non-daemon mode "
+ "so the focus and type calls share a DOMShell lane via group_id. "
+ "(Daemon mode shares the persistent connection's lane "
+ "automatically and doesn't need session.)"
)
translated_path, is_absolute = _translate_path(path)