Skip to content

The agent that grows with you

fix(bot-desktop): the launcher's first idle check waits a window

kshitijk4poor653057deae12
diff --git a/tests/tools/test_bot_desktop_idle_self_stop.py b/tests/tools/test_bot_desktop_idle_self_stop.py
index cc98ec62364..e0bda63cacd 100644
--- a/tests/tools/test_bot_desktop_idle_self_stop.py
+++ b/tests/tools/test_bot_desktop_idle_self_stop.py
@@ -92,6 +92,21 @@ 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")
+def test_the_first_idle_check_waits_until_the_screen_could_be_due(abandoned_screen):
+ """A screen just started cannot be idle: checking at launch costs an interpreter start and races Xfce
+ coming up, so the launcher sleeps a window before its first check."""
+ started = time.monotonic()
+ sd, _ = abandoned_screen(idle_stop_minutes=0.15) # 9 s
+ log = sd / "launcher.log"
+
+ def checked_while_used() -> bool:
+ (sd / "activity").touch() # in use: the first check keeps it
+ return "idle-watch" in log.read_text(encoding="utf-8", errors="replace")
+
+ wait_until(checked_while_used, timeout=30, what="the launcher's first idle check")
+ assert time.monotonic() - started >= 7, "the first idle check ran at launch"
+
+
def test_a_self_stop_does_not_sit_out_the_kill_grace(abandoned_screen):
"""The check's stop() TERMs the launcher's group, which holds the loop that ran the check. A loop blocked
in ``$(check)`` defers its TERM trap until the check returns, while the check waits for the group to
diff --git a/tools/bot_desktop/launcher.sh b/tools/bot_desktop/launcher.sh
index 64c7be3cc38..bfdcfc5b43c 100755
--- a/tools/bot_desktop/launcher.sh
+++ b/tools/bot_desktop/launcher.sh
@@ -290,19 +290,23 @@ if [[ -n "${HERMES_BD_PYTHON:-}" ]]; then
# interruptible: a `$(check)` would defer the trap until the check returns, while a check that is itself
# stopping the screen waits for this group to empty, so every self-stop would sit out stop()'s grace and
# end in SIGKILL with start.lock held. The check is in a session of its own and finishes the stop alone.
+ # Sleep first: a screen just started cannot be idle, so the first check is due a window from now
+ # (runtime.py passes that delay), not while Xfce is still coming up.
(
sleeper=
trap 'kill $sleeper 2>/dev/null; exit 0' TERM
out="$(dirname "$HERMES_BD_ENV_FILE")/idle-watch.out"
- while kill -0 "$$" 2>/dev/null; do
- HERMES_HOME="$HERMES_BD_HOME" PYTHONPATH="$HERMES_BD_SRC_ROOT${PYTHONPATH:+:$PYTHONPATH}" \
- "$HERMES_BD_PYTHON" -m tools.bot_desktop.idle_watch "$$" >"$out" &
- if wait "$!"; then delay=$(<"$out"); else delay=300; fi # broken check: retry slowly
- [[ -n "$delay" ]] || exit 0 # it stopped the screen
+ delay="${HERMES_BD_IDLE_FIRST_DELAY:-300}"
+ while :; do
[[ "$delay" =~ ^[0-9]+$ ]] || delay=300
sleep "$delay" & sleeper=$!
wait "$sleeper"
sleeper=
+ kill -0 "$$" 2>/dev/null || exit 0
+ HERMES_HOME="$HERMES_BD_HOME" PYTHONPATH="$HERMES_BD_SRC_ROOT${PYTHONPATH:+:$PYTHONPATH}" \
+ "$HERMES_BD_PYTHON" -m tools.bot_desktop.idle_watch "$$" >"$out" &
+ if wait "$!"; then delay=$(<"$out"); else delay=300; fi # broken check: retry slowly
+ [[ -n "$delay" ]] || exit 0 # it stopped the screen
done
) &
WATCH_PID=$!
diff --git a/tools/bot_desktop/runtime.py b/tools/bot_desktop/runtime.py
index 410ffcd06cd..189d688606c 100644
--- a/tools/bot_desktop/runtime.py
+++ b/tools/bot_desktop/runtime.py
@@ -751,6 +751,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 tools.bot_desktop.idle_watch import next_check_in
child_env.update({
"HERMES_BD_PROFILE": _profile_name(),
"HERMES_BD_DISPLAY_NUM": str(num),
@@ -759,10 +760,12 @@ 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.
+ # 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]),
"HERMES_BD_HOME": str(get_hermes_home()),
+ "HERMES_BD_IDLE_FIRST_DELAY": str(next_check_in(idle_stop_seconds(), 0.0)),
})
from tools.bot_desktop.browser import dock_exec_line, dock_launch
if (browser := dock_launch()) is not None: