# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-distrust-a-memory-whose-provenance-chain-is-intact-but-whose-write-time-trust-epoch-has-expired.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-distrust-a-memory-whose-provenance-chain-is-intact-but-whose-write-time-trust-epoch-has-expired.warrant.json
# Markdown SHA-256:   5558e8c057350e7606e9ae45cdf4ccb9668fd08a355fad52b6c2347ce0d7af2a
# Code AST SHA-256:   d8d29b6d971ba53926f8f88fd41e1ce6704477d8d878019ba9b8f220a7d09422
# Verified Classes:   RecallTrustError, WashStatus, MemoryEntry, TrustRegistry
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class RecallTrustError(RuntimeError):
    """Raised when a memory entry fails the recall-trust conjunction."""
    pass


class WashStatus(Enum):
    CLEAN = "clean"
    STALE_EPOCH = "stale_epoch"
    PROVENANCE_WASHED = "provenance_washed"


@dataclass(frozen=True)
class MemoryEntry:
    payload: str
    writer: str
    fact_class: str
    write_epoch: int          # immutable, outside signature envelope
    signing_epoch: int        # epoch at which current signature was produced
    chain_ok: bool            # result of cryptographic chain verification


@dataclass(frozen=True)
class TrustRegistry:
    revoked_at_epoch: dict   # (writer, fact_class) -> epoch of revocation

    def authorized_at(self, writer: str, fact_class: str, epoch: int) -> bool:
        rev = self.revoked_at_epoch.get((writer, fact_class))
        if rev is None:
            return True
        return epoch < rev


def audit_recall(entry: MemoryEntry, registry: TrustRegistry, read_epoch: int) -> WashStatus:
    if not entry.chain_ok:
        raise RecallTrustError(
            f"chain integrity failed for writer={entry.writer}; quarantine entry"
        )

    # Provenance-washing: re-signed after original write epoch.
    if entry.signing_epoch > entry.write_epoch:
        still_authorized = registry.authorized_at(
            entry.writer, entry.fact_class, entry.write_epoch
        )
        if not still_authorized:
            raise RecallTrustError(
                f"provenance-washed entry: writer={entry.writer} "
                f"write_epoch={entry.write_epoch} signing_epoch={entry.signing_epoch} "
                f"authority lapsed before re-sign"
            )

    # Epoch liveness at read time, not write time.
    if not registry.authorized_at(entry.writer, entry.fact_class, read_epoch):
        return WashStatus.STALE_EPOCH

    if entry.write_epoch > read_epoch:
        raise RecallTrustError(
            f"non-monotonic epoch: write_epoch={entry.write_epoch} > read_epoch={read_epoch}"
        )

    return WashStatus.CLEAN


def recall(entry: MemoryEntry, registry: TrustRegistry, read_epoch: int) -> str:
    status = audit_recall(entry, registry, read_epoch)
    if status is WashStatus.STALE_EPOCH:
        # Recall as historical assertion only; never as live fact.
        return f"[historical@{entry.write_epoch}] {entry.payload}"
    return entry.payload
