# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-scoped-retention-forget-has-silently-failed-to-achieve-verifiable-deletion-in-an-agent-memory-tool-api.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-scoped-retention-forget-has-silently-failed-to-achieve-verifiable-deletion-in-an-agent-memory-tool-api.warrant.json
# Markdown SHA-256:   4cc133dcc2629243368f193036b3ec14843cb5cb148eefdcb76e974839bc3ec9
# Code AST SHA-256:   8b27b9f742abc4415ab63b9294d1bcba6c54642ebf1e16a9620759ab0c3444ec
# Verified Classes:   UnverifiableDeletionError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class UnverifiableDeletionError(RuntimeError):
    """Raised when a forget() receipt claims success but a post-forget
    read within the same scope still reproduces forgotten content."""


def forget_and_verify(memory, scope, cascade="derived", probe_query=None):
    receipt = memory.forget(scope=scope, cascade=cascade)

    # Structural signal 1: derived cascade with zero derived invalidation.
    if cascade == "derived" and receipt.primary_rows > 0 and receipt.derived_invalidated == 0:
        raise UnverifiableDeletionError(
            f"forget receipt {receipt.receipt_id} removed {receipt.primary_rows} "
            f"primary rows but invalidated 0 derived artifacts under cascade='derived'; "
            f"scope-key translation between tiers is likely broken"
        )

    # Structural signal 2: no forget_epoch bump while a cache is in the read path.
    if memory.has_ttl_cache() and receipt.forget_epoch is None:
        raise UnverifiableDeletionError(
            f"forget receipt {receipt.receipt_id} did not bump forget_epoch; "
            f"cached shards keyed on {scope} can outlive the deletion"
        )

    # Ground-truth probe: read within scope, assert emptiness.
    if probe_query is not None:
        survivors = memory.read(query=probe_query, scope=scope)
        leaked = [r for r in survivors if r.provenance_intersects(receipt.forgotten_set)]
        if leaked:
            raise UnverifiableDeletionError(
                f"post-forget read within scope {scope} returned {len(leaked)} "
                f"record(s) whose provenance intersects the forgotten set "
                f"(e.g. source_episode={leaked[0].source_episode}); "
                f"deletion is not verifiable across the read graph"
            )

    return receipt
