Skip to content

The agent that grows with you

fix(bot-desktop): the idle check runs through the install's stable command

kshitijk4poor7de36168efc8
diff --git a/tests/tools/test_bot_desktop_idle.py b/tests/tools/test_bot_desktop_idle.py
index 83a5e066b84..656e64b177e 100644
--- a/tests/tools/test_bot_desktop_idle.py
+++ b/tests/tools/test_bot_desktop_idle.py
@@ -4,6 +4,7 @@ one a human holds or that saw recent use is not."""
from __future__ import annotations
import os
+import shlex
import subprocess
import threading
import time
@@ -130,3 +131,34 @@ def test_an_idle_check_that_waited_on_the_lock_never_stops_the_replacement_scree
if proc is not None and proc.poll() is None:
proc.kill()
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
+ 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)
+ monkeypatch.setattr(browser, "dock_launch", lambda: None)
+ handed = []
+
+ class _Exited:
+ pid, returncode = os.getpid(), 1
+
+ def poll(self):
+ return 1
+
+ monkeypatch.setattr(runtime.subprocess, "Popen", lambda argv, env, **_: handed.append(env) or _Exited())
+ with pytest.raises(RuntimeError):
+ runtime._spawn_and_wait(tmp_path, 1)
+
+ command = shlex.split(handed[0]["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"]
diff --git a/tests/tools/test_bot_desktop_idle_self_stop.py b/tests/tools/test_bot_desktop_idle_self_stop.py
index 3b98b184219..ca3e939390d 100644
--- a/tests/tools/test_bot_desktop_idle_self_stop.py
+++ b/tests/tools/test_bot_desktop_idle_self_stop.py
@@ -9,7 +9,6 @@ from __future__ import annotations
import contextlib
import json
import os
-import shutil
import signal
import subprocess
import sys
@@ -94,34 +93,32 @@ def test_an_abandoned_screen_stops_itself_after_the_idle_window(abandoned_screen
timeout=30, what="the abandoned screen to be stopped the way stop() does")
[email protected]("venv", ["venv", ".venv"])
-def test_a_screen_started_before_an_update_still_stops_itself(abandoned_screen, tmp_path, venv):
- """``hermes update`` can rebuild the environment at a new path, so the interpreter that started the screen
- is gone by its next idle check. The check then uses the install's current interpreter; before, every
- check failed and was retried in 300 s, forever, and the abandoned screen never stopped."""
- # The checkout, with the rebuilt environment in ``venv`` or ``.venv``. tools/bot_desktop is a real copy:
- # runtime.py resolves symlinks when it names its source root.
- install = tmp_path / "install"
- (install / "tools").mkdir(parents=True)
- for entry in _REPO.iterdir():
- if entry.name not in {"venv", ".venv", "tools"}:
- (install / entry.name).symlink_to(entry)
- for entry in (_REPO / "tools").iterdir():
- if entry.name != "bot_desktop":
- (install / "tools" / entry.name).symlink_to(entry)
- shutil.copytree(_REPO / "tools" / "bot_desktop", install / "tools" / "bot_desktop",
- ignore=shutil.ignore_patterns("__pycache__"))
- used = tmp_path / "current-python-used"
- python = install / venv / "bin" / "python"
- python.parent.mkdir(parents=True)
- python.write_text(f'#!/bin/sh\n: > "{used}"\nexec "{sys.executable}" "$@"\n', encoding="utf-8")
- python.chmod(0o755)
- prelude = (f"runtime.__file__ = {str(install / 'tools/bot_desktop/runtime.py')!r}\n"
- f"sys.executable = {str(tmp_path / 'old-env/bin/python')!r} # removed by the update")
+def test_a_screen_started_before_an_update_still_stops_itself(abandoned_screen, tmp_path):
+ """The screen outlives ``hermes update``, which can drop the starter's interpreter and collect the dependency
+ generation its PYTHONPATH named: a check pinned to either failed with ModuleNotFoundError and was retried in
+ 300 s, forever. The launcher runs the install's stable command (on a store install the ``hermes
+ --run-module`` launcher, which loads the current generation at every run) with its own pid appended."""
+ install = tmp_path / "an install's root" # quoting: the command crosses the env as a shell-quoted string
+ used = tmp_path / "launcher-used"
+ hermes = install / ".hermes" / "bin" / "hermes"
+ hermes.parent.mkdir(parents=True)
+ hermes.write_text(
+ "#!/bin/sh\n"
+ '[ "$1" = --run-module ] && [ "$2" = tools.bot_desktop.idle_watch ] && [ $# -eq 3 ] || exit 64\n'
+ f'echo "$3" > "{used}"\n'
+ "shift 2\n"
+ f'PYTHONPATH="{_REPO}" exec "{sys.executable}" -m tools.bot_desktop.idle_watch "$@"\n', encoding="utf-8")
+ hermes.chmod(0o755)
+ prelude = (
+ "from hermes_cli import _launchers\n"
+ f"_launchers.resolve_store_python = lambda root, **_: Path({str(tmp_path / 'store/bin/python3')!r})\n"
+ f"runtime.__file__ = {str(install / 'tools/bot_desktop/runtime.py')!r}\n"
+ f"sys.executable = {str(tmp_path / 'old-env/bin/python')!r} # removed by the update\n"
+ f"import os; os.environ['PYTHONPATH'] = {str(tmp_path / 'collected-generation')!r}")
sd, launcher = abandoned_screen(idle_stop_minutes=0.02, prelude=prelude)
wait_until(lambda: not _alive(launcher) and not (sd / "launcher.pid").exists(), timeout=30,
- what="the abandoned screen to stop itself with the install's current interpreter")
- assert used.exists()
+ what="the abandoned screen to stop itself through the install's launcher")
+ assert used.read_text(encoding="utf-8").split() == [str(launcher)]
def test_the_first_idle_check_waits_until_the_screen_could_be_due(abandoned_screen):
diff --git a/tools/bot_desktop/launcher.sh b/tools/bot_desktop/launcher.sh
index 4f89c1398a4..c23b05c403a 100755
--- a/tools/bot_desktop/launcher.sh
+++ b/tools/bot_desktop/launcher.sh
@@ -278,10 +278,11 @@ xset -display "$DISPLAY" s off -dpms s noblank 2>/dev/null || true
# ---- idle self-stop: the screen outlives the gateway that started it (a restarted gateway re-attaches),
# so it must also stop itself when no gateway ever comes back. tools/bot_desktop/idle_watch.py applies the
# gateway watcher's own rule (bot_desktop.idle_stop_minutes; never while a human holds the screen) once per
-# run and prints the seconds until the next check; nothing stays resident in between. Skipped when runtime.py
-# passed no interpreter (the sandbox-hosted screen, whose sandbox lifetime bounds it).
+# run and prints the seconds until the next check; nothing stays resident in between. runtime.py passes the
+# install's command for it, shell-quoted (the launcher's pid is appended); skipped when it passed none (the
+# sandbox-hosted screen, whose sandbox lifetime bounds it).
WATCH_PID=
-if [[ -n "${HERMES_BD_PYTHON:-}" ]]; then
+if [[ -n "${HERMES_BD_IDLE_CMD:-}" ]]; then
# A start is use: stamp it now, or an activity file left by an earlier screen could make the first check
# stop this one before runtime.py stamps it.
touch "$(dirname "$HERMES_BD_ENV_FILE")/activity"
@@ -303,13 +304,8 @@ if [[ -n "${HERMES_BD_PYTHON:-}" ]]; then
wait "$sleeper"
sleeper=
kill -0 "$$" 2>/dev/null || exit 0
- # `hermes update` may rebuild the environment at a new path, leaving the starter's interpreter gone:
- # use the install's current one then (both layouts are in the wild), or no check ever runs again.
- py="$HERMES_BD_PYTHON"
- [[ -x "$py" ]] || py="$HERMES_BD_SRC_ROOT/venv/bin/python"
- [[ -x "$py" ]] || py="$HERMES_BD_SRC_ROOT/.venv/bin/python"
- HERMES_HOME="$HERMES_BD_HOME" PYTHONPATH="$HERMES_BD_SRC_ROOT${PYTHONPATH:+:$PYTHONPATH}" \
- "$py" -m tools.bot_desktop.idle_watch "$$" >"$out" &
+ eval "set -- $HERMES_BD_IDLE_CMD"
+ HERMES_HOME="$HERMES_BD_HOME" "$@" "$$" >"$out" &
if wait "$!"; then delay=$(<"$out"); else delay=300; fi # broken check: retry slowly
[[ -n "$delay" ]] || exit 0 # it stopped the screen
done
diff --git a/tools/bot_desktop/runtime.py b/tools/bot_desktop/runtime.py
index 116e1f7a1d7..2ecb8154301 100644
--- a/tools/bot_desktop/runtime.py
+++ b/tools/bot_desktop/runtime.py
@@ -17,6 +17,7 @@ from __future__ import annotations
import contextlib
import logging
import os
+import shlex
import shutil
import signal
import subprocess
@@ -757,6 +758,7 @@ def _spawn_and_wait(sd: Path, wait_seconds: float) -> DesktopStatus:
child_env["HOME"] = child_env["HERMES_REAL_HOME"]
for key in ("DISPLAY", "XAUTHORITY", "WAYLAND_DISPLAY", "DBUS_SESSION_BUS_ADDRESS", "SESSION_MANAGER"):
child_env.pop(key, None)
+ from hermes_cli._launchers import installation_command
from tools.bot_desktop.idle_watch import next_check_in
child_env.update({
"HERMES_BD_PROFILE": _profile_name(),
@@ -766,10 +768,13 @@ def _spawn_and_wait(sd: Path, wait_seconds: float) -> DesktopStatus:
"HERMES_BD_ENV_FILE": str(env_file),
"HERMES_BD_CONFIG_HOME": str(sd / "xdg"),
"HERMES_BD_GEOMETRY": geometry(),
- # The launcher's idle self-stop runs tools.bot_desktop.idle_watch with this interpreter and home,
- # first once the screen could be due (nothing is idle at start).
- "HERMES_BD_PYTHON": sys.executable,
- "HERMES_BD_SRC_ROOT": str(Path(__file__).resolve().parents[2]),
+ # The launcher's idle self-stop runs tools.bot_desktop.idle_watch through the install's stable
+ # command (the ``hermes --run-module`` launcher on a store install, which follows Python pin changes
+ # and picks the current dependency generation at every run; this interpreter elsewhere), in this
+ # home, first once the screen could be due (nothing is idle at start). The screen outlives updates,
+ # so neither this interpreter nor the inherited PYTHONPATH generation may be pinned.
+ "HERMES_BD_IDLE_CMD": shlex.join(installation_command(
+ Path(__file__).resolve().parents[2], module="tools.bot_desktop.idle_watch")),
"HERMES_BD_HOME": str(get_hermes_home()),
"HERMES_BD_IDLE_FIRST_DELAY": str(next_check_in(idle_stop_seconds(), 0.0)),
})