Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 216 additions & 0 deletions packages/flow/openadapt_flow/runtime/identity_vlm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,228 @@
from __future__ import annotations

import tempfile
from dataclasses import dataclass
from enum import Enum
from pathlib import Path
from typing import Any, Optional

from openadapt_flow.ir import Region
from openadapt_flow.runtime.grounder import component_may_egress
from openadapt_flow.runtime.remote_vlm import (
_MAX_MODEL_IMAGE_DIM,
IdentityVerdict,
extract_roi_crop,
gold_crop_for_model,
)

DEFAULT_MODEL = "mlx-community/Qwen3-VL-4B-Instruct-4bit"


class VetoVerdict(str, Enum):
"""Unified veto-only verdict for local and remote VLM guards.

* ``PASS`` -- confident affirmative match/state; fail-to-veto ONLY.
NEVER overrides a failed deterministic check.
* ``VETO`` -- explicit mismatch or violated state; halts execution.
* ``ABSTAIN`` -- uncertain, unreachable, timeout, exception, or malformed
output; halts execution.
"""

PASS = "pass"
VETO = "veto"
ABSTAIN = "abstain"


@dataclass(frozen=True)
class VetoDecision:
"""Outcome of a single veto-only VLM guard check."""

verdict: VetoVerdict
reason: str = ""
used_gold_crop: bool = False

@property
def should_halt(self) -> bool:
"""True for ``VETO`` and ``ABSTAIN``; False only for ``PASS``."""
return self.verdict is not VetoVerdict.PASS

def apply_to_deterministic(self, deterministic_passed: bool) -> bool:
"""Combine with a deterministic check under strict veto-only semantics.

``VETO`` / ``ABSTAIN`` always halt (return ``False``). ``PASS`` lets a
passing deterministic check stand, and NEVER overrides a failing
deterministic check.
"""
return bool(deterministic_passed) and not self.should_halt


class VetoOnlyVLMGuard:
"""Unified veto-only guard over local (MLX) or remote VLM backends.

Enforces two invariants across identity and state checks:

1. **Strict veto-only semantics**: ``VETO`` and ``ABSTAIN`` (including any
transport error, model exception, or unrecognized answer) halt; ``PASS``
only declines to veto and never overrides deterministic checks.
2. **GOLD ROI cropping**: when full-frame screenshots exceed ``max_dim``
(1024px) and a target/alert ``roi`` is provided, the guard extracts
native-resolution ROI crops (or GOLD global+local composites) within the
``max_dim`` budget instead of uniformly downscaling away small text.
"""

def __init__(
self,
*,
identity_vlm: Optional[Any] = None,
state_verifier: Optional[Any] = None,
max_dim: int = _MAX_MODEL_IMAGE_DIM,
) -> None:
self._identity_vlm = identity_vlm
self._state_verifier = state_verifier
self._max_dim = max_dim

@property
def MAY_EGRESS(self) -> bool:
"""Propagate screenshot-egress classification from wrapped backends."""
return component_may_egress(self._identity_vlm) or component_may_egress(
self._state_verifier
)

def check_identity(
self,
recorded_png: bytes,
live_png: bytes,
*,
roi: Optional[Region] = None,
) -> VetoDecision:
"""Run a veto-only identity comparison, applying symmetric ROI cropping if ``roi`` is set."""
if self._identity_vlm is None:
return VetoDecision(VetoVerdict.ABSTAIN, reason="no_identity_backend")

used_gold = False
rec_sent = recorded_png
live_sent = live_png
if roi is not None:
rec_crop = extract_roi_crop(recorded_png, roi, max_dim=self._max_dim)
live_crop = extract_roi_crop(live_png, roi, max_dim=self._max_dim)
if rec_crop is None or live_crop is None:
return VetoDecision(
VetoVerdict.ABSTAIN,
reason="identity_roi_unavailable",
used_gold_crop=False,
)
rec_sent = rec_crop
live_sent = live_crop
used_gold = True

try:
compare_fn = getattr(self._identity_vlm, "compare", None)
if callable(compare_fn):
raw = compare_fn(rec_sent, live_sent)
if raw is IdentityVerdict.VERIFY:
return VetoDecision(VetoVerdict.PASS, used_gold_crop=used_gold)
if raw is IdentityVerdict.MISMATCH:
return VetoDecision(
VetoVerdict.VETO,
reason="identity_mismatch",
used_gold_crop=used_gold,
)
return VetoDecision(
VetoVerdict.ABSTAIN,
reason="identity_abstain",
used_gold_crop=used_gold,
)

same_fn = getattr(self._identity_vlm, "same_or_different", None)
if callable(same_fn):
ans = same_fn(rec_sent, live_sent)
if ans == "same":
return VetoDecision(VetoVerdict.PASS, used_gold_crop=used_gold)
if ans == "different":
return VetoDecision(
VetoVerdict.VETO,
reason="identity_different",
used_gold_crop=used_gold,
)
return VetoDecision(
VetoVerdict.ABSTAIN,
reason="identity_unrecognized",
used_gold_crop=used_gold,
)
except Exception as exc:
return VetoDecision(
VetoVerdict.ABSTAIN,
reason=f"identity_error:{type(exc).__name__}",
used_gold_crop=used_gold,
)

return VetoDecision(
VetoVerdict.ABSTAIN,
reason="unsupported_identity_backend",
used_gold_crop=used_gold,
)

def check_state(
self,
screenshot: bytes,
expected_state: str,
*,
roi: Optional[Region] = None,
) -> VetoDecision:
"""Run a veto-only state check, applying GOLD cropping when ``roi`` is set."""
if self._state_verifier is None:
return VetoDecision(VetoVerdict.ABSTAIN, reason="no_state_backend")

used_gold = False
sent = screenshot
if roi is not None:
gold = gold_crop_for_model(screenshot, roi=roi, max_dim=self._max_dim)
if gold.used_gold:
sent = gold.composite_png
used_gold = True

try:
verify_fn = getattr(self._state_verifier, "verify", None)
if not callable(verify_fn):
return VetoDecision(
VetoVerdict.ABSTAIN,
reason="unsupported_state_backend",
used_gold_crop=used_gold,
)
raw = verify_fn(sent, expected_state)
except Exception as exc:
return VetoDecision(
VetoVerdict.ABSTAIN,
reason=f"state_error:{type(exc).__name__}",
used_gold_crop=used_gold,
)

if raw == "yes":
return VetoDecision(VetoVerdict.PASS, used_gold_crop=used_gold)
if raw == "no":
return VetoDecision(
VetoVerdict.VETO,
reason="state_not_held",
used_gold_crop=used_gold,
)
return VetoDecision(
VetoVerdict.ABSTAIN,
reason="state_uncertain",
used_gold_crop=used_gold,
)

def same_or_different(
self,
recorded_png: bytes,
live_png: bytes,
*,
roi: Optional[Region] = None,
) -> str:
"""Satisfy the ``IdentityVLM`` protocol: ``"same"`` iff ``PASS``, else ``"different"``."""
decision = self.check_identity(recorded_png, live_png, roi=roi)
return "same" if decision.verdict is VetoVerdict.PASS else "different"


class MLXIdentityVLM:
"""Local MLX VLM used as a veto-only same/different identity comparator.

Expand Down
Loading
Loading