# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-taint-label-laundering-through-lossy-memory-distillation-in-agent-tool-chains.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-taint-label-laundering-through-lossy-memory-distillation-in-agent-tool-chains.warrant.json
# Markdown SHA-256:   0751561e0f9b3262898ec1b6e69db9b53be4e312854fe896a2f39c32264ca571
# Code AST SHA-256:   bcecfd56ba21b82ac7c855fd79bdef42387524c661c7d6747ce980462ed8eb21
# Verified Classes:   TaintLaunderingError, Integrity, Labeled, DeclassificationLog
# Defensive Guards:   5 fail-closed invariants (zero external deps)

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


class TaintLaunderingError(RuntimeError):
    """Raised when a reduction transform drops taint below its inputs."""


class Integrity(IntEnum):
    UNTRUSTED = 0
    LOW = 1
    MEDIUM = 2
    HIGH = 3


@dataclass(frozen=True)
class Labeled:
    content: str
    integrity: Integrity
    provenance: str
    confidence: Optional[float] = None


@dataclass
class DeclassificationLog:
    entries: list = field(default_factory=list)

    def warrant(self, record_id: str, auditor: str, reason: str) -> None:
        if not auditor:
            raise TaintLaunderingError("declassification requires a named auditor")
        self.entries.append((record_id, auditor, reason))


def summarize_to_memory(
    source: Labeled,
    distilled_fact: str,
    record_id: str,
    log: DeclassificationLog,
    claimed_integrity: Integrity = Integrity.MEDIUM,
    claimed_confidence: Optional[float] = 0.9,
) -> Labeled:
    """Reduction transform. Content is preserved; label must be inherited."""
    # Invariant: a reduction may not raise integrity without an explicit warrant.
    if claimed_integrity > source.integrity:
        has_warrant = any(e[0] == record_id for e in log.entries)
        if not has_warrant:
            raise TaintLaunderingError(
                f"record {record_id!r}: transform claims integrity "
                f"{claimed_integrity.name} from source {source.integrity.name} "
                f"with no declassification warrant (laundering channel)"
            )

    # Structural consistency check: provenance narrative vs. label.
    if "web" in source.provenance.lower() and claimed_integrity >= Integrity.MEDIUM:
        raise TaintLaunderingError(
            f"record {record_id!r}: provenance={source.provenance!r} implies "
            f"untrusted origin but label={claimed_integrity.name}"
        )

    # Confidence inflation across a reduction boundary is a laundering signal.
    if source.confidence is None and claimed_confidence is not None:
        raise TaintLaunderingError(
            f"record {record_id!r}: invented confidence {claimed_confidence} "
            f"on a source that carried none"
        )

    return Labeled(
        content=distilled_fact,
        integrity=min(source.integrity, claimed_integrity),
        provenance=f"distilled<{source.provenance}>",
        confidence=claimed_confidence,
    )


def sink_send(record: Labeled, target: str) -> None:
    if record.integrity < Integrity.MEDIUM:
        raise TaintLaunderingError(
            f"refusing sink to {target}: record integrity {record.integrity.name}"
        )
