# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-that-a-forget-memory-tool-call-was-silently-downgraded-to-a-scoped-tombstone-and-is-still-leaking-into-retrieval.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-that-a-forget-memory-tool-call-was-silently-downgraded-to-a-scoped-tombstone-and-is-still-leaking-into-retrieval.warrant.json
# Markdown SHA-256:   2ef1822cb97340eed430b611ee8024722def34a04d60111524566e6a279fde15
# Code AST SHA-256:   1434fe26d03086560292e9ea2e04d7773a06fa63c062cceb7533dab0d88df5ea
# Verified Classes:   ForgottenRecordLeak, RetrievedRecord
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class ForgottenRecordLeak(RuntimeError):
    """Raised when a record the agent believes forgotten is returned by retrieval."""
    pass


@dataclass
class RetrievedRecord:
    record_id: str
    content: str
    provenance_deleted_at: Optional[str]
    retained_until: Optional[str]
    visible_to_scope: list[str]


def verify_forget_postcondition(
    forgotten_id: str,
    probe_results: list[RetrievedRecord],
    caller_scope: str,
    now: str,
) -> None:
    """
    Caller-side post-condition for a forget() call whose response only
    contained status/deleted/record_id. Must be run against the RETRIEVAL
    path (not read) across every scope the caller can reach.
    """
    for rec in probe_results:
        if rec.record_id != forgotten_id:
            continue
        # The record is still reachable. Classify the leak.
        if rec.provenance_deleted_at is not None and rec.retained_until and rec.retained_until > now:
            raise ForgottenRecordLeak(
                f"forget({forgotten_id}) was downgraded to a scoped tombstone: "
                f"retained_until={rec.retained_until}, visible_to_scope={rec.visible_to_scope}, "
                f"caller_scope={caller_scope}. Retrieval predicate did not exclude the "
                f"tombstone. Do NOT record 'deleted' in scratchpad; record 'tombstoned, "
                f"verify at retained_until' and re-probe after index compaction."
            )
        if caller_scope not in rec.visible_to_scope and "*" not in rec.visible_to_scope:
            raise ForgottenRecordLeak(
                f"forget({forgotten_id}) leaked across scope boundary into "
                f"caller_scope={caller_scope}; record visible_to_scope={rec.visible_to_scope}. "
                f"Cross-scope fallback query bypassed the visibility predicate."
            )
        # Same scope, no retention marker: the tombstone predicate itself is missing.
        raise ForgottenRecordLeak(
            f"forget({forgotten_id}) returned deleted=true but retrieval still surfaces "
            f"the record in caller_scope={caller_scope} with no deleted_at marker. "
            f"Substrate read/retrieval divergence; halt memory writes pending audit."
        )
