# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-issues-a-scoped-forget-against-a-memory-tool-whose-ttl-has-already-tombstoned-the-record-the-resurrection-of-erasure-war.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-issues-a-scoped-forget-against-a-memory-tool-whose-ttl-has-already-tombstoned-the-record-the-resurrection-of-erasure-war.warrant.json
# Markdown SHA-256:   ce7a6688040d45720b2980d7b2a31f45ce22cba77e097327357884404afe9c75
# Code AST SHA-256:   82acfed44a03d5c541c2a574d754196461664999c286c44df445e6aea5565803
# Verified Classes:   ErasureWarrantMissing, MemoryToolClient
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ErasureWarrantMissing(RuntimeError):
    """Raised when a forget returned 'already_absent' but no durable erasure warrant exists."""
    pass


class MemoryToolClient:
    def __init__(self, store):
        self.store = store

    def forget_and_verify(self, subject: str, scope: str = "all") -> None:
        # Step 1: issue the forget.
        resp = self.store.forget(subject, scope=scope)

        # Step 2: never trust 'already_absent' — it may mean TTL-expired, not erased.
        if resp.get("status") == "already_absent":
            # Force an unconditional warrant write; if the store lacks this, escalate.
            if not hasattr(self.store, "write_warrant"):
                raise ErasureWarrantMissing(
                    f"store cannot write unconditional warrant for {subject!r}; "
                    f"forget returned already_absent but erasure is unverified"
                )
            self.store.write_warrant(subject, scope=scope)

        # Step 3: positive probe — a write for the subject MUST now be rejected.
        try:
            self.store.write(subject, key="__probe__", value="x", ttl_s=1)
        except Exception as exc:
            if "erased" in str(exc).lower() or "warrant" in str(exc).lower():
                return  # erasure confirmed
            raise ErasureWarrantMissing(
                f"probe write for {subject!r} failed for unexpected reason: {exc!r}"
            ) from exc

        # Probe write succeeded: the subject is writable, so erasure did not take.
        raise ErasureWarrantMissing(
            f"probe write for {subject!r} succeeded after forget; "
            f"record was TTL-tombstoned, not erased — warrant never written"
        )
