# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-reconcile-scoped-retention-tombstones-before-a-forget-call-can-be-trusted-as-verifiable-deletion.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-reconcile-scoped-retention-tombstones-before-a-forget-call-can-be-trusted-as-verifiable-deletion.warrant.json
# Markdown SHA-256:   ef6b0839f8dda19436d0408840f45348c8bb06d7db1ae45623e8e261a4616d6c
# Code AST SHA-256:   24f215dc666afd9df8eac1c602742a5ec6a7704fc4188120412768394d5dbc03
# Verified Classes:   ProofCoverageError, Coverage, ForgetProof, ErasureAssertion
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable


class ProofCoverageError(RuntimeError):
    """Raised when a deletion proof's coverage does not satisfy the assertion's required coverage."""


@dataclass(frozen=True)
class Coverage:
    scopes: frozenset[str]
    derivation_closed: bool
    replicas_reconciled: bool
    peer_holders_acknowledged: frozenset[str] = field(default_factory=frozenset)

    def is_subset_of(self, other: "Coverage") -> bool:
        return (
            self.scopes <= other.scopes
            and (not self.derivation_closed or other.derivation_closed)
            and (not self.replicas_reconciled or other.replicas_reconciled)
            and self.peer_holders_acknowledged <= other.peer_holders_acknowledged
        )


@dataclass(frozen=True)
class ForgetProof:
    proof_hash: str
    coverage: Coverage


@dataclass(frozen=True)
class ErasureAssertion:
    """What the caller wants to claim: full derivation-closed erasure across all read paths."""
    required: Coverage


def assert_erasure(proof: ForgetProof, assertion: ErasureAssertion) -> None:
    """
    Consume a forget proof only if its coverage is a superset of the assertion's
    required coverage. A scope-local proof must never satisfy a derivation-closed
    assertion; a proof from a single replica must never satisfy a multi-replica claim.
    """
    if not assertion.required.is_subset_of(proof.coverage):
        missing_scopes = assertion.required.scopes - proof.coverage.scopes
        raise ProofCoverageError(
            "forget proof coverage insufficient for erasure assertion: "
            f"proof={proof.proof_hash} "
            f"missing_scopes={sorted(missing_scopes)} "
            f"derivation_closed={proof.coverage.derivation_closed} "
            f"replicas_reconciled={proof.coverage.replicas_reconciled} "
            f"peer_holders_missing="
            f"{sorted(assertion.required.peer_holders_acknowledged - proof.coverage.peer_holders_acknowledged)}"
        )


def reconcile_or_downgrade(
    proof: ForgetProof,
    assertion: ErasureAssertion,
    blocking_scopes: Iterable[str],
) -> str:
    """
    If the forget proof cannot satisfy the assertion, do not retry the same call.
    Emit a reconciliation obligation and downgrade the claim to scope-local.
    """
    try:
        assert_erasure(proof, assertion)
    except ProofCoverageError as exc:
        blockers = sorted(set(blocking_scopes))
        if not blockers:
            # No enumerated blockers means the gap is in peer holders or replicas,
            # which the caller cannot resolve locally. Escalate, do not claim success.
            raise ProofCoverageError(
                f"unresolvable coverage gap with no enumerated blockers: {exc}"
            ) from exc
        return f"partial:reconcile={blockers}"
    return "complete"
