diff --git a/.github/workflows/packages.yml b/.github/workflows/packages.yml index 894b76c392..38c18ac069 100644 --- a/.github/workflows/packages.yml +++ b/.github/workflows/packages.yml @@ -596,6 +596,7 @@ jobs: openadapt_flow/runtime/identity_template.py \ openadapt_flow/runtime/identity_vlm.py \ openadapt_flow/runtime/replayer.py \ + openadapt_flow/runtime/replay/ \ openadapt_flow/runtime/resolver.py \ openadapt_flow/submit_binding.py \ openadapt_flow/runtime/effects/effect.py \ diff --git a/packages/flow/docs/design/DECOMPOSITION.md b/packages/flow/docs/design/DECOMPOSITION.md index 299218b7ff..167c5992ec 100644 --- a/packages/flow/docs/design/DECOMPOSITION.md +++ b/packages/flow/docs/design/DECOMPOSITION.md @@ -605,7 +605,7 @@ PR #2295 raised `replayer.py`, `ir.py`, `interactive_recorder.py` and `playwrigh | File | Line budget | |---|---:| -| `packages/flow/openadapt_flow/runtime/replayer.py` | 20090 | +| `packages/flow/openadapt_flow/runtime/replayer.py` | 19325 | | `packages/flow/openadapt_flow/__main__.py` | 14331 | | `packages/flow/openadapt_flow/qualification.py` | 5963 | | `packages/flow/openadapt_flow/ir.py` | 6830 | diff --git a/packages/flow/openadapt_flow/runtime/replay/__init__.py b/packages/flow/openadapt_flow/runtime/replay/__init__.py new file mode 100644 index 0000000000..1ab40643a3 --- /dev/null +++ b/packages/flow/openadapt_flow/runtime/replay/__init__.py @@ -0,0 +1,5 @@ +"""Concerns of the replayer, moved out of ``runtime/replayer.py`` unchanged. + +See openadapt-internal ``docs/design/monorepo-refactor-2026-11/PLAN.md``, +track E. ``openadapt_flow.runtime.replayer`` re-exports every moved name. +""" diff --git a/packages/flow/openadapt_flow/runtime/replay/_shared.py b/packages/flow/openadapt_flow/runtime/replay/_shared.py new file mode 100644 index 0000000000..8d5de5c6bb --- /dev/null +++ b/packages/flow/openadapt_flow/runtime/replay/_shared.py @@ -0,0 +1,543 @@ +"""Module-level helpers of the replayer: constants, small types and functions. + +They moved here unchanged from ``openadapt_flow/runtime/replayer.py``, which +re-exports every name, so existing imports and patches of +``openadapt_flow.runtime.replayer.`` keep working for code that stayed +in that module. +""" + +from __future__ import annotations + +import re +import struct +from dataclasses import dataclass +from typing import ( + TYPE_CHECKING, + Any, + Mapping, + Optional, + TypeVar, +) + +from openadapt_flow.ir import ( + ActionKind, + LoopIterationBinding, + Region, + RunReport, + StateKind, + Step, + Workflow, +) +from openadapt_flow.network_scope import EndpointScope, endpoint_scope +from openadapt_flow.runtime.authorization import ( + RuntimeParamScalar, +) +from openadapt_flow.runtime.dialog_delivery import ( + _DIALOG_WAIT_S as _DIALOG_WAIT_S, +) +from openadapt_flow.runtime.durable.program_checkpoint import ( + GraphFrame, + ProgramCheckpoint, +) +from openadapt_flow.runtime.row_dates import _ROW_DATE_PATTERNS, _row_dates # noqa: F401 +from openadapt_flow.secret_inputs import ( # noqa: F401 - compatibility exports + SECRET_ENV_PREFIX, + require_secret_sources, + secret_env_var, + secret_env_vars, +) + +if TYPE_CHECKING: + pass + + +_DeliveryResultT = TypeVar("_DeliveryResultT") +_MAX_FRESH_ACTUATION_REACQUISITIONS = 2 +# Check-to-click guard on the pixel path (``Replayer._pixel_recheck``): how +# often a changed target or identity region is re-observed before the click +# is refused, and how much a pixel must change to count (remote encoders +# shift colours by a few levels; a dialog or a new row changes far more). +_PIXEL_RECHECK_REOBSERVES = 2 +# How far a remote codec moved the same hovered target between frames on real +# AVC444 sessions (benchmark/rdp_avc_real: one pixel on each axis). +_REMOTE_HOVER_JITTER_PX = 2 +_PIXEL_RECHECK_LEVEL = 24 +_PIXEL_RECHECK_MIN_PIXELS = 4 + +# How long past its step's timeout a held record check may run under an +# approval mode before the held report decides the item (a person). +READBACK_DEADLINE_MARGIN_S = 120.0 +# Private module capability. The public ``run`` API cannot mint a durable +# continuation. ``runtime.durable.resume`` imports this object only after it +# has validated the pause, approval, bundle, checkpoint, and live state. +_DURABLE_RESUME_AUTHORITY = object() + + +# REGION_STABLE template check: how far the expected content may shift from +# the recorded region (real apps re-layout by a few pixels between runs), +# and the minimum template-match score to accept it. +PC_TEMPLATE_SEARCH_PAD = 80 + + +_FIELD_CHECK_LIMIT = 32 +_FIELD_VALUE_LIMIT = 256 + + +def _field_text(value: Any) -> Optional[str]: + if value is None: + return None + if isinstance(value, bool): + return "true" if value else "false" + if isinstance(value, (str, int, float)): + return str(value)[:_FIELD_VALUE_LIMIT] + return None + + +def effect_field_checks(effect: Any, verdict: Any, tier: Any) -> list[Any]: + """The field comparisons one effect verdict made, for the local report. + + A ``field_equals`` effect compares one field. A record effect compares + each literal ``match`` value against the first matched record. Values + that aren't plain text or numbers are left out rather than guessed. + """ + + from openadapt_flow.ir import EffectFieldCheck + + tier_value = int(tier) if tier is not None else None + checks: list[EffectFieldCheck] = [] + field = getattr(effect, "field", None) + expected_value = getattr(verdict, "expected_value", None) + if field and expected_value is not None: + checks.append( + EffectFieldCheck( + field=str(field)[:_FIELD_VALUE_LIMIT], + expected=_field_text(expected_value), + observed=_field_text(getattr(verdict, "observed_value", None)), + matched=bool(getattr(verdict, "confirmed", False)), + verification_tier=tier_value, + ) + ) + return checks + records = getattr(verdict, "matched_records", None) or [] + record = records[0] if records and isinstance(records[0], dict) else None + for name, want in (getattr(effect, "match", None) or {}).items(): + expected = _field_text(want) + if expected is None: + continue + observed = _field_text(record.get(name)) if record is not None else None + checks.append( + EffectFieldCheck( + field=str(name)[:_FIELD_VALUE_LIMIT], + expected=expected, + observed=observed, + matched=observed == expected, + verification_tier=tier_value, + ) + ) + if len(checks) >= _FIELD_CHECK_LIMIT: + break + return checks + + +#: A uniform page scale is inferred only from a click point at least this far +#: from both edges at the origin, only when both axes agree to within this +#: fraction, and only inside these bounds (and not within 5% of 1). +_PAGE_SCALE_MIN_COORD = 40 +_PAGE_SCALE_AXIS_AGREEMENT = 0.02 +_PAGE_SCALE_BOUNDS = (0.5, 2.0) +#: One pixel of click-point noise moves the estimate by a few tenths of a +#: percent, enough to drop a wide template below threshold; the check tries +#: this tight band around it. +_PAGE_SCALE_BAND = (0.99, 0.995, 1.0, 1.005, 1.01) + + +def _uniform_page_scale(step: Any, result: Any) -> Optional[float]: + """The page scale this step's click showed, or ``None``. + + The recorded click point and the point the ladder resolved this run, + divided axis by axis. A whole-page zoom multiplies both coordinates by + the same factor; a control that merely moved almost never does, so the + two axes must agree. + """ + + resolution = getattr(result, "resolution", None) + anchor = getattr(step, "anchor", None) + if resolution is None or anchor is None: + return None + try: + rx, ry = (float(v) for v in anchor.click_point) + lx, ly = (float(v) for v in resolution.point) + except (TypeError, ValueError): + return None + if min(rx, ry) < _PAGE_SCALE_MIN_COORD: + return None + sx, sy = lx / rx, ly / ry + if abs(sx - sy) > _PAGE_SCALE_AXIS_AGREEMENT * max(sx, sy): + return None + scale = (sx + sy) / 2 + low, high = _PAGE_SCALE_BOUNDS + if not low <= scale <= high or abs(scale - 1.0) < 0.05: + return None + return round(scale, 3) + + +def _frame_viewport(frame_png: bytes) -> tuple[int, int]: + """Return the viewport of THIS frame, read from the frame itself. + + ``self.backend.viewport`` is a live read. Pairing it with an + already-captured frame lets a resize or move between capture and use + reinterpret that frame under geometry it never had, which can place a + coordinate where the operator never demonstrated one. A PNG carries its own + dimensions in the IHDR chunk, so the frame can always answer for itself. + """ + + if len(frame_png) < 24 or not frame_png.startswith(b"\x89PNG\r\n\x1a\n"): + raise ValueError("frame viewport requires valid PNG bytes") + width, height = struct.unpack(">II", frame_png[16:24]) + if width <= 0 or height <= 0: + raise ValueError("frame viewport must be positive") + return int(width), int(height) + + +PC_TEMPLATE_THRESHOLD = 0.9 +# REGION_STABLE asserts recorded structure, not palette. The ordinary +# grayscale template matcher remains the first (stricter) check; this edge-map +# threshold is a color-invariant fallback. Exact v1.16.1 MockMed evidence +# measured correct theme+parameter substitution at 0.86 and a true blocking +# modal at 0.24, leaving a deliberate margin on both sides. +PC_STRUCTURAL_TEMPLATE_THRESHOLD = 0.8 + +# Typed-input verification: height of the visual readback band around the +# focusing click after TYPE. Without structural field bounds, read the full +# viewport width: a centered crop can hide the value's beginning or suffix. +# With no focusing click (e.g. Tab between fields), use the whole frame. +FIELD_REGION_HEIGHT = 240 +MASKED_FIELD_REGION_WIDTH = 640 +FIELD_REGION_PAD = 16 + +# Typed-input verification, secret-only diff acceptance: when a SECRET typed +# value is OCR-able but OCR cannot find it, a pixel change alone is accepted +# only if the field region gained no other READABLE text — masked fields render +# dots, which OCR reads as nothing, low-confidence noise, or punctuation +# runs, while a dialog rendering over the region adds confident words. +# "Readable" therefore counts alphanumeric characters of lines OCR is +# confident about (>= MASKED_MIN_CONFIDENCE), EXCLUDING homogeneous glyph +# runs: a dot row can misread as a confident digit run (measured on the +# Linux renderer: 17 bullets -> '0000000000006' at 0.81 confidence), and +# such runs are >= MASKED_REPEAT_FRACTION one repeated character — +# no real dialog sentence is. Alnum counts are also invariant to +# segmentation differences between frames. The gain may not exceed +# MASKED_NEW_TEXT_SLACK. +MASKED_NEW_TEXT_SLACK = 3 +MASKED_MIN_CONFIDENCE = 0.6 +MASKED_REPEAT_FRACTION = 0.66 + +# Closed-loop scroll: a SCROLL step keeps scrolling by its recorded delta +# until the NEXT anchored step's anchor resolves on a settled frame, bounded +# by this multiple of the step's own recorded scroll distance. Consecutive +# SCROLL steps hand the loop to each other (each probes first and no-ops +# once the anchor is in view), so a run of N recorded scrolls has a combined +# budget of ~2.5x the total recorded distance. +SCROLL_BUDGET_FACTOR = 2.5 + +# Bounds for the Phase-2 program interpreter -- deterministic termination +# guarantees. A worklist loop is bounded per-loop (LoopSpec.max_iterations); the +# graph walk itself is bounded by a total step budget (so an authored cycle of +# always-true transitions can never spin forever) and subflow/loop nesting is +# depth-bounded. +PROGRAM_MAX_STEPS = 100_000 +PROGRAM_MAX_DEPTH = 64 + + +class _GraphStepContext: + """Per-action-state context the program interpreter feeds ``_run_step``. + + Supplies the two pieces the linear loop derives from list position but a + graph lacks: the previously EXECUTED action (for the click-to-focus TYPE + heuristic) and the SCROLL closed-loop successors (the next anchored action + state and the immediately following state). All optional; a None field means + "no such successor" (the scroll loop then falls back to its open-loop + gesture, exactly as at the end of a linear workflow). + """ + + __slots__ = ( + "prev_action", + "next_anchored", + "following", + "has_exception_handler", + ) + + def __init__( + self, + prev_action: Optional[ActionKind] = None, + next_anchored: Optional[Step] = None, + following: Optional[Step] = None, + has_exception_handler: bool = False, + ) -> None: + self.prev_action = prev_action + self.next_anchored = next_anchored + self.following = following + self.has_exception_handler = has_exception_handler + + +class _ProgramHalt(Exception): + """Internal signal: an unrecoverable state was reached (an unhandled action + failure, a dead-end with no matching transition, a bound exceeded, or a + ``halt`` / ``escalate`` terminal). Carries the outcome + reason the run + report records. Caught only by ``_interpret_program`` -- never escapes the + Replayer.""" + + def __init__(self, outcome: str, reason: str, *, safety: bool = False) -> None: + super().__init__(reason) + self.outcome = outcome + self.reason = reason + self.safety = safety + # Populated at an ACTION-state failure before graph frames unwind. + # Empty means the halt is not eligible for a generic attended + # Continue/Skip transition. + self.program_frames: list[GraphFrame] = [] + self.program_params: dict[str, RuntimeParamScalar] = {} + self.program_history_hash: str = "" + + +def effect_endpoint_scope(verifier: Any) -> EndpointScope: + """Where an effect verifier reads its system of record. + + A verifier that names a ``base_url`` (REST, FHIR) is ``loopback`` when that + URL is on this computer and ``external`` otherwise. A verifier without one + is ``unknown``, which keeps a network substrate counted as egress. + """ + base_url = getattr(verifier, "base_url", None) + return endpoint_scope(base_url) if isinstance(base_url, str) else "unknown" + + +def _record_loop_iteration( + report: RunReport, + loop_state_id: str, + row_index: int, + row: Mapping[str, RuntimeParamScalar], +) -> None: + """Record the row values one loop iteration binds, before it runs. + + ``report.params`` is the run's base scope. A row field overrides it for + that iteration, so the audit report needs each row's own values to state + what the run typed. + """ + report.loop_iterations.append( + LoopIterationBinding( + loop_state_id=loop_state_id, + row_index=row_index, + params=dict(row), + ) + ) + + +def _loop_iterations_from_checkpoints( + checkpoints: list[ProgramCheckpoint], +) -> list[LoopIterationBinding]: + """Rebuild the loop rows that earlier legs of a resumed run bound. + + A resumed report carries the earlier legs' results, so it also needs the + row values those results typed. Each checkpoint's frame stack names the + loop row it ran in. A row is listed once, at its first checkpoint, keyed by + its position in the frame stack so a nested loop's rows under different + outer rows stay distinct. + """ + seen: set[tuple[tuple[str, str, int], ...]] = set() + bindings: list[LoopIterationBinding] = [] + for checkpoint in checkpoints: + path: tuple[tuple[str, str, int], ...] = () + for frame in checkpoint.frames: + cursor = frame.loop + if cursor is None: + path = (*path, (frame.graph_id, "", -1)) + continue + path = (*path, (frame.graph_id, cursor.loop_state_id, cursor.row_index)) + if path in seen or not 0 <= cursor.row_index < len(cursor.rows): + continue + seen.add(path) + bindings.append( + LoopIterationBinding( + loop_state_id=cursor.loop_state_id, + row_index=cursor.row_index, + params=dict(cursor.rows[cursor.row_index]), + ) + ) + return bindings + + +def _all_workflow_steps(workflow: Workflow): + """Yield every ``Step`` in a workflow for whole-bundle audits. + + Linear bundle: the ``steps`` list. Program bundle: every ``action`` state's + step across the top-level program graph AND all subflows (a loop body's + steps included). Order is stable but not execution order -- used only for + static coverage counting, not for running. + """ + if workflow.program is None: + yield from workflow.steps + return + graphs = [workflow.program, *workflow.subflows.values()] + for graph in graphs: + for state in graph.states.values(): + if state.kind is StateKind.ACTION and state.step is not None: + yield state.step + + +class EgressNotPermitted(RuntimeError): + """Raised when an egress-capable model component (a grounder / identity-VLM + / state-verifier that could send a screenshot off the box) is wired without + the operator's explicit ``allow_model_grounding`` opt-in (PHI audit REM-3). + """ + + +#: How far past its demonstrated position (as a fraction of the frame's extent +#: along the scroll axis) a scroll target may still be before the closed-loop +#: scroll counts it as ready. +SCROLL_POSITION_SLACK_FRACTION = 0.25 + + +#: Rungs that show the target itself on screen: what closed-loop scroll +#: readiness accepts, and all a step accepts after its scroll missed it. +class _NotActionableError(str): + """The error ``_resolve_step`` returns when the structural rung found + exactly one candidate but it is covered or off-viewport. + + A ``str`` so it travels as any resolution error; its type marks it. The + step polls on it like any miss, and halts with ``target_not_actionable`` + when it times out or when the fresh re-resolution before input finds it. + """ + + +_DIRECT_TARGET_RUNGS = frozenset({"structural", "template", "template_global", "ocr"}) + +#: Steps that own the halt when the closed-loop scroll before them never found +#: their target (see ``Replayer._act_scroll``). +_SCROLL_HANDOFF_ACTIONS = frozenset( + {ActionKind.CLICK, ActionKind.DOUBLE_CLICK, ActionKind.RIGHT_CLICK} +) + + +def _scroll_target_positioned( + recorded: tuple[int, int], + resolved: tuple[int, int], + scroll_delta: tuple[int, int], + frame_png: bytes, +) -> bool: + """Is the resolved target no further along the scroll than recorded?""" + + dx, dy = scroll_delta + w, h = _frame_viewport(frame_png) + if dy: + slack = SCROLL_POSITION_SLACK_FRACTION * h + ahead = (resolved[1] - recorded[1]) if dy > 0 else (recorded[1] - resolved[1]) + if ahead > slack: + return False + if dx: + slack = SCROLL_POSITION_SLACK_FRACTION * w + ahead = (resolved[0] - recorded[0]) if dx > 0 else (recorded[0] - resolved[0]) + if ahead > slack: + return False + return True + + +def _identity_tokens(text: Any) -> set[str]: + return {t for t in re.split(r"[^0-9a-z]+", str(text or "").lower()) if t} + + +def identity_read_contradicts(check: Any) -> bool: + """A ``mismatch`` read that saw text the expected identity doesn't have. + + "mismatch" also covers a read that is merely incomplete (columns cut + off). Only a read that shows a token the expected record lacks (another + MRN, a different spelling or date) is evidence of a different record. + """ + + if getattr(check, "status", None) != "mismatch": + return False + expected = _identity_tokens(getattr(check, "expected", None)) + observed = _identity_tokens(getattr(check, "observed", None)) + if not expected or not observed: + return True # nothing to compare: keep the refusal + return not observed <= expected + + +def better_identity_read(first: Any, retry: Any) -> Any: + """The identity read to keep from a first read and its upscaled re-read. + + A read that contradicts the expected record (:func:`identity_read_contradicts`) + is final: a second read of the same screen must never turn it into + ``verified``. A mismatch that is only incomplete can be rescued by a + re-read that verifies. Otherwise ``verified`` beats the "cannot certify" + outcomes, then a mismatch, then ``abstain``, then a blank ``unreadable``; + a tie goes to the read that covered more of the expected identity. + """ + + if identity_read_contradicts(first): + return first + if identity_read_contradicts(retry): + return retry + rank = {"unreadable": 0, "abstain": 1, "mismatch": 2, "verified": 3} + if (rank[retry.status], retry.coverage) > (rank[first.status], first.coverage): + return retry + return first + + +#: Server answers kept per step (a save makes one or a few write requests). +_MAX_SERVER_RESPONSES = 64 + + +@dataclass(frozen=True) +class _WebFormApprovalView: + """An API step's prepared request, as a person is asked to approve it.""" + + prepared: Any + #: :func:`~openadapt_flow.runtime.actuators.web_form.operation_digest`. + digest: str + rows: list[tuple[str, str]] + png: bytes + ref: Any + + +# The pixel census's OCR tie-break: a place the pixels do not rule out is +# distinguishable only when OCR reads both crops with every line at or above +# this confidence, neither text contains the other, and the two texts are less +# similar than this ratio. One label read two ways measured 0.82-0.95 +# ("Smith, John" / "Smith, S John" / "Smith, 10 lohn"); sibling tab labels +# ("Contact" / "Stats") measure 0.5 or less. +PIXEL_CENSUS_OCR_MIN_CONFIDENCE = 0.8 +PIXEL_CENSUS_OCR_MAX_SIMILARITY = 0.6 + + +#: How long after a create-new form's save the replayer keeps watching its +#: submit guard before the next step (a script may submit after the click). +_FORM_GUARD_SETTLE_S = 2.0 + + +def _within_hover_jitter( + fresh: tuple[int, int], + prepared: tuple[int, int], + fresh_region: Optional[Region], +) -> bool: + """Whether a re-resolved point may be the prepared one moved by codec jitter. + + Both points must lie inside the fresh match's own box and differ by at + most :data:`_REMOTE_HOVER_JITTER_PX` on each axis. With no box there is + nothing to bind the prepared point to. A True answer only permits one + re-preparation at the fresh point; the click still needs an exact match. + """ + if fresh_region is None: + return False + x, y, w, h = fresh_region + if w <= 0 or h <= 0: + return False + + def inside(point: tuple[int, int]) -> bool: + return x <= point[0] < x + w and y <= point[1] < y + h + + return ( + abs(int(fresh[0]) - int(prepared[0])) <= _REMOTE_HOVER_JITTER_PX + and abs(int(fresh[1]) - int(prepared[1])) <= _REMOTE_HOVER_JITTER_PX + and inside(fresh) + and inside(prepared) + ) diff --git a/packages/flow/openadapt_flow/runtime/replay/qualification_faults.py b/packages/flow/openadapt_flow/runtime/replay/qualification_faults.py new file mode 100644 index 0000000000..869f08edf9 --- /dev/null +++ b/packages/flow/openadapt_flow/runtime/replay/qualification_faults.py @@ -0,0 +1,405 @@ +# mypy: disable-error-code="empty-body" +"""Moved replayer code. + +Moved verbatim from ``openadapt_flow.runtime.replayer.Replayer`` by +scripts/extract_mixin.py (docs/design/DECOMPOSITION.md, section 4.1). +``_QualificationFaultInjectionHost`` declares, for mypy only, what this code reads from the +Replayer it is mixed into. +""" + +from __future__ import annotations + +import hashlib +from typing import TYPE_CHECKING, Any, cast # noqa: F401 + +from openadapt_flow.backend import StructuralResolutionRefused +from openadapt_flow.ir import ObservedSurfaceEnvironment +from openadapt_flow.qualification_faults import ( + QualificationFaultContext, + QualificationFaultGate, + QualificationFaultKind, + sha256_bytes, + verify_fault_mutation_receipt, +) + +if TYPE_CHECKING: + from typing import Callable, Literal, Optional + + from openadapt_flow.backend import Backend + from openadapt_flow.ir import ExecutionTargetKind, RunReport, Step, Workflow + from openadapt_flow.qualification_environment import ( + QualificationEnvironmentObservation, + ) + from openadapt_flow.qualification_faults import ( + FaultMutationReceipt, + QualificationFaultDriver, + ) + from openadapt_flow.runtime.authorization import GovernedRunAuthorization + from openadapt_flow.runtime.surfaces import SurfaceRouter + + +class _QualificationFaultInjectionHost: + def _bind_qualification_boundary( + self, boundary: Any, *, surface_scoped: bool = False + ) -> tuple[ + Optional[str], + Optional[QualificationEnvironmentObservation], + Optional[tuple[str, str]], + ]: ... + + _qualification_case_action_paths: dict[str, Literal["gui", "api"]] + _qualification_environment_bound: Optional[QualificationEnvironmentObservation] + _qualification_environment_call: Optional[ + Callable[[], QualificationEnvironmentObservation] + ] + _qualification_fault_mutations: list[FaultMutationReceipt] + _qualification_fault_public_key: Optional[str] + _run_id: str + _surface_environment_observations: dict[str, ObservedSurfaceEnvironment] + _surface_router: Optional[SurfaceRouter] + backend: Backend + governed_authorization: Optional[GovernedRunAuthorization] + qualification_fault_driver: Optional[QualificationFaultDriver] + vision: Any + + +if TYPE_CHECKING: + _Base = _QualificationFaultInjectionHost +else: + _Base = object + + +class QualificationFaultInjection(_Base): + """Moved replayer code.""" + + @staticmethod + def _qualification_fault_gate_for_kind( + kind: QualificationFaultKind, + ) -> QualificationFaultGate: + gates: dict[QualificationFaultKind, QualificationFaultGate] = { + "ambiguity": "target_resolution", + "wrong_identity": "identity_verification", + "stale_identity": "actuation_revalidation", + "weak_effect": "effect_strength", + "missing_effect": "effect_verifier", + } + return gates[kind] + + def _qualification_fault_context( + self, + *, + gate: QualificationFaultGate, + step: Step, + actuation_path: Literal["gui", "api"], + before_input_sha256: str, + effect_verifier: Any, + effects: tuple[Any, ...] = (), + capture_frame: Any = None, + ) -> Optional[QualificationFaultContext]: + authorization = self.governed_authorization + if authorization is None or authorization.qualification_case_kind in { + None, + "representative", + }: + return None + kind = cast(QualificationFaultKind, authorization.qualification_case_kind) + if self._qualification_fault_gate_for_kind(kind) != gate: + return None + if authorization.qualification_fault_step_id_sha256 != sha256_bytes( + step.id.encode("utf-8") + ): + return None + if self._qualification_case_action_paths.get(step.id) != actuation_path: + return None + required = ( + authorization.qualification_project_id, + authorization.qualification_project_revision, + authorization.qualification_project_contract_sha256, + authorization.qualification_campaign_id_sha256, + self._qualification_id_sha256(authorization.qualification_case_id), + authorization.qualification_case_input_sha256, + authorization.qualification_run_id_sha256, + ) + if any(value is None for value in required): + raise RuntimeError("qualification_fault_binding_incomplete") + return QualificationFaultContext( + project_id=cast(str, required[0]), + project_revision=cast(int, required[1]), + project_contract_sha256=cast(str, required[2]), + campaign_id_sha256=cast(str, required[3]), + case_id_sha256=cast(str, required[4]), + case_input_sha256=cast(str, required[5]), + run_id_sha256=cast(str, required[6]), + step_id=step.id, + actuation_path=actuation_path, + fault_kind=kind, + gate=gate, + before_input_sha256=before_input_sha256, + backend=self.backend, + vision=self.vision, + effect_verifier=effect_verifier, + effects=effects, + capture_frame=capture_frame, + surface_id=( + self._surface_router.active_id + if self._surface_router is not None + else None + ), + ) + + def _require_current_fault_driver_binding(self) -> QualificationFaultDriver: + """Snapshot the configured driver identity immediately before mutation.""" + + driver = self.qualification_fault_driver + authorization = self.governed_authorization + if driver is None or authorization is None: + raise RuntimeError("qualification_fault_driver_missing") + try: + observed = ( + driver.driver_id, + driver.contract_sha256, + driver.attestation_key_id, + ) + except Exception as exc: + raise RuntimeError( + "qualification_fault_driver_identity_unavailable" + ) from exc + expected = ( + authorization.qualification_fault_driver_id, + authorization.qualification_fault_driver_contract_sha256, + authorization.qualification_fault_driver_key_id, + ) + if observed != expected: + raise RuntimeError("qualification_fault_driver_binding_changed") + return driver + + def _validate_fault_mutation( + self, + context: QualificationFaultContext, + receipt: FaultMutationReceipt, + *, + after_input_sha256: str, + ) -> None: + authorization = self.governed_authorization + if authorization is None: + raise RuntimeError("qualification_fault_authorization_missing") + expected = { + "project_id": context.project_id, + "project_revision": context.project_revision, + "project_contract_sha256": context.project_contract_sha256, + "campaign_id_sha256": context.campaign_id_sha256, + "case_id_sha256": context.case_id_sha256, + "case_input_sha256": context.case_input_sha256, + "run_id_sha256": context.run_id_sha256, + "step_id_sha256": sha256_bytes(context.step_id.encode("utf-8")), + "actuation_path": context.actuation_path, + "fault_kind": context.fault_kind, + "gate": context.gate, + "driver_id": authorization.qualification_fault_driver_id, + "driver_contract_sha256": ( + authorization.qualification_fault_driver_contract_sha256 + ), + "attestation_key_id": (authorization.qualification_fault_driver_key_id), + "before_input_sha256": context.before_input_sha256, + "after_input_sha256": after_input_sha256, + } + actual = receipt.model_dump(mode="python") + if any(actual.get(key) != value for key, value in expected.items()): + raise RuntimeError("qualification_fault_receipt_binding_invalid") + if context.before_input_sha256 == after_input_sha256: + raise RuntimeError("qualification_fault_input_unchanged") + trusted_key = self._qualification_fault_public_key + if trusted_key is None or not verify_fault_mutation_receipt( + receipt, + trusted_public_key_base64=trusted_key, + ): + raise RuntimeError("qualification_fault_receipt_signature_invalid") + if self._qualification_fault_mutations: + raise RuntimeError("qualification_fault_mutation_repeated") + self._qualification_fault_mutations.append(receipt) + + def _apply_screen_fault_mutation( + self, + *, + gate: QualificationFaultGate, + step: Step, + before_png: bytes, + effect_verifier: Any, + capture: Optional[Callable[[], bytes]] = None, + ) -> bytes: + if self._qualification_fault_mutations: + return before_png + # The receipt binds the frame the detector reads. ``capture`` is the + # detector's own capture path (see _revalidation_frame_capture): two + # paths can encode identical pixels as different PNG bytes. + capture_frame = capture or self.backend.screenshot + context = self._qualification_fault_context( + gate=gate, + step=step, + actuation_path="gui", + before_input_sha256=sha256_bytes(before_png), + effect_verifier=effect_verifier, + capture_frame=capture_frame, + ) + if context is None: + return before_png + driver = self._require_current_fault_driver_binding() + mutation = driver.mutate(context) + if mutation is None: + return before_png + if mutation.replace_effect_verifier: + raise RuntimeError("qualification_screen_fault_replaced_verifier") + after_png = capture_frame() + self._validate_fault_mutation( + context, + mutation.receipt, + after_input_sha256=sha256_bytes(after_png), + ) + return after_png + + def _bind_surface_qualification_environment( + self, workflow: Workflow, surface_id: str + ) -> Optional[str]: + """Observe the newly active surface against its qualified boundary. + + Only a qualification case run observes environments. The primary + surface is checked against the project's ``environment`` (as at run + start); every other surface against ``surface_environments[id]`` + (``_begin_surfaces`` already refused a case whose project lacks one). + On a match the input-edge guard moves to the new surface's backend, + so every later input on that surface rechecks that environment. + """ + + authorization = self.governed_authorization + if authorization is None or authorization.qualification_case_id is None: + return None + router = self._surface_router + project = workflow.qualification + if router is None or project is None: + return "qualification environment evidence is unavailable" + self._qualification_environment_call = None + self._qualification_environment_bound = None + boundary: Any + if surface_id == router.primary_id: + boundary = project.environment + else: + boundary = project.surface_environments.get(surface_id) + if boundary is None: + return "the qualification project does not bound this surface" + refusal, observed, ids = self._bind_qualification_boundary( + boundary, surface_scoped=surface_id != router.primary_id + ) + if surface_id != router.primary_id and observed is not None and ids is not None: + self._surface_environment_observations[surface_id] = ( + ObservedSurfaceEnvironment( + target_kind=observed.target_kind, + application_sha256=hashlib.sha256( + observed.application_identity.encode("utf-8") + ).hexdigest(), + application_version_sha256=hashlib.sha256( + observed.application_version.encode("utf-8") + ).hexdigest(), + session_sha256=observed.session_identity_sha256, + environment_digest=observed.environment_digest, + observer_id=ids[0], + observer_contract_sha256=ids[1], + binding_sha256=observed.binding_sha256( + observer_id=ids[0], observer_contract_sha256=ids[1] + ), + ) + ) + return refusal + + def _is_qualification_fault_step(self, step: Step) -> bool: + """Whether ``step`` is the fault step of the fault case this run is. + + A readiness drill (a fault qualification case) passes only when the + engine's own detector refuses its changed input before delivery, so + nothing is ever delivered at its fault step. + """ + + authorization = self.governed_authorization + return bool( + authorization is not None + and authorization.qualification_case_kind not in {None, "representative"} + and authorization.qualification_fault_step_id_sha256 is not None + and authorization.qualification_fault_step_id_sha256 + == sha256_bytes(step.id.encode("utf-8")) + ) + + @staticmethod + def _qualification_id_sha256(value: Optional[str]) -> Optional[str]: + if value is None: + return None + return hashlib.sha256(value.encode("utf-8")).hexdigest() + + def _observe_qualification_environment( + self, + workflow: Workflow, + report: RunReport, + *, + execution_target_kind: Optional[ExecutionTargetKind], + ) -> Optional[str]: + """Retain exact live environment evidence for a campaign run. + + Project labels are expected values only. They never become observed + evidence. A qualification case refuses before workflow actuation when + the active backend cannot independently return the application, + application version, and session identity or when either expected + application field differs. The one exception is the signed-in web + session observer: its version is the operator's Check it confirmation, + bound into its contract and environment digests. + """ + + authorization = self.governed_authorization + if authorization is None or authorization.qualification_case_id is None: + return None + project = workflow.qualification + if project is None or execution_target_kind is None: + return "qualification environment evidence is unavailable" + if ( + authorization.qualification_run_id_sha256 + != hashlib.sha256(self._run_id.encode("utf-8")).hexdigest() + ): + return "qualification run identity does not match its authorization" + if execution_target_kind != project.environment.target_kind: + return "qualification target kind does not match the project" + refusal, observed, observer_ids = self._bind_qualification_boundary( + project.environment + ) + if observed is not None and observer_ids is not None: + report.observed_application_sha256 = hashlib.sha256( + observed.application_identity.encode("utf-8") + ).hexdigest() + report.observed_application_version_sha256 = hashlib.sha256( + observed.application_version.encode("utf-8") + ).hexdigest() + report.observed_session_sha256 = observed.session_identity_sha256 + report.observed_environment_digest = observed.environment_digest + report.qualification_environment_observer_id = observer_ids[0] + report.qualification_environment_observer_contract_sha256 = observer_ids[1] + report.observed_environment_binding_sha256 = observed.binding_sha256( + observer_id=observer_ids[0], + observer_contract_sha256=observer_ids[1], + ) + return refusal + + def _require_qualification_environment_current(self) -> None: + """Recheck the bound environment immediately before an input edge.""" + + call = self._qualification_environment_call + expected = self._qualification_environment_bound + if call is None or expected is None: + return + try: + observed = call() + except StructuralResolutionRefused: + raise + except Exception as exc: + raise StructuralResolutionRefused( + "qualification environment could not be observed before input" + ) from exc + if observed != expected: + raise StructuralResolutionRefused( + "qualification environment changed before input" + ) diff --git a/packages/flow/openadapt_flow/runtime/replayer.py b/packages/flow/openadapt_flow/runtime/replayer.py index 107500d876..3518fe7d6a 100644 --- a/packages/flow/openadapt_flow/runtime/replayer.py +++ b/packages/flow/openadapt_flow/runtime/replayer.py @@ -44,18 +44,18 @@ import math import os import re -import struct +import struct # noqa: F401 import threading import time import uuid from collections import Counter from contextlib import ExitStack from copy import deepcopy -from dataclasses import dataclass +from dataclasses import dataclass # noqa: F401 from datetime import datetime, timezone from pathlib import Path from types import SimpleNamespace -from typing import ( +from typing import ( # noqa: F401 TYPE_CHECKING, Any, Callable, @@ -108,7 +108,7 @@ live_control_error, whole_interruption_error, ) -from openadapt_flow.ir import ( +from openadapt_flow.ir import ( # noqa: F401 ActionDeliveryUncertainty, ActionKind, Anchor, @@ -156,7 +156,7 @@ Workflow, predicate_contract_sha256, ) -from openadapt_flow.network_scope import EndpointScope, endpoint_scope +from openadapt_flow.network_scope import EndpointScope, endpoint_scope # noqa: F401 from openadapt_flow.privacy import scrub_image_bytes as _scrub_png from openadapt_flow.privacy import scrub_text as _scrub_phi from openadapt_flow.qualification_environment import ( @@ -164,7 +164,7 @@ QualificationEnvironmentObservation, QualificationEnvironmentObserver, ) -from openadapt_flow.qualification_faults import ( +from openadapt_flow.qualification_faults import ( # noqa: F401 FaultMutationReceipt, QualificationFaultContext, QualificationFaultDriver, @@ -277,27 +277,6 @@ ) from openadapt_flow.vision.ocr import OcrResolutionRefused -_DeliveryResultT = TypeVar("_DeliveryResultT") -_MAX_FRESH_ACTUATION_REACQUISITIONS = 2 -# Check-to-click guard on the pixel path (``Replayer._pixel_recheck``): how -# often a changed target or identity region is re-observed before the click -# is refused, and how much a pixel must change to count (remote encoders -# shift colours by a few levels; a dialog or a new row changes far more). -_PIXEL_RECHECK_REOBSERVES = 2 -# How far a remote codec moved the same hovered target between frames on real -# AVC444 sessions (benchmark/rdp_avc_real: one pixel on each axis). -_REMOTE_HOVER_JITTER_PX = 2 -_PIXEL_RECHECK_LEVEL = 24 -_PIXEL_RECHECK_MIN_PIXELS = 4 - -# How long past its step's timeout a held record check may run under an -# approval mode before the held report decides the item (a person). -READBACK_DEADLINE_MARGIN_S = 120.0 -# Private module capability. The public ``run`` API cannot mint a durable -# continuation. ``runtime.durable.resume`` imports this object only after it -# has validated the pause, approval, bundle, checkpoint, and live state. -_DURABLE_RESUME_AUTHORITY = object() - if TYPE_CHECKING: from openadapt_flow.approval_mode.gate import ApprovalGate from openadapt_flow.production_qualification import ProductionQualificationGuard @@ -311,477 +290,65 @@ ) from openadapt_flow.transaction import IdempotencyLedger -# REGION_STABLE template check: how far the expected content may shift from -# the recorded region (real apps re-layout by a few pixels between runs), -# and the minimum template-match score to accept it. -PC_TEMPLATE_SEARCH_PAD = 80 - - -_FIELD_CHECK_LIMIT = 32 -_FIELD_VALUE_LIMIT = 256 - - -def _field_text(value: Any) -> Optional[str]: - if value is None: - return None - if isinstance(value, bool): - return "true" if value else "false" - if isinstance(value, (str, int, float)): - return str(value)[:_FIELD_VALUE_LIMIT] - return None - - -def effect_field_checks(effect: Any, verdict: Any, tier: Any) -> list[Any]: - """The field comparisons one effect verdict made, for the local report. - - A ``field_equals`` effect compares one field. A record effect compares - each literal ``match`` value against the first matched record. Values - that aren't plain text or numbers are left out rather than guessed. - """ - - from openadapt_flow.ir import EffectFieldCheck - - tier_value = int(tier) if tier is not None else None - checks: list[EffectFieldCheck] = [] - field = getattr(effect, "field", None) - expected_value = getattr(verdict, "expected_value", None) - if field and expected_value is not None: - checks.append( - EffectFieldCheck( - field=str(field)[:_FIELD_VALUE_LIMIT], - expected=_field_text(expected_value), - observed=_field_text(getattr(verdict, "observed_value", None)), - matched=bool(getattr(verdict, "confirmed", False)), - verification_tier=tier_value, - ) - ) - return checks - records = getattr(verdict, "matched_records", None) or [] - record = records[0] if records and isinstance(records[0], dict) else None - for name, want in (getattr(effect, "match", None) or {}).items(): - expected = _field_text(want) - if expected is None: - continue - observed = _field_text(record.get(name)) if record is not None else None - checks.append( - EffectFieldCheck( - field=str(name)[:_FIELD_VALUE_LIMIT], - expected=expected, - observed=observed, - matched=observed == expected, - verification_tier=tier_value, - ) - ) - if len(checks) >= _FIELD_CHECK_LIMIT: - break - return checks - - -#: A uniform page scale is inferred only from a click point at least this far -#: from both edges at the origin, only when both axes agree to within this -#: fraction, and only inside these bounds (and not within 5% of 1). -_PAGE_SCALE_MIN_COORD = 40 -_PAGE_SCALE_AXIS_AGREEMENT = 0.02 -_PAGE_SCALE_BOUNDS = (0.5, 2.0) -#: One pixel of click-point noise moves the estimate by a few tenths of a -#: percent, enough to drop a wide template below threshold; the check tries -#: this tight band around it. -_PAGE_SCALE_BAND = (0.99, 0.995, 1.0, 1.005, 1.01) - - -def _uniform_page_scale(step: Any, result: Any) -> Optional[float]: - """The page scale this step's click showed, or ``None``. - - The recorded click point and the point the ladder resolved this run, - divided axis by axis. A whole-page zoom multiplies both coordinates by - the same factor; a control that merely moved almost never does, so the - two axes must agree. - """ - - resolution = getattr(result, "resolution", None) - anchor = getattr(step, "anchor", None) - if resolution is None or anchor is None: - return None - try: - rx, ry = (float(v) for v in anchor.click_point) - lx, ly = (float(v) for v in resolution.point) - except (TypeError, ValueError): - return None - if min(rx, ry) < _PAGE_SCALE_MIN_COORD: - return None - sx, sy = lx / rx, ly / ry - if abs(sx - sy) > _PAGE_SCALE_AXIS_AGREEMENT * max(sx, sy): - return None - scale = (sx + sy) / 2 - low, high = _PAGE_SCALE_BOUNDS - if not low <= scale <= high or abs(scale - 1.0) < 0.05: - return None - return round(scale, 3) - - -def _frame_viewport(frame_png: bytes) -> tuple[int, int]: - """Return the viewport of THIS frame, read from the frame itself. - - ``self.backend.viewport`` is a live read. Pairing it with an - already-captured frame lets a resize or move between capture and use - reinterpret that frame under geometry it never had, which can place a - coordinate where the operator never demonstrated one. A PNG carries its own - dimensions in the IHDR chunk, so the frame can always answer for itself. - """ - - if len(frame_png) < 24 or not frame_png.startswith(b"\x89PNG\r\n\x1a\n"): - raise ValueError("frame viewport requires valid PNG bytes") - width, height = struct.unpack(">II", frame_png[16:24]) - if width <= 0 or height <= 0: - raise ValueError("frame viewport must be positive") - return int(width), int(height) - - -PC_TEMPLATE_THRESHOLD = 0.9 -# REGION_STABLE asserts recorded structure, not palette. The ordinary -# grayscale template matcher remains the first (stricter) check; this edge-map -# threshold is a color-invariant fallback. Exact v1.16.1 MockMed evidence -# measured correct theme+parameter substitution at 0.86 and a true blocking -# modal at 0.24, leaving a deliberate margin on both sides. -PC_STRUCTURAL_TEMPLATE_THRESHOLD = 0.8 - -# Typed-input verification: height of the visual readback band around the -# focusing click after TYPE. Without structural field bounds, read the full -# viewport width: a centered crop can hide the value's beginning or suffix. -# With no focusing click (e.g. Tab between fields), use the whole frame. -FIELD_REGION_HEIGHT = 240 -MASKED_FIELD_REGION_WIDTH = 640 -FIELD_REGION_PAD = 16 - -# Typed-input verification, secret-only diff acceptance: when a SECRET typed -# value is OCR-able but OCR cannot find it, a pixel change alone is accepted -# only if the field region gained no other READABLE text — masked fields render -# dots, which OCR reads as nothing, low-confidence noise, or punctuation -# runs, while a dialog rendering over the region adds confident words. -# "Readable" therefore counts alphanumeric characters of lines OCR is -# confident about (>= MASKED_MIN_CONFIDENCE), EXCLUDING homogeneous glyph -# runs: a dot row can misread as a confident digit run (measured on the -# Linux renderer: 17 bullets -> '0000000000006' at 0.81 confidence), and -# such runs are >= MASKED_REPEAT_FRACTION one repeated character — -# no real dialog sentence is. Alnum counts are also invariant to -# segmentation differences between frames. The gain may not exceed -# MASKED_NEW_TEXT_SLACK. -MASKED_NEW_TEXT_SLACK = 3 -MASKED_MIN_CONFIDENCE = 0.6 -MASKED_REPEAT_FRACTION = 0.66 - -# Closed-loop scroll: a SCROLL step keeps scrolling by its recorded delta -# until the NEXT anchored step's anchor resolves on a settled frame, bounded -# by this multiple of the step's own recorded scroll distance. Consecutive -# SCROLL steps hand the loop to each other (each probes first and no-ops -# once the anchor is in view), so a run of N recorded scrolls has a combined -# budget of ~2.5x the total recorded distance. -SCROLL_BUDGET_FACTOR = 2.5 - -# Bounds for the Phase-2 program interpreter -- deterministic termination -# guarantees. A worklist loop is bounded per-loop (LoopSpec.max_iterations); the -# graph walk itself is bounded by a total step budget (so an authored cycle of -# always-true transitions can never spin forever) and subflow/loop nesting is -# depth-bounded. -PROGRAM_MAX_STEPS = 100_000 -PROGRAM_MAX_DEPTH = 64 - - -class _GraphStepContext: - """Per-action-state context the program interpreter feeds ``_run_step``. - - Supplies the two pieces the linear loop derives from list position but a - graph lacks: the previously EXECUTED action (for the click-to-focus TYPE - heuristic) and the SCROLL closed-loop successors (the next anchored action - state and the immediately following state). All optional; a None field means - "no such successor" (the scroll loop then falls back to its open-loop - gesture, exactly as at the end of a linear workflow). - """ - - __slots__ = ( - "prev_action", - "next_anchored", - "following", - "has_exception_handler", - ) - - def __init__( - self, - prev_action: Optional[ActionKind] = None, - next_anchored: Optional[Step] = None, - following: Optional[Step] = None, - has_exception_handler: bool = False, - ) -> None: - self.prev_action = prev_action - self.next_anchored = next_anchored - self.following = following - self.has_exception_handler = has_exception_handler - - -class _ProgramHalt(Exception): - """Internal signal: an unrecoverable state was reached (an unhandled action - failure, a dead-end with no matching transition, a bound exceeded, or a - ``halt`` / ``escalate`` terminal). Carries the outcome + reason the run - report records. Caught only by ``_interpret_program`` -- never escapes the - Replayer.""" - - def __init__(self, outcome: str, reason: str, *, safety: bool = False) -> None: - super().__init__(reason) - self.outcome = outcome - self.reason = reason - self.safety = safety - # Populated at an ACTION-state failure before graph frames unwind. - # Empty means the halt is not eligible for a generic attended - # Continue/Skip transition. - self.program_frames: list[GraphFrame] = [] - self.program_params: dict[str, RuntimeParamScalar] = {} - self.program_history_hash: str = "" - - -def effect_endpoint_scope(verifier: Any) -> EndpointScope: - """Where an effect verifier reads its system of record. - - A verifier that names a ``base_url`` (REST, FHIR) is ``loopback`` when that - URL is on this computer and ``external`` otherwise. A verifier without one - is ``unknown``, which keeps a network substrate counted as egress. - """ - base_url = getattr(verifier, "base_url", None) - return endpoint_scope(base_url) if isinstance(base_url, str) else "unknown" - - -def _record_loop_iteration( - report: RunReport, - loop_state_id: str, - row_index: int, - row: Mapping[str, RuntimeParamScalar], -) -> None: - """Record the row values one loop iteration binds, before it runs. - - ``report.params`` is the run's base scope. A row field overrides it for - that iteration, so the audit report needs each row's own values to state - what the run typed. - """ - report.loop_iterations.append( - LoopIterationBinding( - loop_state_id=loop_state_id, - row_index=row_index, - params=dict(row), - ) - ) - - -def _loop_iterations_from_checkpoints( - checkpoints: list[ProgramCheckpoint], -) -> list[LoopIterationBinding]: - """Rebuild the loop rows that earlier legs of a resumed run bound. - - A resumed report carries the earlier legs' results, so it also needs the - row values those results typed. Each checkpoint's frame stack names the - loop row it ran in. A row is listed once, at its first checkpoint, keyed by - its position in the frame stack so a nested loop's rows under different - outer rows stay distinct. - """ - seen: set[tuple[tuple[str, str, int], ...]] = set() - bindings: list[LoopIterationBinding] = [] - for checkpoint in checkpoints: - path: tuple[tuple[str, str, int], ...] = () - for frame in checkpoint.frames: - cursor = frame.loop - if cursor is None: - path = (*path, (frame.graph_id, "", -1)) - continue - path = (*path, (frame.graph_id, cursor.loop_state_id, cursor.row_index)) - if path in seen or not 0 <= cursor.row_index < len(cursor.rows): - continue - seen.add(path) - bindings.append( - LoopIterationBinding( - loop_state_id=cursor.loop_state_id, - row_index=cursor.row_index, - params=dict(cursor.rows[cursor.row_index]), - ) - ) - return bindings - - -def _all_workflow_steps(workflow: Workflow): - """Yield every ``Step`` in a workflow for whole-bundle audits. - - Linear bundle: the ``steps`` list. Program bundle: every ``action`` state's - step across the top-level program graph AND all subflows (a loop body's - steps included). Order is stable but not execution order -- used only for - static coverage counting, not for running. - """ - if workflow.program is None: - yield from workflow.steps - return - graphs = [workflow.program, *workflow.subflows.values()] - for graph in graphs: - for state in graph.states.values(): - if state.kind is StateKind.ACTION and state.step is not None: - yield state.step - - -class EgressNotPermitted(RuntimeError): - """Raised when an egress-capable model component (a grounder / identity-VLM - / state-verifier that could send a screenshot off the box) is wired without - the operator's explicit ``allow_model_grounding`` opt-in (PHI audit REM-3). - """ - - -#: How far past its demonstrated position (as a fraction of the frame's extent -#: along the scroll axis) a scroll target may still be before the closed-loop -#: scroll counts it as ready. -SCROLL_POSITION_SLACK_FRACTION = 0.25 - - -#: Rungs that show the target itself on screen: what closed-loop scroll -#: readiness accepts, and all a step accepts after its scroll missed it. -class _NotActionableError(str): - """The error ``_resolve_step`` returns when the structural rung found - exactly one candidate but it is covered or off-viewport. - - A ``str`` so it travels as any resolution error; its type marks it. The - step polls on it like any miss, and halts with ``target_not_actionable`` - when it times out or when the fresh re-resolution before input finds it. - """ - - -_DIRECT_TARGET_RUNGS = frozenset({"structural", "template", "template_global", "ocr"}) - -#: Steps that own the halt when the closed-loop scroll before them never found -#: their target (see ``Replayer._act_scroll``). -_SCROLL_HANDOFF_ACTIONS = frozenset( - {ActionKind.CLICK, ActionKind.DOUBLE_CLICK, ActionKind.RIGHT_CLICK} +from openadapt_flow.runtime.replay._shared import ( # noqa: F401 + _DIRECT_TARGET_RUNGS, + _DURABLE_RESUME_AUTHORITY, + _FIELD_CHECK_LIMIT, + _FIELD_VALUE_LIMIT, + _FORM_GUARD_SETTLE_S, + _MAX_FRESH_ACTUATION_REACQUISITIONS, + _MAX_SERVER_RESPONSES, + _PAGE_SCALE_AXIS_AGREEMENT, + _PAGE_SCALE_BAND, + _PAGE_SCALE_BOUNDS, + _PAGE_SCALE_MIN_COORD, + _PIXEL_RECHECK_LEVEL, + _PIXEL_RECHECK_MIN_PIXELS, + _PIXEL_RECHECK_REOBSERVES, + _REMOTE_HOVER_JITTER_PX, + _SCROLL_HANDOFF_ACTIONS, + FIELD_REGION_HEIGHT, + FIELD_REGION_PAD, + MASKED_FIELD_REGION_WIDTH, + MASKED_MIN_CONFIDENCE, + MASKED_NEW_TEXT_SLACK, + MASKED_REPEAT_FRACTION, + PC_STRUCTURAL_TEMPLATE_THRESHOLD, + PC_TEMPLATE_SEARCH_PAD, + PC_TEMPLATE_THRESHOLD, + PIXEL_CENSUS_OCR_MAX_SIMILARITY, + PIXEL_CENSUS_OCR_MIN_CONFIDENCE, + PROGRAM_MAX_DEPTH, + PROGRAM_MAX_STEPS, + READBACK_DEADLINE_MARGIN_S, + SCROLL_BUDGET_FACTOR, + SCROLL_POSITION_SLACK_FRACTION, + EgressNotPermitted, + _all_workflow_steps, + _DeliveryResultT, + _field_text, + _frame_viewport, + _GraphStepContext, + _identity_tokens, + _loop_iterations_from_checkpoints, + _NotActionableError, + _ProgramHalt, + _record_loop_iteration, + _scroll_target_positioned, + _uniform_page_scale, + _WebFormApprovalView, + _within_hover_jitter, + better_identity_read, + effect_endpoint_scope, + effect_field_checks, + identity_read_contradicts, +) +from openadapt_flow.runtime.replay.qualification_faults import ( + QualificationFaultInjection, ) -def _scroll_target_positioned( - recorded: tuple[int, int], - resolved: tuple[int, int], - scroll_delta: tuple[int, int], - frame_png: bytes, -) -> bool: - """Is the resolved target no further along the scroll than recorded?""" - - dx, dy = scroll_delta - w, h = _frame_viewport(frame_png) - if dy: - slack = SCROLL_POSITION_SLACK_FRACTION * h - ahead = (resolved[1] - recorded[1]) if dy > 0 else (recorded[1] - resolved[1]) - if ahead > slack: - return False - if dx: - slack = SCROLL_POSITION_SLACK_FRACTION * w - ahead = (resolved[0] - recorded[0]) if dx > 0 else (recorded[0] - resolved[0]) - if ahead > slack: - return False - return True - - -def _identity_tokens(text: Any) -> set[str]: - return {t for t in re.split(r"[^0-9a-z]+", str(text or "").lower()) if t} - - -def identity_read_contradicts(check: Any) -> bool: - """A ``mismatch`` read that saw text the expected identity doesn't have. - - "mismatch" also covers a read that is merely incomplete (columns cut - off). Only a read that shows a token the expected record lacks (another - MRN, a different spelling or date) is evidence of a different record. - """ - - if getattr(check, "status", None) != "mismatch": - return False - expected = _identity_tokens(getattr(check, "expected", None)) - observed = _identity_tokens(getattr(check, "observed", None)) - if not expected or not observed: - return True # nothing to compare: keep the refusal - return not observed <= expected - - -def better_identity_read(first: Any, retry: Any) -> Any: - """The identity read to keep from a first read and its upscaled re-read. - - A read that contradicts the expected record (:func:`identity_read_contradicts`) - is final: a second read of the same screen must never turn it into - ``verified``. A mismatch that is only incomplete can be rescued by a - re-read that verifies. Otherwise ``verified`` beats the "cannot certify" - outcomes, then a mismatch, then ``abstain``, then a blank ``unreadable``; - a tie goes to the read that covered more of the expected identity. - """ - - if identity_read_contradicts(first): - return first - if identity_read_contradicts(retry): - return retry - rank = {"unreadable": 0, "abstain": 1, "mismatch": 2, "verified": 3} - if (rank[retry.status], retry.coverage) > (rank[first.status], first.coverage): - return retry - return first - - -#: Server answers kept per step (a save makes one or a few write requests). -_MAX_SERVER_RESPONSES = 64 - - -@dataclass(frozen=True) -class _WebFormApprovalView: - """An API step's prepared request, as a person is asked to approve it.""" - - prepared: Any - #: :func:`~openadapt_flow.runtime.actuators.web_form.operation_digest`. - digest: str - rows: list[tuple[str, str]] - png: bytes - ref: Any - - -# The pixel census's OCR tie-break: a place the pixels do not rule out is -# distinguishable only when OCR reads both crops with every line at or above -# this confidence, neither text contains the other, and the two texts are less -# similar than this ratio. One label read two ways measured 0.82-0.95 -# ("Smith, John" / "Smith, S John" / "Smith, 10 lohn"); sibling tab labels -# ("Contact" / "Stats") measure 0.5 or less. -PIXEL_CENSUS_OCR_MIN_CONFIDENCE = 0.8 -PIXEL_CENSUS_OCR_MAX_SIMILARITY = 0.6 - - -#: How long after a create-new form's save the replayer keeps watching its -#: submit guard before the next step (a script may submit after the click). -_FORM_GUARD_SETTLE_S = 2.0 - - -def _within_hover_jitter( - fresh: tuple[int, int], - prepared: tuple[int, int], - fresh_region: Optional[Region], -) -> bool: - """Whether a re-resolved point may be the prepared one moved by codec jitter. - - Both points must lie inside the fresh match's own box and differ by at - most :data:`_REMOTE_HOVER_JITTER_PX` on each axis. With no box there is - nothing to bind the prepared point to. A True answer only permits one - re-preparation at the fresh point; the click still needs an exact match. - """ - if fresh_region is None: - return False - x, y, w, h = fresh_region - if w <= 0 or h <= 0: - return False - - def inside(point: tuple[int, int]) -> bool: - return x <= point[0] < x + w and y <= point[1] < y + h - - return ( - abs(int(fresh[0]) - int(prepared[0])) <= _REMOTE_HOVER_JITTER_PX - and abs(int(fresh[1]) - int(prepared[1])) <= _REMOTE_HOVER_JITTER_PX - and inside(fresh) - and inside(prepared) - ) - - -class Replayer: +class Replayer(QualificationFaultInjection): """Replays a Workflow against a Backend using injected vision. Args: @@ -7093,191 +6660,6 @@ def _qualification_fault_driver_refusal(self, workflow: Workflow) -> Optional[st self._qualification_fault_public_key = trusted return None - @staticmethod - def _qualification_fault_gate_for_kind( - kind: QualificationFaultKind, - ) -> QualificationFaultGate: - gates: dict[QualificationFaultKind, QualificationFaultGate] = { - "ambiguity": "target_resolution", - "wrong_identity": "identity_verification", - "stale_identity": "actuation_revalidation", - "weak_effect": "effect_strength", - "missing_effect": "effect_verifier", - } - return gates[kind] - - def _qualification_fault_context( - self, - *, - gate: QualificationFaultGate, - step: Step, - actuation_path: Literal["gui", "api"], - before_input_sha256: str, - effect_verifier: Any, - effects: tuple[Any, ...] = (), - capture_frame: Any = None, - ) -> Optional[QualificationFaultContext]: - authorization = self.governed_authorization - if authorization is None or authorization.qualification_case_kind in { - None, - "representative", - }: - return None - kind = cast(QualificationFaultKind, authorization.qualification_case_kind) - if self._qualification_fault_gate_for_kind(kind) != gate: - return None - if authorization.qualification_fault_step_id_sha256 != sha256_bytes( - step.id.encode("utf-8") - ): - return None - if self._qualification_case_action_paths.get(step.id) != actuation_path: - return None - required = ( - authorization.qualification_project_id, - authorization.qualification_project_revision, - authorization.qualification_project_contract_sha256, - authorization.qualification_campaign_id_sha256, - self._qualification_id_sha256(authorization.qualification_case_id), - authorization.qualification_case_input_sha256, - authorization.qualification_run_id_sha256, - ) - if any(value is None for value in required): - raise RuntimeError("qualification_fault_binding_incomplete") - return QualificationFaultContext( - project_id=cast(str, required[0]), - project_revision=cast(int, required[1]), - project_contract_sha256=cast(str, required[2]), - campaign_id_sha256=cast(str, required[3]), - case_id_sha256=cast(str, required[4]), - case_input_sha256=cast(str, required[5]), - run_id_sha256=cast(str, required[6]), - step_id=step.id, - actuation_path=actuation_path, - fault_kind=kind, - gate=gate, - before_input_sha256=before_input_sha256, - backend=self.backend, - vision=self.vision, - effect_verifier=effect_verifier, - effects=effects, - capture_frame=capture_frame, - surface_id=( - self._surface_router.active_id - if self._surface_router is not None - else None - ), - ) - - def _require_current_fault_driver_binding(self) -> QualificationFaultDriver: - """Snapshot the configured driver identity immediately before mutation.""" - - driver = self.qualification_fault_driver - authorization = self.governed_authorization - if driver is None or authorization is None: - raise RuntimeError("qualification_fault_driver_missing") - try: - observed = ( - driver.driver_id, - driver.contract_sha256, - driver.attestation_key_id, - ) - except Exception as exc: - raise RuntimeError( - "qualification_fault_driver_identity_unavailable" - ) from exc - expected = ( - authorization.qualification_fault_driver_id, - authorization.qualification_fault_driver_contract_sha256, - authorization.qualification_fault_driver_key_id, - ) - if observed != expected: - raise RuntimeError("qualification_fault_driver_binding_changed") - return driver - - def _validate_fault_mutation( - self, - context: QualificationFaultContext, - receipt: FaultMutationReceipt, - *, - after_input_sha256: str, - ) -> None: - authorization = self.governed_authorization - if authorization is None: - raise RuntimeError("qualification_fault_authorization_missing") - expected = { - "project_id": context.project_id, - "project_revision": context.project_revision, - "project_contract_sha256": context.project_contract_sha256, - "campaign_id_sha256": context.campaign_id_sha256, - "case_id_sha256": context.case_id_sha256, - "case_input_sha256": context.case_input_sha256, - "run_id_sha256": context.run_id_sha256, - "step_id_sha256": sha256_bytes(context.step_id.encode("utf-8")), - "actuation_path": context.actuation_path, - "fault_kind": context.fault_kind, - "gate": context.gate, - "driver_id": authorization.qualification_fault_driver_id, - "driver_contract_sha256": ( - authorization.qualification_fault_driver_contract_sha256 - ), - "attestation_key_id": (authorization.qualification_fault_driver_key_id), - "before_input_sha256": context.before_input_sha256, - "after_input_sha256": after_input_sha256, - } - actual = receipt.model_dump(mode="python") - if any(actual.get(key) != value for key, value in expected.items()): - raise RuntimeError("qualification_fault_receipt_binding_invalid") - if context.before_input_sha256 == after_input_sha256: - raise RuntimeError("qualification_fault_input_unchanged") - trusted_key = self._qualification_fault_public_key - if trusted_key is None or not verify_fault_mutation_receipt( - receipt, - trusted_public_key_base64=trusted_key, - ): - raise RuntimeError("qualification_fault_receipt_signature_invalid") - if self._qualification_fault_mutations: - raise RuntimeError("qualification_fault_mutation_repeated") - self._qualification_fault_mutations.append(receipt) - - def _apply_screen_fault_mutation( - self, - *, - gate: QualificationFaultGate, - step: Step, - before_png: bytes, - effect_verifier: Any, - capture: Optional[Callable[[], bytes]] = None, - ) -> bytes: - if self._qualification_fault_mutations: - return before_png - # The receipt binds the frame the detector reads. ``capture`` is the - # detector's own capture path (see _revalidation_frame_capture): two - # paths can encode identical pixels as different PNG bytes. - capture_frame = capture or self.backend.screenshot - context = self._qualification_fault_context( - gate=gate, - step=step, - actuation_path="gui", - before_input_sha256=sha256_bytes(before_png), - effect_verifier=effect_verifier, - capture_frame=capture_frame, - ) - if context is None: - return before_png - driver = self._require_current_fault_driver_binding() - mutation = driver.mutate(context) - if mutation is None: - return before_png - if mutation.replace_effect_verifier: - raise RuntimeError("qualification_screen_fault_replaced_verifier") - after_png = capture_frame() - self._validate_fault_mutation( - context, - mutation.receipt, - after_input_sha256=sha256_bytes(after_png), - ) - return after_png - def _apply_effect_fault_mutation( self, *, @@ -7518,59 +6900,6 @@ def _activate_surface_backend(self, backend: Backend) -> None: self._last_click_region = None self._control_overlay_last_observation_png = None - def _bind_surface_qualification_environment( - self, workflow: Workflow, surface_id: str - ) -> Optional[str]: - """Observe the newly active surface against its qualified boundary. - - Only a qualification case run observes environments. The primary - surface is checked against the project's ``environment`` (as at run - start); every other surface against ``surface_environments[id]`` - (``_begin_surfaces`` already refused a case whose project lacks one). - On a match the input-edge guard moves to the new surface's backend, - so every later input on that surface rechecks that environment. - """ - - authorization = self.governed_authorization - if authorization is None or authorization.qualification_case_id is None: - return None - router = self._surface_router - project = workflow.qualification - if router is None or project is None: - return "qualification environment evidence is unavailable" - self._qualification_environment_call = None - self._qualification_environment_bound = None - boundary: Any - if surface_id == router.primary_id: - boundary = project.environment - else: - boundary = project.surface_environments.get(surface_id) - if boundary is None: - return "the qualification project does not bound this surface" - refusal, observed, ids = self._bind_qualification_boundary( - boundary, surface_scoped=surface_id != router.primary_id - ) - if surface_id != router.primary_id and observed is not None and ids is not None: - self._surface_environment_observations[surface_id] = ( - ObservedSurfaceEnvironment( - target_kind=observed.target_kind, - application_sha256=hashlib.sha256( - observed.application_identity.encode("utf-8") - ).hexdigest(), - application_version_sha256=hashlib.sha256( - observed.application_version.encode("utf-8") - ).hexdigest(), - session_sha256=observed.session_identity_sha256, - environment_digest=observed.environment_digest, - observer_id=ids[0], - observer_contract_sha256=ids[1], - binding_sha256=observed.binding_sha256( - observer_id=ids[0], observer_contract_sha256=ids[1] - ), - ) - ) - return refusal - def _active_surface_binding( self, workflow: Workflow ) -> tuple[Optional[ExecutionTargetKind], Optional[str]]: @@ -13005,23 +12334,6 @@ def _approval_form_state( except Exception: # noqa: BLE001 - unreadable is never "unchanged" return None - def _is_qualification_fault_step(self, step: Step) -> bool: - """Whether ``step`` is the fault step of the fault case this run is. - - A readiness drill (a fault qualification case) passes only when the - engine's own detector refuses its changed input before delivery, so - nothing is ever delivered at its fault step. - """ - - authorization = self.governed_authorization - return bool( - authorization is not None - and authorization.qualification_case_kind not in {None, "representative"} - and authorization.qualification_fault_step_id_sha256 is not None - and authorization.qualification_fault_step_id_sha256 - == sha256_bytes(step.id.encode("utf-8")) - ) - def _approval_step_refusal( self, workflow: Workflow, @@ -14723,63 +14035,6 @@ def _wait_starting_state_settled( ) return frame, None, True - @staticmethod - def _qualification_id_sha256(value: Optional[str]) -> Optional[str]: - if value is None: - return None - return hashlib.sha256(value.encode("utf-8")).hexdigest() - - def _observe_qualification_environment( - self, - workflow: Workflow, - report: RunReport, - *, - execution_target_kind: Optional[ExecutionTargetKind], - ) -> Optional[str]: - """Retain exact live environment evidence for a campaign run. - - Project labels are expected values only. They never become observed - evidence. A qualification case refuses before workflow actuation when - the active backend cannot independently return the application, - application version, and session identity or when either expected - application field differs. The one exception is the signed-in web - session observer: its version is the operator's Check it confirmation, - bound into its contract and environment digests. - """ - - authorization = self.governed_authorization - if authorization is None or authorization.qualification_case_id is None: - return None - project = workflow.qualification - if project is None or execution_target_kind is None: - return "qualification environment evidence is unavailable" - if ( - authorization.qualification_run_id_sha256 - != hashlib.sha256(self._run_id.encode("utf-8")).hexdigest() - ): - return "qualification run identity does not match its authorization" - if execution_target_kind != project.environment.target_kind: - return "qualification target kind does not match the project" - refusal, observed, observer_ids = self._bind_qualification_boundary( - project.environment - ) - if observed is not None and observer_ids is not None: - report.observed_application_sha256 = hashlib.sha256( - observed.application_identity.encode("utf-8") - ).hexdigest() - report.observed_application_version_sha256 = hashlib.sha256( - observed.application_version.encode("utf-8") - ).hexdigest() - report.observed_session_sha256 = observed.session_identity_sha256 - report.observed_environment_digest = observed.environment_digest - report.qualification_environment_observer_id = observer_ids[0] - report.qualification_environment_observer_contract_sha256 = observer_ids[1] - report.observed_environment_binding_sha256 = observed.binding_sha256( - observer_id=observer_ids[0], - observer_contract_sha256=observer_ids[1], - ) - return refusal - def _bind_qualification_boundary( self, boundary: Any, *, surface_scoped: bool = False ) -> tuple[ @@ -15001,26 +14256,6 @@ def _require_locale_invariants_before_keyboard( return exc.reason return None - def _require_qualification_environment_current(self) -> None: - """Recheck the bound environment immediately before an input edge.""" - - call = self._qualification_environment_call - expected = self._qualification_environment_bound - if call is None or expected is None: - return - try: - observed = call() - except StructuralResolutionRefused: - raise - except Exception as exc: - raise StructuralResolutionRefused( - "qualification environment could not be observed before input" - ) from exc - if observed != expected: - raise StructuralResolutionRefused( - "qualification environment changed before input" - ) - def _resettle_after_interstitial( self, before_png: bytes ) -> tuple[bytes, Optional[str]]: diff --git a/packages/flow/scripts/generality_baseline.json b/packages/flow/scripts/generality_baseline.json index a20b2cb098..3093fbed02 100644 --- a/packages/flow/scripts/generality_baseline.json +++ b/packages/flow/scripts/generality_baseline.json @@ -221,7 +221,7 @@ "runtime/identity.py": { "mockmed": 1 }, - "runtime/replayer.py": { + "runtime/replay/_shared.py": { "mockmed": 1 }, "submit_binding.py": {