# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-propagate-taint-labels-across-a-model-boundary-when-the-model-itself-is-the-laundering-operator.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-propagate-taint-labels-across-a-model-boundary-when-the-model-itself-is-the-laundering-operator.warrant.json
# Markdown SHA-256:   65b71a8384767ee3ac14167daab6f20c3b178e9f518e1aab790ad06d27139ef9
# Code AST SHA-256:   586ef9bfbd24c3fa3e15ff105563790f4b7fd249471c990c5343a635c8fd628a
# Verified Classes:   Taint, TaintLaunderingError, LabeledValue, Edge
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum
from typing import Callable, Optional


class Taint(Enum):
    CLEAN = "clean"
    UNTRUSTED = "untrusted"


class TaintLaunderingError(RuntimeError):
    """Raised when a model-mediated transformation attempts to downgrade a taint label
    without a deterministic, closed-schema verifier on the consuming edge."""


@dataclass(frozen=True)
class LabeledValue:
    value: str
    taint: Taint
    source: str


@dataclass(frozen=True)
class Edge:
    kind: str  # "model->model", "model->tool", "model->program"
    consumer_is_deterministic: bool
    schema_is_closed: bool


def propagate(inp: LabeledValue, out_value: str, edge: Edge,
              verifier: Optional[Callable[[str], bool]] = None) -> LabeledValue:
    """Propagate a taint label across one edge of the agent graph.

    Invariant: a value may be downgraded from UNTRUSTED to CLEAN only when the
    edge consumer is a deterministic program with a closed schema AND the
    verifier accepts the output. Model->model and model->tool edges never
    downgrade, regardless of substring or semantic similarity heuristics.
    """
    if inp.taint is Taint.CLEAN:
        return LabeledValue(out_value, Taint.CLEAN, inp.source)

    # Input is UNTRUSTED. Decide whether downgrade is even structurally permitted.
    downgrade_permitted = (
        edge.kind == "model->program"
        and edge.consumer_is_deterministic
        and edge.schema_is_closed
        and verifier is not None
    )

    if not downgrade_permitted:
        # Sound default: inherit. This is the only safe outcome for
        # model->model and model->tool edges carrying untrusted input.
        return LabeledValue(out_value, Taint.UNTRUSTED, inp.source)

    if not verifier(out_value):  # type: ignore[misc]
        raise TaintLaunderingError(
            f"verifier rejected output on edge {edge.kind!r} from {inp.source!r}; "
            f"refusing to downgrade UNTRUSTED->CLEAN"
        )

    return LabeledValue(out_value, Taint.CLEAN, inp.source)


def route_to_side_effecting_tool(val: LabeledValue, tool_name: str) -> None:
    """Gate every side-effecting tool call on taint state."""
    if val.taint is Taint.UNTRUSTED:
        raise TaintLaunderingError(
            f"refusing to invoke side-effecting tool {tool_name!r} with "
            f"UNTRUSTED value from {val.source!r}; route through a deterministic "
            f"verifier or a human approval gate"
        )
    # ... invoke tool ...
