Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
fix(appshot): preserve just-in-time permission lifecycle (#364)
Ruben Lütke0afeb33ae8a3
diff --git a/docs/os-parity.md b/docs/os-parity.mdindex 2e10952fec..9666850ec0 100644--- a/docs/os-parity.md+++ b/docs/os-parity.md@@ -1462,6 +1462,31 @@ German and English, including a quieter English sample. First response textarrived 0.4–1.4 seconds earlier in those three comparisons; these smallsynthetic comparisons are not a physical-device latency guarantee.+## AppShot permission lifecycle++AppShot follows AP-35: startup, settings reads and helper processes never request+macOS access. A user-started area selection asks for Screen Recording before the+picker starts or reads any preview pixels. The picker silently checks its own+usable grant and falls back to a live dim layer when it cannot capture. The final+capture still checks permissions and applies the existing privacy filters.++Saving a changed tap-based AppShot shortcut, or enabling AppShots with one already+configured, asks for Input Monitoring on macOS through the shared permission+service. Clearing a shortcut, saving an unchanged value, disabling AppShots and+ordinary startup never ask. Listener status follows actual readiness and later+grant changes; disabling AppShots stops its listeners. Both-modifier polling+retains its existing behavior without inventing an unverified permission need.++Windows and Linux/X11 retain their existing capture backends. Headless Linux and+Wayland report unavailable support; Jarvis does not yet implement the Wayland+Screenshot portal. Installing a portal package alone does not enable AppShots.+Backend availability never substitutes for a macOS grant and does not disable a+supported Mac's first-use action merely because consent has not been requested.++Validation uses platform fakes, AppShot entry-point and listener lifecycle tests,+and frontend action-state tests. Actual macOS dialogs and helper attribution,+physical modifier gestures, and Linux desktop sessions remain unverified here.+## GPT-Live subscription access (experimental)The optional subscription provider uses the selected Codex account for nativediff --git a/jarvis/appshot/hotkey.py b/jarvis/appshot/hotkey.pyindex 58a51351b1..a7317657da 100644--- a/jarvis/appshot/hotkey.py+++ b/jarvis/appshot/hotkey.py@@ -98,6 +98,30 @@ def configured_hotkeys(block: Any) -> dict[str, str]:}+async def request_saved_shortcut_access(+ previous: dict[str, str], current: dict[str, str], *, was_enabled: bool, enabled: bool,+) -> None:+ """Only saving or enabling a tap shortcut may ask for Input Monitoring."""+ from jarvis.core.instance import current_instance+ from jarvis.platform import detect_platform++ if not enabled or detect_platform() != "darwin" or not current_instance().owns_ambient_duties:+ return+ changed = any(+ combo and not is_gesture(combo) and (not was_enabled or previous[scope] != combo)+ for scope, combo in current.items()+ )+ if not changed:+ return+ from jarvis.platform.permission_service import get_permission_service+ from jarvis.platform.probes import display_present, has_hotkey++ if display_present() and has_hotkey():+ await get_permission_service().ensure_async(+ "input_monitoring", feature="global_shortcuts", interactive=True, wait_s=0.0,+ )++class AppshotShortcut:"""Owns whichever listeners the configured shortcuts need."""@@ -106,6 +130,8 @@ class AppshotShortcut:self._loop: asyncio.AbstractEventLoop | None = Noneself._watchers: list[Any] = []self._trigger_task: asyncio.Task[None] | None = None+ self._trigger: Any | None = None+ self._combo_scopes: set[str] = set()self._busy = Falsenot_started = ShortcutStatus(hotkey="", armed=False, detail="Not started yet.")self._statuses: dict[str, ShortcutStatus] = dict.fromkeys(SCOPE_KEYS, not_started)@@ -117,10 +143,21 @@ class AppshotShortcut:@propertydef status(self) -> ShortcutStatus:"""The front-window shortcut."""- return self._statuses["window"]+ return self.status_for("window")def status_for(self, scope: str) -> ShortcutStatus:- return self._statuses[scope]+ status = self._statuses[scope]+ trigger = self._trigger+ if scope not in self._combo_scopes or trigger is None:+ return status+ if trigger.armed:+ return ShortcutStatus(status.hotkey, True)+ detail = (+ "The shortcut is waiting for Input Monitoring access."+ if trigger.needs_input_monitoring+ else "The global shortcut listener is not running."+ )+ return ShortcutStatus(status.hotkey, False, detail)async def start(self) -> ShortcutStatus:self._loop = asyncio.get_running_loop()@@ -145,6 +182,10 @@ class AppshotShortcut:for scope, hotkey in hotkeys.items():if not hotkey:self._statuses[scope] = ShortcutStatus("", False, "No shortcut set.")+ elif not config.screen_context.enabled:+ self._statuses[scope] = ShortcutStatus(+ hotkey, False, "Appshots are switched off.",+ )elif not owns:self._statuses[scope] = ShortcutStatus(hotkey,@@ -161,7 +202,11 @@ class AppshotShortcut:self._statuses[scope] = self._check_combo(hotkey)if self._statuses[scope].armed:combos[scope] = hotkey+ self._statuses[scope] = ShortcutStatus(+ hotkey, False, "The global shortcut listener is starting.",+ )if combos:+ self._combo_scopes = set(combos)self._trigger_task = asyncio.get_running_loop().create_task(self._run_combos(combos), name="appshot-hotkey")@@ -192,6 +237,12 @@ class AppshotShortcut:task.cancel()with contextlib.suppress(asyncio.CancelledError):await task+ self._trigger = None+ self._combo_scopes.clear()+ self._statuses = {+ scope: ShortcutStatus(status.hotkey, False, "The shortcut is stopped.")+ for scope, status in self._statuses.items()+ }async def _arm_gesture(self, scope: str, hotkey: str) -> ShortcutStatus:from jarvis.appshot.gesture import ( # noqa: PLC0415@@ -240,6 +291,7 @@ class AppshotShortcut:try:trigger = HotkeyTrigger({_BINDINGS[scope]: [combo] for scope, combo in combos.items()})async with trigger:+ self._trigger = triggerasync for name in trigger.events():scope = scopes.get(name)if scope is not None:@@ -248,6 +300,12 @@ class AppshotShortcut:raiseexcept Exception: # noqa: BLE001 - voice and chat keep working without itlog.warning("appshot: shortcut listener stopped", exc_info=True)+ finally:+ self._trigger = None+ for scope, hotkey in combos.items():+ self._statuses[scope] = ShortcutStatus(+ hotkey, False, "The global shortcut listener is not running.",+ )def _fire_threadsafe(self, scope: str) -> None:loop = self._loop@@ -294,7 +352,10 @@ class AppshotShortcut:from jarvis.core.events import ConfigReloaded # noqa: PLC0415async def _on_reload(event: ConfigReloaded) -> None:- if any(key.startswith("appshot.") for key in event.changed_keys):+ if any(+ key.startswith("appshot.") or key == "screen_context.enabled"+ for key in event.changed_keys+ ):await self.reload()self._bus.subscribe(ConfigReloaded, _on_reload)diff --git a/jarvis/appshot/picker/renderer.py b/jarvis/appshot/picker/renderer.pyindex 7d0b6be36a..48d99c6d2d 100644--- a/jarvis/appshot/picker/renderer.py+++ b/jarvis/appshot/picker/renderer.py@@ -57,6 +57,7 @@ from jarvis.appshot.region import (MAG_BOX_PX,MAG_DEFAULT_ZOOM,MAG_ZOOMS,+ grab_preview,magnifier_layout,match_monitor,selection_fractions,@@ -456,12 +457,7 @@ class Picker(QObject):@staticmethoddef _grab(screen) -> QPixmap | None:- try:- pixmap = screen.grabWindow(0)- except Exception: # noqa: BLE001 - no frozen frame: fall back to a live dim layer- sys.stderr.write("appshot-picker: screen grab failed; using a live overlay\n")- return None- return None if pixmap.isNull() or pixmap.width() <= 0 else pixmap+ return grab_preview(screen)def _march(self) -> None:self.ants_offset = (self.ants_offset + 1.0) % (2 * _DASH)diff --git a/jarvis/appshot/region.py b/jarvis/appshot/region.pyindex 3ca49f5766..64b98cf040 100644--- a/jarvis/appshot/region.py+++ b/jarvis/appshot/region.py@@ -34,6 +34,7 @@ import subprocessimport sysfrom dataclasses import dataclassfrom typing import Any+from uuid import UUIDlog = logging.getLogger(__name__)@@ -404,7 +405,28 @@ async def _escape_cancels(proc: subprocess.Popen[str]) -> None:log.debug("appshot: global Escape for the area picker unavailable", exc_info=True)-async def pick_region(*, timeout_s: float = PICK_TIMEOUT_S) -> Selection | None:+def preview_capture_allowed() -> bool:+ """A picker helper may read its grant silently, but must never request one."""+ from jarvis.platform.screen_access import screen_recording_state, state_allows_capture++ return state_allows_capture(screen_recording_state(deep=False))+++def grab_preview(screen: Any) -> Any | None:+ """Read a preview only with this helper's grant; no Qt import or prompt."""+ try:+ if not preview_capture_allowed():+ return None+ pixmap = screen.grabWindow(0)+ return None if pixmap.isNull() or pixmap.width() <= 0 else pixmap+ except Exception: # noqa: BLE001 - the picker can dim the live desktop instead+ log.debug("appshot: preview unavailable; using a live overlay", exc_info=True)+ return None+++async def pick_region(+ *, timeout_s: float = PICK_TIMEOUT_S, trace_id: UUID | None = None,+) -> Selection | None:"""Let the user drag a rectangle. ``None`` = cancelled or timed out.Raises :class:`RegionUnavailable` when no picker can run on this host.@@ -417,6 +439,11 @@ async def pick_region(*, timeout_s: float = PICK_TIMEOUT_S) -> Selection | None:raise RegionUnavailable("An area is already being selected. Finish or press Esc first.")_picking = Truetry:+ # This is the user gesture. Ask in the parent before even enumerating+ # windows or starting a helper that freezes the desktop (AP-35).+ from jarvis.platform.screen_access import require_screen_recording_async++ await require_screen_recording_async("appshot", trace_id=trace_id)payload, code, timed_out = await _run_picker(timeout_s)finally:_picking = Falsediff --git a/jarvis/appshot/service.py b/jarvis/appshot/service.pyindex 2a9bb24fee..d283db6b93 100644--- a/jarvis/appshot/service.py+++ b/jarvis/appshot/service.py@@ -105,19 +105,20 @@ async def take_appshot(message="Appshots are switched off. Turn them on under Settings > Appshots.",)service = get_service(bus=bus)+ capture_trace_id = trace_id or uuid.uuid4()if scope == "region":- picked = await _pick_area(service)+ picked = await _pick_area(service, trace_id=capture_trace_id)if isinstance(picked, AppshotResult):return pickedoutcome = await service.capture(verdict=IntentVerdict(intent=VisualIntent.SCREEN, evidence=("appshot-region",)),- trace_id=trace_id or uuid.uuid4(),+ trace_id=capture_trace_id,region=picked,)else:outcome = await service.capture(verdict=IntentVerdict(intent=VisualIntent.WINDOW, evidence=("appshot",)),- trace_id=trace_id or uuid.uuid4(),+ trace_id=capture_trace_id,)if outcome.status != "captured" or outcome.context is None:return AppshotResult(@@ -159,16 +160,24 @@ async def take_appshot(return AppshotResult(status="captured", shot=replace(shot, delivered_to=delivered_to))-async def _pick_area(service: Any) -> tuple[int, int, int, int] | AppshotResult:+async def _pick_area(+ service: Any, *, trace_id: uuid.UUID | None = None,+) -> tuple[int, int, int, int] | AppshotResult:"""Run the area picker; the chosen rectangle, or the refusal to return."""from jarvis.appshot.region import ( # noqa: PLC0415RegionUnavailable,pick_region,selection_to_bbox,)+ from jarvis.platform.screen_access import ScreenCaptureRefusedtry:- selection = await pick_region()+ selection = await pick_region(trace_id=trace_id)+ except ScreenCaptureRefused as exc:+ log.info("appshot: area selection refused (%s)", exc.reason or "capture_permission")+ return AppshotResult(+ status="refused", reason_code="capture_permission", message=exc.user_detail,+ )except RegionUnavailable as exc: # reported to the caller as a refused appshotreturn AppshotResult(status="refused", reason_code="region_unavailable", message=str(exc))if selection is None:diff --git a/jarvis/screen_context/ports.py b/jarvis/screen_context/ports.pyindex adab57c2e3..fc574ebf2e 100644--- a/jarvis/screen_context/ports.py+++ b/jarvis/screen_context/ports.py@@ -566,9 +566,8 @@ def capture_permission_error(*, deep: bool = True) -> CapturePermissionIssue | Nreturn CapturePermissionIssue(code="wayland_portal",message=(- "Screen capture is unavailable in this Wayland session because "- "no desktop-portal capture backend is installed. Use an X11 "- "session or install a supported portal backend, then ask again."+ "Screen capture is not supported in this Wayland session yet: "+ "Jarvis has no desktop-portal capture adapter. Use an X11 session."),)try:diff --git a/jarvis/screen_context/service.py b/jarvis/screen_context/service.pyindex 233e2c2766..f3ab28260e 100644--- a/jarvis/screen_context/service.py+++ b/jarvis/screen_context/service.py@@ -687,7 +687,8 @@ class ScreenContextService:@staticmethoddef _permission_feature(verdict: IntentVerdict) -> str:"""The permission-episode feature of one capture: an appshot says so in its evidence."""- return "appshot" if "appshot" in verdict.evidence else "screen_context"+ appshot_evidence = {"appshot", "appshot-region", "appshot-screen"}+ return "appshot" if appshot_evidence.intersection(verdict.evidence) else "screen_context"async def _ask_for_capture_permission(self, verdict: IntentVerdict, trace_id: UUID | Nonediff --git a/jarvis/ui/web/appshot_routes.py b/jarvis/ui/web/appshot_routes.pyindex c6308ce0df..096fad0962 100644--- a/jarvis/ui/web/appshot_routes.py+++ b/jarvis/ui/web/appshot_routes.py@@ -66,13 +66,25 @@ def _capability() -> dict[str, Any]:from jarvis.appshot.region import picker_capability # noqa: PLC0415from jarvis.cu.indicator.controller import screen_indicator_capability # noqa: PLC0415+ from jarvis.platform.probes import display_present, is_waylandcapture_ok = importlib.util.find_spec("mss") is not None+ capture_detail = "" if capture_ok else "The screen-capture package is not installed."+ if not display_present():+ capture_ok = False+ capture_detail = "Screen capture needs an interactive desktop session."+ elif is_wayland():+ capture_ok = False+ capture_detail = "Appshot capture is not supported on Wayland yet. Use an X11 session."+ # This is backend support, not a permission preflight. A Mac without a+ # grant must retain its capture button so first use can ask the OS.effect_ok, effect_reason = screen_indicator_capability()region_ok, region_reason = picker_capability()+ if not capture_ok:+ region_ok, region_reason = False, capture_detailreturn {"capture": capture_ok,- "capture_detail": "" if capture_ok else "The screen-capture package is not installed.",+ "capture_detail": capture_detail,"effect": effect_ok,"effect_detail": effect_reason,"region": region_ok,@@ -121,6 +133,7 @@ async def put_settings(request: Request, patch: SettingsPatch) -> dict[str, Any]get_shortcut,is_gesture,normalize_hotkey,+ request_saved_shortcut_access,)from jarvis.core.config import load_config # noqa: PLC0415from jarvis.core.config_writer import ( # noqa: PLC0415@@ -142,8 +155,11 @@ async def put_settings(request: Request, patch: SettingsPatch) -> dict[str, Any]verdict = validate_hotkey(changes[key])if not verdict.ok:raise HTTPException(status_code=400, detail=verdict.reason or "Invalid shortcut.")+ listener_change = enabled is not None or "hotkey" in changes or "region_hotkey" in changes+ previous = await asyncio.to_thread(load_config) if listener_change else None+ if previous is not None:+ current = configured_hotkeys(previous.appshot)if "hotkey" in changes or "region_hotkey" in changes:- current = configured_hotkeys((await asyncio.to_thread(load_config)).appshot)window = changes.get("hotkey", current["window"])region = changes.get("region_hotkey", current["region"])if window and window == region:@@ -168,8 +184,18 @@ async def put_settings(request: Request, patch: SettingsPatch) -> dict[str, Any]from jarvis.screen_context.turn import reset_service # noqa: PLC0415reset_service()+ if previous is not None:+ updated = {+ "window": changes.get("hotkey", current["window"]),+ "region": changes.get("region_hotkey", current["region"]),+ }+ await request_saved_shortcut_access(+ current, updated,+ was_enabled=previous.screen_context.enabled,+ enabled=previous.screen_context.enabled if enabled is None else enabled,+ )shortcut = get_shortcut()- if shortcut is not None and ("hotkey" in changes or "region_hotkey" in changes):+ if shortcut is not None and listener_change:await shortcut.reload()return await asyncio.to_thread(_settings_payload)diff --git a/jarvis/ui/web/frontend/src/views/AppshotsView.test.tsx b/jarvis/ui/web/frontend/src/views/AppshotsView.test.tsxindex fe99484c06..f1b38da60f 100644--- a/jarvis/ui/web/frontend/src/views/AppshotsView.test.tsx+++ b/jarvis/ui/web/frontend/src/views/AppshotsView.test.tsx@@ -88,6 +88,30 @@ describe("AppshotsView", () => {expect((tryButton as HTMLButtonElement).disabled).toBe(true);expect(screen.getByTestId("appshots-sound").hasAttribute("disabled")).toBe(true);});++ it("shows why unsupported capture is unavailable and disables both capture actions", async () => {+ fetchMock.mockImplementation(async (url: string) =>+ url === "/api/appshot/settings"+ ? json({ ...SETTINGS, readiness: {+ ...SETTINGS.readiness, capture: false, capture_detail: "Wayland capture is unavailable.",+ } })+ : json({ appshot: null }),+ );+ render(<AppshotsView />);+ const button = await screen.findByTestId("appshots-try");+ expect((button as HTMLButtonElement).disabled).toBe(true);+ expect((screen.getByTestId("appshots-try-region") as HTMLButtonElement).disabled).toBe(true);+ expect(screen.getByText("Wayland capture is unavailable.")).toBeDefined();+ expect(fetchMock.mock.calls.some(([url]) => url.startsWith("/api/permissions"))).toBe(false);+ });++ it("keeps supported first-use capture available without requesting permissions on mount", async () => {+ render(<AppshotsView />);+ const button = await screen.findByTestId("appshots-try-region");+ expect((button as HTMLButtonElement).disabled).toBe(false);+ expect((screen.getByTestId("appshots-try") as HTMLButtonElement).disabled).toBe(false);+ expect(fetchMock.mock.calls.some(([url]) => url.startsWith("/api/permissions"))).toBe(false);+ });});describe("AppshotsView area appshots", () => {diff --git a/jarvis/ui/web/frontend/src/views/AppshotsView.tsx b/jarvis/ui/web/frontend/src/views/AppshotsView.tsxindex a0f66a83f4..7e455fd162 100644--- a/jarvis/ui/web/frontend/src/views/AppshotsView.tsx+++ b/jarvis/ui/web/frontend/src/views/AppshotsView.tsx@@ -521,7 +521,9 @@ export function AppshotsView() {? t("appshots.try_counting").replace("{0}", String(countdown)): picking? t("appshots.try_picking")- : t("appshots.try_hint")+ : !settings.readiness.capture+ ? settings.readiness.capture_detail+ : t("appshots.try_hint")}control={<div className="flex items-center gap-2">@@ -531,7 +533,8 @@ export function AppshotsView() {variant="secondary"size="sm"disabled={- disabled || picking || !settings.readiness.region || countdown !== null+ disabled || picking || !settings.readiness.capture ||+ !settings.readiness.region || countdown !== null}onClick={() => void tryRegion()}data-testid="appshots-try-region"@@ -544,7 +547,7 @@ export function AppshotsView() {type="button"variant="secondary"size="sm"- disabled={disabled || picking || countdown !== null}+ disabled={disabled || picking || !settings.readiness.capture || countdown !== null}onClick={() => void tryIt()}data-testid="appshots-try">diff --git a/tests/unit/appshot/test_appshot_permissions.py b/tests/unit/appshot/test_appshot_permissions.pynew file mode 100644index 0000000000..24eccd1daa--- /dev/null+++ b/tests/unit/appshot/test_appshot_permissions.py@@ -0,0 +1,375 @@+"""AppShot entry points preserve just-in-time consent and silent lifecycle checks."""++from __future__ import annotations++import asyncio+from types import SimpleNamespace+from uuid import uuid4++import pytest+from fastapi import FastAPI+from fastapi.testclient import TestClient++from jarvis.appshot import hotkey, region+from jarvis.appshot.service import _pick_area+from jarvis.core.events import ConfigReloaded+from jarvis.platform import permission_service, screen_access+from jarvis.screen_context.models import IntentVerdict, VisualIntent+from jarvis.screen_context.service import ScreenContextService+from jarvis.ui.web import appshot_routes+from tests.fakes.fake_permission_service import FakePermissionService+++def gate(monkeypatch):+ fake = FakePermissionService()+ monkeypatch.setattr(screen_access, "permission_gate", lambda: fake)+ monkeypatch.setattr(permission_service, "get_permission_service", lambda: fake)+ return fake+++def desktop(monkeypatch):+ import jarvis.core.instance as instance+ import jarvis.platform as platform+ import jarvis.platform.probes as probes++ monkeypatch.setattr(platform, "detect_platform", lambda: "darwin")+ monkeypatch.setattr(probes, "display_present", lambda: True)+ monkeypatch.setattr(probes, "is_wayland", lambda: False)+ monkeypatch.setattr(probes, "has_hotkey", lambda: True)+ monkeypatch.setattr(+ instance, "current_instance", lambda: SimpleNamespace(owns_ambient_duties=True)+ )+ monkeypatch.setattr(region, "picker_capability", lambda: (True, ""))++[email protected]("state", ["pending", "denied", "needs_settings", "unavailable"])+async def test_region_refuses_before_starting_the_picker(monkeypatch, gate, desktop, state):+ gate.script("screen_recording", state)+ started = []++ async def run_picker(_timeout):+ started.append(True)+ return None, 0, False++ monkeypatch.setattr(region, "_run_picker", run_picker)+ trace_id = uuid4()+ result = await _pick_area(object(), trace_id=trace_id)++ assert result.reason_code == "capture_permission"+ assert result.message+ assert started == []+ assert region._picking is False+ (call,) = gate.ensure_calls("screen_recording")+ assert (call.feature, call.interactive, call.trace_id) == ("appshot", True, trace_id)+++async def test_region_only_starts_after_grant_and_cancel_stays_cancelled(+ monkeypatch, gate, desktop+):+ gate.script("screen_recording", "denied", "granted")+ started = []++ async def run_picker(_timeout):+ started.append(True)+ assert gate.ensure_calls("screen_recording")+ return None, 0, False++ monkeypatch.setattr(region, "_run_picker", run_picker)+ result = await _pick_area(object())+ assert result.reason_code == "capture_permission"+ result = await _pick_area(object())+ assert result.reason_code == "cancelled"+ assert started == [True]+++async def test_existing_grant_and_unavailable_picker_never_ask(monkeypatch, gate, desktop):+ async def run_picker(_timeout):+ return None, 0, False++ monkeypatch.setattr(region, "_run_picker", run_picker)+ assert await region.pick_region() is None+ assert gate.ensure_calls("screen_recording") == []+ gate.calls.clear()+ monkeypatch.setattr(region, "picker_capability", lambda: (False, "Wayland"))+ with pytest.raises(region.RegionUnavailable, match="Wayland"):+ await region.pick_region()+ assert gate.calls == []+++ "state, allowed",+ [+ ("granted", True),+ ("not_required", True),+ ("pending", False),+ ("denied", False),+ ("unavailable", False),+ ],+)+def test_preview_helper_checks_silently(gate, state, allowed):+ gate.script("screen_recording", state)+ assert region.preview_capture_allowed() is allowed+ assert gate.ensure_calls("screen_recording") == []+ assert {call.method for call in gate.calls} == {"check"}++[email protected]("allowed", [False, True])+def test_picker_never_reads_pixels_without_its_own_grant(monkeypatch, allowed):+ grabs = []+ pixmap = SimpleNamespace(isNull=lambda: False, width=lambda: 100)++ def grab_window(_handle):+ grabs.append(True)+ return pixmap++ monkeypatch.setattr(region, "preview_capture_allowed", lambda: allowed)+ result = region.grab_preview(SimpleNamespace(grabWindow=grab_window))+ assert result is (pixmap if allowed else None)+ assert grabs == ([True] if allowed else [])+++def test_failed_preview_uses_live_overlay_without_retrying_or_prompting(gate):+ def broken_capture(_handle):+ raise RuntimeError("capture unavailable")++ assert region.grab_preview(SimpleNamespace(grabWindow=broken_capture)) is None+ assert gate.ensure_calls("screen_recording") == []+++ "evidence, feature",+ [+ ("appshot", "appshot"),+ ("appshot-region", "appshot"),+ ("appshot-screen", "appshot"),+ ("screen", "screen_context"),+ ("not-appshot", "screen_context"),+ ],+)+def test_every_appshot_scope_has_the_same_permission_feature(evidence, feature):+ verdict = IntentVerdict(intent=VisualIntent.SCREEN, evidence=(evidence,))+ assert ScreenContextService._permission_feature(verdict) == feature++[email protected]("platform", ["win32", "linux"])+async def test_shortcut_save_never_requests_macos_access_elsewhere(monkeypatch, gate, platform):+ monkeypatch.setattr("jarvis.platform.detect_platform", lambda: platform)+ await hotkey.request_saved_shortcut_access(+ {"window": "alt+alt", "region": ""},+ {"window": "ctrl+alt+a", "region": ""},+ was_enabled=True,+ enabled=True,+ )+ assert gate.calls == []+++ "old, new, was_enabled, enabled, requests",+ [+ ("alt+alt", "ctrl+alt+a", True, True, 1),+ ("ctrl+alt+a", "ctrl+alt+a", False, True, 1),+ ("ctrl+alt+a", "ctrl+alt+a", True, True, 0),+ ("ctrl+alt+a", "", True, True, 0),+ ("alt+alt", "shift+shift", True, True, 0),+ ("alt+alt", "ctrl+alt+a", True, False, 0),+ ],+)+async def test_only_changed_or_enabled_tap_shortcuts_ask(+ gate,+ desktop,+ old,+ new,+ was_enabled,+ enabled,+ requests,+):+ await hotkey.request_saved_shortcut_access(+ {"window": old, "region": ""},+ {"window": new, "region": ""},+ was_enabled=was_enabled,+ enabled=enabled,+ )+ calls = gate.ensure_calls("input_monitoring")+ assert len(calls) == requests+ if requests:+ assert calls[0].interactive is True+ assert calls[0].wait_s == 0+ assert calls[0].allow_outside_app is False+ assert gate.ensure_calls("screen_recording") == []+++def _config(*, enabled=True, window="alt+alt"):+ return SimpleNamespace(+ screen_context=SimpleNamespace(enabled=enabled),+ appshot=SimpleNamespace(hotkey=window, region_hotkey="shift+shift"),+ )+++async def test_disable_stops_watchers_and_config_reload_reenables_without_prompt(+ monkeypatch,+ gate,+ desktop,+):+ import jarvis.appshot.gesture as gesture++ config = _config()+ monkeypatch.setattr("jarvis.core.config.load_config", lambda: config)+ running = []++ class Watcher:+ def __init__(self, *args, **kwargs):+ pass++ def start(self):+ running.append(self)++ def stop(self):+ running.remove(self)++ callbacks = []+ bus = SimpleNamespace(subscribe=lambda event, callback: callbacks.append(callback))+ monkeypatch.setattr(gesture, "BothKeysWatcher", Watcher)+ monkeypatch.setattr(gesture, "make_probe", lambda _family: (lambda: (False, False), ""))+ shortcut = hotkey.AppshotShortcut(bus)+ try:+ await shortcut.start()+ assert len(running) == 2+ config.screen_context.enabled = False+ await callbacks[0](ConfigReloaded(changed_keys=("screen_context.enabled",)))+ assert running == []+ assert not shortcut.status.armed+ config.screen_context.enabled = True+ await callbacks[0](ConfigReloaded(changed_keys=("screen_context.enabled",)))+ assert len(running) == 2+ assert gate.calls == []+ finally:+ await shortcut.stop()+ assert running == []+ assert not shortcut.status.armed+++async def test_combo_status_tracks_waiting_grant_and_listener_shutdown(monkeypatch, gate, desktop):+ monkeypatch.setattr("jarvis.core.config.load_config", lambda: _config(window="ctrl+alt+a"))+ # Only the combo is relevant to this test.+ monkeypatch.setattr(hotkey, "configured_hotkeys", lambda _block: {"window": "ctrl+alt+a"})++ class Trigger:+ armed = False+ needs_input_monitoring = True++ def __init__(self, bindings):+ pass++ async def __aenter__(self):+ return self++ async def __aexit__(self, *args):+ self.armed = False++ async def events(self):+ await asyncio.Event().wait()+ yield "appshot"++ monkeypatch.setattr("jarvis.trigger.hotkey.HotkeyTrigger", Trigger)+ shortcut = hotkey.AppshotShortcut(object())+ try:+ await shortcut.reload()+ assert not shortcut.status.armed+ await asyncio.sleep(0)+ assert "Input Monitoring" in shortcut.status.detail+ shortcut._trigger.armed = True+ shortcut._trigger.needs_input_monitoring = False+ assert shortcut.status.armed+ shortcut._trigger.armed = False+ assert not shortcut.status.armed+ assert gate.calls == []+ finally:+ await shortcut.stop()+ assert not shortcut.status.armed+++ "display, wayland, supported", [(True, False, True), (False, False, False), (True, True, False)]+)+def test_readiness_is_backend_support_and_does_not_request_consent(+ monkeypatch,+ gate,+ desktop,+ display,+ wayland,+ supported,+):+ monkeypatch.setattr("jarvis.platform.probes.display_present", lambda: display)+ monkeypatch.setattr("jarvis.platform.probes.is_wayland", lambda: wayland)+ monkeypatch.setattr(+ "jarvis.cu.indicator.controller.screen_indicator_capability", lambda: (True, "")+ )+ monkeypatch.setattr("importlib.util.find_spec", lambda _name: object())+ gate.script("screen_recording", "denied")+ payload = appshot_routes._capability()+ assert payload["capture"] is supported+ assert payload["region"] is supported+ assert bool(payload["capture_detail"]) is not supported+ assert gate.calls == []+++ "enabled, patch, requests",+ [+ (True, {"hotkey": "ctrl+alt+a"}, 1),+ (True, {"hotkey": "ctrl+alt+a", "region_hotkey": "ctrl+shift+a"}, 1),+ (False, {"hotkey": "ctrl+alt+a"}, 0),+ (True, {"enabled": False}, 0),+ ],+)+def test_settings_save_asks_after_writing_and_reloads_on_enable_change(+ monkeypatch,+ gate,+ desktop,+ enabled,+ patch,+ requests,+):+ config = _config(enabled=enabled)+ events = []+ monkeypatch.setattr("jarvis.core.config.load_config", lambda: config)+ monkeypatch.setattr(+ "jarvis.core.config_writer.set_appshot_settings", lambda values: events.append("write")+ )+ monkeypatch.setattr(+ "jarvis.core.config_writer.set_screen_context_settings",+ lambda values: events.append("write"),+ )+ monkeypatch.setattr(appshot_routes, "_settings_payload", lambda: {"saved": True})++ async def reload():+ assert events == ["write"]+ assert len(gate.ensure_calls("input_monitoring")) == requests+ events.append("reload")++ monkeypatch.setattr(hotkey, "get_shortcut", lambda: SimpleNamespace(reload=reload))+ app = FastAPI()+ app.include_router(appshot_routes.router)+ with TestClient(app) as client:+ result = client.put("/api/appshot/settings", json=patch)+ assert result.status_code == 200+ assert events == ["write", "reload"]+++def test_disable_is_available_even_with_duplicate_legacy_shortcuts(monkeypatch, gate, desktop):+ config = _config(window="shift+shift")+ writes = []+ monkeypatch.setattr("jarvis.core.config.load_config", lambda: config)+ monkeypatch.setattr(+ "jarvis.core.config_writer.set_screen_context_settings", writes.append,+ )+ monkeypatch.setattr(appshot_routes, "_settings_payload", lambda: {"enabled": False})+ monkeypatch.setattr(hotkey, "get_shortcut", lambda: None)+ app = FastAPI()+ app.include_router(appshot_routes.router)+ with TestClient(app) as client:+ assert client.put("/api/appshot/settings", json={"enabled": False}).status_code == 200+ assert writes == [{"enabled": False}]+ assert gate.calls == []diff --git a/tests/unit/appshot/test_appshot_region.py b/tests/unit/appshot/test_appshot_region.pyindex b0c2ce156d..332b5b1385 100644--- a/tests/unit/appshot/test_appshot_region.py+++ b/tests/unit/appshot/test_appshot_region.py@@ -270,6 +270,9 @@ async def _reload_with(monkeypatch, hotkey: str, region_hotkey: str) -> AppshotSimport jarvis.platform.probes as probesclass Cfg:+ class screen_context: # noqa: N801+ enabled = True+class appshot: # noqa: N801pass@@ -281,7 +284,10 @@ async def _reload_with(monkeypatch, hotkey: str, region_hotkey: str) -> AppshotSarmed: list[dict] = []async def run_combos(self, combos):+ from types import SimpleNamespace+armed.append(dict(combos))+ self._trigger = SimpleNamespace(armed=True, needs_input_monitoring=False)monkeypatch.setattr(hotkey_module.AppshotShortcut, "_run_combos", run_combos)shortcut = AppshotShortcut(bus=object())@@ -394,6 +400,10 @@ def test_one_key_for_both_shortcuts_is_refused_before_writing(client, monkeypatc@pytest.fixturedef picker_host(monkeypatch):+ from jarvis.platform import screen_access+ from tests.fakes.fake_permission_service import FakePermissionService++ monkeypatch.setattr(screen_access, "permission_gate", lambda: FakePermissionService())monkeypatch.setattr(region, "picker_capability", lambda: (True, ""))monkeypatch.setattr(region, "_SETTLE_S", 0.0)region._picking = False@@ -456,6 +466,9 @@ async def test_overlapping_reloads_leave_exactly_one_listener(monkeypatch) -> Noimport jarvis.platform.probes as probesclass Cfg:+ class screen_context: # noqa: N801+ enabled = True+class appshot: # noqa: N801hotkey = "ctrl+alt+a"region_hotkey = "alt+win+a"@@ -594,6 +607,9 @@ async def test_two_gestures_arm_two_watchers(monkeypatch) -> None:import jarvis.core.instance as instance_moduleclass Cfg:+ class screen_context: # noqa: N801+ enabled = True+class appshot: # noqa: N801hotkey = "alt+alt"region_hotkey = "shift+shift"