Skip to content

The agent that grows with you

fix(bot-desktop): a human lease nothing uses for four windows lapses

kshitijk4poor33803072c793
diff --git a/tests/tools/test_bot_desktop_idle.py b/tests/tools/test_bot_desktop_idle.py
index 19caed83c66..83a5e066b84 100644
--- a/tests/tools/test_bot_desktop_idle.py
+++ b/tests/tools/test_bot_desktop_idle.py
@@ -41,7 +41,7 @@ def test_idle_screen_is_stopped_but_a_used_or_held_one_is_not(tmp_path, monkeypa
runtime.touch_activity() # a computer_use action / browser spawn a moment ago
assert runtime.stop_if_idle() is False and stops == []
- _age(sd / "activity", 3600)
+ _age(sd / "activity", 1200)
monkeypatch.setattr(lease, "get", lambda profile_key=None: lease.Lease(holder=lease.HUMAN, viewer_id="v1"))
assert runtime.stop_if_idle() is False and stops == [], "a human mid-login is never timed out"
@@ -49,6 +49,21 @@ def test_idle_screen_is_stopped_but_a_used_or_held_one_is_not(tmp_path, monkeypa
assert runtime.stop_if_idle() is True and stops == [True]
+def test_a_human_lease_keeps_the_screen_until_nothing_has_used_it_for_four_windows(tmp_path, monkeypatch):
+ """A viewer link that dropped (laptop lid, a dead gateway) keeps the human's lease forever, and a corrupt
+ lease reads as human: with nobody coming back the screen must still stop eventually."""
+ sd, stops = _running(monkeypatch, tmp_path)
+ monkeypatch.setattr(lease, "get", lambda profile_key=None: lease.Lease(holder=lease.HUMAN, viewer_id="v1"))
+ (sd / "activity").touch()
+ windows = runtime.HUMAN_LEASE_ABANDONED_WINDOWS
+
+ _age(sd / "activity", 600 * windows - 60)
+ assert runtime.stop_if_idle() is False and stops == [], "kept: a human may still be at it"
+
+ _age(sd / "activity", 600 * windows + 60)
+ assert runtime.stop_if_idle() is True and stops == [True], "abandoned: no use for four windows"
+
+
def test_idle_stop_disabled_by_zero(tmp_path, monkeypatch):
sd, stops = _running(monkeypatch, tmp_path)
monkeypatch.setattr(runtime, "idle_stop_seconds", lambda: 0.0)
diff --git a/tests/tools/test_bot_desktop_idle_self_stop.py b/tests/tools/test_bot_desktop_idle_self_stop.py
index e0bda63cacd..6960bfbae8c 100644
--- a/tests/tools/test_bot_desktop_idle_self_stop.py
+++ b/tests/tools/test_bot_desktop_idle_self_stop.py
@@ -124,7 +124,7 @@ def test_a_screen_in_use_is_kept_and_a_returning_gateway_reattaches(abandoned_sc
from hermes_constants import reset_hermes_home_override, set_hermes_home_override
from tools.bot_desktop import runtime
- sd, launcher = abandoned_screen(idle_stop_minutes=0.02)
+ sd, launcher = abandoned_screen(idle_stop_minutes=0.05) # 3 s; a human lease lapses after 4 windows (12 s)
log = sd / "launcher.log"
def checks_kept() -> int:
@@ -137,14 +137,6 @@ def test_a_screen_in_use_is_kept_and_a_returning_gateway_reattaches(abandoned_sc
wait_until(lambda: kept_while_used(2), timeout=30, what="idle checks that keep a screen in use")
assert _alive(launcher)
- # A human holding the screen long past the window is never timed out.
- (sd / "lease.json").write_text(json.dumps({"holder": "human", "viewer_id": "v", "epoch": 1}), encoding="utf-8")
- old = time.time() - 3600
- os.utime(sd / "activity", (old, old))
- seen = checks_kept()
- wait_until(lambda: checks_kept() > seen, timeout=30, what="an idle check while a human holds the screen")
- assert _alive(launcher)
-
# A gateway that comes back inside the window re-attaches to the same screen instead of starting another.
monkeypatch.setattr(runtime, "missing_binaries", list) # the stub X binaries are only on the launcher's PATH
token = set_hermes_home_override(sd.parent)
@@ -153,5 +145,16 @@ def test_a_screen_in_use_is_kept_and_a_returning_gateway_reattaches(abandoned_sc
finally:
reset_hermes_home_override(token)
- (sd / "lease.json").unlink() # handed back; nobody uses it any more
- wait_until(lambda: not _alive(launcher), timeout=30, what="the screen to stop once nobody holds it")
+ # A human holding the screen past the window is not timed out...
+ (sd / "lease.json").write_text(json.dumps({"holder": "human", "viewer_id": "v", "epoch": 1}), encoding="utf-8")
+ old = time.time() - 3.2
+ os.utime(sd / "activity", (old, old))
+ seen = checks_kept()
+ wait_until(lambda: checks_kept() >= seen + 2, timeout=30, what="idle checks while a human holds the screen")
+ assert _alive(launcher)
+
+ # ...but a human lease nothing has used for four windows (the viewer's link dropped and never came back)
+ # no longer keeps it.
+ old = time.time() - 3600
+ os.utime(sd / "activity", (old, old))
+ wait_until(lambda: not _alive(launcher), timeout=30, what="the screen to stop once its human lease is abandoned")
diff --git a/tools/bot_desktop/runtime.py b/tools/bot_desktop/runtime.py
index 189d688606c..116e1f7a1d7 100644
--- a/tools/bot_desktop/runtime.py
+++ b/tools/bot_desktop/runtime.py
@@ -423,6 +423,11 @@ def _should_auto_start(env: dict[str, str]) -> bool:
# human holds it. Two callers apply it: the gateway's display watcher and the screen's own launcher
# (``idle_watch``), so a screen whose gateway never came back still stops. The next use starts it again.
DEFAULT_IDLE_STOP_MINUTES = 30
+# A human holding the screen is never timed out while there is evidence of one: an attached viewer stamps
+# ``activity`` (every RFB update it receives, at most once a minute; the panel clock repaints every minute).
+# A human lease with no stamp for this many windows is a link that dropped and never came back, or a
+# corrupt lease file (which reads as human): the screen is stopped like any idle one.
+HUMAN_LEASE_ABANDONED_WINDOWS = 4
def touch_activity() -> None:
@@ -469,13 +474,14 @@ def _idle_due(launcher: Optional[tuple[int, float]]) -> Optional[float]:
if idle is None or idle < limit:
return None
from tools.bot_desktop import lease as _bd_lease
- if _bd_lease.get().holder == _bd_lease.HUMAN:
+ if _bd_lease.get().holder == _bd_lease.HUMAN and idle < limit * HUMAN_LEASE_ABANDONED_WINDOWS:
return None
return idle
def stop_if_idle(launcher: Optional[tuple[int, float]] = None) -> bool:
- """Stop this profile's screen when it has been idle past the limit and no human holds it. True when
+ """Stop this profile's screen when it has been idle past the limit and no human holds it (a human lease
+ with no use for ``HUMAN_LEASE_ABANDONED_WINDOWS`` windows counts as abandoned). True when
it was stopped. ``launcher`` names the screen the caller judges (the launcher's own check passes it).
The decision is taken again under ``start.lock`` before anything is killed: a check that waited on the
diff --git a/website/docs/user-guide/features/bot-screen.md b/website/docs/user-guide/features/bot-screen.md
index 2e3345a1d24..6b815fc4abf 100644
--- a/website/docs/user-guide/features/bot-screen.md
+++ b/website/docs/user-guide/features/bot-screen.md
@@ -91,7 +91,10 @@ reverse proxy's access log may record an already-spent ticket.
`bot_desktop.idle_stop_minutes` (default 30) and comes back on the next use, so
an instance pays for a desktop only while something is on it. The screen
applies that window itself too, so it also stops when the gateway that
- started it is gone and never restarts. Practical guidance
+ started it is gone and never restarts. A screen a human has taken over is
+ kept while their viewer keeps it in use; once nothing has used it for four
+ windows (a viewer whose link dropped and never came back) it stops as well.
+ Practical guidance
for small instances: 4 GB runs the desktop, 8 GB is where a takeover with a
browser is comfortable.