# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-right-to-erasure-request-cannot-be-satisfied-by-row-deletion-in-an-agent-memory-store.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-right-to-erasure-request-cannot-be-satisfied-by-row-deletion-in-an-agent-memory-store.warrant.json
# Markdown SHA-256:   cbca43a89318a336bea08bf7ff2f207dae9bddf50683fa362e307156169bb607
# Code AST SHA-256:   f77fed1071435211f3e2b8796e43977e0f0f790b6018728184367d95e573fb80
# Verified Classes:   ErasureUnsatisfiableError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ErasureUnsatisfiableError(RuntimeError):
    """Raised when a right-to-erasure request cannot be satisfied by row deletion
    because derived artifacts (distilled facts, vector ghosts, handoff shadows)
    retain subject content without a recoverable provenance edge."""
    pass


def certify_erasure(store, subject_id: str, probe_material: list[str]) -> dict:
    rows = store.delete_by_subject(subject_id)

    # Residual probe across every tier, not just the source table.
    residuals = {
        "semantic_facts": store.probe_facts(subject_id, probe_material),
        "vector_index": store.probe_vectors(probe_material, min_similarity=0.85),
        "handoff_registry": store.probe_handoffs(subject_id),
    }

    hits = {tier: found for tier, found in residuals.items() if found}
    if hits:
        # Row deletion succeeded but erasure is not certifiable.
        store.mark_pending_derivation_purge(subject_id, hits)
        raise ErasureUnsatisfiableError(
            f"erasure for {subject_id} incomplete: row delete affected "
            f"{rows} rows but residual content found in {sorted(hits)}; "
            f"provenance edges missing or handoff shadows unenumerated"
        )

    if rows == 0 and not store.has_provenance_edge(subject_id):
        raise ErasureUnsatisfiableError(
            f"erasure for {subject_id} matched zero rows and no provenance "
            f"edge exists; cannot distinguish 'never stored' from "
            f"'stored only in derived form'"
        )

    store.mark_complete(subject_id)
    return {"subject_id": subject_id, "rows_deleted": rows, "probe": "empty"}
