# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-taint-labels-across-memory-write-boundaries-to-prevent-prompt-injection-re-entry-through-distilled-episo.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-quarantine-taint-labels-across-memory-write-boundaries-to-prevent-prompt-injection-re-entry-through-distilled-episo.warrant.json
# Markdown SHA-256:   cbff949f6050d738b3d022f145c308f949d03e1b15f79c04b8776fa0118dae37
# Code AST SHA-256:   f84509863b5c492fc3c0d9edd01a1404deadd029e0bcc114e2b2b48693181dbc
# Verified Classes:   TaintLaunderingError, Span, MemoryRecord
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class TaintLaunderingError(RuntimeError):
    """Raised when a memory write would drop taint labels present on its supporting spans."""


@dataclass(frozen=True)
class Span:
    content: str
    taint: FrozenSet[str] = field(default_factory=frozenset)


@dataclass(frozen=True)
class MemoryRecord:
    proposition: str
    taint: FrozenSet[str]
    source: str


def distill_and_write(
    supporting_spans: list[Span],
    proposition: str,
    store_supports_taint: bool,
    trusted_corroboration: bool = False,
) -> MemoryRecord:
    """Persist a distilled proposition, refusing to widen trust.

    Invariant: taint(record) must be a superset of the union of taint on all
    supporting spans. A write that drops taint is a laundering boundary
    violation and must not reach the store.
    """
    inherited: FrozenSet[str] = frozenset().union(*(s.taint for s in supporting_spans))

    if not inherited:
        return MemoryRecord(proposition=proposition, taint=frozenset(), source="agent_inference")

    if trusted_corroboration:
        # A trusted source independently re-derives the proposition; the
        # untrusted origin no longer dominates the record's trust class.
        return MemoryRecord(proposition=proposition, taint=frozenset(), source="corroborated")

    if not store_supports_taint:
        raise TaintLaunderingError(
            f"Refusing to write proposition {proposition!r}: supporting spans carry "
            f"taint {sorted(inherited)} but the memory schema cannot round-trip it. "
            f"Writing would launder untrusted content into first-party knowledge."
        )

    return MemoryRecord(proposition=proposition, taint=inherited, source="distilled")


def retrieve_and_gate(record: MemoryRecord, allow_untrusted_actions: bool) -> str:
    """Return the proposition only if its taint class is admissible for the next action."""
    if record.taint and not allow_untrusted_actions:
        raise TaintLaunderingError(
            f"Record {record.proposition!r} carries taint {sorted(record.taint)}; "
            f"cannot be used to drive a trusted action without corroboration."
        )
    return record.proposition
