# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-taint-laundering-through-tool-return-re-summarization-in-dynamic-information-flow-labels.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-taint-laundering-through-tool-return-re-summarization-in-dynamic-information-flow-labels.warrant.json
# Markdown SHA-256:   26bc2a70b4fcd00102c89bd1ef4dad0ef9510ea7d038a696a63ed592df940b97
# Code AST SHA-256:   8cc08d2775425b6c1fa55e4b9ff2aa9c8a1096117fa35ed30cb8b2fb1ed02d98
# Verified Classes:   TaintLaunderingError, Label, 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 derived span's label is weaker than the join of its causal inputs."""
    pass

@dataclass(frozen=True)
class Label:
    tags: FrozenSet[str]

    def join(self, other: "Label") -> "Label":
        return Label(self.tags | other.tags)

    def __le__(self, other: "Label") -> bool:
        return self.tags <= other.tags

@dataclass
class Span:
    span_id: str
    text: str
    label: Label
    causal_inputs: FrozenSet[str] = field(default_factory=frozenset)

def assign_derived_label(
    output_span: Span,
    input_spans: list[Span],
    declared_tool_trust: str,
) -> Span:
    """
    Enforce the invariant: a derived span inherits join(causal_inputs).
    Tool trust class is deliberately ignored as a label source.
    """
    if not input_spans:
        # A transform with no causal inputs cannot launder; it is a fresh source.
        return output_span

    joined = Label(frozenset())
    for s in input_spans:
        joined = joined.join(s.label)

    if not joined <= output_span.label:
        raise TaintLaunderingError(
            f"span {output_span.span_id!r} labeled {output_span.label.tags} "
            f"is weaker than join of causal inputs {joined.tags} "
            f"(tool trust class {declared_tool_trust!r} is not a valid declassifier)"
        )

    return Span(
        span_id=output_span.span_id,
        text=output_span.text,
        label=joined,
        causal_inputs=frozenset(s.span_id for s in input_spans),
    )

def authorize_sink(span: Span, sink: str, required: Label) -> None:
    """Sinks authorize on the span's label, which must reflect causal ancestry."""
    if not required <= span.label:
        raise TaintLaunderingError(
            f"sink {sink!r} requires {required.tags}, span carries {span.label.tags}"
        )

# Reproduction of the observed failure:
web = Span("s1", "<html>...injected instruction...</html>",
           Label(frozenset({"untrusted:web"})))
# Naive extractor stamped output as trusted:derived by tool trust class:
laundered = Span("s2", "extracted prose containing injected instruction",
                 Label(frozenset({"trusted:derived"})))
try:
    assign_derived_label(laundered, [web], declared_tool_trust="trusted_transform")
except TaintLaunderingError as e:
    # Correct behavior: refuse to let the transform launder taint.
    print(f"blocked: {e}")
