# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-emit-an-erasure-proof-when-the-dp-noise-budget-and-the-audit-log-disagree-about-what-was-deleted.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-emit-an-erasure-proof-when-the-dp-noise-budget-and-the-audit-log-disagree-about-what-was-deleted.warrant.json
# Markdown SHA-256:   5931cc76f7f5f13419ae8bf81aad39c7f1d95e3ec34d788819f83e48a22dca21
# Code AST SHA-256:   65b89245cecf2474e8091aa14214e89c529dde5e72ac38278189017537c5708d
# Verified Classes:   ErasureProofInvalid, AuditLog, DPBudget
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class ErasureProofInvalid(RuntimeError):
    """Raised when a forget tool is asked to emit a proof it cannot justify."""
    pass


@dataclass
class AuditLog:
    compaction_frontier: int
    entries: dict  # seq -> payload

    def contains(self, record_id: str, upto_seq: int) -> bool:
        for seq, payload in self.entries.items():
            if seq <= upto_seq and record_id in str(payload):
                return True
        return False


@dataclass
class DPBudget:
    epsilon_declared: float
    epsilon_spent: float

    def reconciles(self) -> bool:
        return self.epsilon_spent <= self.epsilon_declared


def emit_erasure_proof(
    record_id: str,
    proof_seq: int,
    audit: AuditLog,
    budget: DPBudget,
    noise_paths_covered: bool,
) -> dict:
    # Condition 2: every artifact holding the record must be noise-covered or deleted.
    if not noise_paths_covered:
        raise ErasureProofInvalid(
            f"noise coverage violated for {record_id}: an un-noised artifact "
            f"(snapshot/state-transfer) still holds the record"
        )

    # Condition 1: frontier dominance. The proof seq must not exceed the
    # compaction frontier, or log-tail entries may still reconstruct the record.
    if proof_seq > audit.compaction_frontier:
        if audit.contains(record_id, upto_seq=proof_seq):
            raise ErasureProofInvalid(
                f"frontier lag for {record_id}: proof_seq={proof_seq} > "
                f"frontier={audit.compaction_frontier} and log tail still "
                f"contains the record; return PendingErasure, do not sign"
            )

    # Condition 3: budget reconciliation across all masking since deletion.
    if not budget.reconciles():
        raise ErasureProofInvalid(
            f"DP budget exhausted for {record_id}: spent={budget.epsilon_spent} "
            f"> declared={budget.epsilon_declared}; record recoverable via log "
            f"tail and no longer masked to declared epsilon"
        )

    return {
        "record": record_id,
        "seq": proof_seq,
        "frontier_at_sign": audit.compaction_frontier,
        "epsilon_spent": budget.epsilon_spent,
        "signed_at": time.time(),
    }
