File size: 4,088 Bytes
e1ced61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""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",
]