# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-forget-as-a-tombstone-that-still-leaks-gradient-rather-than-a-deletion-that-removes-evidence.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-forget-as-a-tombstone-that-still-leaks-gradient-rather-than-a-deletion-that-removes-evidence.warrant.json
# Markdown SHA-256:   61fca7d0ee2c08ed92bb948828653dc81d84a044566b41bfabebb0ed6c627e9f
# Code AST SHA-256:   d6cf9340d76a78b4f6857d636326ce2037071006ccbf1bdb7e3b47a10681ef0b
# Verified Classes:   ErasureAuditError, ArtifactClass, ErasureRequest
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable


class ErasureAuditError(RuntimeError):
    """Raised when an erasure request cannot be certified as non-reconstructable."""
    pass


@dataclass
class ArtifactClass:
    name: str
    reconstructable: bool          # did a reconstruction probe recover subject info?
    probed: bool = False


@dataclass
class ErasureRequest:
    subject_id: str
    artifacts: list[ArtifactClass] = field(default_factory=list)


def certify_erasure(req: ErasureRequest, required_classes: Iterable[str]) -> bool:
    required = set(required_classes)
    present = {a.name for a in req.artifacts}

    missing = required - present
    if missing:
        raise ErasureAuditError(
            f"subject {req.subject_id}: artifact classes not inventoried: {sorted(missing)}"
        )

    unprobed = [a.name for a in req.artifacts if a.name in required and not a.probed]
    if unprobed:
        raise ErasureAuditError(
            f"subject {req.subject_id}: reconstruction probe missing for {unprobed}; "
            "cannot certify erasure (fail closed)"
        )

    leaking = [a.name for a in req.artifacts if a.name in required and a.reconstructable]
    if leaking:
        raise ErasureAuditError(
            f"subject {req.subject_id}: tombstone state — information reconstructable from "
            f"{leaking}; retrain/invalidate before marking compliant"
        )

    return True


# Example: retrieval index cleaned, but a retained LoRA adapter still leaks.
req = ErasureRequest(
    subject_id="subj-8842",
    artifacts=[
        ArtifactClass("primary_store", reconstructable=False, probed=True),
        ArtifactClass("vector_index", reconstructable=False, probed=True),
        ArtifactClass("lora_adapter_ckpt", reconstructable=True, probed=True),
    ],
)
certify_erasure(req, required_classes=["primary_store", "vector_index", "lora_adapter_ckpt"])
