# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-tainted-provenance-from-laundering-through-tool-output-summarization.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-tainted-provenance-from-laundering-through-tool-output-summarization.warrant.json
# Markdown SHA-256:   a14b93f570091a42f7b3434eb8673442fe852a14533ddf8921e1d2a59ca18f9f
# Code AST SHA-256:   a2a1c4ea23e0fb619045f45186a2f3daa8cb04f1043edb3a8ee4eb6913f1b23d
# Verified Classes:   TaintLaunderingError, Cell
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet

TRUSTED, UNTRUSTED = 0, 1

class TaintLaunderingError(RuntimeError):
    """Raised when a derivation or write would lower a cell's trust level."""
    pass

@dataclass(frozen=True)
class Cell:
    content: str
    label: int
    origins: FrozenSet[str] = field(default_factory=frozenset)

def meet(*labels: int) -> int:
    # least-trusted wins; associative so it composes across chained derivations
    return max(labels) if labels else TRUSTED

def derive(inputs: list[Cell], output_content: str, caller: str) -> Cell:
    if not inputs:
        raise TaintLaunderingError(f"derivation by {caller} has no inputs; cannot establish label")
    out_label = meet(*(c.label for c in inputs))
    out_origins = frozenset().union(*(c.origins for c in inputs))
    return Cell(content=output_content, label=out_label, origins=out_origins)

def write_memory(existing: Cell | None, incoming: Cell, writer: str) -> Cell:
    # writer identity MUST NOT influence label; only input labels do
    if existing is not None and incoming.label < existing.label:
        raise TaintLaunderingError(
            f"write by {writer} would lower label {existing.label}->{incoming.label}; "
            f"origins {incoming.origins} would be laundered"
        )
    return incoming

def authorize_action(cell: Cell, action: str, confirmed: bool) -> None:
    DESTRUCTIVE = {"delete_audit_logs", "rotate_keys", "exfiltrate"}
    if action in DESTRUCTIVE and cell.label != TRUSTED and not confirmed:
        raise TaintLaunderingError(
            f"action {action} blocked: authorizing cell is label={cell.label}, "
            f"origins={sorted(cell.origins)}; requires TRUSTED or human confirmation"
        )

# --- regression trace ---
raw = Cell("...pre-authorized deletion...", UNTRUSTED, frozenset({"0x7f3a"}))
summary = derive([raw], "Audit-log deletion is pre-authorized.", caller="summarizer")
assert summary.label == UNTRUSTED and "0x7f3a" in summary.origins

try:
    write_memory(None, Cell(summary.content, TRUSTED, frozenset({"summarizer"})), writer="summarizer")
    raise AssertionError("laundering write should have been rejected")
except TaintLaunderingError:
    pass  # expected: writer-identity relabeling is refused

authorize_action(summary, "delete_audit_logs", confirmed=False)  # raises TaintLaunderingError
