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
chore: auto-save WIP via Claude stop-hook safety net (2026-07-02 08:59)
Ruben Lütke27fcc7d2cf09
diff --git a/jarvis/cu/geometry.py b/jarvis/cu/geometry.pynew file mode 100644index 0000000000..57e333c530--- /dev/null+++ b/jarvis/cu/geometry.py@@ -0,0 +1,296 @@+"""Screen geometry and coordinate mapping for Computer-Use v2.++The chronic "clicks land next to the target" class of bugs came from mixing+coordinate spaces: the model sees a (possibly downscaled) image, the capture+covers one monitor of a multi-monitor virtual desktop (origins can be+negative), the OS input APIs consume their own units (physical pixels on+Windows/X11, logical points on macOS), and per-monitor DPI scaling makes the+spaces disagree with each other. This module makes the transform chain+explicit and single-sourced:++ model space -> image space -> screen space (OS input units)++* **Input units** are the units the OS input APIs consume. With the thread+ DPI pin below, ``mss`` monitor rects are expressed in the SAME units on+ every platform: physical virtual-desktop pixels on Windows (per-monitor+ DPI aware) and Linux/X11, logical points on macOS (where ``mss`` reports+ point rects and Quartz events consume points).+* A :class:`CoordinateMapper` is created once per captured frame and records+ the capture rect (input units) plus the exact size of the image the model+ received. Every action coordinate MUST resolve through the mapper of the+ frame the model actually saw — never through a separately-probed monitor+ that can diverge on a mixed-DPI desktop (live bug 2026-06-28).++Windows mixed-DPI note (the open "Face B" of the left-monitor bug): pywebview+flips the PROCESS DPI awareness at runtime (``SetProcessDPIAware()``), which+silently re-virtualizes ``GetSystemMetrics``/monitor rects for threads without+their own context — capture and input then disagree across monitors with+different scale factors. :func:`input_space` pins the CALLING thread to+PER_MONITOR_AWARE_V2 for the duration of any geometry query, capture, or+input dispatch, and restores the previous context afterwards (executor+threads are shared — a sticky pin could leak into unrelated code).+"""+from __future__ import annotations++import logging+import os+from contextlib import contextmanager+from dataclasses import dataclass+from typing import Iterator, Literal++logger = logging.getLogger(__name__)++#: Coordinate conventions a vision model can emit. ``normalized_1000`` is the+#: Gemini family convention (0..1000 grid over the image); ``image_pixels`` is+#: the Claude/OpenAI convention (pixel coordinates on the image as sent).+CoordinateConvention = Literal["normalized_1000", "image_pixels"]++_NORM_MAX = 1000+++# ---------------------------------------------------------------------------+# Thread DPI pin (Windows) — capture, geometry and input must share one space.+# ---------------------------------------------------------------------------++# DPI_AWARENESS_CONTEXT handles (winuser.h): V2 covers child windows and+# non-client areas; -3 is the 1607 fallback.+_DPI_CTX_PER_MONITOR_V2 = -4+_DPI_CTX_PER_MONITOR = -3+++@contextmanager+def input_space() -> Iterator[None]:+ """Pin the calling thread to per-monitor DPI awareness for the block.++ On Windows this guarantees that monitor rects, ``mss`` captures,+ ``GetSystemMetrics`` and ``SendInput`` normalization all read the SAME+ physical-pixel virtual desktop, regardless of any process-level awareness+ flip (pywebview). The previous thread context is restored on exit because+ ``asyncio.to_thread`` pool threads are shared with unrelated code.++ Non-Windows and pre-1607 Windows: a no-op — those platforms have a single+ consistent space already (macOS points / X11 pixels).+ """+ if os.name != "nt":+ yield+ return+ prev = None+ set_ctx = None+ try:+ import ctypes # noqa: PLC0415++ set_ctx = ctypes.windll.user32.SetThreadDpiAwarenessContext+ set_ctx.restype = ctypes.c_void_p+ set_ctx.argtypes = [ctypes.c_void_p]+ for context in (_DPI_CTX_PER_MONITOR_V2, _DPI_CTX_PER_MONITOR):+ prev = set_ctx(ctypes.c_void_p(context))+ if prev is not None:+ break+ except (OSError, AttributeError): # pre-1607 or exotic host: proceed unpinned+ logger.debug("SetThreadDpiAwarenessContext unavailable", exc_info=True)+ set_ctx = None+ try:+ yield+ finally:+ if set_ctx is not None and prev is not None:+ try:+ set_ctx(ctypes.c_void_p(prev))+ except Exception: # noqa: BLE001 — restore is best-effort+ logger.debug("thread DPI context restore failed", exc_info=True)+++# ---------------------------------------------------------------------------+# Monitors+# ---------------------------------------------------------------------------++@dataclass(frozen=True)+class MonitorInfo:+ """One monitor's rect in input units on the virtual desktop.++ ``left``/``top`` can be negative (a monitor left of / above the primary).+ """++ left: int+ top: int+ width: int+ height: int+ is_primary: bool = False+ name: str = ""++ @property+ def bbox(self) -> dict[str, int]:+ """mss-style bbox dict for grabbing this monitor."""+ return {+ "left": self.left, "top": self.top,+ "width": self.width, "height": self.height,+ }++ def contains(self, x: float, y: float) -> bool:+ return (+ self.left <= x < self.left + self.width+ and self.top <= y < self.top + self.height+ )+++def list_monitors() -> list[MonitorInfo]:+ """Enumerate physical monitors in input units (DPI-pinned on Windows).++ Returns an empty list when no display / mss is available (headless) —+ callers must treat that as "cannot act" and degrade honestly.+ """+ try:+ import mss # noqa: PLC0415+ except Exception: # noqa: BLE001 — desktop extras absent (headless base)+ return []+ try:+ with input_space(), mss.mss() as sct:+ monitors = sct.monitors+ if len(monitors) < 2:+ return []+ physical = monitors[1:]+ from jarvis.platform.monitors import resolve_primary_monitor # noqa: PLC0415++ try:+ primary = resolve_primary_monitor(monitors)+ except Exception: # noqa: BLE001+ primary = physical[0]+ return [+ MonitorInfo(+ left=int(m["left"]),+ top=int(m["top"]),+ width=int(m["width"]),+ height=int(m["height"]),+ is_primary=(m is primary),+ name=str(m.get("name", "") or ""),+ )+ for m in physical+ ]+ except Exception: # noqa: BLE001 — display asleep / X server gone+ logger.debug("list_monitors failed", exc_info=True)+ return []+++def virtual_screen_bounds(monitors: list[MonitorInfo]) -> tuple[int, int, int, int]:+ """(left, top, width, height) of the bounding box over all monitors."""+ if not monitors:+ return (0, 0, 0, 0)+ left = min(m.left for m in monitors)+ top = min(m.top for m in monitors)+ right = max(m.left + m.width for m in monitors)+ bottom = max(m.top + m.height for m in monitors)+ return (left, top, right - left, bottom - top)+++# ---------------------------------------------------------------------------+# CoordinateMapper — one per captured frame+# ---------------------------------------------------------------------------++@dataclass(frozen=True)+class CoordinateMapper:+ """Maps model/image coordinates of ONE frame to absolute screen input units.++ ``capture_*`` is the rect that was grabbed, in input units on the virtual+ desktop. ``image_*`` is the pixel size of the image that was actually sent+ to the model (after any downscale). The physical capture resolution drops+ out of the math: image -> screen is a pure scale by+ ``capture_size / image_size`` plus the capture origin, which stays correct+ on Retina (capture rect in points, image in pixels) and on downscaled+ frames alike.+ """++ capture_left: int+ capture_top: int+ capture_width: int+ capture_height: int+ image_width: int+ image_height: int++ def __post_init__(self) -> None:+ if self.capture_width <= 0 or self.capture_height <= 0:+ raise ValueError("capture rect must have positive size")+ if self.image_width <= 0 or self.image_height <= 0:+ raise ValueError("image size must be positive")+ # The capture is downscaled uniformly (no letterbox/crop), so the+ # aspect ratios must agree; a mismatch means the caller built the+ # mapper from the wrong frame — fail loudly instead of mis-clicking.+ cap_aspect = self.capture_width / self.capture_height+ img_aspect = self.image_width / self.image_height+ if abs(cap_aspect - img_aspect) > 0.02 * max(cap_aspect, img_aspect):+ raise ValueError(+ f"aspect mismatch: capture {self.capture_width}x"+ f"{self.capture_height} vs image {self.image_width}x"+ f"{self.image_height}"+ )++ # -- image space -> screen space ------------------------------------++ def image_to_screen(self, ix: float, iy: float) -> tuple[int, int]:+ """Map an image-pixel coordinate to absolute screen input units.++ Input is clamped to the image so a slightly-out-of-range model+ estimate cannot escape the captured monitor. ``+0.5`` centers the+ mapping on the pixel so a 1:1 frame maps pixel N to pixel N.+ """+ ix = min(max(float(ix), 0.0), float(self.image_width - 1))+ iy = min(max(float(iy), 0.0), float(self.image_height - 1))+ sx = self.capture_left + (ix + 0.5) * self.capture_width / self.image_width+ sy = self.capture_top + (iy + 0.5) * self.capture_height / self.image_height+ return (int(sx), int(sy))++ def normalized_to_screen(self, nx: float, ny: float) -> tuple[int, int]:+ """Map a 0..1000 normalized coordinate (Gemini grid) to screen units."""+ nx = min(max(float(nx), 0.0), float(_NORM_MAX))+ ny = min(max(float(ny), 0.0), float(_NORM_MAX))+ sx = self.capture_left + nx / _NORM_MAX * self.capture_width+ sy = self.capture_top + ny / _NORM_MAX * self.capture_height+ # Clamp inside the capture rect (1000/1000 is the bottom-right EDGE).+ sx = min(sx, self.capture_left + self.capture_width - 1)+ sy = min(sy, self.capture_top + self.capture_height - 1)+ return (int(sx), int(sy))++ def model_to_screen(+ self, x: float, y: float, convention: CoordinateConvention,+ ) -> tuple[int, int]:+ """Resolve a model-emitted coordinate per the provider's convention."""+ if convention == "normalized_1000":+ return self.normalized_to_screen(x, y)+ if convention == "image_pixels":+ return self.image_to_screen(x, y)+ raise ValueError(f"unknown coordinate convention: {convention!r}")++ # -- screen space -> image space ------------------------------------++ def screen_to_image(self, sx: float, sy: float) -> tuple[int, int]:+ """Inverse mapping, clamped to the image (for verification crops)."""+ ix = (float(sx) - self.capture_left) * self.image_width / self.capture_width+ iy = (float(sy) - self.capture_top) * self.image_height / self.capture_height+ ix = min(max(ix, 0.0), float(self.image_width - 1))+ iy = min(max(iy, 0.0), float(self.image_height - 1))+ return (int(ix), int(iy))++ # -- helpers ----------------------------------------------------------++ @property+ def screen_rect(self) -> tuple[int, int, int, int]:+ return (+ self.capture_left, self.capture_top,+ self.capture_width, self.capture_height,+ )++ def contains_screen(self, sx: float, sy: float) -> bool:+ return (+ self.capture_left <= sx < self.capture_left + self.capture_width+ and self.capture_top <= sy < self.capture_top + self.capture_height+ )++ def region_around(self, sx: int, sy: int, radius: int) -> dict[str, int]:+ """mss-style bbox of side ``2*radius`` around a screen point, clamped+ to the capture rect (never grabs outside the frame's monitor)."""+ r = max(1, int(radius))+ left = max(self.capture_left, min(int(sx) - r,+ self.capture_left + self.capture_width - 1))+ top = max(self.capture_top, min(int(sy) - r,+ self.capture_top + self.capture_height - 1))+ width = max(1, min(r * 2, self.capture_left + self.capture_width - left))+ height = max(1, min(r * 2, self.capture_top + self.capture_height - top))+ return {"left": left, "top": top, "width": width, "height": height}diff --git a/tests/unit/cu/test_geometry.py b/tests/unit/cu/test_geometry.pynew file mode 100644index 0000000000..16f089661a--- /dev/null+++ b/tests/unit/cu/test_geometry.py@@ -0,0 +1,195 @@+"""CoordinateMapper property tests — the coordinate spaces CU v2 must not mix.++The legacy engine's miss-class bugs are encoded here as concrete cases:+* the live 2026-06-27 left-monitor run (negative virtual-desktop origin),+* macOS Retina (capture rect in points, image in pixels),+* mixed-DPI downscaled frames (4K monitor -> ~1366px model image).+"""+from __future__ import annotations++import pytest++from jarvis.cu.geometry import (+ CoordinateMapper,+ MonitorInfo,+ input_space,+ virtual_screen_bounds,+)+++def _mapper(**kw) -> CoordinateMapper:+ defaults = dict(+ capture_left=0, capture_top=0,+ capture_width=1920, capture_height=1080,+ image_width=1920, image_height=1080,+ )+ defaults.update(kw)+ return CoordinateMapper(**defaults)+++# ---------------------------------------------------------------------------+# Basic mapping+# ---------------------------------------------------------------------------++def test_identity_frame_maps_pixel_to_same_pixel():+ m = _mapper()+ assert m.image_to_screen(0, 0) == (0, 0)+ assert m.image_to_screen(959, 539) == (959, 539)+ assert m.image_to_screen(1919, 1079) == (1919, 1079)+++def test_normalized_center_maps_to_screen_center():+ m = _mapper()+ assert m.normalized_to_screen(500, 500) == (960, 540)+++def test_normalized_extremes_stay_inside_capture_rect():+ m = _mapper()+ assert m.normalized_to_screen(0, 0) == (0, 0)+ x, y = m.normalized_to_screen(1000, 1000)+ assert (x, y) == (1919, 1079)+++def test_out_of_range_model_coords_are_clamped():+ m = _mapper()+ assert m.image_to_screen(-50, -50) == (0, 0)+ x, y = m.image_to_screen(99999, 99999)+ assert (x, y) == (1919, 1079)+ nx, ny = m.normalized_to_screen(1400, -3)+ assert 0 <= nx <= 1919 and 0 <= ny <= 1079+++# ---------------------------------------------------------------------------+# The live-log left-monitor case (negative origin, BUG-CU-MULTIMON forensic)+# ---------------------------------------------------------------------------++def test_left_monitor_negative_origin_matches_live_log():+ # data/jarvis_desktop.log 2026-06-27 12:31: norm=(636,531) on the+ # 3840x2160 monitor at left=-3840 resolved to abs=(-1398,1147).+ m = _mapper(+ capture_left=-3840, capture_top=0,+ capture_width=3840, capture_height=2160,+ image_width=3840, image_height=2160,+ )+ assert m.normalized_to_screen(636, 531) == (-1398, 1147)+++def test_negative_origin_roundtrip_screen_image():+ m = _mapper(+ capture_left=-3840, capture_top=-500,+ capture_width=3840, capture_height=2160,+ image_width=1366, image_height=768,+ )+ for sx, sy in [(-3840, -500), (-1920, 500), (-1, 1659)]:+ ix, iy = m.screen_to_image(sx, sy)+ rx, ry = m.image_to_screen(ix, iy)+ # One image pixel covers capture/image ~2.8 screen units here.+ assert abs(rx - sx) <= 3+ assert abs(ry - sy) <= 3+++# ---------------------------------------------------------------------------+# Retina / downscale: image pixels differ from capture units+# ---------------------------------------------------------------------------++def test_retina_points_capture_with_pixel_image():+ # macOS: capture rect is 1440x900 POINTS; the model gets a 2880x1800+ # pixel image (2x backing scale). A model pixel coordinate must land on+ # the correct POINT for Quartz input.+ m = _mapper(+ capture_left=0, capture_top=0,+ capture_width=1440, capture_height=900,+ image_width=2880, image_height=1800,+ )+ # Image center -> point center.+ assert m.image_to_screen(1440, 900) == (720, 450)+ # A click at image pixel (2878, 1798) must stay on-screen in points.+ x, y = m.image_to_screen(2878, 1798)+ assert 0 <= x < 1440 and 0 <= y < 900+++def test_downscaled_4k_frame_maps_back_to_full_monitor():+ # 4K monitor captured, downscaled to 1366-wide for the model.+ m = _mapper(+ capture_left=0, capture_top=0,+ capture_width=3840, capture_height=2160,+ image_width=1366, image_height=768,+ )+ # Image center -> monitor center (within one scale quantum).+ x, y = m.image_to_screen(683, 384)+ assert abs(x - 1920) <= 3 and abs(y - 1080) <= 3+ # normalized convention is image-size independent.+ assert m.normalized_to_screen(500, 500) == (1920, 1080)+++def test_aspect_mismatch_is_rejected():+ with pytest.raises(ValueError):+ _mapper(+ capture_width=3840, capture_height=2160, # 16:9+ image_width=1024, image_height=1024, # square+ )+++def test_zero_sizes_are_rejected():+ with pytest.raises(ValueError):+ _mapper(capture_width=0)+ with pytest.raises(ValueError):+ _mapper(image_height=0)+++# ---------------------------------------------------------------------------+# Helpers+# ---------------------------------------------------------------------------++def test_contains_screen_and_region_clamping():+ m = _mapper(capture_left=-1280, capture_top=0,+ capture_width=1280, capture_height=720,+ image_width=1280, image_height=720)+ assert m.contains_screen(-1, 0)+ assert m.contains_screen(-1280, 719)+ assert not m.contains_screen(0, 0) # first pixel of the NEXT monitor+ assert not m.contains_screen(-1281, 10)+ region = m.region_around(-1280, 0, radius=50)+ assert region["left"] == -1280 and region["top"] == 0+ assert region["width"] >= 1 and region["height"] >= 1+ # Region never leaves the capture rect.+ assert region["left"] + region["width"] <= -1280 + 1280+ assert region["top"] + region["height"] <= 0 + 720+++def test_virtual_screen_bounds_spans_negative_origins():+ mons = [+ MonitorInfo(left=0, top=0, width=2560, height=1440, is_primary=True),+ MonitorInfo(left=-3840, top=0, width=3840, height=2160),+ ]+ assert virtual_screen_bounds(mons) == (-3840, 0, 6400, 2160)+ assert virtual_screen_bounds([]) == (0, 0, 0, 0)+++def test_monitor_info_contains_and_bbox():+ m = MonitorInfo(left=-1280, top=100, width=1280, height=720)+ assert m.contains(-640, 400)+ assert not m.contains(1, 400)+ assert m.bbox == {"left": -1280, "top": 100, "width": 1280, "height": 720}+++def test_input_space_is_reentrant_and_safe_everywhere():+ # On non-Windows a pure no-op; on Windows pins + restores. Either way it+ # must never raise and must nest.+ with input_space():+ with input_space():+ pass+++def test_model_to_screen_dispatches_conventions():+ m = _mapper(+ capture_left=100, capture_top=200,+ capture_width=1000, capture_height=500,+ image_width=500, image_height=250,+ )+ assert m.model_to_screen(500, 500, "normalized_1000") == (600, 450)+ # image pixel (250,125) = image center -> capture center+ x, y = m.model_to_screen(250, 125, "image_pixels")+ assert abs(x - 600) <= 2 and abs(y - 450) <= 2+ with pytest.raises(ValueError):+ m.model_to_screen(1, 1, "polar") # type: ignore[arg-type]