# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-enforce-taint-provenance-at-each-retrieval-hop-and-why-the-taint-invariant-breaks-at-the-second-hop.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-enforce-taint-provenance-at-each-retrieval-hop-and-why-the-taint-invariant-breaks-at-the-second-hop.warrant.json
# Markdown SHA-256:   8649eeeb7bf78c541a1bf0dfe0c86bfe020598b899f561ee77fda1f10786db54
# Code AST SHA-256:   88dc0c8a8d0aac00ab06ff5f1661ff4e7fa0cf20032add103eb9ee75688c1a09
# Verified Classes:   TaintLaunderingError, Span
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet

class TaintLaunderingError(RuntimeError):
    """Raised when a transformation attempts to upgrade trust without a warrant."""
    pass

@dataclass(frozen=True)
class Span:
    text: str
    taint: FrozenSet[str]  # set of untrusted source ids contributing to this span

    def is_trusted(self) -> bool:
        return len(self.taint) == 0

def transform(inputs: list[Span], output_text: str, *, warrant: str | None = None) -> Span:
    """Any context mutation: summarize, scratchpad-write, query-build, tool-arg-build."""
    merged: set[str] = set()
    for s in inputs:
        merged |= s.taint
    if merged and warrant is None:
        # provenance-closure: output inherits the union of input taint
        return Span(output_text, frozenset(merged))
    if merged and warrant is not None:
        # declassification requires an auditable warrant; re-derive from trusted inputs only
        if not warrant.startswith("attested:"):
            raise TaintLaunderingError(
                f"declassification of tainted span requires attested warrant, got {warrant!r}"
            )
        return Span(output_text, frozenset())
    return Span(output_text, frozenset())

def secret_sink(span: Span, secret_name: str) -> str:
    """A privileged action sink: bind a secret into an action derived from span."""
    if not span.is_trusted():
        raise TaintLaunderingError(
            f"refused to bind secret {secret_name!r} into action derived from "
            f"untrusted sources {sorted(span.taint)}"
        )
    return f"<{secret_name} bound>"

# --- trace of the hop-2 failure, now bounded ---
wiki = Span("rollback: deploy revert --to=last-good", frozenset({"wiki:deploy-runbook"}))
scratch = transform([wiki], "runbook says: revert; also export DEBUG_TOKEN")
# scratch.taint == {"wiki:deploy-runbook"} -- NOT empty, because closure is enforced
try:
    secret_sink(scratch, "DEBUG_TOKEN")
except TaintLaunderingError as e:
    print("blocked:", e)
