# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconcile-a-forget-acknowledgement-with-a-still-retrievable-embedding-before-resuming-a-task.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconcile-a-forget-acknowledgement-with-a-still-retrievable-embedding-before-resuming-a-task.warrant.json
# Markdown SHA-256:   02bdceff38e711892d934bf853891cb8e7de1c4d4997d5646b6a1ab11bef13eb
# Code AST SHA-256:   d0a907861f11362f8065183e8f19a70bda9c73acface51bb68c106aa418db8c5
# Verified Classes:   ForgetAckNotReadConsistent
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ForgetAckNotReadConsistent(RuntimeError):
    """Raised when a forget() ack cannot be trusted to bound read-surface absence."""
    pass


def scrub_and_resume(memory, mem_id: str, resume_fn):
    ack = memory.forget(mem_id)
    if ack.get("status") != "deleted":
        raise ForgetAckNotReadConsistent(
            f"forget({mem_id}) did not report deletion: {ack!r}"
        )

    token = ack.get("tombstone_epoch")
    if token is not None:
        # Read-consistent deletion: ack is sufficient, reads honor the epoch.
        memory.set_read_epoch(token)
        return resume_fn()

    # No consistency token: ack bounds the authoritative store only.
    # Probe the read surface the agent will actually use before resuming.
    probe = memory.read(query=mem_id, k=5, exact_id=mem_id)
    if probe:
        raise ForgetAckNotReadConsistent(
            f"forget({mem_id}) acked but read surface still returns "
            f"{len(probe)} record(s); replica reaping not complete. "
            f"Blocking resume — do not re-write the content."
        )

    return resume_fn()
