# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-verify-that-a-deleted-embedding-is-truly-unrecoverable-from-a-vector-store-s-residual-index-structures.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-verify-that-a-deleted-embedding-is-truly-unrecoverable-from-a-vector-store-s-residual-index-structures.warrant.json
# Markdown SHA-256:   df4a093b36c5c498fb1dcb3b980828d08fcfb020f622f88143d30de87594b322
# Code AST SHA-256:   1043c9596e115383c50d6d6370474b7ef592d4bbab50b50696c1e70cbe827225
# Verified Classes:   ErasureVerificationError, StructureReader, DeletionManifest
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import math
from dataclasses import dataclass, field
from typing import Callable, Sequence


class ErasureVerificationError(RuntimeError):
    """Raised when a deletion cannot be proven unrecoverable from residual structures."""


@dataclass
class StructureReader:
    name: str
    reads_tombstoned_payloads: bool
    can_reconstruct: bool


@dataclass
class DeletionManifest:
    target_id: str
    readers: list[StructureReader] = field(default_factory=list)

    def add_reader(self, reader: StructureReader) -> None:
        self.readers.append(reader)


def cosine(a: Sequence[float], b: Sequence[float]) -> float:
    dot = sum(x * y for x, y in zip(a, b))
    na = math.sqrt(sum(x * x for x in a))
    nb = math.sqrt(sum(y * y for y in b))
    if na == 0.0 or nb == 0.0:
        return 0.0
    return dot / (na * nb)


def verify_erasure(
    manifest: DeletionManifest,
    original: Sequence[float],
    reconstruct: Callable[[StructureReader], Sequence[float] | None],
    reidentification_threshold: float,
    compaction_run: bool,
) -> None:
    # Condition 3: any reader that touches tombstoned payloads must be post-compaction.
    unsafe_readers = [
        r for r in manifest.readers
        if r.reads_tombstoned_payloads and not compaction_run
    ]
    if unsafe_readers:
        names = ", ".join(r.name for r in unsafe_readers)
        raise ErasureVerificationError(
            f"Erasure for {manifest.target_id!r} cannot be verified: readers [{names}] "
            f"touch tombstoned payloads and compaction has not run. "
            f"Tombstone-and-defer is unsound for this deployment."
        )

    # Condition 2: reconstruction test against every structure that can reconstruct.
    for reader in manifest.readers:
        if not reader.can_reconstruct:
            continue
        recovered = reconstruct(reader)
        if recovered is None:
            continue
        sim = cosine(original, recovered)
        if sim >= reidentification_threshold:
            raise ErasureVerificationError(
                f"Erasure for {manifest.target_id!r} failed: structure {reader.name!r} "
                f"reconstructs the target at similarity {sim:.4f} "
                f"(threshold {reidentification_threshold:.4f}). "
                f"Residual payload is re-identifying; structural erasure required."
            )


def run_erasure(
    manifest: DeletionManifest,
    original: Sequence[float],
    reconstruct: Callable[[StructureReader], Sequence[float] | None],
    reidentification_threshold: float,
    compaction_run: bool,
) -> str:
    verify_erasure(manifest, original, reconstruct, reidentification_threshold, compaction_run)
    return f"ERASED:{manifest.target_id}:verified"
