"""Question-preserving visual intervention operators (``docs/02`` §10, P5). Four deterministic operators transform a structured :class:`World` without touching the human-authored question/choices and without assigning a gold state: * :class:`RedactSolidOperator` (``REDACT_SOLID_V1``) — hide a node's pixels, keep it in the world (§10.1). * :class:`CleanDeleteOperator` (``CLEAN_DELETE_V1``) — delete a node and its incident relations, re-validate feasibility (§10.3). * :class:`SubstituteOperator` (``SUBSTITUTE_V1``) — sample a typed, admissible new value (§10.4), rejecting out-of-range / colliding / infeasible / choice- leaving substitutes. * :class:`ControlMatchedOperator` (``CONTROL_MATCHED_V1``) — edit a matched non-dependency control node (§10.5). Plus :mod:`completion` (§11.2 ``U_MISSING`` enumeration) and :mod:`feasibility` (structural well-formedness / contradiction checks). Every operator returns an immutable :class:`TransformedWorld`; the source world is never mutated, and the resulting state is left for P7 to derive by execution. """ from __future__ import annotations from typing import Any from ..sources.base import World from .base import ( InterventionError, InterventionOperator, OperatorName, TransformedWorld, hidden_nodes, set_hidden, transformed_hash, with_value, world_sha256, ) from .clean_delete import CleanDeleteOperator from .clevr import ( ATTRIBUTE_DOMAINS, ClevrAttribute, ClevrMutation, ClevrMutationKind, redact_clevr_attribute, substitute_clevr_attribute, with_clevr_attribute, ) from .clevr import object_ids as clevr_object_ids from .completion import Completion, completion_candidates, iter_completions, valid_values from .control import ControlMatchedOperator, find_control_match from .feasibility import FeasibilityError, is_feasible from .redact_solid import RedactSolidOperator from .substitute import SubstituteOperator # The four operators carry heterogeneous required keyword args (``dsl`` for # delete/substitute, ``executor``/``program``/``choices`` for the multiple-choice # substitute gate), so the registry is typed as the concrete union — not the # :class:`InterventionOperator` Protocol, whose fixed signature cannot promise # every operator's required kwargs. The union keeps call sites type-checked. AnyOperator = ( RedactSolidOperator | CleanDeleteOperator | SubstituteOperator | ControlMatchedOperator ) _OPERATORS: dict[OperatorName, AnyOperator] = { "REDACT_SOLID_V1": RedactSolidOperator(), "CLEAN_DELETE_V1": CleanDeleteOperator(), "SUBSTITUTE_V1": SubstituteOperator(), "CONTROL_MATCHED_V1": ControlMatchedOperator(), } def get_operator(name: OperatorName) -> AnyOperator: """Look up an operator by its schema enum name.""" try: return _OPERATORS[name] except KeyError as exc: # pragma: no cover - defensive, schema-bound raise InterventionError(f"unknown intervention operator {name!r}") from exc def apply_operator( name: OperatorName, world: World, target_node: str, *, seed: int, **kwargs: Any, ) -> TransformedWorld: """Convenience: dispatch ``name`` over ``(world, target_node)``.""" return get_operator(name).apply(world, target_node, seed=seed, **kwargs) __all__ = [ "AnyOperator", "ATTRIBUTE_DOMAINS", "CleanDeleteOperator", "Completion", "ControlMatchedOperator", "ClevrAttribute", "ClevrMutation", "ClevrMutationKind", "FeasibilityError", "InterventionError", "InterventionOperator", "OperatorName", "RedactSolidOperator", "SubstituteOperator", "TransformedWorld", "apply_operator", "completion_candidates", "clevr_object_ids", "find_control_match", "get_operator", "hidden_nodes", "is_feasible", "iter_completions", "set_hidden", "redact_clevr_attribute", "substitute_clevr_attribute", "transformed_hash", "valid_values", "with_value", "with_clevr_attribute", "world_sha256", ]