# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-regulatory-erasure-tombstones-from-resurrecting-forgotten-facts-through-episodic-audit-replay.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-regulatory-erasure-tombstones-from-resurrecting-forgotten-facts-through-episodic-audit-replay.warrant.json
# Markdown SHA-256:   0ca1f0d990682751807d95952052efea1d735221f1eb9b65c0bae4b87612159e
# Code AST SHA-256:   cb084f5108052d5f162410abb1213935a22e83a3337a3c8030d1ea05ff79dced
# Verified Classes:   ErasureClosureViolation, AuditEvent, RehydrationPlan
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable


class ErasureClosureViolation(RuntimeError):
    """Raised when a rehydration path can regenerate a tombstoned subject's facts."""


@dataclass
class AuditEvent:
    event_id: str
    subject: str
    payload_ref: str | None      # None => digest-only, non-reconstructable
    tombstoned: bool = False


@dataclass
class RehydrationPlan:
    name: str
    reads_audit_payloads: bool   # True => derive-based, dangerous
    reads_signed_snapshot: bool


def assert_erasure_closure(
    plan: RehydrationPlan,
    audit_events: Iterable[AuditEvent],
) -> None:
    """Fail closed if rehydration can derive facts for any tombstoned subject."""
    tombstoned = {e.subject for e in audit_events if e.tombstoned}
    if not tombstoned:
        return

    if plan.reads_audit_payloads:
        # Any retained reconstructable payload for a tombstoned subject is
        # a resurrection vector under derive-based rehydration.
        leaking = [
            e.event_id
            for e in audit_events
            if e.tombstoned and e.payload_ref is not None
        ]
        if leaking:
            raise ErasureClosureViolation(
                f"plan '{plan.name}' reads audit payloads; tombstoned subjects "
                f"{sorted(tombstoned)} still have reconstructable payloads at "
                f"events {leaking}. Erasure is not closed over the derivation graph."
            )

    if not plan.reads_signed_snapshot and not plan.reads_audit_payloads:
        raise ErasureClosureViolation(
            f"plan '{plan.name}' has no restore source; cannot guarantee "
            f"tombstoned subjects {sorted(tombstoned)} stay erased after cold start."
        )


def cold_start(plan: RehydrationPlan, audit_events: list[AuditEvent]) -> None:
    assert_erasure_closure(plan, audit_events)
    # ... restore from signed snapshot only; never re-derive from audit ...
