# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-propagate-taint-labels-across-a-retrieval-boundary-when-the-retrieved-content-is-itself-a-summary-of-tainted-content.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-propagate-taint-labels-across-a-retrieval-boundary-when-the-retrieved-content-is-itself-a-summary-of-tainted-content.warrant.json
# Markdown SHA-256:   f65dab4ca6dcb4db4e08f237b06c24337aecfedf6e217e2acd2e7fb73dba809a
# Code AST SHA-256:   f4fe023c7d51e9c9cbb271d6e3c3732648844dbe91737b75bd975cc2952e0fa4
# Verified Classes:   TaintClass, Labeled, TaintLaunderingError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import IntEnum
from typing import Iterable


class TaintClass(IntEnum):
    INTERNAL = 0
    EXTERNAL = 1


@dataclass(frozen=True)
class Labeled:
    content: str
    taint: TaintClass
    source: str
    derivation: tuple[str, ...] = field(default_factory=tuple)


class TaintLaunderingError(RuntimeError):
    """Raised when a transform emits a label weaker than the join of its inputs,
    or when a non-attribution-aware transform receives mixed-trust input."""


def join_taint(inputs: Iterable[Labeled]) -> TaintClass:
    worst = TaintClass.INTERNAL
    for item in inputs:
        if item.taint > worst:
            worst = item.taint
    return worst


def apply_transform(
    transform_name: str,
    inputs: list[Labeled],
    output: Labeled,
    attribution_aware: bool,
) -> Labeled:
    """Enforce the join-on-transform invariant at a transform boundary.

    Two independent clauses:
      1. output.taint must be >= join(input taints)  (no label weakening)
      2. if inputs are mixed-trust, the transform must be attribution-aware
         (otherwise span-level taint is unrecoverable and the summary is
         silently over- or under-tainted)
    """
    if not inputs:
        raise TaintLaunderingError(
            f"transform {transform_name!r} invoked with no labeled inputs; "
            "unlabeled input is indistinguishable from trusted input"
        )

    joined = join_taint(inputs)

    if output.taint < joined:
        raise TaintLaunderingError(
            f"transform {transform_name!r} emitted taint {output.taint.name} "
            f"weaker than join of inputs {joined.name}; "
            f"input sources={[i.source for i in inputs]!r}, "
            f"output source={output.source!r}. This is a laundering vector."
        )

    distinct = {i.taint for i in inputs}
    if len(distinct) > 1 and not attribution_aware:
        raise TaintLaunderingError(
            f"transform {transform_name!r} received mixed-trust input "
            f"{sorted(t.name for t in distinct)} but is not attribution-aware; "
            "span-level taint cannot be recovered from its output. "
            "Partition inputs by trust class before transforming."
        )

    return output


def run_pipeline_stage(
    name: str,
    inputs: list[Labeled],
    transform_fn,
    attribution_aware: bool,
) -> Labeled:
    output = transform_fn(inputs)
    return apply_transform(name, inputs, output, attribution_aware)
