Skip to content

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

Python

fix(browser): detect ANSI-red command-prefixed errors (PR #308 round 7.1)

apireno1d52334304dc
diff --git a/browser/agent-harness/HARNESS.md b/browser/agent-harness/HARNESS.md
index 7a2dbf66b..74a231068 100644
--- a/browser/agent-harness/HARNESS.md
+++ b/browser/agent-harness/HARNESS.md
@@ -39,7 +39,7 @@ DOMShell is an npm package that exposes Chrome's Accessibility Tree via MCP:
```bash
# Verify DOMShell is available (2.0.0 or newer required)
-npx @apireno/[email protected] --version
+npx @apireno/domshell --version
# Install Chrome extension
# https://chromewebstore.google.com/detail/domshell
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 349c40a12..bb9fbcbf5 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
@@ -216,9 +216,14 @@ def test_grep_rooted_absolute_restores_to_session_wd(mock_call):
@patch.object(backend, "_call_execute", new_callable=AsyncMock)
def test_ls_anchor_failure_short_circuits(mock_call):
- """ls absolute-path anchor failure → only the anchor call ran."""
+ """ls absolute-path anchor failure → only the anchor call ran.
+
+ Uses DOMShell's actual error shape: ANSI-red-wrapped, command-
+ prefixed ("cd: ...") — not the imaginary "Error: ..." prefix that
+ silently bypassed ``_is_error`` for several rounds.
+ """
mock_call.return_value = _make_result(
- "\x1b[31mError: chrome:// not debuggable\x1b[0m"
+ "\x1b[31mcd: tab 12345 is outside the session group\x1b[0m"
)
sess = _make_session(working_dir="/")
result = backend.ls("/main", session=sess)
@@ -230,7 +235,7 @@ def test_ls_anchor_failure_short_circuits(mock_call):
@patch.object(backend, "_call_execute", new_callable=AsyncMock)
def test_cat_anchor_failure_short_circuits(mock_call):
mock_call.return_value = _make_result(
- "\x1b[31mError: no such path\x1b[0m"
+ "\x1b[31mcd: chrome:// not debuggable\x1b[0m"
)
sess = _make_session(working_dir="/")
result = backend.cat("/main/btn", session=sess)
@@ -245,7 +250,7 @@ def test_click_anchor_failure_short_circuits(mock_call):
unintended action. Anchor must halt before the click runs.
"""
mock_call.return_value = _make_result(
- "\x1b[31mError: chrome:// not debuggable\x1b[0m"
+ "\x1b[31mcd: chrome:// not debuggable\x1b[0m"
)
sess = _make_session(working_dir="/")
result = backend.click("/main/button[0]", session=sess)
@@ -261,7 +266,7 @@ def test_grep_rooted_anchor_failure_short_circuits(mock_call):
never moved off the original cwd.
"""
mock_call.return_value = _make_result(
- "\x1b[31mError: no such path /missing\x1b[0m"
+ "\x1b[31mcd: /missing: No such directory\x1b[0m"
)
sess = _make_session(working_dir="/")
result = backend.grep("Login", path="/missing", prev="/", session=sess)
@@ -306,13 +311,17 @@ def test_parse_execute_result_grep_extracts_matches():
def test_parse_execute_result_error_returns_error_dict():
"""Errors get `error` AND `output` keys — the CLI checks `"error" in
- result` for cd-style guards but also pretty-prints `output`."""
+ result` for cd-style guards but also pretty-prints `output`.
+
+ Uses the actual DOMShell error shape: ANSI-red-wrapped, command-
+ prefixed (``ls: ...``).
+ """
result = _make_result(
- "\x1b[31mError: no such element\x1b[0m\n[lane: 7]"
+ "\x1b[31mls: main: No such directory\x1b[0m\n[lane: 7]"
)
parsed = backend._parse_execute_result(result, "ls")
assert "error" in parsed
- assert parsed["error"].startswith("\x1b[31mError")
+ assert parsed["error"].startswith("\x1b[31mls:")
assert parsed["output"] == parsed["error"]
@@ -363,9 +372,10 @@ def test_cd_returns_dict_with_output(mock_call):
def test_cd_error_returns_error_dict(mock_call):
"""`fs.change_directory` checks `"error" not in result` — make sure
error paths surface the error key so the harness's working-dir
- update is correctly skipped."""
+ update is correctly skipped. Uses the actual DOMShell error shape.
+ """
mock_call.return_value = _make_result(
- "\x1b[31mError: no such path\x1b[0m"
+ "\x1b[31mcd: /missing: No such directory\x1b[0m"
)
result = backend.cd("/missing")
assert "error" in result
@@ -385,30 +395,55 @@ def test_is_error_detects_dict_keys():
assert backend._is_error({"path": "/main"}) is False
-def test_is_error_detects_error_prefixed_text():
- assert backend._is_error(_make_result("Error: no such element")) is True
- assert backend._is_error(_make_result("ERROR: case insensitive")) is True
- assert backend._is_error(_make_result("✓ Focused\n[lane: 1]")) is False
+def test_is_error_detects_ansi_red_wrapper():
+ """Common DOMShell error shape: ANSI red around a command-prefixed
+ message. These do NOT start with the literal "error" — the earlier
+ detection that ANSI-stripped first then ``startswith("error")``
+ failed every one of these, silently regressing the safety chain
+ across every wrapper. Catch on the ``\\x1b[31m`` red marker
+ instead.
+ """
+ assert backend._is_error(
+ _make_result("\x1b[31mcd: foo: No such directory\x1b[0m")
+ ) is True
+ assert backend._is_error(
+ _make_result("\x1b[31mfocus: No such element\x1b[0m")
+ ) is True
+ assert backend._is_error(
+ _make_result("\x1b[31mls: main: No such directory\x1b[0m")
+ ) is True
+ assert backend._is_error(
+ _make_result("\x1b[31mcd: tab 12345 is outside the session group\x1b[0m")
+ ) is True
-def test_is_error_strips_ansi_red_wrapper():
- """DOMShell colours errors red — strip ANSI before prefix-matching.
+def test_is_error_detects_explicit_error_prefix_without_ansi():
+ """Fallback: rarer non-coloured errors or pre-stripped input still
+ detected via ``"error:"`` prefix.
+ """
+ assert backend._is_error(_make_result("Error: bad command")) is True
+ assert backend._is_error(_make_result("ERROR: case insensitive")) is True
+
- Without this, a coloured error like `\\x1b[31mError: ...\\x1b[0m`
- would be misread as success (its first chars are `\\x1b[31m`, not
- `error`), letting downstream safety chains proceed.
+def test_is_error_does_not_flag_success_in_other_colors():
+ """Anti-false-positive: only ``\\x1b[31m`` (red) signals an error.
+ Success commonly wraps in ``\\x1b[32m`` (green) — must not flag.
"""
assert backend._is_error(
- _make_result("\x1b[31mError: focus: No such element\x1b[0m")
- ) is True
- # Plain `Error:` still detected.
- assert backend._is_error(_make_result("Error: oops")) is True
- # Mixed ANSI prefix without "Error" stays False.
+ _make_result("\x1b[32m✓ ok\x1b[0m")
+ ) is False
+ assert backend._is_error(
+ _make_result("✓ command completed")
+ ) is False
assert backend._is_error(
- _make_result("\x1b[32m✓ All good\x1b[0m")
+ _make_result("✓ Focused\n[lane: 1]")
) is False
+def test_is_error_handles_empty():
+ assert backend._is_error(_make_result("")) is False
+
+
def test_is_error_handles_missing_content():
assert backend._is_error(SimpleNamespace()) is False
@@ -557,10 +592,10 @@ def test_type_text_absolute_path_focus_failure_skips_type_and_restores(mock_call
"""
sess = _make_session(working_dir="/main")
mock_call.side_effect = [
- _make_result("✓ Entered tab 123\n[lane: 1]"), # cd anchor ok
- _make_result("\x1b[31mError: focus: No such element\x1b[0m"), # focus fails
- _make_result("✓ Typed\n[lane: 1]"), # MUST NOT be reached
- _make_result("✓\n[lane: 1]"), # restore
+ _make_result("✓ Entered tab 123\n[lane: 1]"), # cd anchor ok
+ _make_result("\x1b[31mfocus: No such element\x1b[0m"), # focus fails
+ _make_result("✓ Typed\n[lane: 1]"), # MUST NOT be reached
+ _make_result("✓\n[lane: 1]"), # restore
]
backend.type_text("/main/missing", "secret_password", session=sess)
@@ -584,7 +619,7 @@ def test_type_text_anchor_failure_does_not_attempt_focus(mock_call):
"""
sess = _make_session(working_dir="/")
mock_call.return_value = _make_result(
- "\x1b[31mError: chrome:// not debuggable\x1b[0m"
+ "\x1b[31mcd: chrome:// not debuggable\x1b[0m"
)
backend.type_text("/some/path", "text", session=sess)
@@ -603,7 +638,7 @@ def test_type_text_relative_path_focus_failure_skips_type(mock_call):
"""
sess = _make_session(working_dir="/")
mock_call.side_effect = [
- _make_result("Error: focus: No such element"),
+ _make_result("\x1b[31mfocus: No such element\x1b[0m"), # focus fails
_make_result("✓ Typed"), # should NOT be reached
]
@@ -614,8 +649,8 @@ def test_type_text_relative_path_focus_failure_skips_type(mock_call):
assert mock_call.call_args_list[0].args[0] == "focus stale_input"
# The focus result is parsed (CLI consumes dicts, not CallToolResult).
assert result == {
- "error": "Error: focus: No such element",
- "output": "Error: focus: No such element",
+ "error": "\x1b[31mfocus: No such element\x1b[0m",
+ "output": "\x1b[31mfocus: No such element\x1b[0m",
}
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 8cd3ab067..cb493b1bc 100644
--- a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
+++ b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py
@@ -297,32 +297,55 @@ _ANSI_CSI_RE = re.compile(r"\x1b\[[0-9;]*[A-Za-z]")
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, then ``isError`` /
- ``error`` keys on dict-shaped test fixtures, and finally scans the
- concatenated text content for a leading "error" — with ANSI escape
- sequences stripped first, since DOMShell colours error lines red
- (``\\x1b[31mError: ...\\x1b[0m``). Robust to the raw
- ``CallToolResult`` and to ``SimpleNamespace(content=[...])`` test
- fixtures.
+ """Detect whether a ``domshell_execute`` result represents an error.
+
+ DOMShell's actual error shape (verified upstream) is ANSI-red-wrapped
+ AND command-prefixed, e.g.:
+
+ * ``\\x1b[31mcd: foo: No such directory\\x1b[0m``
+ * ``\\x1b[31mfocus: No such element\\x1b[0m``
+ * ``\\x1b[31mls: main: No such directory\\x1b[0m``
+
+ Note none of these start with the literal "error" — the earlier
+ detection that ANSI-stripped first then checked ``startswith("error")``
+ silently failed every command-prefixed case, which silently regressed
+ the safety chain across ``ls`` / ``cat`` / ``click`` / ``grep`` /
+ ``type_text`` (Codex PR #308 round 7+). The fix: detect the
+ ``\\x1b[31m`` red color marker BEFORE ANSI stripping — DOMShell wraps
+ every error in red and uses different codes (e.g. ``\\x1b[32m``
+ green) for success.
+
+ Inspects ``isError`` and dict ``error``/``isError`` keys first
+ (covers the MCP SDK's explicit error flag and dict test fixtures),
+ then the ANSI-red marker, with a final fallback to a stripped
+ ``Error:`` prefix for any input that has been pre-stripped (e.g. by
+ a transport layer) or that arrives without DOMShell's coloring.
"""
if hasattr(result, "isError") and result.isError:
return True
if isinstance(result, dict):
- if result.get("isError"):
+ if result.get("isError") or result.get("error"):
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
- text = _ANSI_CSI_RE.sub("", text)
- return text.strip().lower().startswith("error")
+
+ text = _extract_text(result)
+ if not text:
+ return False
+
+ # DOMShell wraps errors in ANSI red. Detect BEFORE stripping; the
+ # color code IS the signal, and the message that follows is
+ # typically command-prefixed (cd: …, focus: …, ls: …) rather than
+ # "Error:"-prefixed.
+ if "\x1b[31m" in text:
+ return True
+
+ # Fallback: ANSI-stripped text with explicit "Error:" prefix.
+ # Catches rarer non-colored errors and any input that arrives
+ # pre-stripped.
+ cleaned = _ANSI_CSI_RE.sub("", text).strip().lower()
+ if cleaned.startswith("error:"):
+ return True
+
+ return False
def _extract_text(result: Any) -> str:
@@ -558,11 +581,22 @@ def ls(path: str = "/", use_daemon: bool = False, *, session: Any = None) -> dic
session: Harness session whose DOMShell lane id is reused / updated
Returns:
- Dict with 'entries' key containing list of accessible elements
+ Dict shaped ``{"entries": [{"name": str, "role": "", "path": str}, ...],
+ "raw": str}`` (or ``{"error": str, "output": str}`` on failure —
+ wrappers reuse ``_parse_execute_result``'s contract).
+
+ ``name`` and ``path`` carry the raw line text from DOMShell's
+ ``ls`` output; directory entries currently include a trailing
+ ``/`` (cosmetic, see follow-up). ``role`` is always empty in
+ this round — structured role extraction will land alongside a
+ finer column parser in the follow-up.
Example:
- >>> ls("/")
- {"path": "/", "entries": [{"name": "main", "role": "landmark", ...}]}
+ >>> ls("/main")
+ {"entries": [{"name": "heading_1", "role": "", "path": "heading_1"},
+ {"name": "div/", "role": "", "path": "div/"},
+ ...],
+ "raw": "heading_1\\ndiv/\\n..."}
"""
translated, is_absolute = _translate_path(path)
if is_absolute:
@@ -601,11 +635,21 @@ def cd(path: str, use_daemon: bool = False, *, session: Any = None) -> dict:
session: Harness session whose DOMShell lane id is reused / updated
Returns:
- Dict with 'path' key confirming current location
+ Dict shaped ``{"output": str}`` on success, or
+ ``{"error": str, "output": str}`` on failure.
+
+ Note no ``"path"`` key — ``fs.change_directory`` falls back to
+ the input ``path`` arg via ``result.get("path", path)``, so the
+ harness's ``working_dir`` ends up at the requested target. The
+ ``output`` field carries DOMShell's raw confirmation text for
+ display.
Example:
>>> cd("/main/div[0]")
- {"path": "/main/div[0]", "element": {...}}
+ {"output": "✓ Entered /main/div[0]"}
+ >>> cd("/missing")
+ {"error": "cd: /missing: No such directory",
+ "output": "cd: /missing: No such directory"}
"""
translated, is_absolute = _translate_path(path)
# cd is the one wrapper where the operation IS the new state — no
@@ -718,7 +762,13 @@ def grep(
))
return _parse_execute_result(op, "grep")
- _assert_single_line("path", path)
+ # `path` was already newline-guarded by `_translate_path` above
+ # (round 6's translation-boundary check) so the field-named
+ # `_assert_single_line("path", path)` we used to call here would be
+ # dead. `prev`, however, is only translated in the relative branch
+ # below — the absolute branch doesn't touch it. Keep the explicit
+ # field-named guard for `prev` so a newlined-prev kwarg raises a
+ # useful error regardless of which branch consumes it.
_assert_single_line("prev", prev)
# Split-and-check. Anchor success is load-bearing: if the cd
@@ -801,11 +851,22 @@ def open_url(url: str, use_daemon: bool = False, *, session: Any = None) -> dict
session: Harness session whose DOMShell lane id is reused / updated
Returns:
- Dict with navigation result
+ Dict shaped ``{"output": 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).
Example:
>>> open_url("https://example.com")
- {"url": "https://example.com", "status": "loaded"}
+ {"output": "✓ Opened https://example.com\\n[lane: 1]"}
"""
result = asyncio.run(
_call_execute(f"open {_q(url)}", use_daemon, session=session)
@@ -835,7 +896,15 @@ def back(use_daemon: bool = False, *, session: Any = None) -> dict:
session: Harness session whose DOMShell lane id is reused / updated
Returns:
- Dict with navigation result
+ 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.)
"""
result = asyncio.run(_call_execute("back", use_daemon, session=session))
return _parse_execute_result(result, "back")
@@ -849,7 +918,15 @@ def forward(use_daemon: bool = False, *, session: Any = None) -> dict:
session: Harness session whose DOMShell lane id is reused / updated
Returns:
- Dict with navigation result
+ 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.)
"""
result = asyncio.run(_call_execute("forward", use_daemon, session=session))
return _parse_execute_result(result, "forward")