# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-forgets-a-memory-it-never-fully-read-deletion-attestation-against-a-partially-materialized-retention-scope.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-forgets-a-memory-it-never-fully-read-deletion-attestation-against-a-partially-materialized-retention-scope.warrant.json
# Markdown SHA-256:   a9c6a2c740b4512516aa30aa8d54bcd7c0ea9251ca873e93c5c3e4d2843beb8a
# Code AST SHA-256:   f3b2ce2119ba36d3fc3ad21ca596ceb97c34846184ea6dfb7b5e2e15d5a6e1ad
# Verified Classes:   DeletionAttestationIncomplete, ForgetAttestation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable


class DeletionAttestationIncomplete(RuntimeError):
    """Raised when a forget attestation does not cover the record's full fan-out closure."""


@dataclass
class ForgetAttestation:
    scope: str
    attestation_hash: str
    covered_scopes: frozenset[str]
    covered_edges: frozenset[tuple[str, str]]


def verify_erasure(
    record_id: str,
    attestation: ForgetAttestation,
    fanout_scopes: Iterable[str],
    forward_edges: Iterable[tuple[str, str]],
    probe_retrievable,
) -> None:
    """
    Confirm that a forget attestation actually covers physical erasure, not just
    logical retraction of the named scope.

    probe_retrievable(scope) -> bool  returns True if any payload survives in scope.
    """
    required_scopes = frozenset(fanout_scopes) | {attestation.scope}
    required_edges = frozenset(forward_edges)

    missing_scopes = required_scopes - attestation.covered_scopes
    missing_edges = required_edges - attestation.covered_edges

    if missing_scopes or missing_edges:
        raise DeletionAttestationIncomplete(
            f"forget({attestation.scope}) for {record_id} attests only to "
            f"{sorted(attestation.covered_scopes)}; uncovered derivative scopes="
            f"{sorted(missing_scopes)}, uncovered provenance edges="
            f"{sorted(missing_edges)}. Scope-local attestation cannot certify erasure."
        )

    live = [s for s in required_scopes if probe_retrievable(s)]
    if live:
        raise DeletionAttestationIncomplete(
            f"post-forget retrieval probe for {record_id} still resolves in "
            f"{live}; attestation hash {attestation.attestation_hash} is valid "
            f"but insufficient — derivative payload survived."
        )
