# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-emit-a-cryptographic-erasure-proof-when-downstream-replica-invalidation-is-incomplete.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-emit-a-cryptographic-erasure-proof-when-downstream-replica-invalidation-is-incomplete.warrant.json
# Markdown SHA-256:   678799f9ae81d87c391a28b7c7a54a1bfe772fd2e930ae800ce7d93b751b3ca9
# Code AST SHA-256:   2bf726c0d4ce7fa7e7f49cf2296793313f63a864ff993c07d89e5be35e07259e
# Verified Classes:   ErasureProofWithheld
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ErasureProofWithheld(RuntimeError):
    """Raised when a cryptographic-erasure proof cannot be truthfully emitted."""
    pass


def emit_erasure_proof(
    subject_id: str,
    destruction_seq: int,
    replica_acks: dict[str, int],      # region -> fencing seq acknowledged
    derived_purge_counts: dict[str, int],
    active_worker_leases: int,
) -> dict:
    # A proof is a conjunction over all replicas and derived stores.
    # Any unknown term makes the conjunction unknown, not true.
    for region, fenced_seq in replica_acks.items():
        if fenced_seq < destruction_seq:
            raise ErasureProofWithheld(
                f"replica {region} fencing seq {fenced_seq} < destruction seq "
                f"{destruction_seq}; pre-destruction key-wrap replay possible"
            )

    for store, count in derived_purge_counts.items():
        if count < 0:
            raise ErasureProofWithheld(
                f"derived store {store} purge unconfirmed (timeout, not ack)"
            )

    if active_worker_leases > 0:
        raise ErasureProofWithheld(
            f"{active_worker_leases} worker lease(s) may hold the DEK; "
            "in-flight re-encryption/backup barrier not quiescent"
        )

    return {
        "subject": subject_id,
        "destruction_seq": destruction_seq,
        "replica_fencing": replica_acks,
        "derived_purges": derived_purge_counts,
        "attestation": "unrecoverable_all_replicas_fenced",
    }
