# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconcile-a-right-to-erasure-tombstone-against-a-semantic-memory-index-that-has-already-absorbed-the-erased-episode.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconcile-a-right-to-erasure-tombstone-against-a-semantic-memory-index-that-has-already-absorbed-the-erased-episode.warrant.json
# Markdown SHA-256:   c6f324500b3ac33224a8c78a0f1e0388b8ae9e184290f7d8d3fcf6a67eb9634d
# Code AST SHA-256:   7775144918226c47526c5f70f8c3da282cf990692200af5b640fb4c76d72c812
# Verified Classes:   ErasureProvenanceError, SemanticFact
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ErasureProvenanceError(RuntimeError):
    """Raised when an erasure request cannot be certified because a derived
    semantic artifact has an incomplete or unresolvable provenance set."""
    pass


class SemanticFact:
    def __init__(self, fact_id, content, source_episode_ids, provenance_complete):
        self.fact_id = fact_id
        self.content = content
        self.source_episode_ids = set(source_episode_ids)
        self.provenance_complete = provenance_complete


def certify_erasure(subject_id, subject_episode_ids, semantic_facts):
    """
    Returns the list of fact_ids that must be deleted to certify erasure.
    Raises ErasureProvenanceError if any fact *might* contain the subject's
    data but cannot be proven not to, because its provenance is incomplete.
    """
    subject_episode_ids = set(subject_episode_ids)
    must_delete = []
    unprovable = []

    for fact in semantic_facts:
        cites_subject = bool(fact.source_episode_ids & subject_episode_ids)
        if cites_subject:
            must_delete.append(fact.fact_id)
        elif not fact.provenance_complete:
            # Fact does not cite the subject, but we cannot prove it doesn't
            # encode the subject's content. Fail closed.
            unprovable.append(fact.fact_id)

    if unprovable:
        raise ErasureProvenanceError(
            f"Cannot certify erasure of subject {subject_id}: "
            f"{len(unprovable)} facts have incomplete provenance and may "
            f"launder erased content: {unprovable}"
        )

    return must_delete


def handle_erasure_request(subject_id, store):
    episodes = store.episodes_for_subject(subject_id)
    if not episodes:
        return {"status": "no_data"}

    facts = store.all_semantic_facts()
    try:
        to_delete = certify_erasure(subject_id, episodes, facts)
    except ErasureProvenanceError:
        # Refuse to report success. Escalate to full-index scan + delete.
        raise ErasureProvenanceError(
            f"Erasure for {subject_id} blocked pending provenance repair; "
            f"do not return success to the requester."
        )

    store.delete_episodes(episodes)
    store.delete_facts(to_delete)
    return {"status": "certified", "facts_deleted": len(to_delete)}
