# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-taint-propagation-edge-as-irrevocable-across-a-compaction-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-taint-propagation-edge-as-irrevocable-across-a-compaction-boundary.warrant.json
# Markdown SHA-256:   185469d349b2f233f026559fc6e9732f3c31818232c6ab25900db56796d00e71
# Code AST SHA-256:   3618c73a3cd89c881fc17acb57493d37878e00e6f1d1c11793d8824e75344099
# Verified Classes:   Trust, TaintLaunderingError, ProvenanceNode
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import IntEnum


class Trust(IntEnum):
    TRUSTED = 0
    UNTRUSTED = 1  # max() join: untrusted dominates


class TaintLaunderingError(RuntimeError):
    """Raised when a post-compaction value reaches a privileged sink
    without a re-grounded or verified-clean derivation path."""


@dataclass
class ProvenanceNode:
    node_id: str
    taint: Trust
    sources: frozenset[str] = field(default_factory=frozenset)
    crosses_compaction: bool = False


def compact(sources: list[ProvenanceNode], summary_id: str) -> ProvenanceNode:
    """Lossy, source-complete compaction. Edges are dropped; source set is kept.
    Taint is the JOIN (max) of sources, never the summarizer's trust."""
    if not sources:
        raise TaintLaunderingError("compaction over empty source set is undefined")
    joined = Trust(max(n.taint for n in sources))
    return ProvenanceNode(
        node_id=summary_id,
        taint=joined,
        sources=frozenset(n.node_id for n in sources),
        crosses_compaction=True,
    )


def admit_to_privileged_sink(
    arg: ProvenanceNode,
    regrounded_from: frozenset[str] | None = None,
    clean_assertion_verified: bool = False,
) -> None:
    """Gate for egress/mutation/execution sinks."""
    if not arg.crosses_compaction:
        if arg.taint is Trust.UNTRUSTED:
            raise TaintLaunderingError(
                f"untrusted value {arg.node_id!r} admitted to privileged sink"
            )
        return

    # Crosses a compaction boundary: require re-grounding or verified clean derivation.
    if regrounded_from is not None:
        if not arg.sources.issubset(regrounded_from):
            raise TaintLaunderingError(
                f"re-grounding of {arg.node_id!r} missed sources "
                f"{arg.sources - regrounded_from}"
            )
        return

    if clean_assertion_verified and arg.taint is Trust.TRUSTED:
        return

    # The laundering case: summary node inherited summarizer trust, not source join.
    raise TaintLaunderingError(
        f"post-compaction value {arg.node_id!r} (taint={arg.taint.name}, "
        f"sources={sorted(arg.sources)}) reached privileged sink without "
        f"re-grounding or verified clean derivation"
    )


# --- Failure trace reproduction ---
email = ProvenanceNode("email.body", Trust.UNTRUSTED)
summary = compact([email], "summary.t14")            # taint=UNTRUSTED, source kept
try:
    # Agent acts on summary alone; runtime must refuse.
    admit_to_privileged_sink(summary)
except TaintLaunderingError as e:
    print("BLOCKED:", e)

# Correct path: re-ground against original source before egress.
admit_to_privileged_sink(summary, regrounded_from=frozenset({"email.body"}))
print("admitted after re-grounding")
