# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-quarantine-a-provenance-label-rather-than-the-payload-taint-laundering-across-summarizer-and-retriever-hops.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-quarantine-a-provenance-label-rather-than-the-payload-taint-laundering-across-summarizer-and-retriever-hops.warrant.json
# Markdown SHA-256:   b5f5b3ea06c341fe2245bf3e7c4d017f7cb9c9ab167e5f1bee2778f54baf5294
# Code AST SHA-256:   d7a92ee92bc0d5144390dd7fdbf0abceeb2a387822c5801e623d320de42dbe4d
# Verified Classes:   TaintMonotonicityViolation, Provenance, Chunk
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class TaintMonotonicityViolation(RuntimeError):
    """Raised when a transformation hop emits a chunk whose taint is not a
    superset of the taint of its inputs — the signature of taint laundering."""


@dataclass(frozen=True)
class Provenance:
    taint: FrozenSet[str]          # e.g. frozenset({"untrusted", "injected"})
    ancestors: FrozenSet[str]      # origin ids in the provenance DAG

    def join(self, other: "Provenance") -> "Provenance":
        return Provenance(
            taint=self.taint | other.taint,
            ancestors=self.ancestors | other.ancestors,
        )


@dataclass
class Chunk:
    text: str
    provenance: Provenance


def transform_hop(
    inputs: list[Chunk],
    producer_id: str,
    producer_trusted: bool,
    output_text: str,
) -> Chunk:
    """A summarizer/rewriter hop. Enforces inherited-provenance: the output's
    taint is the join of all inputs' taint, never the producer's own identity.
    Producer-provenance is the vulnerable alternative and is rejected here."""
    if not inputs:
        raise TaintMonotonicityViolation("hop invoked with no inputs")

    inherited = Provenance(taint=frozenset(), ancestors=frozenset())
    for c in inputs:
        inherited = inherited.join(c.provenance)

    # Producer-provenance would assign taint based on producer_trusted.
    # That is exactly the laundering channel; assert it is not what we do.
    producer_taint = frozenset() if producer_trusted else frozenset({"untrusted"})
    if producer_taint != inherited.taint and not inherited.taint.issuperset(producer_taint):
        # Output would be *less* tainted than its inputs: laundering.
        raise TaintMonotonicityViolation(
            f"hop {producer_id} would emit taint {set(producer_taint)} "
            f"but inputs carry {set(inherited.taint)}; "
            f"ancestors={sorted(inherited.ancestors)}"
        )

    return Chunk(
        text=output_text,
        provenance=Provenance(
            taint=inherited.taint,
            ancestors=inherited.ancestors | {producer_id},
        ),
    )


def is_actionable(chunk: Chunk, required_trust: bool = True) -> bool:
    """Downstream gate: quarantines on lineage, not on payload signature."""
    if required_trust and "untrusted" in chunk.provenance.taint:
        return False
    return True
