# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-prove-deletion-before-it-may-reuse-a-scoped-memory-slot-tombstone-reclamation-versus-retrieval-index-residue-in-write-forget-me.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-prove-deletion-before-it-may-reuse-a-scoped-memory-slot-tombstone-reclamation-versus-retrieval-index-residue-in-write-forget-me.warrant.json
# Markdown SHA-256:   3ada6eb21efc610fe08fe198de61b0a9be71550fbb64f470fab7eefed6de0ffb
# Code AST SHA-256:   7f584b05779b377734858bb0df76cee5a7ddce6ce00fdef60b49cc17d09a4649
# Verified Classes:   DeletionNotInertError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class DeletionNotInertError(RuntimeError):
    """Raised when a forgotten scope still influences the semantic read path."""
    pass


def reclaim_scope(memory, scope: str, probe_query: str, k: int = 5) -> None:
    """
    Gate reuse of a scope on proven retrieval inertness, not on the
    delete acknowledgment. Call immediately before writing into a scope
    whose previous lifecycle ended in forget().
    """
    ack = memory.forget(scope=scope)
    if not ack.get("deleted"):
        raise DeletionNotInertError(
            f"forget({scope!r}) did not acknowledge deletion: {ack!r}"
        )

    # The ack only certifies the write path. Probe the read path.
    residue = memory.read(query=probe_query, scope=scope, k=k)
    if residue:
        # Stale ANN neighbors, summary-cache hits, or dangling IDs.
        # Do NOT write here; quarantine and allocate a fresh scope.
        raise DeletionNotInertError(
            f"scope {scope!r} still retrieval-active after forget: "
            f"{len(residue)} hit(s); first id={residue[0].get('record_id')!r} "
            f"absent_from_live={not memory.exists(residue[0].get('record_id'))}"
        )

    # Optional: verify summary cache separately, since it is keyed on scope_id
    # and may not be exercised by the record-level probe above.
    summary = memory.read(scope=scope, mode="summary")
    if summary and summary.get("content"):
        raise DeletionNotInertError(
            f"summary cache for {scope!r} survived scope-level forget; "
            f"distilled content is not covered by record tombstones"
        )
