Skip to content

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.

chore: auto-save WIP via Claude stop-hook safety net (2026-07-02 17:55)

Ruben Lütke91db33e12bbd
diff --git a/jarvis/cu/engine.py b/jarvis/cu/engine.py
index 86b8f4596d..28efa6f762 100644
--- a/jarvis/cu/engine.py
+++ b/jarvis/cu/engine.py
@@ -61,6 +61,7 @@ from jarvis.cu.verify import (
crop_raw,
foreground_ui_snapshot,
regions_equal,
+ snap_point_to_element,
verify_typed_text,
)
@@ -333,6 +334,100 @@ def _summarize_action(action: dict[str, Any]) -> str:
return str(kind)
+#: Zoom-refine crop half-side in input units. Near-square context crops
+#: preserve surroundings better than tight boxes (MEGA-GUI / UI-Zoomer).
+_REFINE_RADIUS = 220
+
+_REFINE_SYSTEM = (
+ "You are a precision click-refinement assistant. The attached image is a "
+ "ZOOMED-IN crop of the live screen, centered on a click that produced NO "
+ "visible effect. Locate the TARGET element inside the crop. Output "
+ "exactly ONE JSON object, no prose, no fences:\n"
+ ' {"found": true, "x": <0-1000>, "y": <0-1000>} — x/y on a 0-1000 grid '
+ "WITHIN THIS CROP (0,0 = crop top-left), aimed at the CENTER of the "
+ "target.\n"
+ ' {"found": false} — the target is not visible anywhere in the crop.\n'
+ "Never guess a position for an element you cannot actually see."
+)
+
+
+async def _zoom_refine_point(
+ ctx: Any,
+ frame: Frame,
+ x: int,
+ y: int,
+ *,
+ goal: str,
+ target: str,
+) -> tuple[int, int] | None:
+ """One coarse-to-fine grounding round after a VERIFIED miss.
+
+ Grabs a native-resolution crop around the missed point (no downscale —
+ this is where zoom methods earn their ScreenSpot-Pro gains) and asks the
+ FAST vision chain to re-locate the target inside it. Returns the refined
+ absolute point, or ``None`` (keep/abandon). Runs ONLY on the miss path,
+ so the ordinary click costs zero extra model calls.
+ """
+ import io # noqa: PLC0415
+ import json as _json # noqa: PLC0415
+ import re as _re # noqa: PLC0415
+
+ bbox = frame.mapper.region_around(int(x), int(y), _REFINE_RADIUS)
+ raw = await asyncio.to_thread(grab_region, bbox)
+ if raw is None:
+ return None
+ try:
+ from PIL import Image # noqa: PLC0415
+
+ img = Image.frombytes("RGB", raw[0], raw[1])
+ buf = io.BytesIO()
+ img.save(buf, format="JPEG", quality=90)
+ crop_jpeg = buf.getvalue()
+ except Exception: # noqa: BLE001
+ return None
+ user = (
+ f"TARGET: {target or 'the element the GOAL needs clicked next'}\n"
+ f"GOAL: {goal}\n"
+ f"The crop is {bbox['width']}x{bbox['height']} screen units, centered "
+ "on the missed click. Reply with the JSON object only."
+ )
+ try:
+ reply = await call_vision_brain(
+ ctx.brain_manager,
+ build_prompt=lambda provider, brain: (_REFINE_SYSTEM, user),
+ images=[ImageBlock(
+ mime="image/jpeg",
+ data_b64=base64.b64encode(crop_jpeg).decode("ascii"),
+ )],
+ max_tokens=64,
+ early_stop_json=True,
+ )
+ except Exception: # noqa: BLE001 — refine is strictly best-effort
+ log.debug("[cu] zoom refine call failed", exc_info=True)
+ return None
+ cleaned = (reply.text or "").strip()
+ fence = _re.search(r"```(?:json)?\s*(.+?)\s*```", cleaned, _re.DOTALL)
+ if fence is not None:
+ cleaned = fence.group(1).strip()
+ start, end = cleaned.find("{"), cleaned.rfind("}")
+ if start < 0 or end <= start:
+ return None
+ try:
+ obj = _json.loads(cleaned[start:end + 1])
+ except _json.JSONDecodeError:
+ return None
+ if not isinstance(obj, dict) or obj.get("found") is not True:
+ return None
+ try:
+ nx = min(max(float(obj.get("x", 0)), 0.0), 1000.0)
+ ny = min(max(float(obj.get("y", 0)), 0.0), 1000.0)
+ except (TypeError, ValueError):
+ return None
+ rx = bbox["left"] + round(nx / 1000.0 * bbox["width"])
+ ry = bbox["top"] + round(ny / 1000.0 * bbox["height"])
+ return (rx, ry)
+
+
async def _judge_done(
ctx: Any,
goal: str,
@@ -483,7 +578,7 @@ async def run_cu_loop(
blob_dir=image_cfg.blob_dir,
)
snapshot_coro = foreground_ui_snapshot()
- frame, (labels, field_hint, handoff) = await asyncio.wait_for(
+ frame, (labels, field_hint, handoff, clickables) = await asyncio.wait_for(
asyncio.gather(frame_coro, snapshot_coro),
timeout=_OBSERVE_TIMEOUT_S,
)
@@ -716,6 +811,26 @@ async def run_cu_loop(
resolved_xy = frame.mapper.model_to_screen(
action["x"], action["y"], convention,
)
+ # Element anchoring: a point INSIDE a clickable element snaps
+ # to that element's center (smallest containing rect, capped
+ # against container traps) — the model only has to point
+ # anywhere inside the right control, its residual 1-3%
+ # pointing error stops mattering. Zero added latency: the
+ # rects ride the snapshot already captured in parallel.
+ snapped = snap_point_to_element(
+ resolved_xy[0], resolved_xy[1], clickables,
+ capture_area=monitor.width * monitor.height,
+ )
+ if snapped is not None:
+ sx2, sy2, snap_label = snapped
+ if (sx2, sy2) != resolved_xy:
+ log.info(
+ "[cu] click (%d,%d) anchored to element center "
+ "(%d,%d)%s", resolved_xy[0], resolved_xy[1],
+ sx2, sy2,
+ f" '{snap_label[:40]}'" if snap_label else "",
+ )
+ resolved_xy = (sx2, sy2)
elif kind == "drag":
resolved_xy = frame.mapper.model_to_screen(
action["x"], action["y"], convention,
@@ -789,6 +904,60 @@ async def run_cu_loop(
"element on the fresh screenshot (or use "
"click_element with its exact label)."
)
+ # Coarse-to-fine rescue (miss path only): re-ground
+ # the target inside a native-resolution zoom crop
+ # and retry ONCE at the refined point. This is the
+ # step behind the ScreenSpot-Pro zoom gains; the
+ # happy path never pays for it.
+ refined = await _zoom_refine_point(
+ ctx, frame, *resolved_xy,
+ goal=goal, target=str(action.get("target", "")),
+ )
+ if refined is not None and (
+ abs(refined[0] - resolved_xy[0]) > 12
+ or abs(refined[1] - resolved_xy[1]) > 12
+ ):
+ log.info(
+ "[cu] zoom refine: retrying click at "
+ "(%d,%d) after miss at (%d,%d)",
+ refined[0], refined[1], *resolved_xy,
+ )
+ pre2 = await asyncio.to_thread(
+ grab_region, monitor.bbox,
+ )
+ ok2, detail2 = await _dispatch_tool(
+ ctx, "click",
+ {
+ "x": refined[0], "y": refined[1],
+ "button": action["button"],
+ "double": action["double"],
+ },
+ trace_id,
+ )
+ if ok2:
+ ledger.record(
+ action, frame.thumb, resolved_xy=refined,
+ )
+ await asyncio.sleep(
+ _EFFECT_SETTLE_S * settle_scale,
+ )
+ post2 = await asyncio.to_thread(
+ grab_region, monitor.bbox,
+ )
+ lp2 = crop_raw(
+ pre2, rect_t, *refined,
+ _EFFECT_CROP_RADIUS,
+ ) if pre2 else None
+ lq2 = crop_raw(
+ post2, rect_t, *refined,
+ _EFFECT_CROP_RADIUS,
+ ) if post2 else None
+ if regions_equal(lp2, lq2) is not True:
+ ok = True
+ detail = (
+ f"{detail2} (zoom-refined retry "
+ "after a verified miss)"
+ )
profiler.add("verify", t0, step_idx)
elif kind == "type":
diff --git a/jarvis/cu/verify.py b/jarvis/cu/verify.py
index ce3570e48a..f3b3e80476 100644
--- a/jarvis/cu/verify.py
+++ b/jarvis/cu/verify.py
@@ -181,28 +181,98 @@ def clickable_labels(nodes: tuple, max_n: int = 28) -> list[str]:
return names
+def clickable_rects(nodes: tuple, max_n: int = 200) -> list[tuple[str, str, tuple[int, int, int, int]]]:
+ """Enabled clickable elements WITH their bounding rects, as
+ ``(name, role, (x, y, w, h))`` in screen input units.
+
+ The rects come from the accessibility tree the snapshot already walks
+ (UIA physical pixels / AX points / AT-SPI pixels — the same units the
+ actuator clicks in, now that the process is per-monitor DPI aware).
+ Unlike :func:`clickable_labels` this keeps NAMELESS elements too: an
+ icon-only button has no label but its rect still anchors a click.
+ """
+ out: list[tuple[str, str, tuple[int, int, int, int]]] = []
+ for node in nodes or ():
+ if not getattr(node, "enabled", True):
+ continue
+ role = getattr(node, "role", "")
+ if role not in CLICKABLE_UIA_ROLES:
+ continue
+ bounds = getattr(node, "bounds", None) or (0, 0, 0, 0)
+ try:
+ x, y, w, h = (int(v) for v in bounds)
+ except (TypeError, ValueError):
+ continue
+ if w <= 0 or h <= 0:
+ continue
+ out.append(((getattr(node, "name", "") or "").strip(), role, (x, y, w, h)))
+ if len(out) >= max_n:
+ break
+ return out
+
+
+#: An element larger than this fraction of the capture area is a CONTAINER
+#: (panel, document body) — snapping to its center would be a wrong-but-
+#: plausible click far from the model's intent.
+_SNAP_MAX_AREA_FRACTION = 0.15
+
+
+def snap_point_to_element(
+ x: int,
+ y: int,
+ clickables: list[tuple[str, str, tuple[int, int, int, int]]],
+ *,
+ capture_area: int,
+) -> tuple[int, int, str] | None:
+ """Snap a model-predicted point to the CENTER of the smallest clickable
+ element whose rect contains it — or ``None`` (keep the raw point).
+
+ Converts the vision model's job from "hit an exact pixel" to "point
+ anywhere inside the right element": the residual 1-3% pointing error
+ stops mattering, at zero added latency (the rects ride the snapshot that
+ already runs in parallel with the capture). The SMALLEST containing rect
+ wins (leaf over ancestor); rects above ``_SNAP_MAX_AREA_FRACTION`` of
+ the capture area never snap (container trap). Pure; never raises.
+ """
+ best: tuple[int, str, int, int] | None = None # (area, label, cx, cy)
+ max_area = max(1, int(capture_area * _SNAP_MAX_AREA_FRACTION))
+ for name, _role, (rx, ry, rw, rh) in clickables or ():
+ if not (rx <= x < rx + rw and ry <= y < ry + rh):
+ continue
+ area = rw * rh
+ if area > max_area:
+ continue
+ if best is None or area < best[0]:
+ best = (area, name, rx + rw // 2, ry + rh // 2)
+ if best is None:
+ return None
+ return (best[2], best[3], best[1])
+
+
# ---------------------------------------------------------------------------
# Async tree snapshots (best-effort; failures degrade to "cannot tell")
# ---------------------------------------------------------------------------
async def foreground_ui_snapshot(
timeout_s: float = UIA_TIMEOUT_S, max_labels: int = 28,
-) -> tuple[list[str], str, str | None]:
+) -> tuple[list[str], str, str | None, list[tuple[str, str, tuple[int, int, int, int]]]]:
"""One tree observation → (clickable labels, field-values hint, handoff
- reason). ``([], "", None)`` on any failure or a label-less surface —
- that empty path self-gates the loop back to pixel clicks."""
+ reason, clickable rects). ``([], "", None, [])`` on any failure or a
+ label-less surface — that empty path self-gates the loop back to raw
+ pixel clicks (canvas apps expose no useful tree)."""
try:
obs = await asyncio.wait_for(
_get_ui_tree_source().observe(), timeout=timeout_s,
)
except Exception as exc: # noqa: BLE001 — best-effort enumeration
logger.debug("[cu] UI-tree snapshot failed (non-fatal): %s", exc)
- return [], "", None
+ return [], "", None, []
nodes = tuple(getattr(obs, "nodes", ()) or ())
return (
clickable_labels(nodes, max_n=max_labels),
field_values_hint(nodes),
human_handoff_reason(nodes),
+ clickable_rects(nodes),
)