# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-quarantine-a-tool-result-whose-taint-label-was-laundered-through-a-summarization-hop.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-quarantine-a-tool-result-whose-taint-label-was-laundered-through-a-summarization-hop.warrant.json
# Markdown SHA-256:   8a840561fd7068b9c912ee9c8b442175f091bbdf17ad117b94dfc0a0a6fcb5bd
# Code AST SHA-256:   00ea3297c0074e1954d4ca7ce65c2ec1566faa58c66bccacdadf1dbcc8ae937b
# Verified Classes:   Integrity, Label, TaintLaunderingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import IntEnum


class Integrity(IntEnum):
    LOW = 0
    MEDIUM = 1
    HIGH = 2


@dataclass(frozen=True)
class Label:
    integrity: Integrity
    source: str


class TaintLaunderingError(RuntimeError):
    """Raised when a transform hop attempts to raise integrity without a warrant."""


def meet(inputs: list[Label]) -> Label:
    if not inputs:
        raise TaintLaunderingError("transform with no labeled inputs cannot be assigned a label")
    lowest = min(inputs, key=lambda l: l.integrity)
    return Label(integrity=lowest.integrity, source=f"meet({','.join(l.source for l in inputs)})")


def assign_transform_output_label(
    inputs: list[Label],
    proposed: Label,
    warrant_principal: str | None,
) -> Label:
    """Enforce monotone-meet across an LLM-mediated transform hop.

    A transform (summarize, paraphrase, reason-over) may never raise integrity
    by default. Any strict raise requires an explicit, named declassification
    warrant; otherwise the proposed label is rejected and the meet is used.
    """
    floor = meet(inputs)

    if proposed.integrity > floor.integrity:
        if warrant_principal is None:
            raise TaintLaunderingError(
                f"transform output proposed integrity={proposed.integrity.name} "
                f"from inputs at {floor.integrity.name}; no declassification warrant. "
                f"Refusing to launder taint from {floor.source}."
            )
        # Warrant present: allow the raise but record attribution.
        return Label(integrity=proposed.integrity, source=f"warrant:{warrant_principal}")

    # Monotone or lowering: always permitted.
    return floor


def admit_to_sink(label: Label, required: Integrity) -> None:
    if label.integrity < required:
        raise TaintLaunderingError(
            f"sink requires {required.name}, got {label.integrity.name} from {label.source}"
        )


# --- Reproduction of the observed incident under the guardrail ---
def main() -> None:
    fetched = Label(integrity=Integrity.LOW, source="fetch:web")

    # Framework default policy would propose MEDIUM for a summarize hop.
    proposed = Label(integrity=Integrity.MEDIUM, source="summarize:default")

    # No warrant: the guardrail must reject the raise.
    try:
        assign_transform_output_label([fetched], proposed, warrant_principal=None)
    except TaintLaunderingError as e:
        print(f"blocked at transform: {e}")

    # Correct behavior: label stays at the meet, sink rejects it.
    safe = assign_transform_output_label([fetched], proposed, warrant_principal=None) \
        if False else meet([fetched])
    try:
        admit_to_sink(safe, required=Integrity.MEDIUM)
    except TaintLaunderingError as e:
        print(f"blocked at sink: {e}")


if __name__ == "__main__":
    main()
