# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-forget-a-record-while-its-embedding-still-resolves-tombstone-embedding-divergence-in-gdpr-deletion-proofs-for-vector-backed-agent.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-forget-a-record-while-its-embedding-still-resolves-tombstone-embedding-divergence-in-gdpr-deletion-proofs-for-vector-backed-agent.warrant.json
# Markdown SHA-256:   e600d7bb3b44d85d22008e5c66814c18d9d9c7b7eb113790ebe834b293b5c343
# Code AST SHA-256:   93c04acee90a3ab3cbcd81eb44cf0cd12d7b85d958de2eb035bcff1322adcdec
# Verified Classes:   DeletionProofInvalid, DeletionProof
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class DeletionProofInvalid(RuntimeError):
    """Raised when a deletion proof is row-closed but not retrieval-closed."""


@dataclass
class DeletionProof:
    subject_id: str
    vector_quarantined: bool
    payload_reconstructable: bool
    derived_edges_live: int
    exposure_window_seconds: float
    filter_replicated_to_peers: bool


def assert_retrieval_closed(proof: DeletionProof) -> None:
    """
    A deletion proof is valid only if the retrieval surface is closed:
      - the vector is quarantined behind a pre-join tombstone filter,
      - the payload carries no reconstructable attributes,
      - no derived memory has a live provenance edge to the subject,
      - the tombstone filter is replicated to every peer instance.
    The exposure window is permitted only if the filter is replicated;
    otherwise peers can retrieve the vector and launder it into derived memory.
    """
    if not proof.vector_quarantined:
        raise DeletionProofInvalid(
            f"subject {proof.subject_id}: vector still resolvable in ANN index; "
            f"row-level proof is not retrieval-closed"
        )
    if proof.payload_reconstructable:
        raise DeletionProofInvalid(
            f"subject {proof.subject_id}: vector payload carries reconstructable "
            f"attributes; embedding inversion can recover the erased record"
        )
    if proof.derived_edges_live > 0:
        raise DeletionProofInvalid(
            f"subject {proof.subject_id}: {proof.derived_edges_live} derived memory "
            f"edge(s) still live; laundering channel open"
        )
    if proof.exposure_window_seconds > 0 and not proof.filter_replicated_to_peers:
        raise DeletionProofInvalid(
            f"subject {proof.subject_id}: {proof.exposure_window_seconds}s exposure "
            f"window with filter not replicated to peers; peer retrieval will launder "
            f"the tombstoned vector into provenance-clean derived memory"
        )


def issue_deletion_proof(proof: DeletionProof) -> dict:
    assert_retrieval_closed(proof)
    return {
        "subject_id": proof.subject_id,
        "status": "erased",
        "scope": "retrieval-closed",
        "vector_quarantined": True,
        "derived_edges_live": 0,
        "exposure_window_seconds": proof.exposure_window_seconds,
    }
