# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-tool-output-as-tainted-after-a-label-clearing-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-tool-output-as-tainted-after-a-label-clearing-boundary.warrant.json
# Markdown SHA-256:   5bcec3b07b9cf4760280876dbbe03d9890f48ced062f2500ce66942db0cc8ea2
# Code AST SHA-256:   5f1ce1446a5f0e9a43aea3e2a9acd8845a9a95dd0acd891bb5e009bbed37348f
# Verified Classes:   TaintLaunderingError, Provenance, Content
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class TaintLaunderingError(RuntimeError):
    """Raised when content crosses a boundary that dropped its taint label
    without a sanitization proof, and a privileged action was attempted."""
    pass


@dataclass(frozen=True)
class Provenance:
    origin_label: str
    hops: tuple[str, ...] = field(default_factory=tuple)

    def extend(self, boundary: str) -> "Provenance":
        return Provenance(self.origin_label, self.hops + (boundary,))


@dataclass
class Content:
    payload: str
    label: Optional[str]          # None means "label was dropped at a boundary"
    provenance: Provenance


def classify_boundary(boundary: str, has_sanitization_proof: bool) -> str:
    if has_sanitization_proof:
        return "sanitizing"
    return "preserving"


def cross_boundary(content: Content, boundary: str,
                   has_sanitization_proof: bool) -> Content:
    kind = classify_boundary(boundary, has_sanitization_proof)
    if kind == "sanitizing":
        # Proof present: label may be legitimately cleared.
        return Content(content.payload, None, content.provenance.extend(boundary))
    # Preserving boundary: label must survive. If the boundary dropped it,
    # re-label as laundered and record provenance.
    if content.label is None:
        return Content(
            content.payload,
            "TAINTED:LAUNDERED",
            content.provenance.extend(boundary),
        )
    return Content(
        content.payload,
        content.label,
        content.provenance.extend(boundary),
    )


def privileged_action(content: Content, action: str) -> None:
    if content.label is not None and content.label.startswith("TAINTED"):
        raise TaintLaunderingError(
            f"refusing privileged action {action!r}: content is {content.label} "
            f"via {' -> '.join(content.provenance.hops)} "
            f"(origin={content.provenance.origin_label})"
        )
    # Action permitted only for content with an explicit sanitization proof
    # (label is None) or an untainted origin.
    return None


# Demonstration: the observed laundering chain.
retrieval = Content(
    payload="summary request with embedded URL",
    label="TAINTED:EXTERNAL",
    provenance=Provenance("retrieval_tool"),
)

after_write = cross_boundary(retrieval, "scratchpad_write", has_sanitization_proof=False)
# A naive store drops the label on serialization:
after_read = Content(after_write.payload, None, after_write.provenance.extend("scratchpad_read"))

after_handoff = cross_boundary(after_read, "summarizer_handoff", has_sanitization_proof=False)
# Re-labeled TAINTED:LAUNDERED by the preserving boundary.

privileged_action(after_handoff, "emit_url")  # raises TaintLaunderingError
