# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-verify-a-forget-tool-erasure-proof-when-the-deletion-target-is-an-embedding-not-a-row.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-verify-a-forget-tool-erasure-proof-when-the-deletion-target-is-an-embedding-not-a-row.warrant.json
# Markdown SHA-256:   e875969f3e98e5857f36ede8144f8529491e2954dfeff2303200a24918bda35b
# Code AST SHA-256:   40151a7bdfdc376e5e8745d2a713c685cbac0bf4baadd3911867ea79af64d654
# Verified Classes:   ErasureProofIncomplete, ErasureProof
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass, field
from typing import Callable


class ErasureProofIncomplete(RuntimeError):
    """Raised when a forget-tool attestation cannot be shown to cover the
    full retrieval surface for a subject. Never downgrade to a warning:
    a partial erasure reported as success is a false attestation."""


@dataclass
class ErasureProof:
    subject_id: str
    index_epoch: str
    probe_query: str
    max_similarity: float
    deleted_artifacts: list = field(default_factory=list)

    def digest(self) -> str:
        payload = f"{self.subject_id}|{self.index_epoch}|{self.probe_query}|{self.max_similarity}"
        return hashlib.sha256(payload.encode()).hexdigest()


def attest_erasure(
    subject_id: str,
    lineage_closure: Callable[[str], list],
    delete_from_store: Callable[[list], int],
    delete_from_index: Callable[[list], str],
    reid_probe: Callable[[str], float],
    similarity_floor: float = 0.80,
) -> ErasureProof:
    # 1. Referential closure: walk lineage transitively. Orphaned distilled
    #    artifacts (subject_id IS NULL) are reachable only through this walk.
    artifacts = lineage_closure(subject_id)
    if not artifacts:
        raise ErasureProofIncomplete(
            f"lineage closure for {subject_id} is empty; cannot prove coverage "
            f"(possible orphaned semantic rows with NULL subject_id)"
        )

    # 2. Delete from storage AND index; capture the compaction epoch so the
    #    proof is replayable against the same index generation.
    rows = delete_from_store(artifacts)
    epoch = delete_from_index(artifacts)

    # 3. Re-identification probe against the live retrieval surface. A summary
    #    that paraphrases the subject is still personal data if it singles out.
    sim = reid_probe(subject_id)
    if sim >= similarity_floor:
        raise ErasureProofIncomplete(
            f"re-identification probe for {subject_id} returned {sim:.3f} "
            f">= floor {similarity_floor} at index epoch {epoch}; "
            f"deletion not effective on retrieval surface"
        )

    # 4. Emit a signed, replayable proof artifact — not a boolean.
    return ErasureProof(
        subject_id=subject_id,
        index_epoch=epoch,
        probe_query=subject_id,
        max_similarity=sim,
        deleted_artifacts=artifacts,
    )
