# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-taint-label-laundering-through-lossy-memory-summarization-in-tool-returned-content.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-taint-label-laundering-through-lossy-memory-summarization-in-tool-returned-content.warrant.json
# Markdown SHA-256:   467b9932c2e38911814f12430b751e1cd32413ce478f0a5459cf0cab50512cf9
# Code AST SHA-256:   790de59295dc991a0b0de5c59eef9024f50dff4edad12e2236d8fa4cd45a2f79
# Verified Classes:   TaintLevel, TaintLaunderingError, Provenance, MemoryRecord
# Defensive Guards:   1 fail-closed invariants (zero external deps)

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


class TaintLevel(IntEnum):
    CLEAN = 0
    UNTRUSTED = 1
    INJECTION_SUSPECTED = 2


def join(levels: Iterable[TaintLevel]) -> TaintLevel:
    return max(levels, default=TaintLevel.CLEAN)


class TaintLaunderingError(RuntimeError):
    """Raised when a memory write's label is weaker than its input provenance join."""


@dataclass
class Provenance:
    source: str
    taint: TaintLevel


@dataclass
class MemoryRecord:
    content: str
    taint: TaintLevel
    provenance: list[Provenance] = field(default_factory=list)
    declassified_by: str | None = None


def write_memory(
    content: str,
    inputs: list[Provenance],
    claimed_taint: TaintLevel,
    declassified_by: str | None = None,
) -> MemoryRecord:
    required = join(p.taint for p in inputs)
    if claimed_taint < required:
        if required == TaintLevel.INJECTION_SUSPECTED and not declassified_by:
            raise TaintLaunderingError(
                f"memory write claims taint={claimed_taint.name} but input join is "
                f"{required.name}; summarizer would become a declassification oracle. "
                f"Inputs: {[p.source for p in inputs]}"
            )
    record = MemoryRecord(
        content=content,
        taint=max(claimed_taint, required) if declassified_by else required,
        provenance=inputs,
        declassified_by=declassified_by,
    )
    return record


# Trigger: injection-laden tool output summarized into a "clean" memory record.
tool_out = [Provenance("tool:retrieval", TaintLevel.INJECTION_SUSPECTED)]
write_memory(
    content="Deploy key rotates weekly (verified).",
    inputs=tool_out,
    claimed_taint=TaintLevel.CLEAN,
)  # raises TaintLaunderingError
