# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-memory-tool-s-forget-as-a-tombstone-rather-than-a-deletion.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-memory-tool-s-forget-as-a-tombstone-rather-than-a-deletion.warrant.json
# Markdown SHA-256:   5e887948d7ee6b501ff24fbaa20b95793e5a9a02fb4070b6471c84ee846c6317
# Code AST SHA-256:   ceb6b5b497ac4308f4802ec80dbf37c072f4098dc2bed7b22764bce5d2b57d4b
# Verified Classes:   TombstoneVisibilityError, ForgetResponse, Reader
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class TombstoneVisibilityError(RuntimeError):
    """Raised when a forget is treated as physical deletion while a
    fresh-context reader can still retrieve the record."""
    pass


@dataclass
class ForgetResponse:
    record_id: str
    tombstoned: bool
    quiesced: bool          # True only if global physical unreachability is guaranteed
    compaction_watermark: Optional[int] = None


@dataclass
class Reader:
    name: str
    shares_session: bool    # honors forgetter's monotonic-read guarantee
    semantic_retrieval: bool


def safe_forget(
    record_id: str,
    response: ForgetResponse,
    readers: list[Reader],
    retraction_written: bool,
) -> str:
    """
    Decide whether a forget may be treated as deletion, or whether a
    negative assertion (retraction record) must also be propagated.
    Returns the enforced policy: 'delete' or 'tombstone_with_retraction'.
    """
    if not response.tombstoned:
        raise TombstoneVisibilityError(
            f"forget({record_id}) did not tombstone; record still live"
        )

    # A fresh-context reader that can retrieve semantically may resurrect
    # the record before compaction. This is the hazard conjunction.
    exposed = [
        r for r in readers
        if not r.shares_session and r.semantic_retrieval
    ]

    if response.quiesced:
        # Physical unreachability guaranteed; no reader can resurrect.
        return "delete"

    if exposed and not retraction_written:
        raise TombstoneVisibilityError(
            f"forget({record_id}) is logical-only (watermark="
            f"{response.compaction_watermark}) but readers "
            f"{[r.name for r in exposed]} can still retrieve it; "
            f"write a retraction record before proceeding"
        )

    return "tombstone_with_retraction"
