# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-deletes-a-vector-but-the-embedding-s-nearest-neighbors-still-reconstruct-it-the-deletion-proof-boundary-in-agent-memory.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-deletes-a-vector-but-the-embedding-s-nearest-neighbors-still-reconstruct-it-the-deletion-proof-boundary-in-agent-memory.warrant.json
# Markdown SHA-256:   7a2e15b56e13b637d9a50042b363a4f6fd3dbbbbf9ac19500f2c5b860c56b60b
# Code AST SHA-256:   7a150a971f65ce7495dc65bdc75db7eaaf05e8d6963ef868c8119183aad7970d
# Verified Classes:   ErasureProofInsufficient, ErasureContext
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass


class ErasureProofInsufficient(RuntimeError):
    """Raised when a deletion receipt proves row absence but not information absence."""
    pass


@dataclass
class ErasureContext:
    chunk_id: str
    chunk_text: str
    tau: float                      # reconstruction similarity floor for consumer model
    k: int                          # top-k used by retrieval surface
    neighbors_above_tau: int        # surviving neighbors with sim >= tau, post-rebuild
    probe_set_retained: bool        # can an adversary re-embed a probe into this space?
    salt_rotated: bool              # per-subject embedding salt destroyed on erasure?


def build_deletion_receipt(ctx: ErasureContext) -> dict:
    rho = ctx.neighbors_above_tau / ctx.k if ctx.k else 0.0

    # Regime 1: dense neighborhood + retained probe capability => row deletion is
    # not an erasure proof. Must escalate; do NOT emit a row-only receipt.
    if rho >= 0.4 and ctx.probe_set_retained and not ctx.salt_rotated:
        raise ErasureProofInsufficient(
            f"chunk {ctx.chunk_id}: rho={rho:.2f} with retained probe set and no salt "
            f"rotation; surviving neighbors reconstruct erased content above tau={ctx.tau}. "
            f"Escalate to neighborhood-aware erasure or salt rotation before attesting."
        )

    # Regime 2: dense neighborhood but probe capability destroyed by salt rotation.
    if rho >= 0.4 and ctx.salt_rotated:
        return {
            "chunk_id": ctx.chunk_id,
            "proof_type": "information_absence_via_salt_rotation",
            "rho": rho,
            "tau": ctx.tau,
            "neighbors_erased": 0,
            "salt_rotated": True,
            "row_sha256": hashlib.sha256(ctx.chunk_text.encode()).hexdigest(),
        }

    # Regime 3: sparse neighborhood; row deletion + reconstruction audit is defensible.
    return {
        "chunk_id": ctx.chunk_id,
        "proof_type": "row_absence_with_reconstruction_audit",
        "rho": rho,
        "tau": ctx.tau,
        "neighbors_erased": ctx.neighbors_above_tau,
        "salt_rotated": ctx.salt_rotated,
        "row_sha256": hashlib.sha256(ctx.chunk_text.encode()).hexdigest(),
    }
