# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-gdpr-erasure-request-has-been-satisfied-in-a-persistent-agent-store-and-why-row-deleted-is-a-false-completion-signa.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-gdpr-erasure-request-has-been-satisfied-in-a-persistent-agent-store-and-why-row-deleted-is-a-false-completion-signa.warrant.json
# Markdown SHA-256:   c3ef71799c99c26b3952f5fe2441cadfa443d82b247b0ec95ea9df988a2b5f9a
# Code AST SHA-256:   077dfd151548bb614a76de575ee4d80c1e7757f544f99174300dc349de63a25d
# Verified Classes:   ErasureIncompleteError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ErasureIncompleteError(RuntimeError):
    """Raised when a GDPR-style erasure request is certified satisfied
    but reconstructable information about the subject survives in the
    agent store's derived artifact classes."""
    pass


NOISE_FLOOR = 0.72          # embedding similarity below this is not recoverable
ORPHAN_TOLERANCE = 0.0      # any provenance-orphan node is a hard failure


def certify_erasure(store, subject_id: str, erased_fact_embeddings: list) -> dict:
    """
    Completion predicate for erasure in a persistent agent store.
    Returns a certificate only if all three structural signals are clean.
    """
    # Signal 1: provenance closure. A semantic node whose entire source
    # set is dead is a surviving derivative of erased data.
    orphans = [
        node for node in store.semantic_nodes()
        if node.source_episode_ids
        and all(store.episode_deleted(eid) for eid in node.source_episode_ids)
    ]
    orphan_rate = len(orphans) / max(1, len(store.semantic_nodes()))

    # Signal 2: embedding recoverability. Embeddings are not subject-keyed,
    # so they must be tested by content, not by row identity.
    recoverable = []
    for emb in erased_fact_embeddings:
        for hit in store.nearest_neighbors(emb, k=20):
            if hit.similarity >= NOISE_FLOOR:
                recoverable.append(hit.artifact_id)

    # Signal 3: cache residue. Task-scoped scratch blobs are not subject-keyed
    # and are missed by any identifier-based sweep.
    residue = store.sweep_by_content_hash(store.fact_hashes_for(subject_id))

    report = {
        "orphan_rate": orphan_rate,
        "recoverable_artifacts": sorted(set(recoverable)),
        "cache_residue": residue,
    }

    if orphan_rate > ORPHAN_TOLERANCE:
        raise ErasureIncompleteError(
            f"provenance-orphan rate {orphan_rate:.3f} > {ORPHAN_TOLERANCE}: "
            f"{len(orphans)} distilled nodes survive with fully-erased sources"
        )
    if recoverable:
        raise ErasureIncompleteError(
            f"{len(set(recoverable))} artifacts recoverable above noise floor "
            f"{NOISE_FLOOR} from erased-fact embeddings"
        )
    if residue:
        raise ErasureIncompleteError(
            f"{len(residue)} task-scoped cache entries contain erased fact content"
        )

    return {"status": "SATISFIED", **report}
