# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-right-to-be-forgotten-erasure-proof-has-silently-failed-under-scoped-retention.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-right-to-be-forgotten-erasure-proof-has-silently-failed-under-scoped-retention.warrant.json
# Markdown SHA-256:   2a551fe47eb225e35ca690aac267e103109a8853bd85a9951ae58004a796b3bd
# Code AST SHA-256:   a0cb90fce01c1aa833ded9cae5d326f077fe884f64fccf251db24ab5e63239e3
# Verified Classes:   ErasureProofUnsound
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class ErasureProofUnsound(RuntimeError):
    """Raised when a forget receipt's scope set does not cover the read path,
    or when the proof digest is not a state-deterministic function."""


def validate_erasure_receipt(receipt, reachable_scopes, current_state, replay_digest):
    proof_scopes = set(receipt["proof_scopes"])
    reachable = set(reachable_scopes)

    missing = reachable - proof_scopes
    if missing:
        raise ErasureProofUnsound(
            f"proof omits reachable scopes {sorted(missing)}; "
            f"status={receipt.get('status')!r} is unsound for subject "
            f"{receipt.get('subject')!r}"
        )

    if receipt.get("digest_inputs_include_epoch"):
        raise ErasureProofUnsound(
            "digest folds a sweep_epoch/counter; not replayable against state"
        )

    if receipt.get("pending_sweep"):
        raise ErasureProofUnsound(
            "expired-but-unswept records present; TTL expiry is not erasure"
        )

    expected = replay_digest(receipt["subject"], current_state)
    if expected != receipt["digest"]:
        raise ErasureProofUnsound(
            f"digest replay mismatch for {receipt['subject']!r}: "
            f"receipt={receipt['digest']} replay={expected}"
        )

    return True
