# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-launders-a-tainted-label-through-a-summarization-tool-it-calls-on-its-own-retrieved-context.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-launders-a-tainted-label-through-a-summarization-tool-it-calls-on-its-own-retrieved-context.warrant.json
# Markdown SHA-256:   ad57de53443a19c59c71a2247dec8e8e1a00f6cdc8e4aba71dbadd0860ae24bf
# Code AST SHA-256:   f74a46f21a9e6202ae78eb9e6069ecb671361a891b5e888cd932c35b5d15919a
# Verified Classes:   TaintLaunderingError, Label, ToolSpec
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable

class TaintLaunderingError(RuntimeError):
    """Raised when a tool output would carry a trust label weaker than the
    join of its inputs' labels, i.e. an UNTRUSTED segment was laundered
    through a first-party transform."""

@dataclass(frozen=True)
class Label:
    trust: str          # "TRUSTED" | "UNTRUSTED"
    source: str
    conf: float = 1.0

    def join(self, other: "Label") -> "Label":
        if self.trust == "UNTRUSTED" or other.trust == "UNTRUSTED":
            return Label("UNTRUSTED", f"{self.source}+{other.source}",
                         min(self.conf, other.conf))
        return Label("TRUSTED", f"{self.source}+{other.source}",
                     min(self.conf, other.conf))

@dataclass
class ToolSpec:
    name: str
    trust_class: str
    passthrough: bool = False   # declares it preserves input content
    destructive: bool = False   # proves payload cannot survive

def resolve_output_label(tool: ToolSpec,
                         input_labels: Iterable[Label]) -> Label:
    base = Label(tool.trust_class, f"tool:{tool.name}")
    joined = base
    for lbl in input_labels:
        joined = joined.join(lbl)
    if joined.trust == "UNTRUSTED" and not tool.destructive:
        if not tool.passthrough:
            raise TaintLaunderingError(
                f"tool '{tool.name}' would launder UNTRUSTED input "
                f"({joined.source}) into label {base.trust}; declare "
                f"passthrough=true to inherit the join, or destructive=true "
                f"to prove the payload cannot survive."
            )
    return joined

# --- trace reproduction -------------------------------------------------
summarizer = ToolSpec(name="summarize", trust_class="TRUSTED",
                      passthrough=False, destructive=False)
web_chunk = Label("UNTRUSTED", "web/8821", conf=0.4)

try:
    out = resolve_output_label(summarizer, [web_chunk])
    print("output label:", out)
except TaintLaunderingError as e:
    print("BLOCKED:", e)
    # runtime records the causal chain and continues with redaction
