# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-regulatory-deletion-request-has-been-silently-defeated-by-memory-forget-tool-semantics.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-regulatory-deletion-request-has-been-silently-defeated-by-memory-forget-tool-semantics.warrant.json
# Markdown SHA-256:   48f2f521afa24d7316f018a7df44ccd0ccd5d06f32ba7723abf6eef9656aa81c
# Code AST SHA-256:   a277dec9e6a60a597dc53a9aa487e5b14a9f48bb808f44973a7b7e9e1f4c8f24
# Verified Classes:   DeletionAttestationError
# Defensive Guards:   6 fail-closed invariants (zero external deps)

class DeletionAttestationError(RuntimeError):
    """Raised when a forget() success return does not satisfy the
    content-unreachability invariant across the memory derivation graph."""
    pass


TOMBSTONE_TTL_CYCLES = 2  # consolidation cycles a tombstone must outlive


def verify_deletion(store, subject_id: str, subject_tokens: set[str]) -> None:
    """Close a regulatory deletion obligation only if no reachable
    representation can reconstruct the subject's content.

    Raises DeletionAttestationError on the first defeated tier.
    """
    tombstone = store.tombstones.get(subject_id)
    if tombstone is None:
        raise DeletionAttestationError(
            f"no tombstone for {subject_id}; forget() was a key-delete only"
        )

    # Tier 1: derived summaries may cite a synthetic source ID that the
    # FK cascade never matched.
    for summary in store.semantic_store.iter_summaries():
        if subject_tokens & summary.tokens:
            raise DeletionAttestationError(
                f"semantic summary {summary.id} still reconstructs subject "
                f"{subject_id}; consolidation re-derived deleted content"
            )

    # Tier 2: ANN index retains vectors until compaction.
    for hit in store.vector_index.probe(subject_tokens, k=8):
        if subject_tokens & hit.payload_tokens:
            raise DeletionAttestationError(
                f"vector {hit.id} in ANN index still yields subject tokens; "
                f"index compaction not triggered by forget()"
            )

    # Tier 3: live session caches rehydrate erased content on resume.
    for cache in store.session_caches.iter_live():
        if subject_tokens & cache.tokens:
            raise DeletionAttestationError(
                f"session cache {cache.id} retains subject {subject_id}; "
                f"resume would re-seed consolidation with erased content"
            )

    # Tombstone must survive long enough that no in-flight consolidation
    # pass can re-derive from residue it read before the delete.
    if tombstone.age_cycles < TOMBSTONE_TTL_CYCLES:
        raise DeletionAttestationError(
            f"tombstone for {subject_id} too young "
            f"({tombstone.age_cycles} < {TOMBSTONE_TTL_CYCLES}); "
            f"deletion-then-recreation window still open"
        )


def forget_and_attest(store, subject_id: str, subject_tokens: set[str]) -> str:
    result = store.forget(subject_id)
    if result.get("status") != "deleted":
        raise DeletionAttestationError(f"forget() failed: {result}")
    verify_deletion(store, subject_id, subject_tokens)
    return "obligation_closed"
