# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-forget-verification-proof-has-silently-degraded-into-a-tombstone-claim-under-scoped-retention.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-forget-verification-proof-has-silently-degraded-into-a-tombstone-claim-under-scoped-retention.warrant.json
# Markdown SHA-256:   eb226a7f5baabb6d9f7226a9cdcaeff21c6d9ed77a9a3c4cf0a786b7ab7dcda9
# Code AST SHA-256:   cef52fe09f2417535dee8a18a1ec112786634c04abf984fb15f47d800e49ca7a
# Verified Classes:   ForgetProofInvalid, ForgetProof, WriteEvent, RecordFootprint
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class ForgetProofInvalid(RuntimeError):
    """Raised when a forget-verification proof is a tombstone claim, not an erasure proof."""


@dataclass
class ForgetProof:
    record_id: str
    scope: str
    epoch: int
    leaf_hash: str
    status: str  # "erased" — but erased-from-what is the question


@dataclass
class WriteEvent:
    scope: str
    epoch: int


@dataclass
class RecordFootprint:
    record_id: str
    writes: list[WriteEvent] = field(default_factory=list)

    def scopes(self) -> set[str]:
        return {w.scope for w in self.writes}

    def max_write_epoch(self) -> int:
        if not self.writes:
            raise ForgetProofInvalid(f"record {self.record_id} has no recorded writes; footprint unknown")
        return max(w.epoch for w in self.writes)


def verify_forget_is_erasure(
    proof: ForgetProof,
    footprint: RecordFootprint,
    re_forget_proof: Optional[ForgetProof],
) -> None:
    """Accept only if the proof attests to erasure of the record's full footprint.

    Raises ForgetProofInvalid otherwise; the caller must NOT mark the forget confirmed.
    """
    if proof.record_id != footprint.record_id:
        raise ForgetProofInvalid(
            f"proof record {proof.record_id} does not match footprint {footprint.record_id}"
        )

    # Condition 1: scope closure. The proof's scope must cover every scope the record occupies.
    uncovered = footprint.scopes() - {proof.scope}
    if uncovered:
        raise ForgetProofInvalid(
            f"tombstone claim: proof scope {proof.scope!r} does not cover {sorted(uncovered)}; "
            f"issue scoped forgets for each uncovered scope before confirming"
        )

    # Condition 2: epoch ordering. Proof must dominate every copy it claims to cover.
    if proof.epoch <= footprint.max_write_epoch():
        raise ForgetProofInvalid(
            f"proof epoch {proof.epoch} does not dominate max write epoch "
            f"{footprint.max_write_epoch()}; proof predates a copy it claims to cover"
        )

    # Condition 3: idempotent stability. Re-forget must return a byte-identical proof.
    if re_forget_proof is None:
        raise ForgetProofInvalid("no re-forget proof supplied; cannot establish idempotent stability")
    if (re_forget_proof.leaf_hash != proof.leaf_hash
            or re_forget_proof.epoch != proof.epoch
            or re_forget_proof.status != proof.status):
        raise ForgetProofInvalid(
            f"re-derivable tombstone: re-forget returned {re_forget_proof.leaf_hash[:8]}@{re_forget_proof.epoch} "
            f"vs original {proof.leaf_hash[:8]}@{proof.epoch}; not a stable absence fact"
        )

    # All three hold: the proof is an erasure proof over the full footprint.
    return None
