# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-forget-acknowledgement-as-a-tombstone-rather-than-a-completed-deletion-and-what-breaks-when-it-does-not.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-forget-acknowledgement-as-a-tombstone-rather-than-a-completed-deletion-and-what-breaks-when-it-does-not.warrant.json
# Markdown SHA-256:   7fd69cf651e6a608b133f1d95aa18f0ab3511bca0ad265e01c1bceb25d904e22
# Code AST SHA-256:   6acbf362fabb91b92ef01d538cc6b9bcc4bf0f3471105fbd01a2a4c212153361
# Verified Classes:   ForgetAckClass, ForgetIntent, MemoryContinuityError, ForgetAck, QuarantineLedger
# Defensive Guards:   3 fail-closed invariants (zero external deps)

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


class ForgetAckClass(Enum):
    ERASURE_STRONG = "erasure_strong"   # ack implies unrecoverable
    TOMBSTONE_WEAK = "tombstone_weak"   # ack implies only logical hiding


class ForgetIntent(Enum):
    RELEVANCE = "relevance"   # stale / superseded -> tombstone is fine
    INTEGRITY = "integrity"   # poison / injection / privacy -> needs erasure


class MemoryContinuityError(RuntimeError):
    """Raised when an agent would make a security claim the ack cannot support."""


@dataclass
class ForgetAck:
    memory_id: str
    ack_class: ForgetAckClass
    cascade_confirmed: bool = False
    retention_days_remaining: Optional[int] = None


@dataclass
class QuarantineLedger:
    # class-level quarantine so derivation jobs skip the whole lineage
    quarantined_classes: set[str] = field(default_factory=set)

    def quarantine(self, memory_class: str) -> None:
        self.quarantined_classes.add(memory_class)

    def derivation_allowed(self, source_class: str) -> bool:
        return source_class not in self.quarantined_classes


def classify_and_act(
    ack: ForgetAck,
    intent: ForgetIntent,
    memory_class: str,
    ledger: QuarantineLedger,
) -> str:
    """
    Decide whether the ack discharges the agent's claim, and mutate state
    accordingly. Returns the honest postcondition string.
    """
    if ack.ack_class is ForgetAckClass.ERASURE_STRONG:
        return f"{ack.memory_id}: erased; influence channel closed"

    # Tombstone-weak path.
    if intent is ForgetIntent.RELEVANCE:
        return (
            f"{ack.memory_id}: tombstoned; hidden from read, "
            f"retention {ack.retention_days_remaining}d remaining"
        )

    # INTEGRITY intent against a tombstone-weak ack: must quarantine the class
    # and must NOT claim the memory is neutralized.
    ledger.quarantine(memory_class)

    if not ack.cascade_confirmed:
        # Cannot prove derived records are covered -> hard stop.
        raise MemoryContinuityError(
            f"forget({ack.memory_id}) returned tombstone-weak ack with no "
            f"cascade coverage; class '{memory_class}' quarantined but derived "
            f"memories may still carry poisoned content. Refusing to assert "
            f"erasure. Escalate for erasure-strong path or lineage audit."
        )

    return (
        f"{ack.memory_id}: quarantined class '{memory_class}', cascade "
        f"confirmed; derived records blocked from distillation"
    )


def guard_derived_read(
    derived_class: str,
    derived_from: Optional[str],
    ledger: QuarantineLedger,
) -> None:
    """
    Called before trusting any semantic memory. Blocks reads whose lineage
    is quarantined or unknown.
    """
    if derived_from is None:
        raise MemoryContinuityError(
            f"derived memory class '{derived_class}' has no lineage edge; "
            f"cannot prove it does not descend from a quarantined source. "
            f"Treat as untrusted and re-verify against policy."
        )
    if not ledger.derivation_allowed(derived_from):
        raise MemoryContinuityError(
            f"derived memory class '{derived_class}' descends from quarantined "
            f"source '{derived_from}'; refusing to surface poisoned lineage."
        )
