# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-issues-a-forget-and-the-verifier-audits-the-wrong-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-issues-a-forget-and-the-verifier-audits-the-wrong-epoch.warrant.json
# Markdown SHA-256:   4a1abf22911b5765165f49b587cb8426980f443e6b0202c532bfd7456667f1b8
# Code AST SHA-256:   129647b2a442a6f378e32d9706da31ccff579dee168dd8e41f6cce529ee1c595
# Verified Classes:   ForgetVerificationError, ForgetOp, ReadResult
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class ForgetVerificationError(RuntimeError):
    """Raised when a forget-verification would assert deletion on insufficient evidence."""


@dataclass(frozen=True)
class ForgetOp:
    op_id: str
    subject: str
    epoch: int


@dataclass(frozen=True)
class ReadResult:
    results: list
    epoch: int
    tombstone_visible: bool
    includes_compacted: bool


def verify_forget(op: ForgetOp, read: ReadResult) -> str:
    """
    Return 'FORGET_VERIFIED' only when the read is causally downstream of the
    forget AND the tombstone itself is observable. Absence alone is noise.
    """
    # Precondition 1: causal ordering. The read's epoch must include the forget.
    if read.epoch < op.epoch:
        raise ForgetVerificationError(
            f"read epoch {read.epoch} predates forget epoch {op.epoch} "
            f"for op {op.op_id}; absence in a pre-tombstone view is not evidence of deletion"
        )

    # Precondition 2: the mutation's own marker must be visible. Absence of the
    # record is a consequence; the tombstone is the marker. Check the marker.
    if not read.tombstone_visible:
        raise ForgetVerificationError(
            f"tombstone for op {op.op_id} not observable in read domain; "
            f"cannot distinguish deletion from a view that never held the record"
        )

    # Precondition 3: the read must cover the same storage surfaces as the
    # forget's scope, or absence is only true of the fast path.
    if not read.includes_compacted:
        raise ForgetVerificationError(
            f"verification read for op {op.op_id} excluded compacted segments; "
            f"record may survive in segment bytes and resurrect on replay"
        )

    if read.results:
        raise ForgetVerificationError(
            f"forget op {op.op_id} reported verified but record still present "
            f"at epoch {read.epoch}: {read.results!r}"
        )

    return "FORGET_VERIFIED"


def audit_pass(op: ForgetOp, read: ReadResult) -> str:
    try:
        return verify_forget(op, read)
    except ForgetVerificationError:
        # Downgrade, never upgrade. Re-queue against a downstream domain.
        return "FORGET_UNCONFIRMED"
