# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-tombstoned-memory-namespace-has-silently-resurrected-through-a-stale-retrieval-cache.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-tombstoned-memory-namespace-has-silently-resurrected-through-a-stale-retrieval-cache.warrant.json
# Markdown SHA-256:   6839580fa0ffef8dafc5ff37b456e57fb65771e85d54c34d45153d0454a9c5f4
# Code AST SHA-256:   cceb38c9743951df448c4f3333d21097ffda5db01775c65219e577feef96d6d0
# Verified Classes:   MemoryResurrectionError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class MemoryResurrectionError(RuntimeError):
    """Raised when a probe read returns records that the tombstone ledger marks deleted."""
    pass


def verify_namespace_sealed(
    memory_tool,
    namespace: str,
    probe_queries: list[str],
    tombstone_ledger: dict[str, str],
    expected_epoch: int,
) -> None:
    """
    Seal-and-probe verification. Fails closed if any probe returns a record that
    the ledger says was tombstoned, or if the namespace epoch did not advance.
    """
    current_epoch = memory_tool.get_namespace_epoch(namespace)
    if current_epoch < expected_epoch:
        raise MemoryResurrectionError(
            f"namespace {namespace!r} epoch {current_epoch} < expected {expected_epoch}; "
            f"forget did not advance the seal"
        )

    for query in probe_queries:
        results = memory_tool.read(namespace=namespace, query=query, bypass_cache=True)
        for record in results:
            rid = record["id"]
            if rid in tombstone_ledger:
                raise MemoryResurrectionError(
                    f"probe {query!r} returned record {rid!r} tombstoned at "
                    f"{tombstone_ledger[rid]} despite namespace epoch {current_epoch}; "
                    f"retrieval cache is serving pre-tombstone state"
                )
            if rid not in tombstone_ledger:
                raise MemoryResurrectionError(
                    f"probe {query!r} returned record {rid!r} absent from tombstone ledger; "
                    f"deletion did not cover it, re-issue forget for this record"
                )
