Files
hermes-agent/tests/tools/test_computer_use_capture_fence.py
alt-glitch c5eb493375 refactor(browser-desktop): move computer_use test seams out of tools/computer_use/tool.py
Four things in tool.py existed only for tests:

- the module-level _backend injection hook (its own comment: "backward-
  compatible empty-session injection hook (older tests)") and its mirror
  branches in _install_backend, _detach_locked, _get_backend and
  _shutdown_backend_atexit. Production installs backends only through
  _new_backend into the _backends cache, so with nothing assigning _backend
  every branch was a no-op. The two tests that assigned it now patch
  _new_backend, the seam the rest of the suite already uses; the atexit
  tests seed _backends instead.
- reset_backend_for_tests, now tests/computer_use_fakes.reset_backend (the
  same two calls: _shutdown_backend_atexit, _reset_screenshot_dedup).
- _noop_stub and _NoopBackend (pragma: no cover, docstring "Test stub"),
  moved verbatim to tests/computer_use_fakes.py as noop_stub / NoopBackend.
- the _bounds_scale and _bounds_space_note lambdas, which only indexed
  _bounds_hints; the tests index _bounds_hints directly.

None of these names is in hermes_cli/data/plugin_api.json (it publishes
tools.computer_use.backend only) or tests/compat/old_updater_surface.json.
2026-11-16 21:54:46 +05:30

60 lines
2.3 KiB
Python

"""A frame captured across a human takeover never leaves the process: the lease fence runs as soon as the
backend hands the frame back, before it is persisted to the media cache, spilled, or routed to aux vision."""
from __future__ import annotations
import base64
import json
import pytest
from tools.bot_desktop import lease
from tools.computer_use import tool
from tools.computer_use.backend import ActionResult, CaptureResult
from tests.bot_desktop_lease import reset_lease
from tests.computer_use_fakes import NoopBackend, reset_backend
@pytest.fixture(autouse=True)
def _fresh_lease():
reset_backend()
reset_lease()
yield
reset_backend()
reset_lease()
class _TakeoverBackend(NoopBackend):
"""Driver whose capture returns while a take-over / hand-back cycle happened underneath it."""
_last_target = None
_last_app = None
def capture(self, **kw):
lease.acquire("human")
lease.release("human")
return CaptureResult(mode="som", width=64, height=64, png_b64=base64.b64encode(b"SECRETPNG").decode(), elements=[],
app="Bank", window_title="login")
def click(self, **kw):
return ActionResult(ok=True, action="click")
def _spy_sinks(monkeypatch, tool):
leaked: list = []
monkeypatch.setattr(tool, "_persist_capture_image", lambda cap: leaked.append(("persist", cap)))
monkeypatch.setattr(tool, "_spill_elements_to_file", lambda cap: leaked.append(("spill", cap)))
monkeypatch.setattr(tool, "_should_route_through_aux_vision", lambda: leaked.append(("aux-decide",)) or True)
monkeypatch.setattr(tool, "_route_capture_through_aux_vision", lambda cap, summary, **kw: leaked.append(("aux", cap)))
return leaked
@pytest.mark.parametrize("args", [{"action": "capture"}, {"action": "click", "coordinate": [1, 1], "capture_after": True}])
def test_frame_captured_across_a_takeover_is_dropped_before_any_sink(monkeypatch, args):
monkeypatch.setattr(tool, "_new_backend", lambda mode: _TakeoverBackend())
monkeypatch.setattr(tool, "_request_approval", lambda *a, **k: None)
leaked = _spy_sinks(monkeypatch, tool)
res = json.loads(tool.handle_computer_use(args))
assert res["code"] == "human_has_control", res
assert leaked == [], f"the human's frame reached a sink: {leaked}"
assert "SECRETPNG" not in json.dumps(res)