# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-prove-deletion-of-a-memory-it-never-knew-it-had-unpropagated-erasure-residue-across-a-swarm.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-prove-deletion-of-a-memory-it-never-knew-it-had-unpropagated-erasure-residue-across-a-swarm.warrant.json
# Markdown SHA-256:   1d0ae7a70cf9e2c77b43f7737ffad158adea1ef911ce24b5da3bbbc82dca7085
# Code AST SHA-256:   ca2a59d03336adc0d8f35cbe7a9c90d7821bb741d55181451aa2e7e7ed19fd74
# Verified Classes:   ErasureProtocolError, Verdict, MemoryArtifact, Instance
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from __future__ import annotations

from dataclasses import dataclass, field
from enum import Enum
from typing import Callable, Optional


class ErasureProtocolError(RuntimeError):
    """Raised when an instance confirms erasure it cannot substantiate."""


class Verdict(str, Enum):
    DELETED = "DELETED"
    INDETERMINATE = "INDETERMINATE"
    NOT_HELD = "NOT_HELD"


@dataclass
class MemoryArtifact:
    artifact_id: str
    # Provenance sidecar: commitment to the subject, written at distillation time.
    subject_commitment: Optional[str]
    # Predicate the instance can actually evaluate over this artifact.
    provenance_predicate: Optional[Callable[[str], bool]] = None


@dataclass
class Instance:
    instance_id: str
    artifacts: list[MemoryArtifact] = field(default_factory=list)

    def confirm_erasure(self, subject_commitment: str) -> tuple[Verdict, Optional[str]]:
        """Return a verdict plus the predicate source it evaluated, or None."""
        held = False
        for art in self.artifacts:
            if art.subject_commitment is None:
                # Distillation destroyed provenance: cannot decide membership.
                return Verdict.INDETERMINATE, None
            if art.provenance_predicate is None:
                raise ErasureProtocolError(
                    f"instance {self.instance_id} artifact {art.artifact_id} "
                    "has a subject commitment but no evaluable predicate"
                )
            if art.provenance_predicate(subject_commitment):
                held = True
        if not held:
            return Verdict.NOT_HELD, None
        # Only a subject-keyed artifact with a working predicate may confirm.
        return Verdict.DELETED, "subject_commitment == artifact.subject_commitment"


def close_erasure_request(
    instances: list[Instance], subject_commitment: str
) -> dict[str, tuple[Verdict, Optional[str]]]:
    receipts: dict[str, tuple[Verdict, Optional[str]]] = {}
    for inst in instances:
        verdict, predicate = inst.confirm_erasure(subject_commitment)
        if verdict is Verdict.DELETED and predicate is None:
            # A confirmation with no predicate is the exact failure mode.
            raise ErasureProtocolError(
                f"instance {inst.instance_id} returned DELETED without a provenance predicate"
            )
        receipts[inst.instance_id] = (verdict, predicate)

    if any(v is Verdict.INDETERMINATE for v, _ in receipts.values()):
        # Fail closed: an unconfirmable artifact blocks closure.
        raise ErasureProtocolError(
            "erasure request cannot be closed: INDETERMINATE residue present; "
            "re-derive provenance or quarantine-and-rebuild the affected store"
        )
    return receipts
