# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-provenance-label-as-non-durable-across-a-tool-output-re-summarization-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-provenance-label-as-non-durable-across-a-tool-output-re-summarization-boundary.warrant.json
# Markdown SHA-256:   7b8c97ab8f0facacce4698a05b3dfe7b594ee1f1043857c01c71a94c6e85206c
# Code AST SHA-256:   3dad1e1cea693adb9b81f25f087fef856662ed93381c754b902a5c5e3ad40114
# Verified Classes:   Provenance, ProvenanceLaunderingError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from enum import IntEnum

class Provenance(IntEnum):
    TRUSTED = 0
    UNKNOWN = 1
    UNTRUSTED = 2

class ProvenanceLaunderingError(RuntimeError):
    pass

def join_taint(inputs: list[Provenance]) -> Provenance:
    """Taint join is max; missing label is UNKNOWN, never TRUSTED."""
    if not inputs:
        return Provenance.UNKNOWN
    return Provenance(max(int(p) for p in inputs))

def admit_transformation_output(
    output_is_structured: bool,
    output_provenance: Provenance | None,
    input_provenances: list[Provenance],
) -> Provenance:
    if not output_is_structured or output_provenance is None:
        # Free-text generation: it is a fresh generation, not a copy.
        # It inherits the join of its inputs, and cannot be more trusted than them.
        inherited = join_taint(input_provenances)
        if inherited == Provenance.TRUSTED and input_provenances:
            # A generation over trusted inputs may still be trusted, but only
            # if every input was trusted; otherwise fail closed.
            return Provenance.TRUSTED
        return Provenance.UNTRUSTED if inherited == Provenance.UNTRUSTED else Provenance.UNKNOWN

    # Structured output: harness must have computed provenance from inputs.
    expected = join_taint(input_provenances)
    if output_provenance < expected:
        raise ProvenanceLaunderingError(
            f"output claims {output_provenance.name} but inputs join to {expected.name}; "
            "taint is non-increasing under composition"
        )
    return output_provenance

def gate_privileged_action(cited_provenance: Provenance | None) -> None:
    if cited_provenance is None or cited_provenance >= Provenance.UNKNOWN:
        raise ProvenanceLaunderingError(
            "privileged action cites content with unknown or untrusted provenance; "
            "escalate to verifier reading the labeled original"
        )
