The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-screen): refusing a bare release/stop under a human is one lease transition
teknium1745f88ae4919
diff --git a/tests/tui_gateway/test_display_methods.py b/tests/tui_gateway/test_display_methods.pyindex 952bcf73699..824cd85a6bf 100644--- a/tests/tui_gateway/test_display_methods.py+++ b/tests/tui_gateway/test_display_methods.py@@ -144,6 +144,24 @@ def test_stop_cannot_kill_the_screen_under_a_human_without_force(monkeypatch, _fassert forced["stopped"] is True and stops == [1]assert _fresh_lease.get().holder == _fresh_lease.AGENT+ # The refusal must be decided in the SAME transition as the release: a takeover that lands after a+ # separate "is a human holding?" read but before the release would be acknowledged, then revoked.+ # Simulate it by making the release transition itself see a human (a write raced in under the lock).+ real_transition = _fresh_lease._transition++ def transition_after_takeover(profile_key, mutate):+ def mutate_seeing_human(lease_now):+ lease_now.holder, lease_now.viewer_id = _fresh_lease.HUMAN, "late-viewer"+ return mutate(lease_now)+ return real_transition(profile_key, mutate_seeing_human)++ monkeypatch.setattr(_fresh_lease, "_transition", transition_after_takeover)+ stops.clear()+ for method, params in (("display.stop", {}), ("display.lease.release", {})):+ res = _call(server, method, params)+ assert "error" in res and res["error"]["data"]["code"] == "viewer_mismatch", (method, res)+ assert stops == []+def test_lease_acquire_and_release_only_honour_an_id_this_connection_minted(monkeypatch, tmp_path, _fresh_lease):"""The minted identity is worthless if acquire takes any string: a caller could acquire under adiff --git a/tools/bot_desktop/lease.py b/tools/bot_desktop/lease.pyindex b4e3533f85c..121df974db8 100644--- a/tools/bot_desktop/lease.py+++ b/tools/bot_desktop/lease.py@@ -178,10 +178,17 @@ def acquire(viewer_id: str, *, profile_key: Optional[str] = None, reason: str =return _transition(profile_key, _m)-def release(viewer_id: Optional[str] = None, *, profile_key: Optional[str] = None) -> Lease:+def release(viewer_id: Optional[str] = None, *, profile_key: Optional[str] = None,+ unless_human: bool = False) -> Lease:"""Return control to the agent. With ``viewer_id`` only that holder may release (a stale viewer- closing its window must not yank control from the one who took over after it)."""+ closing its window must not yank control from the one who took over after it). ``unless_human``+ makes a bare release a no-op while any human holds: the decision is taken under the file lock,+ so a takeover racing a check-then-release cannot be silently revoked. Callers read the returned+ lease's holder to learn whether anything happened."""def _m(lease: Lease) -> bool:+ if unless_human and lease.holder == HUMAN:+ logger.info("bot-desktop lease: bare release ignored, a human holds")+ return Falseif viewer_id is not None and lease.holder == HUMAN and lease.viewer_id != viewer_id:# Ignored, not an error: the returned lease still shows the real holder. Logged so a# caller that never inspects the return value leaves a trace.diff --git a/tui_gateway/methods_display.py b/tui_gateway/methods_display.pyindex 607c336e12c..406336a4bce 100644--- a/tui_gateway/methods_display.py+++ b/tui_gateway/methods_display.py@@ -108,11 +108,13 @@ def _(rid, params: dict) -> dict:"""Stopping kills the screen under whoever is on it, so it obeys the same rule as a baredisplay.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():+ force = bool(params.get("force"))+ # Refusal and release are ONE lease transition: a takeover landing between a separate human_holds()+ # check and the release would be acknowledged to the human and then silently revoked here.+ if _bd_lease.release(unless_human=not force).holder == _bd_lease.HUMAN: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()return _ok(rid, {**_display_snapshot(), "stopped": stopped})except Exception as e:@@ -242,12 +244,13 @@ def _(rid, params: dict) -> dict:viewer_id = str(params.get("viewer_id") or "").strip() or None# lease.release(None) skips the holder check; a client that lost its viewer id must not be able to# yank control from whoever holds it unless it says so explicitly (force).- if viewer_id is None and not params.get("force") and _bd_lease.human_holds():- return _err(rid, _DISPLAY_ERR, "viewer_id required to release another viewer's lease (or pass force: true)",- data={"code": "viewer_mismatch"})if viewer_id is not None and not params.get("force") and (refused := _foreign_viewer_id(rid, viewer_id)) is not None:return refused- lease = _bd_lease.release(viewer_id)+ # Same atomicity as display.stop: the "is a human holding?" decision happens inside the transition.+ lease = _bd_lease.release(viewer_id, unless_human=viewer_id is None and not params.get("force"))+ if viewer_id is None and lease.holder == _bd_lease.HUMAN:+ return _err(rid, _DISPLAY_ERR, "viewer_id required to release another viewer's lease (or pass force: true)",+ data={"code": "viewer_mismatch"})return _ok(rid, {"lease": _lease_view(lease)})