Skip to content

The agent that grows with you

fix(bot-screen): a viewer who handed back is not evicted by a later takeover

teknium1ab5a30468960
diff --git a/hermes_cli/web_routers/display.py b/hermes_cli/web_routers/display.py
index 541be828eb7..577036294ce 100644
--- a/hermes_cli/web_routers/display.py
+++ b/hermes_cli/web_routers/display.py
@@ -33,6 +33,21 @@ _CLOSE_NOT_ALLOWED = 4403
_CLOSE_PROTOCOL = 1003
+def _should_evict(held: dict, lease, viewer_id: str) -> bool:
+ """A viewer that held control during this connection and lost it to ANOTHER human is kicked so its
+ UI repaints; a plain hand-back to the agent, pure watchers and the new holder stay connected. The
+ hand-back also forgets that this viewer ever held: after it, they are a plain watcher again and a
+ later takeover by someone else must not evict them. ``held`` is the per-connection memory."""
+ from tools.bot_desktop import lease as _lease
+ if lease.holder != _lease.HUMAN:
+ held["ever"] = False
+ return False
+ if lease.viewer_id == viewer_id:
+ held["ever"] = True
+ return False
+ return bool(held["ever"])
+
+
def _consume_display_ticket(ws: WebSocket) -> Optional[dict]:
from hermes_cli.dashboard_auth.ws_tickets import TicketInvalid, consume_ticket
ticket = ws.query_params.get("display_ticket", "")
@@ -86,14 +101,9 @@ async def _bridge(ws: WebSocket, info: dict) -> None:
held = {"ever": _lease.viewer_may_send_input(viewer_id, profile_key=profile_home)}
def _on_lease(key: str, lease) -> None:
- # A viewer that held control during this connection and lost it to ANOTHER human is kicked so
- # its UI repaints; a plain hand-back to the agent, pure watchers and the new holder stay connected.
if key != profile_key:
return
- mine = lease.holder == _lease.HUMAN and lease.viewer_id == viewer_id
- if mine:
- held["ever"] = True
- elif held["ever"] and lease.holder == _lease.HUMAN:
+ if _should_evict(held, lease, viewer_id):
loop.call_soon_threadsafe(evicted.set)
unsubscribe = _lease.on_change(_on_lease)
diff --git a/tests/hermes_cli/test_display_ws_drop_keeps_lease.py b/tests/hermes_cli/test_display_ws_drop_keeps_lease.py
index e1ea23d7329..00be45e0c18 100644
--- a/tests/hermes_cli/test_display_ws_drop_keeps_lease.py
+++ b/tests/hermes_cli/test_display_ws_drop_keeps_lease.py
@@ -60,3 +60,16 @@ def test_only_a_clean_viewer_close_hands_the_screen_back(monkeypatch, close_code
after = asyncio.run(_bridge_once(close_code, home))
lease._reset_for_tests()
assert (after.holder == lease.HUMAN) is human_keeps_control, after
+
+
+def test_ex_holder_who_handed_back_is_not_evicted_by_a_later_takeover():
+ """desk-1 holds, hands back to the agent, then desk-2 takes over: desk-1 is a plain watcher again
+ and must stay connected; only a takeover WHILE desk-1 held (or believed it held) kicks it."""
+ held = {"ever": False}
+ assert display._should_evict(held, lease.Lease(holder=lease.HUMAN, viewer_id="desk-1"), "desk-1") is False
+ assert display._should_evict(held, lease.Lease(holder=lease.AGENT), "desk-1") is False
+ assert display._should_evict(held, lease.Lease(holder=lease.HUMAN, viewer_id="desk-2"), "desk-1") is False
+
+ held = {"ever": False}
+ display._should_evict(held, lease.Lease(holder=lease.HUMAN, viewer_id="desk-1"), "desk-1")
+ assert display._should_evict(held, lease.Lease(holder=lease.HUMAN, viewer_id="desk-2"), "desk-1") is True