# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-forget-a-scoped-deletion-when-the-tombstone-is-written-before-the-write-ahead-log-flush.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-forget-a-scoped-deletion-when-the-tombstone-is-written-before-the-write-ahead-log-flush.warrant.json
# Markdown SHA-256:   d2d55e0c6d5f7a32904192258b69c0ebd3c93130f7a04bbc9c386062cd0b0377
# Code AST SHA-256:   c2886b97fb59d1ad9f157e53f51d397f594836d8366078c0e3282f6dded0dea8
# Verified Classes:   DeletionResurrectionError, DeletionReceipt
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class DeletionResurrectionError(RuntimeError):
    """Raised when a committed deletion receipt cannot be verified as durable
    because the payload's WAL record is not provably older than the tombstone
    flush, or because a post-restart read still resolves the payload."""


class DeletionReceipt:
    def __init__(self, receipt_id, tombstone_seq, wal_flush_seq=None,
                 payload_erasure_seq=None):
        self.receipt_id = receipt_id
        self.tombstone_seq = tombstone_seq
        self.wal_flush_seq = wal_flush_seq
        self.payload_erasure_seq = payload_erasure_seq


def assert_verifiable_deletion(receipt: DeletionReceipt, post_restart_read) -> None:
    # Guardrail 1: the receipt must expose the ordering proof.
    if receipt.wal_flush_seq is None or receipt.payload_erasure_seq is None:
        raise DeletionResurrectionError(
            f"receipt {receipt.receipt_id} lacks wal_flush_seq/payload_erasure_seq; "
            "tombstone durability does not imply payload erasure durability"
        )
    # Guardrail 2: payload removal must be durable before the tombstone commits.
    if receipt.wal_flush_seq < receipt.payload_erasure_seq:
        raise DeletionResurrectionError(
            f"receipt {receipt.receipt_id}: wal_flush_seq={receipt.wal_flush_seq} "
            f"< payload_erasure_seq={receipt.payload_erasure_seq}; "
            "WAL replay can resurrect the payload"
        )
    # Guardrail 3: independent post-restart probe must confirm absence.
    result = post_restart_read()
    if result is not None:
        raise DeletionResurrectionError(
            f"receipt {receipt.receipt_id} committed but post-restart read "
            f"returned payload digest {result!r}; fast-path bypassed tombstone watermark"
        )
