# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-treat-a-memory-tool-forget-as-a-decay-event-versus-a-hard-delete-and-why-the-distinction-breaks-resumption.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-treat-a-memory-tool-forget-as-a-decay-event-versus-a-hard-delete-and-why-the-distinction-breaks-resumption.warrant.json
# Markdown SHA-256:   61e29a2f183014e55decf2a68568d22b5e1076e83df01f13c7ab169f1260d0b6
# Code AST SHA-256:   dc77ea56809c02ad44615d2a8125caee9c9799a4ec07a9d06c43169599b7fec1
# Verified Classes:   ForgetSemanticsError, ForgetSemantics, ForgetReceipt, ReadResult
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum
from typing import Optional


class ForgetSemanticsError(RuntimeError):
    """Raised when an agent attempts to treat a read miss as authoritative
    absence without a hard-delete receipt, or re-asserts a decayed entry."""


class ForgetSemantics(Enum):
    HARD = "hard"
    DECAY = "decay"


@dataclass(frozen=True)
class ForgetReceipt:
    key: str
    semantics: ForgetSemantics
    forgotten_at_seq: int
    floor_confidence: Optional[float] = None
    eviction_eta_seq: Optional[int] = None


@dataclass(frozen=True)
class ReadResult:
    key: str
    present: bool
    confidence: Optional[float]
    read_at_seq: int


def classify_absence(
    receipt: Optional[ForgetReceipt],
    read: ReadResult,
) -> str:
    """Decide whether a read result is authoritative absence, forgotten-but-visible,
    or indeterminate. Must be called before checkpointing any 'known absent' belief."""
    if receipt is None:
        # No forget on record: absence is just a miss, not a fact of forgetting.
        return "indeterminate"

    if read.read_at_seq <= receipt.forgotten_at_seq:
        raise ForgetSemanticsError(
            f"read for {read.key!r} at seq {read.read_at_seq} is not causally "
            f"after forget at seq {receipt.forgotten_at_seq}; cannot order them"
        )

    if receipt.semantics is ForgetSemantics.HARD:
        return "absent_fact" if not read.present else "inconsistent"

    # Decay semantics: a present low-confidence hit is a retracted memory,
    # NOT a corroboration. A miss is only a fact after eviction_eta.
    if read.present:
        if receipt.floor_confidence is None or read.confidence is None:
            raise ForgetSemanticsError(
                f"decay receipt for {read.key!r} missing floor_confidence; "
                "cannot distinguish forgotten-but-visible from live memory"
            )
        if read.confidence <= receipt.floor_confidence:
            return "forgotten_but_visible"
        return "indeterminate"

    if receipt.eviction_eta_seq is not None and read.read_at_seq >= receipt.eviction_eta_seq:
        return "absent_fact"
    return "indeterminate"


def checkpoint_belief(
    receipt: Optional[ForgetReceipt],
    read: ReadResult,
) -> str:
    verdict = classify_absence(receipt, read)
    if verdict == "forgotten_but_visible":
        raise ForgetSemanticsError(
            f"refusing to re-assert decayed entry {read.key!r}: it was forgotten "
            f"at seq {receipt.forgotten_at_seq} and must not re-enter the working set"
        )
    if verdict == "indeterminate":
        raise ForgetSemanticsError(
            f"cannot checkpoint 'known absent' for {read.key!r}: forget semantics "
            "or causal ordering unresolved; re-derive from primary source"
        )
    if verdict == "inconsistent":
        raise ForgetSemanticsError(
            f"hard-delete receipt for {read.key!r} but read returned present; "
            "tool contract violated"
        )
    return "known_absent"
