# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-a-poison-detection-pass-from-legitimizing-the-very-memory-it-quarantines.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-a-poison-detection-pass-from-legitimizing-the-very-memory-it-quarantines.warrant.json
# Markdown SHA-256:   82d6962bfc3add0c8bf773937847212b327bbb52da728ca4d9689469e446bf07
# Code AST SHA-256:   aec936fbb8186517d2f08d1c490e4a43f605f747e425fc335915f853a589f7e9
# Verified Classes:   TrustTier, TrustRatchetViolation, MemoryRecord
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import IntEnum


class TrustTier(IntEnum):
    QUARANTINED = 0
    UNAUDITED = 1
    # NOTE: there is deliberately no AUDITED_CLEARED tier above UNAUDITED.
    # Audit may only move trust down, never up.


class TrustRatchetViolation(RuntimeError):
    """Raised when an audit pass attempts to raise a record's trust tier,
    or when provenance is recomputed from derived (retrieval) state."""


@dataclass(frozen=True)
class MemoryRecord:
    record_id: str
    content: str
    # Provenance is frozen at write time from source attestation.
    # Never recomputed from retrieval frequency or co-citation.
    write_time_provenance: float
    trust_tier: TrustTier


def apply_audit_verdict(record: MemoryRecord, verdict: str, score: float,
                        quarantine_cutoff: float) -> MemoryRecord:
    """Apply a poison-detection verdict under the trust-monotonicity invariant.

    Invariant: post-audit trust_tier <= pre-audit trust_tier, always.
    A retained-but-suspect verdict is a no-op on trust.
    """
    if verdict == "quarantine" or score >= quarantine_cutoff:
        new_tier = TrustTier.QUARANTINED
    elif verdict in ("clean", "suspect_but_retained"):
        # Retained records keep their existing tier. Audit confers NO trust.
        new_tier = record.trust_tier
    else:
        raise TrustRatchetViolation(
            f"unknown verdict {verdict!r} for {record.record_id}"
        )

    if new_tier > record.trust_tier:
        raise TrustRatchetViolation(
            f"audit would raise trust of {record.record_id} "
            f"from {record.trust_tier.name} to {new_tier.name}; "
            "audit must be monotone-decreasing in trust"
        )

    return MemoryRecord(
        record_id=record.record_id,
        content=record.content,
        write_time_provenance=record.write_time_provenance,  # frozen
        trust_tier=new_tier,
    )


def recompute_provenance_from_retrieval(record: MemoryRecord,
                                        retrieval_count: int) -> float:
    """Guard: provenance must never be derived from retrieval state."""
    raise TrustRatchetViolation(
        f"refusing to recompute provenance for {record.record_id} from "
        f"retrieval_count={retrieval_count}; provenance is write-time-frozen"
    )
