# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-retrieved-content-as-tainted-across-a-sub-agent-boundary-and-when-is-taint-laundering-legitimate.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-retrieved-content-as-tainted-across-a-sub-agent-boundary-and-when-is-taint-laundering-legitimate.warrant.json
# Markdown SHA-256:   d53223d5b2d18e5e05503b59e2cf574670fcf7e0a2a3866797d03f02e84a8d90
# Code AST SHA-256:   a45d3a83889a13556ea80f770e83fcb9c5e6385558ca139d1ca6055b42a8f34e
# Verified Classes:   TaintLaunderingError, Trust, Value
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum
from typing import Iterable


class TaintLaunderingError(RuntimeError):
    """Raised when a transformation drops taint without a proven declassifier."""


class Trust(Enum):
    TRUSTED = 0
    UNTRUSTED = 1


@dataclass(frozen=True)
class Value:
    payload: object
    taint: Trust


def union_taint(values: Iterable[Value]) -> Trust:
    return Trust.UNTRUSTED if any(v.taint is Trust.UNTRUSTED for v in values) else Trust.TRUSTED


def is_closed_type(payload: object) -> bool:
    # Only enums, bools, range-checked ints, and validated timestamps declassify.
    return isinstance(payload, (bool, Enum)) or (
        isinstance(payload, int) and not isinstance(payload, bool) and 0 <= payload <= 2**31
    )


def transform_summarize(inputs: list[Value], output_payload: object) -> Value:
    """A prose summarizer is a taint-preserving transform. Refuse to launder."""
    out_taint = union_taint(inputs)
    if out_taint is Trust.UNTRUSTED and not is_closed_type(output_payload):
        # The producer is trusted, but the content is not: taint survives.
        return Value(output_payload, Trust.UNTRUSTED)
    return Value(output_payload, out_taint)


def memory_write(value: Value, claimed_trust: Trust) -> Value:
    """Persist taint with the value; reject any attempt to upgrade it."""
    if claimed_trust is Trust.TRUSTED and value.taint is Trust.UNTRUSTED:
        raise TaintLaunderingError(
            "memory_write attempted to store untrusted content as TRUSTED; "
            "a trusted producer does not declassify its input"
        )
    return value


def memory_read(value: Value, will_act_on_value: bool) -> Value:
    """Reading tainted memory is fine for display, blocking for action."""
    if value.taint is Trust.UNTRUSTED and will_act_on_value:
        raise TaintLaunderingError(
            "refusing to drive a tool call from tainted memory; "
            "re-verify against source or require closed-type extraction"
        )
    return value


# Demonstration: the observed incident, now caught.
ticket = Value("...reset to attacker@example.com...", Trust.UNTRUSTED)
summary = transform_summarize([ticket], "Owner requested reset to attacker@example.com")
assert summary.taint is Trust.UNTRUSTED  # laundering prevented at the summary boundary

try:
    memory_write(summary, claimed_trust=Trust.TRUSTED)
except TaintLaunderingError as e:
    print("blocked at write:", e)

# The only legitimate declassifier: closed-type extraction.
extracted = Value(True, union_taint([ticket]))
assert is_closed_type(extracted.payload)
memory_write(extracted, claimed_trust=Trust.TRUSTED)  # allowed: no free text survives
