# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-hallucinate-a-forgotten-memory-after-a-successful-erase-tombstone-propagation-lag-across-the-episodic-store-the-semantic-index-an.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-hallucinate-a-forgotten-memory-after-a-successful-erase-tombstone-propagation-lag-across-the-episodic-store-the-semantic-index-an.warrant.json
# Markdown SHA-256:   b19ec9670db4fd6377d0914137b77a918f361b40cb6dd67d57df6c084c5d743b
# Code AST SHA-256:   3cfb9cfc6d16a9ad5fbdb75c60934b787ddd2e9f767a83e813a7e2fab4e5c91e
# Verified Classes:   ErasurePropagationError, ForgetReceipt
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable


class ErasurePropagationError(RuntimeError):
    """Raised when a forget() call reports success but leaves a reachable derivation."""


@dataclass
class ForgetReceipt:
    memory_id: str
    store_erased: bool
    tombstone_closure: set[str] = field(default_factory=set)
    query_path_gated: bool = False
    context_slots_invalidated: set[str] = field(default_factory=set)
    pending_reindex: set[str] = field(default_factory=set)


def assert_erasure_effective(
    receipt: ForgetReceipt,
    live_context_slots: Iterable[str],
    queryable_derivations: Iterable[str],
) -> None:
    """
    Enforce the derivation-closure invariant before the agent may proceed.

    A forget is effective only if:
      - the store erased the canonical row,
      - every derivation is in the tombstone closure (not merely pending reindex),
      - the query path gates on tombstones (no stale-vector window),
      - every live context slot holding the record or its derivations was invalidated.
    """
    if not receipt.store_erased:
        raise ErasurePropagationError(
            f"canonical record {receipt.memory_id} not erased; cannot claim erasure"
        )

    # Derivations flagged for async reindex are still queryable until the sweep runs.
    reachable = set(queryable_derivations) - receipt.tombstone_closure
    if reachable:
        raise ErasurePropagationError(
            f"derivations {sorted(reachable)} of {receipt.memory_id} remain queryable "
            f"(pending_reindex={sorted(receipt.pending_reindex)}); "
            "tombstone the transitive closure synchronously before proceeding"
        )

    if not receipt.query_path_gated:
        raise ErasurePropagationError(
            f"query path for {receipt.memory_id} not gated on tombstones; "
            "eventually-consistent index will serve the erased fact until reindex"
        )

    surviving_slots = set(live_context_slots) - receipt.context_slots_invalidated
    if surviving_slots:
        raise ErasurePropagationError(
            f"live context slots {sorted(surviving_slots)} still hold a copy of "
            f"{receipt.memory_id}; the instance will re-emit the erased fact"
        )
