# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-prove-deletion-when-a-scoped-retention-memory-tool-reports-ttl-expiry-as-a-tombstone-rather-than-an-erasure.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-prove-deletion-when-a-scoped-retention-memory-tool-reports-ttl-expiry-as-a-tombstone-rather-than-an-erasure.warrant.json
# Markdown SHA-256:   19682038fd7dc61b066f32b9c80342d14e8581808c176d5fb3fcda64bf58f82c
# Code AST SHA-256:   79325d477efe41f0c2a839964a21fc3833448b9a7b7655f77ae94cd7e34d05dc
# Verified Classes:   DeletionProofError, ErasureProof, ForgetAck
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import hashlib
import time
from dataclasses import dataclass, field


class DeletionProofError(RuntimeError):
    """Raised when a forget() acknowledgement cannot be upgraded to a verifiable erasure proof."""


@dataclass
class ErasureProof:
    proof_scope: frozenset          # stores/indexes the proof attests to
    digest: str                     # hash of the erasure manifest
    signed_at: float

    def verifies_against(self, required_scopes: frozenset, manifest: bytes) -> bool:
        if not required_scopes.issubset(self.proof_scope):
            return False
        return hashlib.sha256(manifest).hexdigest() == self.digest


@dataclass
class ForgetAck:
    status: str
    mode: str                       # "ttl_expire" | "purge"
    records_affected: int
    erasure_proof: ErasureProof | None = None


def assert_rtbf_complete(
    ack: ForgetAck,
    required_scopes: frozenset,
    manifest: bytes,
    max_wait_s: float = 30.0,
    poll_s: float = 2.0,
) -> ErasureProof:
    """
    Enforce the invariant: RTBF completion requires a verifiable erasure proof
    covering every read path, NOT merely an empty default read.
    """
    if ack.mode == "ttl_expire":
        raise DeletionProofError(
            f"ttl_expire tombstones {ack.records_affected} records but produces no "
            "erasure proof; RTBF requires mode='purge' with proof_scope"
        )

    deadline = time.monotonic() + max_wait_s
    proof = ack.erasure_proof
    while time.monotonic() < deadline:
        if proof is not None and proof.verifies_against(required_scopes, manifest):
            return proof
        # index/replica propagation lag: re-poll prove_deletion, do not re-read KV
        proof = poll_prove_deletion(required_scopes, manifest)
        if proof is None:
            time.sleep(poll_s)

    raise DeletionProofError(
        f"purge acknowledged for {ack.records_affected} records but no proof covering "
        f"{sorted(required_scopes)} verified within {max_wait_s}s; record is tombstoned, "
        "not erased — halt downstream dispatch and escalate"
    )


def poll_prove_deletion(required_scopes: frozenset, manifest: bytes) -> ErasureProof | None:
    # Placeholder for the tool's prove_deletion endpoint; returns None until
    # every required store has acknowledged the purge.
    return None
