# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-forget-acknowledgement-as-insufficient-proof-of-deletion-in-scoped-retention-memory-apis.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-forget-acknowledgement-as-insufficient-proof-of-deletion-in-scoped-retention-memory-apis.warrant.json
# Markdown SHA-256:   8da3387916c3a5f6f8204f132e45a2d3bbd0cc47964b818637124a48853dee7d
# Code AST SHA-256:   3320fec42296a8874e9e404b693aa5772aa8d235d0c2a7aa1a90c2cd32d5f746
# Verified Classes:   InsufficientDeletionProof, Tier, ForgetAck
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable


class InsufficientDeletionProof(RuntimeError):
    """Raised when a forget acknowledgement does not establish unreachability."""


@dataclass
class Tier:
    name: str
    retention_days: int
    is_derivative: bool


@dataclass
class ForgetAck:
    status: str
    verified: bool
    tiers_covered: set[str] = field(default_factory=set)


def assert_deletion_provable(
    ack: ForgetAck,
    scope_tiers: Iterable[Tier],
    assumed_horizon_days: int,
    residue_probes: dict[str, bool],
) -> None:
    """Enforce: cleared means no derivative reconstructs the value.

    Raises InsufficientDeletionProof if any tier can still materialize the
    forgotten content within the agent's assumed threat horizon.
    """
    if ack.status != "deleted":
        raise InsufficientDeletionProof(
            f"forget not accepted: status={ack.status!r}"
        )

    tiers = list(scope_tiers)

    uncovered = [t for t in tiers if t.name not in ack.tiers_covered]
    if uncovered:
        raise InsufficientDeletionProof(
            "forget did not cover all materializable tiers: "
            + ", ".join(t.name for t in uncovered)
        )

    long_lived = [t for t in tiers if t.retention_days > assumed_horizon_days]
    if long_lived:
        raise InsufficientDeletionProof(
            "tier retention exceeds assumed horizon: "
            + ", ".join(f"{t.name}={t.retention_days}d" for t in long_lived)
        )

    derivative_residue = [
        name for name, hit in residue_probes.items()
        if hit and any(t.is_derivative and t.name == name for t in tiers)
    ]
    if derivative_residue:
        raise InsufficientDeletionProof(
            "derivative tier still reconstructs content: "
            + ", ".join(derivative_residue)
        )

    any_residue = [name for name, hit in residue_probes.items() if hit]
    if any_residue:
        raise InsufficientDeletionProof(
            "residue detected on probe: " + ", ".join(any_residue)
        )


def clear_scope_after_forget(ack: ForgetAck, scope_tiers, horizon, probes) -> str:
    assert_deletion_provable(ack, scope_tiers, horizon, probes)
    return "CLEARED"
