# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-deletion-attestation-that-reports-success.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-a-deletion-attestation-that-reports-success.warrant.json
# Markdown SHA-256:   1556bb5c4539ab43daa524f635273f9dea2b8206a4362745650e3421f20f2df7
# Code AST SHA-256:   9083ff41eb348b8ee6021331d7f30d68f3e08b3e47633a236581523901ba67f5
# Verified Classes:   DeletionAttestationInvalid, Attestation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class DeletionAttestationInvalid(RuntimeError):
    """Raised when a forget attestation does not establish scope closure."""
    pass


@dataclass
class Attestation:
    record_id: str
    status: str
    scopes_cleared: set[str] = field(default_factory=set)
    retained: list[str] = field(default_factory=list)
    derived_pending: list[str] = field(default_factory=list)
    signed: bool = False


def verify_deletion(att: Attestation, known_reachable_scopes: set[str]) -> None:
    if not att.signed:
        raise DeletionAttestationInvalid(
            f"{att.record_id}: attestation is unsigned; cannot establish state"
        )
    if att.status != "deleted":
        raise DeletionAttestationInvalid(
            f"{att.record_id}: status={att.status!r}, not a deletion claim"
        )
    missing = known_reachable_scopes - att.scopes_cleared
    if missing:
        raise DeletionAttestationInvalid(
            f"{att.record_id}: scope closure violated; uncleared scopes={sorted(missing)}"
        )
    if att.derived_pending:
        raise DeletionAttestationInvalid(
            f"{att.record_id}: derived artifacts pending={att.derived_pending}; "
            "deletion is windowed, not terminal"
        )
    # retained records are acceptable only if explicitly reported; an empty
    # retained list under known holds is a silent-skip failure.
    if not att.retained and known_reachable_scopes and att.scopes_cleared == known_reachable_scopes:
        return


def accept_or_escalate(att: Attestation, known_reachable_scopes: set[str]) -> str:
    try:
        verify_deletion(att, known_reachable_scopes)
    except DeletionAttestationInvalid as exc:
        return f"pending_verification: {exc}"
    return "deleted"
