# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-tombstone-only-erasure-has-left-a-persistent-agent-memory-store-in-a-recoverable-but-unreachable-state.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-tombstone-only-erasure-has-left-a-persistent-agent-memory-store-in-a-recoverable-but-unreachable-state.warrant.json
# Markdown SHA-256:   b90605575e9e569506384fdcd860f5530b41b8e47266366fa79f8745dba9248e
# Code AST SHA-256:   51371d23a388238bd93717bd23be31cfe1045be4354720939ab29420f3298509
# Verified Classes:   ErasureClosureViolation
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ErasureClosureViolation(RuntimeError):
    """Raised when a tombstone-only erasure leaves reachable derived artifacts."""


def erase_subject(store, user_id: str, probe_text: str) -> dict:
    tombstoned = store.tombstone_episodes(user_id)
    if not tombstoned:
        raise ErasureClosureViolation(f"no episodes found for {user_id}; refusing to certify")

    # Forward closure over the derivation graph.
    dirty_facts = store.facts_referencing(tombstoned)
    store.rederive_or_delete(dirty_facts)

    # ANN invalidation must be by ID; re-embedding leaves stale vectors.
    store.ann_invalidate_by_id(tombstoned | dirty_facts)

    # Cache residue: provenance hash must map to an erased session.
    store.purge_cache_by_provenance(tombstoned)

    # Post-condition: no query surface may resolve the subject.
    hits = store.semantic_search(probe_text, k=10)
    leaked = [h for h in hits if h.provenance in tombstoned or h in dirty_facts]
    if leaked:
        raise ErasureClosureViolation(
            f"erasure incomplete: {len(leaked)} derived artifacts still reachable"
        )

    return {
        "erased_episodes": len(tombstoned),
        "erased_derivatives": len(dirty_facts),
        "closure_certificate": True,
    }
