# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-forget-operation-s-redaction-has-silently-degraded-into-a-tombstone-that-the-retrieval-layer-still-surfaces.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-forget-operation-s-redaction-has-silently-degraded-into-a-tombstone-that-the-retrieval-layer-still-surfaces.warrant.json
# Markdown SHA-256:   9a62185d2bbc99e0574206fd4b766b97d7376e499a26a5e617565fe620d21814
# Code AST SHA-256:   18b8b84be808fc9265fc50a294469068459a4d39841efc42bea46376323c8771
# Verified Classes:   RedactionLeakError, ForgetReceipt
# Defensive Guards:   6 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field


class RedactionLeakError(RuntimeError):
    """Raised when a forget operation reports success but content remains retrievable."""


@dataclass
class ForgetReceipt:
    record_id: str
    mode: str                      # "hard_delete" | "redact" | "tombstone"
    status: str
    derived_ids: list[str] = field(default_factory=list)


def verify_forget(memory, receipt: ForgetReceipt, probe_query: str, k: int = 8) -> None:
    """Two-phase forget gate: a status of 'forgotten' is provisional until a
    semantic probe proves the record is unretrievable AND the result set is
    not silently displaced by a residual tombstone."""
    if receipt.status != "forgotten":
        raise RedactionLeakError(
            f"forget for {receipt.record_id} did not report success: {receipt.status}"
        )

    # Phase 1: id lookup is necessary but proves nothing about retrieval.
    if memory.read_memory(id=receipt.record_id).get("found"):
        raise RedactionLeakError(
            f"{receipt.record_id} still resolvable by id after forget"
        )

    # Phase 2: semantic probe — the only test that exercises the ANN path.
    results = memory.read_memory(query=probe_query, k=k)

    if len(results) < k:
        # Post-filter displacement: a tombstone consumed a slot and was dropped
        # after ranking. Legitimate recall deficit, not "fewer matches".
        raise RedactionLeakError(
            f"result-count integrity violated: requested k={k}, got {len(results)}; "
            f"likely tombstone displacement for {receipt.record_id}"
        )

    for hit in results:
        if hit.get("id") == receipt.record_id:
            raise RedactionLeakError(
                f"forgotten record {receipt.record_id} surfaced via semantic query "
                f"(score={hit.get('score')})"
            )
        if hit.get("redacted_at") and (hit.get("snippet") or hit.get("tags")):
            raise RedactionLeakError(
                f"tombstone {hit.get('id')} leaked derived fields "
                f"snippet={bool(hit.get('snippet'))} tags={bool(hit.get('tags'))}"
            )

    # Phase 3: distillation lineage — derivatives carry their own forget obligation.
    for did in receipt.derived_ids:
        if memory.read_memory(id=did).get("found"):
            raise RedactionLeakError(
                f"derived record {did} from {receipt.record_id} not forgotten; "
                f"source redaction does not propagate to distilled tier"
            )
