# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-deletion-acknowledged-as-distinct-from-deletion-proven-in-forget-memory-compliance-erasure-across-vector.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-deletion-acknowledged-as-distinct-from-deletion-proven-in-forget-memory-compliance-erasure-across-vector.warrant.json
# Markdown SHA-256:   3ffee78ef734b02bf5e305d5482354e6bede84ef027fd3b8878296fb9bc2f033
# Code AST SHA-256:   d560549b913aa7329cd100e205444e7eaef7073e28f614b93e94601cdb5a776c
# Verified Classes:   ErasureProofError, ErasureStatus, ErasureProof
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum
from typing import Optional


class ErasureProofError(RuntimeError):
    """Raised when an instance attempts to certify erasure from acknowledgements alone."""
    pass


class ErasureStatus(Enum):
    ACKNOWLEDGED_UNPROVEN = "acknowledged_unproven"
    PROVEN = "proven"


@dataclass
class ErasureProof:
    epoch: int
    vector_compaction_watermark: Optional[int]
    kv_unreferenced_blocks: Optional[int]
    neighbor_probe_max_similarity: Optional[float]
    derived_artifacts_cleared: bool
    open_paths: list = field(default_factory=list)


def certify_erasure(
    epoch: int,
    vector_delete_acked: bool,
    kv_delete_acked: bool,
    vector_compaction_watermark: Optional[int],
    kv_unreferenced_blocks: Optional[int],
    neighbor_probe_max_similarity: Optional[float],
    derived_artifacts_cleared: bool,
    similarity_threshold: float = 0.85,
) -> ErasureProof:
    # Acknowledgements are necessary but never sufficient.
    if not (vector_delete_acked and kv_delete_acked):
        raise ErasureProofError(
            "cannot certify: a store did not acknowledge the tombstone"
        )

    open_paths = []

    # Condition 2: compaction barrier, not tombstone barrier.
    if vector_compaction_watermark is None or vector_compaction_watermark < epoch:
        open_paths.append("vector_compaction_watermark_not_advanced")

    # Condition 4: shared-block refcount audit.
    if kv_unreferenced_blocks is None:
        open_paths.append("kv_block_refcount_unverifiable")
    elif kv_unreferenced_blocks > 0:
        open_paths.append("kv_shared_blocks_still_referenced")

    # Condition 3: neighbor-reconstruction probe.
    if neighbor_probe_max_similarity is None:
        open_paths.append("neighbor_probe_not_run")
    elif neighbor_probe_max_similarity >= similarity_threshold:
        open_paths.append("surviving_neighbor_reconstructs_datum")

    # Condition 5: derived-artifact sweep.
    if not derived_artifacts_cleared:
        open_paths.append("derived_artifacts_not_cleared")

    if open_paths:
        # Honest terminal state: acknowledged but unproven.
        return ErasureProof(
            epoch=epoch,
            vector_compaction_watermark=vector_compaction_watermark,
            kv_unreferenced_blocks=kv_unreferenced_blocks,
            neighbor_probe_max_similarity=neighbor_probe_max_similarity,
            derived_artifacts_cleared=derived_artifacts_cleared,
            open_paths=open_paths,
        )

    return ErasureProof(
        epoch=epoch,
        vector_compaction_watermark=vector_compaction_watermark,
        kv_unreferenced_blocks=kv_unreferenced_blocks,
        neighbor_probe_max_similarity=neighbor_probe_max_similarity,
        derived_artifacts_cleared=derived_artifacts_cleared,
    )


def emit_deletion_proof(proof: ErasureProof) -> ErasureStatus:
    if proof.open_paths:
        # Refuse to certify; surface the open read paths instead.
        raise ErasureProofError(
            f"refusing to certify epoch {proof.epoch}: "
            f"open reconstruction paths {proof.open_paths}"
        )
    return ErasureStatus.PROVEN
