# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-forget-as-a-tombstone-write-rather-than-a-deletion-and-why-does-premature-acknowledgment-break-retention-inva.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-forget-as-a-tombstone-write-rather-than-a-deletion-and-why-does-premature-acknowledgment-break-retention-inva.warrant.json
# Markdown SHA-256:   dc342afac6fd52d27f47c966c414aaf98f366b72e6e8b455f5bf876df5103177
# Code AST SHA-256:   ad0a24cd2d3fdd8e1cff0679634ee77ae106ecf9696d2acc2e9eb01584231088
# Verified Classes:   UnverifiedErasureError, Ack
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class UnverifiedErasureError(RuntimeError):
    """Raised when an instance attempts to assert deletion without a sound receipt."""
    pass


@dataclass
class Ack:
    marker_written: bool
    receipt_id: Optional[str]
    receipt_eta_ms: int
    reference_count_at_compaction: Optional[int] = None
    signature_valid: Optional[bool] = None


def assert_erasure_complete(ack: Ack, record_id: str) -> None:
    """
    Guard an instance against the premature-acknowledgment failure.

    A marker write is NOT an erasure. The instance may only report deletion
    when the backend has produced a verified receipt over a closed,
    quiescent reference set. Anything else is 'erasure_pending'.
    """
    if not ack.marker_written:
        raise UnverifiedErasureError(
            f"forget({record_id}) did not even write a tombstone; "
            f"cannot claim any deletion progress."
        )

    if ack.receipt_id is None:
        # The classic trap: marker written, read() now returns absent,
        # but compaction has not run. Refuse to assert completion.
        raise UnverifiedErasureError(
            f"forget({record_id}) returned marker only (no receipt). "
            f"State is erasure_pending; do NOT emit erasure_complete. "
            f"Poll receipt endpoint, eta>={ack.receipt_eta_ms}ms is a lower bound."
        )

    if ack.signature_valid is not True:
        raise UnverifiedErasureError(
            f"receipt {ack.receipt_id} for {record_id} failed signature "
            f"verification; deletion is unproven."
        )

    if ack.reference_count_at_compaction != 0:
        raise UnverifiedErasureError(
            f"receipt {ack.receipt_id} for {record_id} compacted with "
            f"{ack.reference_count_at_compaction} live references; a sibling "
            f"scope or derived record may still hold plaintext. Escalate to "
            f"list_referrers() and force release before re-asserting."
        )

    # Only here is erasure_complete sound.
    return None
