# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-of-a-memory-tool-s-forget-operation-succeed-while-the-poisoned-fact-it-was-supposed-to-remove-remains-retrievable-through-provena.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-of-a-memory-tool-s-forget-operation-succeed-while-the-poisoned-fact-it-was-supposed-to-remove-remains-retrievable-through-provena.warrant.json
# Markdown SHA-256:   ae071ffa8ae33d3690aa1c7b7753c329c3aa45259d686d615544c6301953616e
# Code AST SHA-256:   15079373eaa422cfe6e21c86dc71f5732f97d950c9c26cd570ee0816a7d9adca
# Verified Classes:   ProvenanceRetractionError
# Defensive Guards:   4 fail-closed invariants (zero external deps)

class ProvenanceRetractionError(RuntimeError):
    """Raised when a forget call deletes a node but the fact remains derivable."""
    pass


def assert_fact_retracted(store, fact_id: str, fact_query: str) -> None:
    """
    Verify that forgetting fact_id actually retracted the knowledge, not just
    the addressed node. Must be called after store.forget(fact_id) succeeds.
    """
    manifest = store.last_forget_manifest()
    if manifest is None:
        raise ProvenanceRetractionError(
            f"store did not return an invalidation manifest for {fact_id!r}; "
            "cannot distinguish node deletion from knowledge retraction"
        )

    surviving = [d for d in manifest.transitive_derivations if store.exists(d)]
    if surviving:
        raise ProvenanceRetractionError(
            f"forget({fact_id!r}) left live derivations {surviving!r}; "
            "these can re-derive the poisoned fact via provenance traversal"
        )

    hits = store.read(fact_query)
    if hits:
        raise ProvenanceRetractionError(
            f"post-forget semantic read for {fact_query!r} still returns "
            f"{[h.id for h in hits]!r}; fact is retrievable despite deletion"
        )

    if store.provenance_is_invertible(fact_id):
        raise ProvenanceRetractionError(
            f"provenance for {fact_id!r} is invertible; backward traversal "
            "from surviving children can re-materialize the source content"
        )
