# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-scoped-forget-acknowledgement-when-the-retention-tombstone-has-not-yet-propagated-to-replica-memory-shards.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-scoped-forget-acknowledgement-when-the-retention-tombstone-has-not-yet-propagated-to-replica-memory-shards.warrant.json
# Markdown SHA-256:   ba4669b6035999c26ac85a3f6941a623d63af5b45137c934cce273e10a45210c
# Code AST SHA-256:   795415884154fda4cc7220252b767997128ad84fd075e32b1aaadf180820f18c
# Verified Classes:   UnquiescedForgetError, ForgetAck, ReadResult
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class UnquiescedForgetError(RuntimeError):
    """Raised when an agent attempts to act on a forget acknowledgement
    whose tombstone has not propagated to all replicas the agent may read from."""
    pass


@dataclass
class ForgetAck:
    scope: str
    epoch: int
    propagation: str          # "quiesced" | "primary_only"
    replica_watermarks: dict  # replica_id -> applied_epoch


@dataclass
class ReadResult:
    scope: str
    content: str | None
    replica_id: str
    replica_epoch: int


def verify_erasure(ack: ForgetAck, read_result: ReadResult, min_epoch_floor: int) -> bool:
    # Gate 1: refuse to verify through an unpinned/stale replica.
    if read_result.replica_epoch < ack.epoch:
        raise UnquiescedForgetError(
            f"verification read on {read_result.replica_id} at epoch "
            f"{read_result.replica_epoch} < forget epoch {ack.epoch}; "
            f"content={read_result.content!r} is a stale read, not a deletion failure"
        )

    # Gate 2: refuse to treat a primary-only ack as a retention guarantee.
    if ack.propagation != "quiesced":
        lagging = {r: w for r, w in ack.replica_watermarks.items() if w < ack.epoch}
        raise UnquiescedForgetError(
            f"forget of {ack.scope!r} not quiesced; lagging replicas={lagging}. "
            f"Do not retry with a mutated scope; wait or pin reads to min_epoch={ack.epoch}"
        )

    if read_result.replica_epoch < min_epoch_floor:
        raise UnquiescedForgetError(
            f"read epoch {read_result.replica_epoch} below session floor {min_epoch_floor}"
        )

    # Only here is content absence a valid erasure confirmation.
    return read_result.content is None
