The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-screen): only a real headed browser command auto-starts the screen; the env builder stays pure
teknium1ada05381999d
diff --git a/tests/tools/test_bot_desktop_browser.py b/tests/tools/test_bot_desktop_browser.pyindex 7cd119fc6d1..d7dc4d92f9b 100644--- a/tests/tools/test_bot_desktop_browser.py+++ b/tests/tools/test_bot_desktop_browser.py@@ -241,19 +241,31 @@ def test_headless_shell_override_is_not_a_headed_browser(tmp_path, monkeypatch):assert browser.executable() == sys_exe # a real headed browser elsewhere still wins over the override[email protected]("headed", [True, False])-def test_headed_browser_env_asks_the_screen_to_start_like_computer_use_does(monkeypatch, headed):- """Regression for #110050: `browser.headed: true` + `bot_desktop.auto_start: true` on a fresh headless- profile gave the headed child no DISPLAY, because only computer_use called ensure_started_for_tool() and- the browser env builder merely READ the published env. First browser use is a first use too — but only- when a window is asked for; a headless browser must never bring a screen up."""[email protected]("engine, headed, starts", [("chrome", True, 1), ("chrome", False, 0), ("lightpanda", True, 0)])+def test_headed_chromium_spawn_asks_the_screen_to_start_but_the_env_builder_never_does(tmp_path, monkeypatch, engine, headed, starts):+ """Regression for #110050 at the right boundary: a real browser command that forks a headed Chromium daemon+ starts the profile's screen (bot_desktop.auto_start) like computer_use dispatch does; `_build_browser_env()`+ itself stays pure — it also serves the npx cache warmer, the Chromium auto-installer and the Lightpanda+ engine, none of which may block on Xvnc+Xfce coming up."""from tools import browser_tool as btfrom tools import browser_tool_cloud as cloud+ from tools import browser_tool_session as sessioncalls: list = []monkeypatch.setattr(runtime, "ensure_started_for_tool", lambda: calls.append(1))monkeypatch.setattr(runtime, "published_env", lambda: {})monkeypatch.setattr(cloud, "_is_headed_mode", lambda: headed)- env = bt._build_browser_env()- assert isinstance(env, dict)- assert len(calls) == (1 if headed else 0)+ bt._build_browser_env()+ assert calls == []++ class _Done:+ returncode = 0+ def wait(self, timeout=None): return 0+ def _fake_popen(argv, env, socket_dir, tag):+ for slot in ("stdout", "stderr"):+ Path(socket_dir, f"_{slot}_{tag}").write_text("{}" if slot == "stdout" else "")+ return _Done()+ monkeypatch.setattr(session, "_popen_agent_browser", _fake_popen)+ monkeypatch.setattr(session, "_prepare_session_socket_dir", lambda name: str(tmp_path))+ session._spawn_and_collect("t", {"session_name": "h_x"}, ["agent-browser"], "open", engine, 5)+ assert len(calls) == startsdiff --git a/tools/bot_desktop/runtime.py b/tools/bot_desktop/runtime.pyindex 07e89c90661..c747aca7d3a 100644--- a/tools/bot_desktop/runtime.py+++ b/tools/bot_desktop/runtime.py@@ -327,10 +327,10 @@ def desktop_env(base_env: Optional[Dict[str, str]] = None) -> Dict[str, str]:def ensure_started_for_tool() -> None:- """Tool-boundary hook (``computer_use`` dispatch): with ``bot_desktop.auto_start`` (opt-in, default off) a Linux- host that has NO display and the packages installed gets its screen started on first use, so a headless- gateway works the first time instead of answering "no DISPLAY is set". Failure is not an error here;- the tool's own "no display" diagnosis is the right message then."""+ """Tool-boundary hook (``computer_use`` dispatch and the headed Chromium spawn sites of the browser tool): with+ ``bot_desktop.auto_start`` (opt-in, default off) a Linux host that has NO display and the packages installed gets+ its screen started on first use, so a headless gateway works the first time instead of answering "no DISPLAY is+ set". Failure is not an error here; the tool's own "no display" diagnosis is the right message then."""if published_env():touch_activity()returndiff --git a/tools/browser_tool.py b/tools/browser_tool.pyindex e4dc4e3cd70..9740722cca1 100644--- a/tools/browser_tool.py+++ b/tools/browser_tool.py@@ -57,9 +57,9 @@ def _build_browser_env() -> dict:env[key] = value# The Browser Use harness dials the resolved local CDP URL over ``websockets``; without a# loopback NO_PROXY a macOS system proxy captures that dial (#110565).- # Headed Chromium opens on this profile's Bot Desktop when one is running (human can take it over).- from tools.bot_desktop.runtime import desktop_env as _bot_desktop_env- # Headed Chromium opens on this profile's Bot Desktop when one is running (human can take it over).+ # Headed Chromium opens on this profile's Bot Desktop when one is running (human can take it over). Pure: this+ # builder also serves the npx cache warmer, the Chromium auto-installer and the Lightpanda engine, none of which+ # may bring a screen up — the auto-start hook lives at the headed Chromium spawn sites (browser_tool_session).from tools.bot_desktop.runtime import desktop_env as _bot_desktop_envreturn add_loopback_no_proxy(_bot_desktop_env(env))diff --git a/tools/browser_tool_lightpanda_fallback.py b/tools/browser_tool_lightpanda_fallback.pyindex f1f86aa855d..9a400b787d1 100644--- a/tools/browser_tool_lightpanda_fallback.py+++ b/tools/browser_tool_lightpanda_fallback.py@@ -149,7 +149,8 @@ def _run_chrome_fallback_command(task_id: str, command: str, args: List[str], tibase_args = _session._agent_browser_argv(browser_cmd) + ["--engine", "chrome", "--session", tmp_session, "--json"]task_socket_dir = _session._prepare_session_socket_dir(tmp_session)- # Bypasses _run_browser_command, so apply the same Chromium sandbox policy explicitly.+ # Bypasses _run_browser_command, so apply the same Chromium sandbox/screen policy explicitly.+ _session._ensure_screen_for_headed_chromium()browser_env = _session._agent_browser_command_env(task_socket_dir)_session._apply_chromium_sandbox_args(browser_env)diff --git a/tools/browser_tool_real_profile.py b/tools/browser_tool_real_profile.pyindex c983c91641a..2b1dba617fa 100644--- a/tools/browser_tool_real_profile.py+++ b/tools/browser_tool_real_profile.py@@ -165,6 +165,7 @@ def _launch_real_profile_chrome(real_binary: str, copy_dir: str) -> Tuple[Optionexcept OSError:passchrome_argv = [real_binary, f"--user-data-dir={copy_dir}", *_REAL_PROFILE_CHROME_FLAGS]+ _session._ensure_screen_for_headed_chromium()browser_env = _bt._build_browser_env() # carries the Bot Desktop DISPLAY when one is running_has_display = bool(browser_env.get("DISPLAY") or browser_env.get("WAYLAND_DISPLAY"))if not (_cloud._is_headed_mode() and (_has_display or not sys.platform.startswith("linux"))):diff --git a/tools/browser_tool_session.py b/tools/browser_tool_session.pyindex 6f4af458041..c62afb228a7 100644--- a/tools/browser_tool_session.py+++ b/tools/browser_tool_session.py@@ -174,6 +174,15 @@ def _agent_browser_command_env(socket_dir: str) -> Dict[str, str]:return env+def _ensure_screen_for_headed_chromium() -> None:+ """Tool-call boundary for ``bot_desktop.auto_start`` (mirrors computer_use dispatch): a headed Chromium is+ about to be spawned for a real browser action, so a fresh headless profile gets its screen first. Headless+ browsing, Lightpanda and the env-only callers of ``_build_browser_env`` never bring one up."""+ if _cloud._is_headed_mode():+ from tools.bot_desktop.runtime import ensure_started_for_tool+ ensure_started_for_tool()++def _popen_agent_browser(argv: List[str], env: Dict[str, str], socket_dir: str, tag: str,stdin_payload: Optional[bytes] = None) -> "subprocess.Popen":"""Spawn agent-browser with stdout/stderr redirected to ``socket_dir/_std{out,err}_<tag>``;@@ -565,6 +574,8 @@ def _spawn_and_collect(task_socket_dir = _prepare_session_socket_dir(session_info["session_name"])_bt.logger.debug("browser cmd=%s task=%s socket_dir=%s (%d chars)",command, task_id, task_socket_dir, len(task_socket_dir))+ if engine != "lightpanda":+ _ensure_screen_for_headed_chromium() # first command forks the daemon; a headed window needs the screen upbrowser_env = _agent_browser_command_env(task_socket_dir)# Lightpanda rejects Chromium-only launch flags: strip current and legacy vars;