# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-forget-acknowledgement-from-a-vector-store-as-non-evidentiary-until-an-independent-tombstone-replay-con.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-forget-acknowledgement-from-a-vector-store-as-non-evidentiary-until-an-independent-tombstone-replay-con.warrant.json
# Markdown SHA-256:   3f775a0dd47289dc60daa2ac044af6d8837c287c3fd6db9c0409140c7f44bfa0
# Code AST SHA-256:   ac15aae5098fbf4237da56e06bd347a3d014da71d629b0209a0c52e38862173c
# Verified Classes:   ErasureAuditError, CompactionState, DeleteAck, AuditConfig
# Defensive Guards:   8 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class ErasureAuditError(RuntimeError):
    """Raised when a forget acknowledgement cannot be admitted as evidence."""


class CompactionState(Enum):
    TOMBSTONED = "tombstoned"
    COMPACTED = "compacted"


@dataclass
class DeleteAck:
    subject_id: str
    deleted: bool
    compaction_state: CompactionState
    replica_lag_seconds: float


@dataclass
class AuditConfig:
    certification_threshold: float      # tau used when signing
    strictest_downstream_threshold: float  # loosest future audit tau
    margin: float
    max_replica_lag_seconds: float
    provenance_edges_nullable: bool


def admit_acknowledgement(
    ack: DeleteAck,
    cfg: AuditConfig,
    intra_subject_min_similarity: float,
    reverse_provenance_hits: int,
    probe_uses_deletion_filter: bool,
) -> bool:
    if not ack.deleted:
        raise ErasureAuditError(
            f"subject {ack.subject_id}: store reports deleted=false; nothing to audit"
        )

    if ack.compaction_state is not CompactionState.COMPACTED:
        raise ErasureAuditError(
            f"subject {ack.subject_id}: tombstoned but not compacted; "
            "logical deletion is non-evidentiary for RTBF"
        )

    if ack.replica_lag_seconds > cfg.max_replica_lag_seconds:
        raise ErasureAuditError(
            f"subject {ack.subject_id}: replica lag {ack.replica_lag_seconds}s "
            f"exceeds bound {cfg.max_replica_lag_seconds}s; stale replicas may "
            "still serve the erased record"
        )

    if probe_uses_deletion_filter:
        raise ErasureAuditError(
            f"subject {ack.subject_id}: verification probe is filtered by the "
            "tombstone it is verifying; circular audit, path not independent"
        )

    # Certification threshold must be at least as strict as the strictest
    # downstream audit, minus margin. Otherwise a blind band exists.
    required = cfg.strictest_downstream_threshold - cfg.margin
    if cfg.certification_threshold > required:
        raise ErasureAuditError(
            f"subject {ack.subject_id}: certification tau "
            f"{cfg.certification_threshold} is looser than required {required}; "
            "records in the blind band would be invisible to the certifier but "
            "visible to a stricter downstream audit"
        )

    if intra_subject_min_similarity < required:
        raise ErasureAuditError(
            f"subject {ack.subject_id}: intra-subject min similarity "
            f"{intra_subject_min_similarity} falls below required floor "
            f"{required}; paraphrase probes may miss surviving records"
        )

    if cfg.provenance_edges_nullable and reverse_provenance_hits > 0:
        raise ErasureAuditError(
            f"subject {ack.subject_id}: {reverse_provenance_hits} derived "
            "semantic node(s) reference the subject but provenance edges are "
            "nullable; distillation closure is broken and keyed erasure cannot "
            "reach orphans"
        )

    if reverse_provenance_hits > 0:
        raise ErasureAuditError(
            f"subject {ack.subject_id}: {reverse_provenance_hits} derived "
            "node(s) survive erasure; fan-out incomplete"
        )

    return True
