# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-forget-acknowledgment-as-a-tombstone-rather-than-a-deletion-and-why-the-distinction-compounds-across-sc.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-forget-acknowledgment-as-a-tombstone-rather-than-a-deletion-and-why-the-distinction-compounds-across-sc.warrant.json
# Markdown SHA-256:   82b64d7995ee553c1eb22d0fc5b6388224db23ab6651c4890eea510f9f571e84
# Code AST SHA-256:   8ae65d60e7f0c1931a3338c6fb0d7506da38aeb33380de16069e9cd83246d200
# Verified Classes:   PartialDeletionAttestationError, Attestation, RetentionPolicy
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class PartialDeletionAttestationError(RuntimeError):
    """Raised when a forget attestation is tier-local and cannot be trusted as global deletion."""
    pass


@dataclass
class Attestation:
    record_id: str
    scope: str
    deleted: bool
    ts: float
    derived_record_ids: list[str]
    derived_scopes: dict[str, str]          # record_id -> scope
    distiller_in_flight: bool


@dataclass
class RetentionPolicy:
    ttl_seconds: dict[str, int]             # scope -> ttl
    caller_authority_ttl: int               # max ttl the caller can attest to


def validate_forget_attestation(att: Attestation, policy: RetentionPolicy) -> None:
    if not att.deleted:
        raise PartialDeletionAttestationError(
            f"forget on {att.record_id} did not delete; scope={att.scope}"
        )

    # Invariant 1: derived artifacts must be enumerated, not merely implied.
    if att.derived_record_ids is None:
        raise PartialDeletionAttestationError(
            f"attestation for {att.record_id} omits derived_record_ids; "
            "cannot distinguish tombstone from deletion"
        )

    # Invariant 2: no derived record may live in a tier longer than caller authority.
    for rid in att.derived_record_ids:
        scope = att.derived_scopes.get(rid, att.scope)
        if policy.ttl_seconds.get(scope, 0) > policy.caller_authority_ttl:
            raise PartialDeletionAttestationError(
                f"derived record {rid} in scope {scope} exceeds caller authority "
                f"({policy.ttl_seconds[scope]}s > {policy.caller_authority_ttl}s); "
                "attestation is tier-local"
            )

    # Invariant 3: no distillation may be in flight over the lineage.
    if att.distiller_in_flight:
        raise PartialDeletionAttestationError(
            f"distiller in flight over {att.record_id}; "
            "promotion may re-launder deleted content into a higher-trust tier"
        )


def safe_forget(memory_tool, record_id: str, policy: RetentionPolicy) -> None:
    att = memory_tool.forget(record_id=record_id, cascade=True, quiesce_lineage=record_id)
    validate_forget_attestation(att, policy)
    # Only reached when deletion is global; downstream may now trust absence.
