The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-screen): browser_console obeys the screen lease on the supervisor fast path
teknium174fca28fe632
diff --git a/tests/tools/test_bot_desktop_browser_fence.py b/tests/tools/test_bot_desktop_browser_fence.pyindex 743deec2de8..d8875e11afd 100644--- a/tests/tools/test_bot_desktop_browser_fence.py+++ b/tests/tools/test_bot_desktop_browser_fence.py@@ -76,3 +76,36 @@ def test_real_profile_local_browser_is_fenced_by_provenance_even_without_a_live_result = json.loads(browser.browser_click("e1", task_id="review"))assert commands == [], f"human holds the lease, yet a real-profile browser command was dispatched: {commands}"assert result.get("code") == "human_has_control"+++def test_browser_console_supervisor_fast_path_is_fenced_while_human_controls_shared_browser(monkeypatch):+ """`browser_console(expression=...)` answers over the CDP supervisor's WebSocket without ever reaching+ `_run_browser_command`, so the fence must sit in front of that fast path too — otherwise the one+ command that reads arbitrary page state is the one command the human's takeover does not stop."""+ import tools.browser_supervisor as supervisor_mod++ commands: list = []+ browser, _ = _wire(monkeypatch, commands)+ browser._active_sessions["review"] = {"session_name": "review", "cdp_url": "ws://127.0.0.1:9222/devtools/browser/x",+ "features": {"local": True}}+ evaluated: list = []++ class FakeSupervisor:+ def evaluate_runtime(self, expression, **_kw):+ evaluated.append(expression)+ return {"ok": True, "result": "WHAT-THE-HUMAN-TYPED", "result_type": "string"}++ class FakeRegistry:+ def get(self, task_id):+ return FakeSupervisor()++ monkeypatch.setattr(supervisor_mod, "SUPERVISOR_REGISTRY", FakeRegistry())+ try:+ lease.acquire("human-viewer")+ raw = browser.browser_console(expression="document.title", task_id="review")+ finally:+ browser._active_sessions.pop("review", None)+ result = json.loads(raw)+ assert evaluated == [] and commands == [], "human holds the lease, yet the page was evaluated"+ assert "WHAT-THE-HUMAN-TYPED" not in raw+ assert result.get("code") == "human_has_control"diff --git a/tools/browser_tool.py b/tools/browser_tool.pyindex 99af9a0a070..556b432e9ac 100644--- a/tools/browser_tool.py+++ b/tools/browser_tool.py@@ -1067,9 +1067,15 @@ def _browser_eval(expression: str, task_id: Optional[str] = None) -> str:if _is_camofox_mode():return _camofox_eval(expression, task_id)- fast = _eval_supervisor_fast_path(effective_task_id, expression)- if fast is not None:- return fast+ # The supervisor answers over its own WebSocket and never reaches _run_browser_command, so the Bot+ # Desktop lease fence has to bracket it here too — otherwise the one command that reads arbitrary+ # page state is the one a human's takeover does not stop. Same fence, same session identity.+ fenced = _session.run_fenced(_active_sessions.get(effective_task_id) or {},+ lambda: {"fast": _eval_supervisor_fast_path(effective_task_id, expression)})+ if fenced.get("code") == "human_has_control":+ return _dumps(fenced)+ if fenced["fast"] is not None:+ return fenced["fast"]result = _session._run_browser_command(effective_task_id, "eval", [expression])if not result.get("success"):diff --git a/tools/browser_tool_session.py b/tools/browser_tool_session.pyindex e762d191075..c818dfb70b9 100644--- a/tools/browser_tool_session.py+++ b/tools/browser_tool_session.py@@ -11,7 +11,7 @@ import shutilimport subprocessimport uuidfrom pathlib import Path-from typing import Any, Dict, List, Optional+from typing import Any, Callable, Dict, List, Optionalfrom hermes_cli._subprocess_compat import windows_hide_flagsfrom tools.browser_tool_origin import origin as _bt@@ -574,23 +574,32 @@ def _run_browser_command(except Exception as e:_bt.logger.warning("Failed to create browser session for task=%s: %s", task_id, e)return {"success": False, "error": f"Failed to create browser session: {str(e)}"}- # The bot's LOCAL browser lives on its Bot Desktop screen, in the same profile a human who took- # over is typing into. While the human holds the lease every action AND read against it is- # refused (the page may show their credential); the fence brackets the whole run so a takeover- # mid-command also voids the result. Cloud / user-supplied CDP sessions are a different browser.- if _shares_bot_desktop_browser(session_info):- from tools.bot_desktop import lease as _bd_lease- try:- admitted = _bd_lease.assert_agent_may_act()- except _bd_lease.HumanHasControl as e:- return {"success": False, "error": str(e), "code": "human_has_control"}- result = _run_browser_command_unfenced(task_id, command, args, timeout, _engine_override, browser_cmd, session_info)- if _bd_lease.get().epoch != admitted.epoch:- return {"success": False, "code": "human_has_control",- "error": "A human took over the bot's screen while this browser command ran; its result was "- "discarded. Call computer_use action='wait_for_human' to block until they hand back."}- return result- return _run_browser_command_unfenced(task_id, command, args, timeout, _engine_override, browser_cmd, session_info)+ return run_fenced(session_info, lambda: _run_browser_command_unfenced(+ task_id, command, args, timeout, _engine_override, browser_cmd, session_info))+++def run_fenced(session_info: Dict[str, Any], fn: Callable[[], Dict[str, Any]]) -> Dict[str, Any]:+ """Run ``fn`` under the Bot Desktop lease fence when ``session_info`` is the bot's LOCAL browser.++ That browser lives on the Bot Desktop screen, in the same profile a human who took over is typing+ into. While the human holds the lease every action AND read against it is refused (the page may show+ their credential); the fence brackets the whole run so a takeover mid-command also voids the result.+ Cloud / user-supplied CDP sessions are a different browser and run unfenced. This is THE fence: every+ path that reaches the page (agent-browser subprocess, CDP supervisor fast path) goes through here.+ """+ if not _shares_bot_desktop_browser(session_info):+ return fn()+ from tools.bot_desktop import lease as _bd_lease+ try:+ admitted = _bd_lease.assert_agent_may_act()+ except _bd_lease.HumanHasControl as e:+ return {"success": False, "error": str(e), "code": "human_has_control"}+ result = fn()+ if _bd_lease.get().epoch != admitted.epoch:+ return {"success": False, "code": "human_has_control",+ "error": "A human took over the bot's screen while this browser command ran; its result was "+ "discarded. Call computer_use action='wait_for_human' to block until they hand back."}+ return resultdef _shares_bot_desktop_browser(session_info: Dict[str, Any]) -> bool: