# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-prove-erasure-of-a-memory-it-can-no-longer-see.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-prove-erasure-of-a-memory-it-can-no-longer-see.warrant.json
# Markdown SHA-256:   bc5f6d7d940cd49b9d3015ed007455ef391325f1fd817669e8d9b79c65980c88
# Code AST SHA-256:   99767f8a55c75749f4f959d191dc3a8cec49b0d58d41ecad99f0e0dff605681c
# Verified Classes:   ErasureProofIncomplete, Artifact, ErasureManifest
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass, field


class ErasureProofIncomplete(RuntimeError):
    """Raised when a strong erasure attestation is requested but some derived
    artifact lacks a subject back-link, physical removal, or query-path exclusion."""
    pass


@dataclass
class Artifact:
    artifact_id: str
    has_subject_link: bool
    physically_removed: bool
    query_excluded: bool


@dataclass
class ErasureManifest:
    subject_key: str
    artifacts: list[Artifact] = field(default_factory=list)

    def commitment(self) -> str:
        h = hashlib.sha256()
        h.update(self.subject_key.encode())
        for a in sorted(self.artifacts, key=lambda x: x.artifact_id):
            h.update(a.artifact_id.encode())
        return h.hexdigest()


def attest_erasure(manifest: ErasureManifest, require_strong: bool = True) -> dict:
    gaps = [
        a.artifact_id
        for a in manifest.artifacts
        if not (a.has_subject_link and a.physically_removed and a.query_excluded)
    ]
    if gaps and require_strong:
        # Fail closed: never emit a "complete" attestation over a known gap.
        raise ErasureProofIncomplete(
            f"cannot issue strong erasure proof for subject "
            f"{manifest.subject_key[:8]}...: {len(gaps)} artifact(s) unproven: {gaps}"
        )
    return {
        "subject_commitment": manifest.commitment(),
        "strength": "strong" if not gaps else "partial",
        "unproven_artifacts": gaps,
        "verified": not gaps,
    }
