Skip to content

The agent that grows with you

fix(bot-screen): a thumbnail grabbed across a takeover is suppressed

teknium1b948d9bd5f6f
diff --git a/tests/tui_gateway/test_display_methods.py b/tests/tui_gateway/test_display_methods.py
index 6594a594523..952bcf73699 100644
--- a/tests/tui_gateway/test_display_methods.py
+++ b/tests/tui_gateway/test_display_methods.py
@@ -203,3 +203,19 @@ def test_install_sudo_card_ignores_a_client_supplied_session_id(monkeypatch):
assert resp["result"]["started"], resp
assert asked.wait(5)
assert blocks and all(sid != "victim-session" for _ev, sid in blocks), blocks
+
+
+def test_thumbnail_grabbed_across_a_takeover_is_suppressed(monkeypatch, _fresh_lease):
+ """The human_holds() check happens before the grab; a takeover that lands while the framebuffer
+ is being read means the returned frame may already show the human's session. The lease epoch
+ must match before and after the grab or the frame is dropped."""
+ import tui_gateway.server as server
+ from tools.bot_desktop import thumbnail
+
+ def grab_while_human_takes_over():
+ _fresh_lease.acquire("viewer-1")
+ return "data:image/jpeg;base64,SECRET"
+
+ monkeypatch.setattr(thumbnail, "thumbnail_data_url", grab_while_human_takes_over)
+ result = _call(server, "display.thumbnail", {})["result"]
+ assert result["data_url"] is None and result["suppressed"] == "human_has_control", result
diff --git a/tui_gateway/methods_display.py b/tui_gateway/methods_display.py
index 299c93cbae0..607c336e12c 100644
--- a/tui_gateway/methods_display.py
+++ b/tui_gateway/methods_display.py
@@ -73,13 +73,19 @@ def _(rid, params: dict) -> dict:
@_profile_scoped
def _(rid, params: dict) -> dict:
"""One JPEG grab of the bot's screen (``data_url``: null while stopped). Read-only: no lease change.
- Suppressed while a human holds the lease — the frame may show what they are typing."""
+ Suppressed while a human holds the lease — the frame may show what they are typing — and when
+ the lease moved DURING the grab: a takeover racing the framebuffer read means the frame may
+ already be the human's session, so it is dropped rather than shipped to every client."""
try:
from tools.bot_desktop import lease as _bd_lease
- if _bd_lease.human_holds():
+ before = _bd_lease.get()
+ if before.holder == _bd_lease.HUMAN:
return _ok(rid, {"data_url": None, "suppressed": "human_has_control"})
from tools.bot_desktop.thumbnail import thumbnail_data_url
- return _ok(rid, {"data_url": thumbnail_data_url()})
+ data_url = thumbnail_data_url()
+ if _bd_lease.get().epoch != before.epoch:
+ return _ok(rid, {"data_url": None, "suppressed": "human_has_control"})
+ return _ok(rid, {"data_url": data_url})
except Exception as e:
return _err(rid, _DISPLAY_ERR, str(e))