# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-forget-tool-compliance-as-unprovable-rather-than-confirmed-when-erasure-targets-were-never-resident-in-its-li.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-forget-tool-compliance-as-unprovable-rather-than-confirmed-when-erasure-targets-were-never-resident-in-its-li.warrant.json
# Markdown SHA-256:   8475ae9c317740aab86ae466164cde945e9125ad9fecfed3eb34f888b6418225
# Code AST SHA-256:   52b75dd55bb3a4f7449d2f4e622668a1fd174b2835e965ae129563339cf3b7ae
# Verified Classes:   ErasureState, UnwitnessedErasureAttestationError, ForgetRequest, ForgetResult
# Defensive Guards:   2 fail-closed invariants (zero external deps)

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


class ErasureState(str, Enum):
    CONFIRMED = "confirmed"
    RESIDUAL = "residual"
    UNVERIFIED_PENDING_STORE_PROBE = "unverified_pending_store_probe"


class UnwitnessedErasureAttestationError(RuntimeError):
    """Raised when an instance attempts to promote a forget-tool acknowledgment
    into a confirmed-erasure attestation for a target it never held in context."""


@dataclass
class ForgetRequest:
    target_key: str
    target_in_live_context: bool
    context_paraphrases: list[str] = field(default_factory=list)


@dataclass
class ForgetResult:
    tool_ack: dict
    retrievable_until: Optional[float] = None  # compaction barrier, epoch seconds


def attest_erasure(
    req: ForgetRequest,
    result: ForgetResult,
    *,
    post_probe_ran: bool,
    post_probe_empty: bool,
    now: float,
) -> ErasureState:
    # Gate 1: no witnessed pre-state => no local attestation authority.
    if not req.target_in_live_context:
        if result.tool_ack.get("status") == "ok":
            # Do NOT promote to CONFIRMED. Hand off to store-side verifier.
            return ErasureState.UNVERIFIED_PENDING_STORE_PROBE
        return ErasureState.UNVERIFIED_PENDING_STORE_PROBE

    # Gate 2: witnessed target requires a witnessed post-state probe.
    if not post_probe_ran:
        raise UnwitnessedErasureAttestationError(
            f"target {req.target_key!r} was in context but no post-forget "
            "retrieval probe was executed; cannot attest"
        )

    # Gate 3: soft-delete compaction barrier must have elapsed.
    if result.retrievable_until is not None and now < result.retrievable_until:
        return ErasureState.UNVERIFIED_PENDING_STORE_PROBE

    # Gate 4: semantic derivatives in context survive record-level deletes.
    if req.context_paraphrases and not post_probe_empty:
        return ErasureState.RESIDUAL

    if post_probe_empty and not req.context_paraphrases:
        return ErasureState.CONFIRMED

    return ErasureState.RESIDUAL


def assert_attestation_authorized(state: ErasureState, target_in_context: bool) -> None:
    if state is ErasureState.CONFIRMED and not target_in_context:
        raise UnwitnessedErasureAttestationError(
            "CONFIRMED erasure asserted for an unwitnessed target; "
            "instance lacks standing to make this claim"
        )
