Skip to content

The agent that grows with you

fix(bot-screen): a human holding the lease keeps the shared browser alive

teknium1b2629519e794
diff --git a/tests/tools/test_bot_desktop_browser.py b/tests/tools/test_bot_desktop_browser.py
index 5d6b9fee7ef..0d2ce30188e 100644
--- a/tests/tools/test_bot_desktop_browser.py
+++ b/tests/tools/test_bot_desktop_browser.py
@@ -269,3 +269,49 @@ def test_headed_chromium_spawn_asks_the_screen_to_start_but_the_env_builder_neve
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) == starts
+
+
+def test_janitor_keeps_the_shared_browser_alive_while_a_human_holds_the_lease(monkeypatch):
+ """#110064: the agent goes idle BECAUSE the human took over to log in; the janitor must count the human's
+ lease as activity for the shared local browser, and reap it again once the lease is handed back."""
+ from tools import browser_tool_lifecycle as lifecycle, browser_tool_session as session
+ from tools.bot_desktop import lease
+
+ monkeypatch.setattr(runtime, "published_env", lambda: {"DISPLAY": ":37"})
+ holder = {"human": True}
+ monkeypatch.setattr(lease, "human_holds", lambda *a, **k: holder["human"])
+ reaped: list = []
+ monkeypatch.setattr(lifecycle, "cleanup_browser", lambda task_id: reaped.append(task_id))
+ monkeypatch.setattr(lifecycle._bt, "BROWSER_SESSION_INACTIVITY_TIMEOUT", 1)
+ monkeypatch.setattr(lifecycle._bt, "_active_sessions", {
+ "bot": {"session_name": "h_bot", "features": {"local": True}},
+ "cloud": {"session_name": "c_1", "bb_session_id": "bb", "features": {}},
+ })
+ monkeypatch.setattr(lifecycle._bt, "_session_last_activity", {"bot": 0.0, "cloud": 0.0})
+ monkeypatch.setattr(lifecycle._bt, "_session_owner_homes", {})
+
+ lifecycle._cleanup_inactive_browser_sessions()
+ assert reaped == ["cloud"], "only the browser the human is not typing into may be reaped"
+ assert lifecycle._bt._session_last_activity["bot"] > 0, "the human's lease refreshed the bot browser's activity"
+
+ holder["human"] = False
+ lifecycle._bt._session_last_activity["bot"] = 0.0
+ lifecycle._cleanup_inactive_browser_sessions()
+ assert reaped == ["cloud", "bot"]
+ assert session.human_holds_shared_browser({"features": {"local": True}}) is False
+
+
+def test_daemon_idle_timer_defers_to_the_janitor_only_for_the_shared_headed_browser(monkeypatch):
+ """The agent-browser daemon cannot see the lease, so on the Bot Desktop its self-termination timer steps
+ back and the lease-aware janitor owns the browser's lifetime; headless browsing keeps the mirror."""
+ from tools import browser_tool_session as session
+
+ monkeypatch.setattr(session._bt, "BROWSER_SESSION_INACTIVITY_TIMEOUT", 120)
+ monkeypatch.setattr(runtime, "published_env", lambda: {"DISPLAY": ":37"})
+ monkeypatch.setattr(session._cloud, "_is_headed_mode", lambda: True)
+ assert session._daemon_idle_timeout_seconds() > 120
+ monkeypatch.setattr(session._cloud, "_is_headed_mode", lambda: False)
+ assert session._daemon_idle_timeout_seconds() == 120
+ monkeypatch.setattr(session._cloud, "_is_headed_mode", lambda: True)
+ monkeypatch.setattr(runtime, "published_env", lambda: {})
+ assert session._daemon_idle_timeout_seconds() == 120
diff --git a/tools/browser_tool_lifecycle.py b/tools/browser_tool_lifecycle.py
index b8b2793bed4..e3fbebdc2c0 100644
--- a/tools/browser_tool_lifecycle.py
+++ b/tools/browser_tool_lifecycle.py
@@ -160,6 +160,11 @@ def _cleanup_inactive_browser_sessions():
if current_time - last_time > _bt.BROWSER_SESSION_INACTIVITY_TIMEOUT]
for task_id in sessions_to_cleanup:
+ with _session_owner_scope(task_id):
+ if _human_holds_shared_browser(task_id):
+ # A human took the bot's screen (login, 2FA) — the agent is idle BECAUSE they are working.
+ _update_session_activity(task_id)
+ continue
elapsed = int(current_time - _bt._session_last_activity.get(task_id, current_time))
_bt.logger.info("Cleaning up inactive session for task: %s (inactive for %ss)", task_id, elapsed)
try:
@@ -184,6 +189,15 @@ def _cleanup_inactive_browser_sessions():
_forget_session_tracking(task_id, activity=False)
+def _human_holds_shared_browser(task_id: str) -> bool:
+ """Lease check for the janitor, under the owner's profile scope (the lease is per profile)."""
+ with _bt._cleanup_lock:
+ session_info = _bt._active_sessions.get(task_id)
+ if not session_info:
+ return False
+ return _session.human_holds_shared_browser(session_info)
+
+
def _write_owner_pid(socket_dir: str, session_name: str) -> None:
"""Record this hermes PID in ``<socket_dir>/<session>.owner_pid`` so the orphan
reaper can tell live-owner daemons from crashed-owner ones. Best-effort: an
diff --git a/tools/browser_tool_session.py b/tools/browser_tool_session.py
index c62afb228a7..bfdce8b8ad3 100644
--- a/tools/browser_tool_session.py
+++ b/tools/browser_tool_session.py
@@ -170,10 +170,35 @@ def _agent_browser_command_env(socket_dir: str) -> Dict[str, str]:
env["PATH"] = _install._merge_browser_path(env.get("PATH", ""))
env["AGENT_BROWSER_SOCKET_DIR"] = socket_dir
if "AGENT_BROWSER_IDLE_TIMEOUT_MS" not in env:
- env["AGENT_BROWSER_IDLE_TIMEOUT_MS"] = str(_bt.BROWSER_SESSION_INACTIVITY_TIMEOUT * 1000)
+ env["AGENT_BROWSER_IDLE_TIMEOUT_MS"] = str(_daemon_idle_timeout_seconds() * 1000)
return env
+_SHARED_HEADED_DAEMON_IDLE_SECONDS = 24 * 3600
+
+
+def _daemon_idle_timeout_seconds() -> int:
+ """The daemon's self-termination idle timer. The bot's headed Chromium on the Bot Desktop screen is
+ shared with a human who may take the lease to log in: the agent is idle by definition then, so the
+ daemon's own timer must not decide (it cannot see the lease); the lease-aware Python janitor owns that
+ browser's lifetime, and a crashed hermes leaves it to the orphan reaper (#110064)."""
+ if _cloud._is_headed_mode():
+ from tools.bot_desktop.runtime import published_env
+ if published_env().get("DISPLAY"):
+ return _SHARED_HEADED_DAEMON_IDLE_SECONDS
+ return _bt.BROWSER_SESSION_INACTIVITY_TIMEOUT
+
+
+def human_holds_shared_browser(session_info: Dict[str, Any]) -> bool:
+ """True while a human holds the Bot Desktop lease over the browser ``session_info`` shares with them.
+ The janitor treats that as activity: reaping the browser mid-login is the human's session dying under
+ them, not an idle agent's cleanup (#110064)."""
+ if not _shares_bot_desktop_browser(session_info):
+ return False
+ from tools.bot_desktop import lease as _bd_lease
+ return _bd_lease.human_holds()
+
+
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