# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-reconcile-ttl-expiry-against-provenance-anchored-erasure-in-a-shared-memory-tool-schema.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-reconcile-ttl-expiry-against-provenance-anchored-erasure-in-a-shared-memory-tool-schema.warrant.json
# Markdown SHA-256:   00fa1df7f36720955b66c52cc70391252fa54c8a74793046c3136927415199c7
# Code AST SHA-256:   d92a3672de35eaa378bacc0fd2136d697fe6d58c0c7e8556968083d657acb31e
# Verified Classes:   ErasureIncompleteError
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class ErasureIncompleteError(RuntimeError):
    """Raised when forget(P) cannot prove absence across all representations."""
    pass


REPRESENTATIONS = ("body", "derived", "vector", "replica")


def forget_provenance(index, stores, key, max_retries=3):
    for attempt in range(max_retries):
        version = index.version(key)
        ids = index.enumerate_ids(key, version)
        derived = index.enumerate_derived(key, version)
        # Tombstone first so the reaper cannot reclaim under us.
        index.tombstone(key, version)
        for rep in REPRESENTATIONS:
            stores[rep].delete_many(ids + derived)
        # Re-read: any intervening mutation invalidates this attempt.
        if index.version(key) != version:
            continue
        missing = [rep for rep in REPRESENTATIONS
                   if not stores[rep].confirm_absent(ids + derived)]
        if missing:
            index.mark_pending(key, version, missing)
            raise ErasureIncompleteError(
                f"forget({key}) at version {version}: "
                f"representations {missing} still hold records"
            )
        index.drop(key, version)
        return True
    raise ErasureIncompleteError(
        f"forget({key}) aborted after {max_retries} retries: "
        f"index version kept mutating (reaper or concurrent writer)"
    )
