# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-tool-s-output-as-tainted-by-the-input-it-was-derived-from-and-when-does-provenance-laundering-across-a.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-tool-s-output-as-tainted-by-the-input-it-was-derived-from-and-when-does-provenance-laundering-across-a.warrant.json
# Markdown SHA-256:   f81cf58930c3f135e456ec0ae47b631cbe6eb9063c8dd6ab0fe73cc8e2083c17
# Code AST SHA-256:   19a95d36986947f8334772599da44825e2c63260ffeb11efb650ee66c483c50a
# Verified Classes:   TaintLaunderingError, ToolClass, Taint, Value, ToolSpec
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from enum import Enum
from dataclasses import dataclass


class TaintLaunderingError(RuntimeError):
    """Raised when a tainted value would cross a generative tool boundary
    and be re-admitted to a trusted context without declassification."""
    pass


class ToolClass(Enum):
    BYTE_CONTAINMENT = "byte_containment"
    ATTESTED_NON_LLM = "attested_non_llm"
    GENERATIVE = "generative"


class Taint(Enum):
    NONE = 0
    UNKNOWN = 1
    EXTERNAL_UNTRUSTED = 2


@dataclass(frozen=True)
class Value:
    payload: str
    taint: Taint


@dataclass(frozen=True)
class ToolSpec:
    name: str
    cls: ToolClass


def resolve_output_taint(spec: ToolSpec, input_value: Value) -> Taint:
    """Compute the taint of a tool's output from the taint of its input.

    Fail-closed: any tool not provably byte-containment or attested
    non-LLM yields UNKNOWN, never NONE.
    """
    if spec.cls is ToolClass.BYTE_CONTAINMENT:
        return input_value.taint
    if spec.cls is ToolClass.ATTESTED_NON_LLM:
        # Contract requires the tool to echo the label; if it cannot,
        # we do not trust the echo and fall through to UNKNOWN.
        return input_value.taint
    # GENERATIVE, including every LLM-backed sub-agent.
    return Taint.UNKNOWN


def admit_to_trusted_context(value: Value, declassified_by: str | None) -> Value:
    """Gate a value into a trusted context. UNKNOWN and EXTERNAL_UNTRUSTED
    are rejected unless a distinct principal declassified them."""
    if value.taint in (Taint.UNKNOWN, Taint.EXTERNAL_UNTRUSTED):
        if declassified_by is None:
            raise TaintLaunderingError(
                f"refusing to admit taint={value.taint.name} to trusted context "
                f"without declassification (payload starts: {value.payload[:40]!r})"
            )
    return value


def run_pipeline(doc: Value, summarize: ToolSpec) -> Value:
    # doc arrives from a retrieval tool, already tainted.
    if doc.taint is not Taint.EXTERNAL_UNTRUSTED:
        raise TaintLaunderingError("retrieval output must arrive EXTERNAL_UNTRUSTED")

    summary_taint = resolve_output_taint(summarize, doc)
    summary = Value(payload=f"<summary of {len(doc.payload)} chars>", taint=summary_taint)

    # Planner context is trusted; admission must not silently pass UNKNOWN.
    return admit_to_trusted_context(summary, declassified_by=None)
