Skip to content

The agent that grows with you

fix(bot-screen): display.stop refuses to kill a screen a human holds unless forced

teknium1625259c164e1
diff --git a/tests/tui_gateway/test_display_methods.py b/tests/tui_gateway/test_display_methods.py
index 4998946b47f..b8435a0e289 100644
--- a/tests/tui_gateway/test_display_methods.py
+++ b/tests/tui_gateway/test_display_methods.py
@@ -123,3 +123,20 @@ def test_observe_mints_the_viewer_id_and_status_never_discloses_the_holder(monke
assert lease_events and all(holder not in json.dumps(p) for p in lease_events)
finally:
lease._reset_for_tests()
+
+
+def test_stop_cannot_kill_the_screen_under_a_human_without_force(monkeypatch, _fresh_lease):
+ """display.stop released the lease unconditionally before stopping Xvnc: any authenticated caller
+ could yank a human mid-login and kill the screen under them. Same rule as display.lease.release."""
+ import tui_gateway.server as server
+ from tools.bot_desktop import runtime
+
+ stops = []
+ monkeypatch.setattr(runtime, "stop", lambda: stops.append(1) or True)
+ _fresh_lease.acquire("viewer-1")
+ refused = _call(server, "display.stop", {})
+ assert refused["error"]["data"]["code"] == "viewer_mismatch"
+ assert _fresh_lease.get().holder == _fresh_lease.HUMAN and stops == []
+ forced = _call(server, "display.stop", {"force": True})["result"]
+ assert forced["stopped"] is True and stops == [1]
+ assert _fresh_lease.get().holder == _fresh_lease.AGENT
diff --git a/tui_gateway/methods_display.py b/tui_gateway/methods_display.py
index 660a8a685ff..2f9dca3ab80 100644
--- a/tui_gateway/methods_display.py
+++ b/tui_gateway/methods_display.py
@@ -99,7 +99,12 @@ def _(rid, params: dict) -> dict:
@method("display.stop")
@_profile_scoped
def _(rid, params: dict) -> dict:
+ """Stopping kills the screen under whoever is on it, so it obeys the same rule as a bare
+ display.lease.release: refused while a human holds unless the caller says ``force``."""
from tools.bot_desktop import lease as _bd_lease, runtime as _bd_runtime
+ if not params.get("force") and _bd_lease.human_holds():
+ return _err(rid, _DISPLAY_ERR, "a human holds this screen; pass force: true to stop it anyway",
+ data={"code": "viewer_mismatch"})
try:
_bd_lease.release()
stopped = _bd_runtime.stop()