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 09:14)

Ruben Lütke042ded8f19a7
diff --git a/jarvis/cu/brain_call.py b/jarvis/cu/brain_call.py
new file mode 100644
index 0000000000..1c371ef506
--- /dev/null
+++ b/jarvis/cu/brain_call.py
@@ -0,0 +1,166 @@
+"""Provider-agnostic vision-brain dispatch for Computer-Use v2.
+
+A lean re-implementation of the legacy engine's battle-tested dispatch
+behavior, without importing the monolith:
+
+* FakeBrain test shim (``manager.complete_text``) first.
+* Candidate order = ``BrainManager._build_fallback_chain("fast")`` with the
+ per-provider CU model override — never a provider name hardcode (AP-21/22).
+* Health/vision filtering via :class:`ComputerUsePlannerSelector` (skips
+ dead/cooldown/blind providers) plus the stale-dead-flag last resort.
+* The prompt is built PER CANDIDATE via a callback, because the coordinate
+ convention (0-1000 grid vs image pixels) depends on which provider the
+ call actually lands on — a mid-mission cross-family fallback must not be
+ parsed in the wrong space.
+
+Raises :class:`CUNoVisionProviderError` when the whole chain is exhausted —
+the engine maps it to exit 3 ("check your keys/credit"), keeping the honest
+credential readback the legacy engine had.
+"""
+from __future__ import annotations
+
+import logging
+from dataclasses import dataclass
+from typing import Any, Callable
+
+from jarvis.core.protocols import BrainMessage, BrainRequest, ImageBlock
+
+logger = logging.getLogger(__name__)
+
+
+class CUBrainCallError(RuntimeError):
+ """The model call failed structurally (no provider produced text)."""
+
+
+class CUNoVisionProviderError(CUBrainCallError):
+ """Every candidate was keyless/dead/blind — an account fact, exit 3."""
+
+
+@dataclass(frozen=True)
+class BrainReply:
+ """One successful model reply plus its provenance."""
+
+ text: str
+ provider: str
+ model: str | None
+
+
+#: Builds (system_prompt, user_message) for the provider the call lands on.
+PromptBuilder = Callable[[str, Any], tuple[str, str]]
+
+
+async def call_vision_brain(
+ manager: Any,
+ *,
+ build_prompt: PromptBuilder,
+ images: list[ImageBlock],
+ max_tokens: int = 256,
+ early_stop_json: bool = False,
+) -> BrainReply:
+ """Dispatch one screenshot-grounded call through the provider chain."""
+ # FakeBrain test shim: single-call managers used across the test suite.
+ complete_text = getattr(manager, "complete_text", None)
+ if complete_text is not None:
+ system, user = build_prompt("fake", manager)
+ result = await complete_text(system=system, user=user)
+ return BrainReply(text=str(result), provider="fake", model=None)
+
+ if not hasattr(manager, "_get_brain"):
+ raise CUBrainCallError(
+ "BrainManager exposes neither complete_text nor _get_brain — "
+ "CU v2 cannot dispatch",
+ )
+
+ from jarvis.brain.streaming import aggregate, aggregate_first_json # noqa: PLC0415
+ from jarvis.harness.computer_use_planner import ( # noqa: PLC0415
+ ComputerUsePlannerSelector,
+ iter_last_resort_vision,
+ )
+
+ agg = aggregate_first_json if early_stop_json else aggregate
+
+ chain: list[tuple[str, str | None]] = []
+ build_chain = getattr(manager, "_build_fallback_chain", None)
+ if callable(build_chain):
+ try:
+ chain = list(build_chain("fast") or [])
+ except Exception: # noqa: BLE001
+ logger.debug("[cu] fallback-chain build failed", exc_info=True)
+ if not chain and getattr(manager, "active_provider", None):
+ chain = [(manager.active_provider, None)]
+ if not chain:
+ raise CUBrainCallError("BrainManager fallback chain is empty")
+
+ # Per-provider CU model override (Settings can pin a dedicated CU model).
+ cu_model = getattr(manager, "_cu_model", None)
+ if callable(cu_model):
+ resolved_chain: list[tuple[str, str | None]] = []
+ for provider, model in chain:
+ try:
+ resolved_chain.append((provider, cu_model(provider) or model))
+ except Exception: # noqa: BLE001
+ resolved_chain.append((provider, model))
+ chain = resolved_chain
+
+ images_attached = bool(images)
+ selector = ComputerUsePlannerSelector(manager=manager, chain=chain)
+ attempted = 0
+
+ async def _try(provider: str, model: str | None, brain: Any) -> BrainReply | None:
+ system, user = build_prompt(provider, brain)
+ req = BrainRequest(
+ messages=(BrainMessage(
+ role="user", content=user, images=tuple(images),
+ ),),
+ system=system,
+ temperature=0.0,
+ max_tokens=max_tokens,
+ stream=True,
+ )
+ result = await agg(brain.complete(req))
+ text = (result.text or "").strip()
+ if not text:
+ selector.record_empty(provider, model)
+ return None
+ return BrainReply(text=text, provider=provider, model=model)
+
+ for idx, provider, model, brain in selector.iter_candidates(
+ images_attached=images_attached,
+ ):
+ attempted += 1
+ try:
+ reply = await _try(provider, model, brain)
+ except Exception as exc: # noqa: BLE001
+ selector.record_failure(provider, model, exc)
+ logger.warning("[cu] brain provider %s(%s) failed: %s", provider, model, exc)
+ continue
+ if reply is not None:
+ if idx > 0:
+ logger.info(
+ "[cu] fallback hit: %s(%s) after %d skipped provider(s)",
+ provider, model, idx,
+ )
+ return reply
+
+ # Stale-dead-flag resilience: retry every REGISTERED vision provider once.
+ if images_attached:
+ already = set(chain) | {(e.provider, e.model) for e in selector.errors}
+ for provider, model, brain in iter_last_resort_vision(
+ manager, already_tried=already,
+ ):
+ try:
+ reply = await _try(provider, model, brain)
+ except Exception as exc: # noqa: BLE001
+ selector.record_failure(provider, model, exc)
+ continue
+ if reply is not None:
+ logger.warning(
+ "[cu] LAST-RESORT vision brain %s(%s) reached — normal "
+ "chain had no vision provider (stale dead-flag?)",
+ provider, model,
+ )
+ return reply
+
+ raise CUNoVisionProviderError(
+ selector.error_message(images_attached=images_attached, attempted=attempted),
+ )
diff --git a/jarvis/cu/verify.py b/jarvis/cu/verify.py
new file mode 100644
index 0000000000..4e4381a7d5
--- /dev/null
+++ b/jarvis/cu/verify.py
@@ -0,0 +1,259 @@
+"""Pre/post verification for Computer-Use v2 actions.
+
+Ports the legacy engine's PROVEN deterministic read-back helpers (type
+read-back, focus confirmation, field-value hints, human-handoff detection —
+all pure accessibility-tree inspections, no LLM calls) and adds the v2
+effect-check primitives:
+
+* :func:`regions_equal` — raw pre/post pixel comparison around an action
+ point ("did anything near the click visibly react?").
+* :func:`screen_drifted` — has the screen visibly changed since the frame
+ the model decided on? If yes, acting on stale coordinates is a guess —
+ the loop re-perceives instead (pre-execution UI-state verification).
+
+CRITICAL sourcing rule (documented trap): UIA nodes/values come ONLY from
+the dedicated tree-source enumeration below — never from a screenshot-mode
+``observation.nodes``, which is always empty in the CU path.
+"""
+from __future__ import annotations
+
+import asyncio
+import logging
+from typing import Any
+
+logger = logging.getLogger(__name__)
+
+UIA_TIMEOUT_S = 3.0
+TYPE_VERIFY_MIN_CHARS = 3
+
+EDITABLE_UIA_ROLES = frozenset({"Edit", "Document", "ComboBox"})
+CLICKABLE_UIA_ROLES = frozenset({
+ "Button", "MenuItem", "ListItem", "TabItem", "CheckBox", "RadioButton",
+ "Hyperlink", "Edit", "ComboBox", "TreeItem", "SplitButton", "Text",
+})
+
+# Human-handoff cues: screens the USER must handle — the agent must never
+# type a secret it does not hold (AP-2).
+_CAPTCHA_CUES = (
+ "captcha", "recaptcha", "hcaptcha", "not a robot", "verify you are human",
+ "verify you're human", "are you human", "human verification",
+)
+_TWOFACTOR_CUES = (
+ "two-factor", "two factor", "2fa", "one-time code", "one time code",
+ "verification code", "security code", "authenticator", "otp",
+)
+_PASSWORD_FIELD_TOKENS = ("password", "passwort", "passcode") # noqa: S105 — field-label tokens, not a credential; "passwort" is German UI matching data (product surface bucket 3)
+
+# Cached UI tree source (constructing one per step pays setup cost for the
+# same foreground enumeration).
+_UI_TREE_SOURCE: Any = None
+
+
+def _get_ui_tree_source() -> Any:
+ global _UI_TREE_SOURCE
+ if _UI_TREE_SOURCE is None:
+ from jarvis.vision.tree_factory import make_ui_tree_source # noqa: PLC0415
+
+ _UI_TREE_SOURCE = make_ui_tree_source()
+ return _UI_TREE_SOURCE
+
+
+# ---------------------------------------------------------------------------
+# Pure node inspections (ported from the legacy engine, behavior-identical)
+# ---------------------------------------------------------------------------
+
+def normalize_for_value_match(text: str) -> str:
+ """Lowercase, trim, strip URL scheme + ``www.`` so ``https://example.com``
+ matches a goal phrased ``go to example.com``."""
+ s = (text or "").strip().casefold()
+ for scheme in ("https://", "http://"):
+ if s.startswith(scheme):
+ s = s[len(scheme):]
+ break
+ if s.startswith("www."):
+ s = s[len("www."):]
+ return s.strip("/").strip()
+
+
+def typed_text_landed(nodes: tuple, typed: str) -> bool | None:
+ """Tri-state type read-back (conservative):
+
+ ``True`` — an editable field's value contains the typed text.
+ ``False`` — editable fields ARE readable and none holds it (confirmed miss).
+ ``None`` — nothing editable readable / text too short (cannot tell).
+ """
+ t = normalize_for_value_match(typed)
+ if len(t) < TYPE_VERIFY_MIN_CHARS:
+ return None
+ editable_seen = False
+ for node in nodes or ():
+ if getattr(node, "role", "") not in EDITABLE_UIA_ROLES:
+ continue
+ editable_seen = True
+ val = normalize_for_value_match(getattr(node, "value", "") or "")
+ if t in val:
+ return True
+ return False if editable_seen else None
+
+
+def element_is_focused(nodes: tuple, name: str) -> bool | None:
+ """Positive-only focus confirmation after ``click_element`` (``True`` or
+ ``None`` — a button legitimately may not retain focus, so absence never
+ flips a click to a false failure)."""
+ target = (name or "").strip().lower()
+ if len(target) < 2:
+ return None
+ for node in nodes or ():
+ if not getattr(node, "focused", False):
+ continue
+ nm = (getattr(node, "name", "") or "").strip().lower()
+ if nm and (nm == target or target in nm or nm in target):
+ return True
+ return None
+
+
+def field_values_hint(nodes: tuple) -> str:
+ """Model-facing hint listing editable controls that already hold text
+ (drives correct ``clear_first`` decisions). Values ride only in the model
+ message — never a log line or TTS (AP-2)."""
+ lines: list[str] = []
+ for node in nodes or ():
+ if getattr(node, "role", "") not in EDITABLE_UIA_ROLES:
+ continue
+ val = (getattr(node, "value", "") or "").strip()
+ if not val:
+ continue
+ nm = (getattr(node, "name", "") or "").strip()
+ lines.append(f'"{nm or "field"}" currently contains "{val[:120]}"')
+ if len(lines) >= 8:
+ break
+ if not lines:
+ return ""
+ return (
+ "\n\nFIELD CONTENTS (a field already holding text — to REPLACE it, "
+ "set clear_first on the type action):\n" + "\n".join(lines)
+ )
+
+
+def human_handoff_reason(nodes: tuple) -> str | None:
+ """Detect a screen the human must handle (CAPTCHA > 2FA > password entry).
+
+ Inspects only node names/roles + the secure-edit flag, never a field
+ VALUE (AP-2). Conservative: a bare "password" word is not enough — a
+ real password EDIT field or an explicit CAPTCHA/2FA phrase is required.
+ """
+ captcha = twofactor = has_password_field = False
+ for node in nodes or ():
+ if getattr(node, "is_password", False):
+ has_password_field = True
+ nm = (getattr(node, "name", "") or "").strip().lower()
+ if nm:
+ if any(c in nm for c in _CAPTCHA_CUES):
+ captcha = True
+ if any(c in nm for c in _TWOFACTOR_CUES):
+ twofactor = True
+ if getattr(node, "role", "") in EDITABLE_UIA_ROLES and any(
+ t in nm for t in _PASSWORD_FIELD_TOKENS
+ ):
+ has_password_field = True
+ if captcha:
+ return "captcha challenge"
+ if twofactor:
+ return "two-factor / one-time code"
+ if has_password_field:
+ return "login / password entry"
+ return None
+
+
+def clickable_labels(nodes: tuple, max_n: int = 28) -> list[str]:
+ """Foreground clickable control names for the CLICKABLE ELEMENTS hint."""
+ names: list[str] = []
+ for node in nodes or ():
+ if not getattr(node, "enabled", True):
+ continue
+ if getattr(node, "role", "") not in CLICKABLE_UIA_ROLES:
+ continue
+ nm = (getattr(node, "name", "") or "").strip()
+ if nm and len(nm) <= 40 and nm not in names:
+ names.append(nm)
+ if len(names) >= max_n:
+ break
+ return names
+
+
+# ---------------------------------------------------------------------------
+# 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]:
+ """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."""
+ 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
+ nodes = tuple(getattr(obs, "nodes", ()) or ())
+ return (
+ clickable_labels(nodes, max_n=max_labels),
+ field_values_hint(nodes),
+ human_handoff_reason(nodes),
+ )
+
+
+async def verify_typed_text(typed: str) -> bool | None:
+ """Fresh tree → :func:`typed_text_landed`. Any error → ``None``."""
+ try:
+ obs = await asyncio.wait_for(
+ _get_ui_tree_source().observe(), timeout=UIA_TIMEOUT_S,
+ )
+ except Exception: # noqa: BLE001
+ return None
+ return typed_text_landed(tuple(getattr(obs, "nodes", ()) or ()), typed)
+
+
+async def verify_click_focus(name: str) -> bool | None:
+ """Fresh tree → :func:`element_is_focused`. Any error → ``None``."""
+ try:
+ obs = await asyncio.wait_for(
+ _get_ui_tree_source().observe(), timeout=UIA_TIMEOUT_S,
+ )
+ except Exception: # noqa: BLE001
+ return None
+ return element_is_focused(tuple(getattr(obs, "nodes", ()) or ()), name)
+
+
+# ---------------------------------------------------------------------------
+# Pixel-effect primitives
+# ---------------------------------------------------------------------------
+
+def regions_equal(
+ a: tuple[tuple[int, int], bytes] | None,
+ b: tuple[tuple[int, int], bytes] | None,
+) -> bool | None:
+ """Raw pre/post region comparison. ``None`` when either grab failed
+ (cannot tell) — never turns a missing grab into a false miss."""
+ if a is None or b is None:
+ return None
+ return a[0] == b[0] and a[1] == b[1]
+
+
+def screen_drifted(
+ reference: tuple[tuple[int, int], bytes] | None,
+ current: tuple[tuple[int, int], bytes] | None,
+) -> bool:
+ """Has the screen visibly changed between two raw grabs? Used as the
+ pre-execution check: acting on coordinates from a frame the screen has
+ since departed from is a guess. Unknown (failed grab) counts as NOT
+ drifted — refusing to act on an unreadable-but-fine screen would stall
+ missions on hosts where region grabs are flaky."""
+ if reference is None or current is None:
+ return False
+ from jarvis.cu.capture import frames_differ # noqa: PLC0415
+
+ return frames_differ(reference, current)