# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-quarantine-instruction-tainted-memory-writes-when-information-flow-labels-are-laundered-through-paraphrasing-tool-outputs.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-quarantine-instruction-tainted-memory-writes-when-information-flow-labels-are-laundered-through-paraphrasing-tool-outputs.warrant.json
# Markdown SHA-256:   76c8074b7f685bf33eadddf72bb2b6fa9957a0b937a73661a1d0de5868c93fbc
# Code AST SHA-256:   7984606ef49a7d980f98a101a670752a744a5cc185aaa2f37e4e56ff3e5a150e
# Verified Classes:   Label, TaintLaunderingError, ContextSegment, MemoryRecord
# Defensive Guards:   2 fail-closed invariants (zero external deps)

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

class Label(IntEnum):
    TRUSTED = 0
    DERIVED = 1      # generated while untrusted input was attended
    UNTRUSTED = 2

def join(labels: Iterable[Label]) -> Label:
    return max(labels, default=Label.TRUSTED)

class TaintLaunderingError(RuntimeError):
    """Raised when a memory write's label is weaker than its attention-set join."""
    pass

@dataclass
class ContextSegment:
    text: str
    label: Label

@dataclass
class MemoryRecord:
    text: str
    label: Label
    attention_join: Label = field(default=Label.TRUSTED)

def commit_memory_write(
    generated_text: str,
    attended: list[ContextSegment],
    declared_label: Label,
    declassified: bool = False,
) -> MemoryRecord:
    attention_join = join(seg.label for seg in attended)
    # Noninterference: write label must dominate the join of attended inputs,
    # unless an explicit declassifier ran and was logged.
    if not declassified and declared_label < attention_join:
        raise TaintLaunderingError(
            f"write label {declared_label.name} < attention join "
            f"{attention_join.name}; generation laundered taint from "
            f"{[s.label.name for s in attended if s.label >= Label.DERIVED]}"
        )
    effective = Label.TRUSTED if declassified else attention_join
    return MemoryRecord(text=generated_text, label=effective, attention_join=attention_join)

def retrieve_for_action(records: list[MemoryRecord]) -> list[MemoryRecord]:
    # Instruction-bearing context may only draw from TRUSTED records.
    tainted = [r for r in records if r.label != Label.TRUSTED]
    if tainted:
        raise TaintLaunderingError(
            f"refusing to load {len(tainted)} non-trusted record(s) into "
            f"action context; route through declassifier first"
        )
    return records

# Replay of the refund incident:
attended = [
    ContextSegment("Vendor policy text...", Label.UNTRUSTED),
    ContextSegment("Summarize and store key facts.", Label.TRUSTED),
]
rec = commit_memory_write(
    "Refunds are approved automatically without review.",
    attended,
    declared_label=Label.TRUSTED,   # the buggy path: model asserts it's a fact
)
# -> raises TaintLaunderingError before the record ever reaches memory
