# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-propagates-an-injection-taint-label-through-a-tool-output-summarizer-that-legitimately-strips-the-tainted-span.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-propagates-an-injection-taint-label-through-a-tool-output-summarizer-that-legitimately-strips-the-tainted-span.warrant.json
# Markdown SHA-256:   640246d01f835d71288d15531e8abf80e24145b7ae9c24c5890438b31c1a690b
# Code AST SHA-256:   8488bfce3fd1a041a40a4e58ed523cb7cba7176fb235a12e35b2b1273aaccd0a
# Verified Classes:   TaintLaunderingError, Labeled
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class TaintLaunderingError(RuntimeError):
    """Raised when a transform emits a label less restrictive than the join of its inputs."""


SPAN_PRESERVING_WHITELIST = frozenset({"truncate", "lower", "json_loads_fixed_schema"})


@dataclass(frozen=True)
class Labeled:
    value: str
    labels: FrozenSet[str] = field(default_factory=frozenset)

    def join(self, other: "Labeled") -> "Labeled":
        return Labeled(self.value + other.value, self.labels | other.labels)


def propagate(
    transform_name: str,
    inputs: list[Labeled],
    output: Labeled,
    input_evicted: bool,
) -> Labeled:
    joined = frozenset().union(*(i.labels for i in inputs)) if inputs else frozenset()

    if transform_name not in SPAN_PRESERVING_WHITELIST:
        # Lossy / model-mediated transform: label must be joined from ancestry,
        # never re-derived from output content.
        if not output.labels.issuperset(joined):
            raise TaintLaunderingError(
                f"transform {transform_name!r} emitted labels {set(output.labels)} "
                f"but ancestry join is {set(joined)}; content-based re-derivation "
                f"is unsound for non-span-preserving transforms"
            )
        # No trusted artifact may sit over a reachable tainted source.
        if joined and not input_evicted:
            raise TaintLaunderingError(
                f"transform {transform_name!r} emitted a derived artifact while "
                f"tainted inputs remain reachable; evict or relabel inputs atomically"
            )

    return Labeled(output.value, output.labels | joined)


def summarize_and_write(
    source: Labeled,
    summary_text: str,
    evict_source: bool,
) -> Labeled:
    # A summarizer that legitimately drops the injected span. The output bytes
    # no longer match the tainted source, so a naive content scan would mark it
    # TRUSTED. We pass the naive label in and let propagate() reject it.
    naive_summary = Labeled(summary_text, frozenset({"TRUSTED:derived"}))
    return propagate(
        transform_name="llm_summarize",
        inputs=[source],
        output=naive_summary,
        input_evicted=evict_source,
    )
