Skip to content

The agent that grows with you

fix(bot-screen): a launcher that dies without touching its files is broadcast as stopped

teknium194b03e6d8f81
diff --git a/tests/tui_gateway/test_display_watch.py b/tests/tui_gateway/test_display_watch.py
index ca7e70a6249..ffcf7215810 100644
--- a/tests/tui_gateway/test_display_watch.py
+++ b/tests/tui_gateway/test_display_watch.py
@@ -92,8 +92,8 @@ def test_screen_started_or_stopped_by_another_process_is_broadcast_as_status(tmp
assert not [e for e in events if e[0] == "display.status"]
# what runtime.start() publishes from another process: launcher.pid then env
- (home / "bot-desktop" / "launcher.pid").write_text("424242 1.5")
- (home / "bot-desktop" / "env").write_text("DISPLAY=:77\n")
+ (home / "bot-desktop" / "launcher.pid").write_text("424242 1.5", encoding="utf-8")
+ (home / "bot-desktop" / "env").write_text("DISPLAY=:77\n", encoding="utf-8")
server._poll_runtime_files()
statuses = [p for e, p in events if e == "display.status"]
assert statuses and statuses[-1]["profile_key"] == str(home)
@@ -102,3 +102,33 @@ def test_screen_started_or_stopped_by_another_process_is_broadcast_as_status(tmp
server._poll_runtime_files()
assert len([e for e in events if e[0] == "display.status"]) == 2
assert [p for e, p in events if e == "display.status"][-1]["running"] is False
+
+
+def test_launcher_dying_without_touching_its_files_is_broadcast_as_stopped(tmp_path, monkeypatch):
+ """Xvnc/the launcher crashing leaves env and launcher.pid exactly as they were, so a watcher keyed
+ on mtimes alone never told the Desktop the screen was gone. The mark must include liveness."""
+ import psutil
+ import tui_gateway.server as server
+
+ home = tmp_path / "home"
+ (home / "bot-desktop").mkdir(parents=True)
+ launcher = subprocess.Popen([sys.executable, "-c", "import time; time.sleep(30)"], stdin=subprocess.DEVNULL) # noqa: S603
+ try:
+ born = psutil.Process(launcher.pid).create_time()
+ (home / "bot-desktop" / "launcher.pid").write_text(f"{launcher.pid} {born!r}", encoding="utf-8")
+ (home / "bot-desktop" / "env").write_text("DISPLAY=:77\n", encoding="utf-8")
+ events = _watching(server, home, monkeypatch)
+ server._poll_runtime_files() # seed: running
+ server._poll_runtime_files()
+ assert not [e for e in events if e[0] == "display.status"], "unchanged state was re-broadcast"
+
+ launcher.kill()
+ launcher.wait(5)
+ server._poll_runtime_files()
+ server._poll_runtime_files()
+ statuses = [p for e, p in events if e == "display.status"]
+ assert len(statuses) == 1 and statuses[0]["running"] is False, statuses
+ finally:
+ if launcher.poll() is None:
+ launcher.kill()
+ launcher.wait(5)
diff --git a/tui_gateway/methods_display_watch.py b/tui_gateway/methods_display_watch.py
index e45af89f790..811e069ecdc 100644
--- a/tui_gateway/methods_display_watch.py
+++ b/tui_gateway/methods_display_watch.py
@@ -23,8 +23,9 @@ _lease_watcher_started = threading.Event()
# made by THIS process is not re-broadcast when its file write is noticed a tick later).
_lease_epochs: dict[str, int] = {}
_lease_mtimes: dict[str, int | None] = {}
-# profile key → (env mtime, launcher.pid mtime): the screen's running/display identity. A start,
-# stop or crash made by another process (CLI, gateway auto-start) moves one of these.
+# profile key → (env mtime, launcher.pid mtime, launcher alive): the screen's running/display
+# identity. A start or stop made by another process (CLI, gateway auto-start) moves a file; a crash
+# leaves both files and only flips liveness.
_runtime_marks: dict[str, tuple] = {}
@@ -46,20 +47,23 @@ def _mtime(path: Path):
def _poll_runtime_files() -> None:
- """Broadcast ``display.status`` when a home's screen started/stopped outside this process."""
+ """Broadcast ``display.status`` when a home's screen started/stopped outside this process — a
+ file move (start/stop by the CLI or gateway) or the launcher dying without touching its files
+ (Xvnc crash: env and launcher.pid stay put, only the pid stops being live)."""
from hermes_constants import hermes_home_key, reset_hermes_home_override, set_hermes_home_override
+ from tools.bot_desktop import runtime as _bd_runtime
for home in _watched_lease_homes():
key = hermes_home_key(home)
sd = home / "bot-desktop"
- mark = (_mtime(sd / "env"), _mtime(sd / "launcher.pid"))
- first = key not in _runtime_marks
- if _runtime_marks.get(key) == mark:
- continue
- _runtime_marks[key] = mark
- if first: # seeding: display.status carries the current state
- continue
token = set_hermes_home_override(home)
try:
+ mark = (_mtime(sd / "env"), _mtime(sd / "launcher.pid"), _bd_runtime._launcher_pid() is not None)
+ first = key not in _runtime_marks
+ if _runtime_marks.get(key) == mark:
+ continue
+ _runtime_marks[key] = mark
+ if first: # seeding: display.status carries the current state
+ continue
payload = _display_snapshot()
finally:
reset_hermes_home_override(token)