# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-forget-tombstone-as-non-final-when-the-vector-index-deletion-is-acknowledged-before-the-episodic-store-comm.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-forget-tombstone-as-non-final-when-the-vector-index-deletion-is-acknowledged-before-the-episodic-store-comm.warrant.json
# Markdown SHA-256:   8507dabb4a8c11c61f60dcd6101b285896a0ebb13aeb9eaa31d70fb74d26e89d
# Code AST SHA-256:   14b3efd11bc2e40a452967eca0be0009eb5653721a5e89fbe98f7ae22d26681a
# Verified Classes:   ForgetInvariantError, ForgetState, DeletionProof
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class ForgetInvariantError(RuntimeError):
    """Raised when a forget is acknowledged as final while a rebuild path could resurrect it."""
    pass


class ForgetState(Enum):
    PENDING = "pending"
    COMPLETE = "complete"


@dataclass(frozen=True)
class DeletionProof:
    memory_id: str
    vector_epoch: int          # generation of the vector shard at delete time
    episodic_compacted: bool   # has the slow store physically dropped the record?
    current_index_generation: int


def acknowledge_forget(
    proof: DeletionProof,
    *,
    rebuild_from_episodic: bool,
) -> ForgetState:
    # Fast path is only valid when no rebuild-from-source path exists,
    # or when the vector delete is itself replayed on rebuild.
    if not rebuild_from_episodic:
        return ForgetState.COMPLETE

    # Rebuild-from-source exists: the tombstone is non-final until the
    # slow store has compacted AND the proof still matches the live generation.
    if not proof.episodic_compacted:
        return ForgetState.PENDING

    if proof.vector_epoch != proof.current_index_generation:
        # Proof refers to a generation that no longer exists; a promotion or
        # rebuild happened inside the compaction gap. Do not trust it.
        raise ForgetInvariantError(
            f"forget proof for {proof.memory_id} is generation-stale: "
            f"epoch {proof.vector_epoch} != live generation "
            f"{proof.current_index_generation}; re-verify before acking final"
        )

    return ForgetState.COMPLETE
