# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-trust-a-poisoned-fact-after-the-writer-that-injected-it-has-been-evicted-from-the-shared-store.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-trust-a-poisoned-fact-after-the-writer-that-injected-it-has-been-evicted-from-the-shared-store.warrant.json
# Markdown SHA-256:   fe09b574835265c0bd4ad23f6456d1c288185bc71c34056c40f8d87315e2c834
# Code AST SHA-256:   cb930775755e3c1f20626c4c6147196c4087ffea0aee6c1273d52dad926907f6
# Verified Classes:   ProvenanceDecayError, Record, Roster
# Defensive Guards:   4 fail-closed invariants (zero external deps)

writer = roster.get(rec.writer_id)
if writer is None:
    rec.status = "legacy"      # <-- the bug
    continue                   # legacy records are accepted
if not writer.warrant_valid(rec.warrant_hash):
    rec.status = "suspect"

from dataclasses import dataclass
from typing import Optional


class ProvenanceDecayError(RuntimeError):
    """Raised when a record's trust verdict would improve due to author eviction."""
    pass


@dataclass
class Record:
    key: str
    value: str
    writer_id: str
    warrant_hash: str
    write_epoch: int
    trusted_at_write: bool
    signature_valid: bool
    status: str = "trusted"


@dataclass
class Roster:
    active: dict            # writer_id -> warrant_hash
    revoked_before: dict    # writer_id -> epoch of revocation


def seal_on_write(rec: Record, roster: Roster) -> Record:
    """Freeze trust into the record at write time."""
    if rec.writer_id not in roster.active:
        raise ProvenanceDecayError(
            f"write from unknown principal {rec.writer_id!r} at epoch {rec.write_epoch}"
        )
    if roster.active[rec.writer_id] != rec.warrant_hash:
        raise ProvenanceDecayError(
            f"warrant mismatch for {rec.writer_id!r}: record {rec.warrant_hash!r}"
        )
    rec.trusted_at_write = True
    return rec


def verdict(rec: Record, roster: Roster) -> str:
    """Trust is a function of the record, not the liveness of its author."""
    if not rec.signature_valid:
        return "suspect"
    if not rec.trusted_at_write:
        return "suspect"
    rev_epoch = roster.revoked_before.get(rec.writer_id)
    if rev_epoch is not None and rec.write_epoch <= rev_epoch:
        return "suspect"
    return "trusted"


def evict_writer(recs: list, roster: Roster, writer_id: str, epoch: int) -> Roster:
    """Revoke-then-evict: mark dependent records BEFORE dropping the principal."""
    if writer_id not in roster.active:
        raise ProvenanceDecayError(f"cannot evict absent writer {writer_id!r}")
    # 1. persist revocation scoped to epoch
    roster.revoked_before[writer_id] = epoch
    # 2. re-verdict every dependent record in the same transaction
    for r in recs:
        if r.writer_id == writer_id and r.write_epoch <= epoch:
            r.status = "suspect"
    # 3. only now remove from active roster
    del roster.active[writer_id]
    return roster


def monotonicity_check(before: str, after: str) -> None:
    """Eviction may only tighten, never loosen, a record's verdict."""
    order = {"suspect": 0, "trusted": 1}
    if order[after] > order[before]:
        raise ProvenanceDecayError(
            f"eviction loosened verdict {before!r} -> {after!r}; "
            f"record was laundered by author disappearance"
        )
