"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/
Python
fix(browser): bare cat for root/empty path in fs cat (#398)
Rāna(Bass Ver.)d60c558ed11e
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 2b75fe009..8b99dba53 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@@ -147,31 +147,54 @@ def test_cat_relative_no_wrap(mock_call):@patch.object(backend, "_call_execute", new_callable=AsyncMock)-def test_cat_root_path_does_not_raise_and_returns_parseable_result(mock_call):- """backend.cat("/") and backend.cat("") at the absolute root should- NOT raise a Python ValueError. Pre-migration behavior was to send- ``cat ''`` to DOMShell and surface its ``Usage: cat <name>`` error- string; the round-5 Python-side guard converted the kernel error- into a ValueError, which broke ``fs.read_element(session, "")``- callers landed at ``/``. @yuh-yang R3 blocker 1 required restoring- pre-migration behavior — guard removed, both inputs now reach- DOMShell, which has the same ``if (!targetName)`` check and- returns its standard Usage error.+def test_cat_root_path_reads_current_node_at_anchor(mock_call):+ """backend.cat("/") anchors at tab root, then issues bare `read`.++ Empty-after-translation absolute paths are the natural no-arg form+ (``fs cat`` with ``session.working_dir == "/"``). DOMShell requires+ a target for ``cat``; ``read`` is its supported current-node operation."""- mock_call.return_value = _make_result(- "\x1b[31mUsage: cat <name> (see cat --help)\x1b[0m\n[lane: shared]"- )- sess = _make_session(working_dir="/") # satisfies absolute branch's session check+ mock_call.return_value = _make_result("root [document]\n button [button]\n[lane: 1]")+ sess = _make_session(working_dir="/")++ result = backend.cat("/", session=sess)++ assert isinstance(result, dict)+ assert "error" not in result+ assert mock_call.call_count == 3+ assert mock_call.call_args_list[0].args[0] == "cd %here%"+ assert mock_call.call_args_list[1].args[0] == "read"+ assert mock_call.call_args_list[2].args[0] == "cd %here%"++[email protected](backend, "_call_execute", new_callable=AsyncMock)+def test_cat_empty_relative_path_reads_current_node(mock_call):+ """backend.cat("") uses DOMShell read on the lane's current cursor."""+ mock_call.return_value = _make_result("button [button]\n[lane: 1]")- # MUST NOT raise — that was the regression.- result_root = backend.cat("/", session=sess)- result_empty = backend.cat("")+ result = backend.cat("")- # Both surface DOMShell's error as a parsed dict, not a Python exception.- for r in (result_root, result_empty):- assert isinstance(r, dict)- # _parse_execute_result detects ANSI red → routes through error path.- assert "error" in r+ assert isinstance(result, dict)+ assert "error" not in result+ assert mock_call.call_count == 1+ assert mock_call.call_args.args[0] == "read"++[email protected](backend, "_call_execute", new_callable=AsyncMock)+def test_read_element_empty_path_at_root_does_not_raise(mock_call):+ """fs.read_element(session, "") at default working_dir="/" must not raise."""+ from cli_anything.browser.core import fs as fs_mod++ mock_call.return_value = _make_result("root [document]\n content [text]\n[lane: 1]")+ sess = _make_session(working_dir="/")++ result = fs_mod.read_element(sess, "")++ assert isinstance(result, dict)+ assert "error" not in result+ # Falls through to working_dir="/", so absolute split-and-check path.+ assert mock_call.call_count == 3+ assert mock_call.call_args_list[1].args[0] == "read"@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 d344a2b3f..c813e6573 100644--- a/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py+++ b/browser/agent-harness/cli_anything/browser/utils/domshell_backend.py@@ -785,41 +785,35 @@ def cat(path: str, use_daemon: bool = False, *, session: Any = None) -> dict:Example:>>> cat("/main/button[0]", session=session) # required for absolute paths- {"output": "button: Submit\\n..."}+ {"output": "button: Submit\n..."}"""translated, is_absolute = _translate_path(path)- # Note: a falsy `translated` (root path `/` or empty string) is no- # longer rejected here. Pre-migration `fs cat` at the root surfaced- # DOMShell's own `Usage: cat <name>` error string; the round-5 guard- # converted that into a Python ValueError, which broke the- # `fs.read_element(session, "")` fall-through to `session.working_dir`- # for callers landed at `/`. Removed per @yuh-yang R3 review of- # `b684732` — `cat ''` reaches DOMShell, which has the same- # `if (!targetName)` check and returns its standard Usage error.+ # DOMShell's `cat` requires a child name; its no-argument `read`+ # command is the supported way to read the current cursor.+ command = "read" if not translated else f"cat {_q(translated)}"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- # against the wrong cwd.+ # otherwise read the current cursor or named child, then restore.+ # Anchor success is+ # load-bearing — without it cat resolves against the wrong cwd.anchor = asyncio.run(_call_execute(_anchor_path_cmd(""), use_daemon, session=session,))if _is_error(anchor):return _parse_execute_result(anchor, "cat")op = asyncio.run(_call_execute(- f"cat {_q(translated)}", use_daemon, session=session,+ command, use_daemon, session=session,))asyncio.run(_call_execute(_restore_cwd_cmd(session), use_daemon, session=session,))return _parse_execute_result(op, "cat")op = asyncio.run(_call_execute(- f"cat {_q(translated)}", use_daemon, session=session,+ command, use_daemon, session=session,))return _parse_execute_result(op, "cat")-def grep(pattern: str,*,