Skip to content

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

Python

fix(browser): PR #308 cleanup round — imports, ls dead branch, _q echo, * placement

apireno441918581311
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 0b00b253c..fcc8f095a 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
@@ -6,9 +6,12 @@ regressions (quoting, command names, multi-line layout, restore ordering)
fail loudly.
"""
-import pytest
+from types import SimpleNamespace
from unittest.mock import AsyncMock, call, patch
+import pytest
+
+from cli_anything.browser.core.session import Session
from cli_anything.browser.utils import domshell_backend as backend
@@ -232,11 +235,6 @@ def test_cd_rejects_newlines(mock_call):
# on every subsequent call.
-from types import SimpleNamespace
-
-from cli_anything.browser.core.session import Session
-
-
def _make_result(text: str):
"""Build a fake CallToolResult whose ``content[0].text`` is ``text``."""
return SimpleNamespace(content=[SimpleNamespace(text=text)])
@@ -391,10 +389,15 @@ def test_distinct_sessions_have_isolated_lanes():
# ── Keyword-only enforcement on session= ─────────────────────────────
+#
+# Only ``session`` is keyword-only; ``use_daemon`` stays positional so
+# pre-2.0.0 callers like ``ls("/", True)`` keep working. ``grep`` is a
+# deliberate exception (fully keyword-only after the round-1 review).
def test_session_is_keyword_only_on_click():
- """Discipline matches grep's earlier keyword-only fix."""
+ """Trailing positional `None` is interpreted as a 3rd positional arg
+ that the wrapper doesn't accept — must raise TypeError."""
with pytest.raises(TypeError):
backend.click("submit_btn", False, None) # type: ignore[misc]
@@ -412,3 +415,40 @@ def test_session_is_keyword_only_on_type_text():
def test_session_is_keyword_only_on_open_url():
with pytest.raises(TypeError):
backend.open_url("https://example.com", False, None) # type: ignore[misc]
+
+
+# ── Positional `use_daemon` stays valid ──────────────────────────────
+#
+# These guard against accidentally pulling ``use_daemon`` behind the ``*``
+# in the future. Pre-2.0.0 calls written as ``ls("/", True)`` must not
+# regress to ``TypeError``.
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_use_daemon_positional_on_ls(mock_call):
+ mock_call.return_value = {}
+ backend.ls("/", True) # use_daemon=True positionally
+ assert mock_call.call_args == call("ls /", True, session=None)
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_use_daemon_positional_on_click(mock_call):
+ mock_call.return_value = {}
+ backend.click("submit_btn", True)
+ assert mock_call.call_args == call("click submit_btn", True, session=None)
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_use_daemon_positional_on_reload(mock_call):
+ mock_call.return_value = {}
+ backend.reload(True)
+ assert mock_call.call_args == call("refresh", True, session=None)
+
+
[email protected](backend, "_call_execute", new_callable=AsyncMock)
+def test_use_daemon_positional_on_type_text(mock_call):
+ mock_call.return_value = {}
+ backend.type_text("input", "hello", True)
+ assert mock_call.call_args == call(
+ "focus input\ntype hello", True, session=None
+ )
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 c2055245a..9ef830d53 100644
--- a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
+++ b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
@@ -132,11 +132,15 @@ def _q(arg: str) -> str:
the generic one raised here.
"""
if "\n" in arg or "\r" in arg:
+ # Bound the echoed value so an arbitrarily large untrusted payload
+ # (e.g. a multi-line paste from the page) doesn't end up verbatim
+ # in error messages or downstream logs / telemetry.
+ preview = arg[:80] + ("…" if len(arg) > 80 else "")
raise ValueError(
"Newline characters are not allowed in command arguments — "
"DOMShell's domshell_execute treats them as command separators, "
"so a newline inside any wrapper input would inject additional "
- f"commands. Got: {arg!r}"
+ f"commands. Got ({len(arg)} chars): {preview!r}"
)
return shlex.quote(arg)
@@ -339,7 +343,7 @@ def daemon_started() -> bool:
# `domshell_execute`. The public Python API is unchanged from the
# pre-2.0.0 per-tool wrappers.
-def ls(path: str = "/", *, use_daemon: bool = False, session: Any = None) -> dict:
+def ls(path: str = "/", use_daemon: bool = False, *, session: Any = None) -> dict:
"""List directory contents in the accessibility tree.
Args:
@@ -354,11 +358,10 @@ def ls(path: str = "/", *, use_daemon: bool = False, session: Any = None) -> dic
>>> ls("/")
{"path": "/", "entries": [{"name": "main", "role": "landmark", ...}]}
"""
- command = f"ls {_q(path)}" if path else "ls"
- return asyncio.run(_call_execute(command, use_daemon, session=session))
+ return asyncio.run(_call_execute(f"ls {_q(path)}", use_daemon, session=session))
-def cd(path: str, *, use_daemon: bool = False, session: Any = None) -> dict:
+def cd(path: str, use_daemon: bool = False, *, session: Any = None) -> dict:
"""Change directory in the accessibility tree.
Args:
@@ -376,7 +379,7 @@ def cd(path: str, *, use_daemon: bool = False, session: Any = None) -> dict:
return asyncio.run(_call_execute(f"cd {_q(path)}", use_daemon, session=session))
-def cat(path: str, *, use_daemon: bool = False, session: Any = None) -> dict:
+def cat(path: str, use_daemon: bool = False, *, session: Any = None) -> dict:
"""Read element content from the accessibility tree.
Args:
@@ -447,7 +450,7 @@ def grep(
)
-def click(path: str, *, use_daemon: bool = False, session: Any = None) -> dict:
+def click(path: str, use_daemon: bool = False, *, session: Any = None) -> dict:
"""Click an element in the accessibility tree.
Args:
@@ -467,7 +470,7 @@ def click(path: str, *, use_daemon: bool = False, session: Any = None) -> dict:
)
-def open_url(url: str, *, use_daemon: bool = False, session: Any = None) -> dict:
+def open_url(url: str, use_daemon: bool = False, *, session: Any = None) -> dict:
"""Navigate to a URL in Chrome.
Args:
@@ -487,7 +490,7 @@ def open_url(url: str, *, use_daemon: bool = False, session: Any = None) -> dict
)
-def reload(*, use_daemon: bool = False, session: Any = None) -> dict:
+def reload(use_daemon: bool = False, *, session: Any = None) -> dict:
"""Reload the current page.
Args:
@@ -500,7 +503,7 @@ def reload(*, use_daemon: bool = False, session: Any = None) -> dict:
return asyncio.run(_call_execute("refresh", use_daemon, session=session))
-def back(*, use_daemon: bool = False, session: Any = None) -> dict:
+def back(use_daemon: bool = False, *, session: Any = None) -> dict:
"""Navigate back in history.
Args:
@@ -513,7 +516,7 @@ def back(*, use_daemon: bool = False, session: Any = None) -> dict:
return asyncio.run(_call_execute("back", use_daemon, session=session))
-def forward(*, use_daemon: bool = False, session: Any = None) -> dict:
+def forward(use_daemon: bool = False, *, session: Any = None) -> dict:
"""Navigate forward in history.
Args:
@@ -529,8 +532,8 @@ def forward(*, use_daemon: bool = False, session: Any = None) -> dict:
def type_text(
path: str,
text: str,
- *,
use_daemon: bool = False,
+ *,
session: Any = None,
) -> dict:
"""Type text into an input element.