# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-successful-forget-call-as-a-tombstone-rather-than-as-an-erasure-and-what-breaks-when-it-doesn-t.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-successful-forget-call-as-a-tombstone-rather-than-as-an-erasure-and-what-breaks-when-it-doesn-t.warrant.json
# Markdown SHA-256:   49b11e098d95956b9ec60771c4ee470db20c1f01b48ee0ee522432b29bbd2857
# Code AST SHA-256:   ceed65a965f7381d08a5feefa3e03d372fc6c26022112867f0f35754af67e5a1
# Verified Classes:   UnverifiedErasureError, ForgetReceipt, MemoryClient
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable


class UnverifiedErasureError(RuntimeError):
    """Raised when a forget result is used to discharge a non-retrievability
    obligation without a confirming read under the full reachable scope mask."""


@dataclass
class ForgetReceipt:
    record_id: str
    scope: tuple
    status: str
    projections_pending: bool
    derived_artifacts: list = field(default_factory=list)


class MemoryClient:
    """Minimal stand-in for a scoped read/write/forget memory tool."""

    def __init__(self, store):
        self._store = store

    def forget(self, record_id: str, scope: tuple) -> ForgetReceipt:
        return self._store.forget(record_id, scope)

    def read(self, query: str, scope: tuple) -> list:
        return self._store.read(query, scope)


def discharge_non_retrievability(
    client: MemoryClient,
    record_id: str,
    forget_scope: tuple,
    verification_scopes: Iterable[tuple],
    query: str,
) -> ForgetReceipt:
    """Forget a record and certify it is non-retrievable across every scope
    the instance may later read through. Raises if certification fails."""
    receipt = client.forget(record_id, scope=forget_scope)

    if receipt.status != "deleted":
        raise UnverifiedErasureError(
            f"forget for {record_id} returned status={receipt.status!r}; "
            "cannot certify erasure"
        )

    if receipt.projections_pending:
        raise UnverifiedErasureError(
            f"forget for {record_id} issued while projections were in flight; "
            f"derived artifacts {receipt.derived_artifacts!r} may survive. "
            "Re-issue forget against derived artifacts or treat as tombstone."
        )

    for scope in verification_scopes:
        hits = client.read(query=query, scope=scope)
        if hits:
            raise UnverifiedErasureError(
                f"record {record_id} still retrievable under scope {scope!r} "
                f"after forget in {forget_scope!r}: {len(hits)} hit(s). "
                "Tombstone semantics apply; do not emit deletion attestation."
            )

    return receipt
