Skip to content

The agent that grows with you

fix(bot-desktop): a stale idle check never writes into the next screen's log

kshitijk4poor279f89e994a0
diff --git a/tests/tools/test_bot_desktop_idle.py b/tests/tools/test_bot_desktop_idle.py
index 656e64b177e..27081a6fd55 100644
--- a/tests/tools/test_bot_desktop_idle.py
+++ b/tests/tools/test_bot_desktop_idle.py
@@ -133,16 +133,10 @@ def test_an_idle_check_that_waited_on_the_lock_never_stops_the_replacement_scree
proc.wait()
-def test_the_launcher_runs_its_idle_check_through_the_installs_stable_command(tmp_path, monkeypatch):
- """The screen outlives ``hermes update``: a check pinned to the starter's interpreter and its inherited
- dependency generation fails with ModuleNotFoundError once that generation is collected, forever. On a store
- install the launcher gets the ``hermes --run-module`` launcher, which loads the current one at every run."""
- from hermes_cli import _launchers
+def _spawn(monkeypatch, tmp_path) -> dict[str, str]:
+ """Runs ``_spawn_and_wait`` up to the launcher's Popen (a launcher that exits at once); the env it got."""
from tools.bot_desktop import browser
- root = runtime._LAUNCHER.resolve().parents[2]
- monkeypatch.setattr(_launchers, "resolve_store_python", lambda repo_root, **_: tmp_path / "store/bin/python3")
monkeypatch.setattr(runtime, "state_dir", lambda: tmp_path)
monkeypatch.setattr(runtime, "_ALLOC_LOCK", tmp_path / "alloc.lock")
monkeypatch.setattr(runtime, "_pick_display", lambda: 20)
@@ -158,7 +152,34 @@ def test_the_launcher_runs_its_idle_check_through_the_installs_stable_command(tm
monkeypatch.setattr(runtime.subprocess, "Popen", lambda argv, env, **_: handed.append(env) or _Exited())
with pytest.raises(RuntimeError):
runtime._spawn_and_wait(tmp_path, 1)
+ return handed[0]
+
+
+def test_the_launcher_runs_its_idle_check_through_the_installs_stable_command(tmp_path, monkeypatch):
+ """The screen outlives ``hermes update``: a check pinned to the starter's interpreter and its inherited
+ dependency generation fails with ModuleNotFoundError once that generation is collected, forever. On a store
+ install the launcher gets the ``hermes --run-module`` launcher, which loads the current one at every run."""
+ from hermes_cli import _launchers
- command = shlex.split(handed[0]["HERMES_BD_IDLE_CMD"])
+ root = runtime._LAUNCHER.resolve().parents[2]
+ monkeypatch.setattr(_launchers, "resolve_store_python", lambda repo_root, **_: tmp_path / "store/bin/python3")
+
+ command = shlex.split(_spawn(monkeypatch, tmp_path)["HERMES_BD_IDLE_CMD"])
assert command == _launchers.installation_command(root, module="tools.bot_desktop.idle_watch")
assert command == [str(root / ".hermes/bin/hermes"), "--run-module", "tools.bot_desktop.idle_watch"]
+
+
+def test_a_stale_idle_check_never_writes_into_the_replacement_screens_log(tmp_path, monkeypatch):
+ """The old screen's idle check runs in a session of its own and can outlive its screen (it waited on
+ ``start.lock`` while a replacement started) with the old ``launcher.log`` open at its offset. A start that
+ truncated the same file let that late write leave a run of NUL bytes and a "screen kept" line in the
+ replacement's log, reading as if it were about the new screen."""
+ log = tmp_path / "launcher.log"
+ log.write_bytes(b"x" * 4096)
+ with open(log, "r+b") as stale: # the old check's stderr: the launcher's, not opened for append
+ stale.seek(0, os.SEEK_END)
+ _spawn(monkeypatch, tmp_path)
+ stale.write(b"idle-watch changed while the idle stop waited; kept\n")
+ assert log.read_bytes() == b""
diff --git a/tools/bot_desktop/runtime.py b/tools/bot_desktop/runtime.py
index 2ecb8154301..cd8cd265a0f 100644
--- a/tools/bot_desktop/runtime.py
+++ b/tools/bot_desktop/runtime.py
@@ -783,7 +783,10 @@ def _spawn_and_wait(sd: Path, wait_seconds: float) -> DesktopStatus:
# The bare executable (the launcher checks it exists) and the ready-made, spec-quoted Exec= line.
child_env["HERMES_BD_BROWSER_EXEC"] = browser[0]
child_env["HERMES_BD_BROWSER_EXEC_LINE"] = dock_exec_line(*browser)
- # Truncated per start: the log is a diagnostic for THIS launch, and nothing rotates it otherwise.
+ # A new file per start: the log is a diagnostic for THIS launch, and nothing rotates it otherwise. Not
+ # truncated in place: the old screen's idle check (in a session of its own) may still hold the old file
+ # open, and its late write would land in this one behind a run of NUL bytes.
+ (sd / "launcher.log").unlink(missing_ok=True)
log = open(sd / "launcher.log", "wb")
# The profile's screen outlives whichever CLI/gateway started it, on purpose: launcher.pid carries
# (pid, create_time) so any later process reports/stops it, and the idle stop (idle_stop_minutes) ends it.