# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-decay-based-eviction-when-an-audit-logged-deletion-proof-cannot-reconstruct-the-evicted-memory-s-contrib.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-decay-based-eviction-when-an-audit-logged-deletion-proof-cannot-reconstruct-the-evicted-memory-s-contrib.warrant.json
# Markdown SHA-256:   e9901e47254a31f9d7f77c39a3b0109291ac567f91134d317907893f207b6280
# Code AST SHA-256:   d4530b23d25a79f7697f51d7d20553fab07685c93410a631d37a9b153dcd02ad
# Verified Classes:   DeletionProofIncomplete, ProvenanceLiveEviction, Memory, DeletionProof
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Optional


class DeletionProofIncomplete(RuntimeError):
    """Raised when an eviction would proceed without a reconstructable proof."""
    pass


class ProvenanceLiveEviction(DeletionProofIncomplete):
    """Raised when a retention-dead memory is still cited by a live derivation."""
    pass


@dataclass
class Memory:
    id: str
    content_hash: str
    assertion_summary: Optional[str]
    decay_score: float
    neighborhood_size: int
    cited_by_live_derivations: list[str] = field(default_factory=list)
    irreplaceable: bool = False


@dataclass
class DeletionProof:
    memory_id: str
    content_hash: Optional[str]
    assertion_summary: Optional[str]
    cited_by: list[str]
    decay_score_at_eviction: float
    lambda_: float
    neighborhood_size: int

    def is_content_complete(self) -> bool:
        return (
            self.content_hash is not None
            and self.assertion_summary is not None
        )


def evaluate_eviction(
    mem: Memory,
    threshold: float,
    lambda_: float,
    reverse_index: dict[str, list[str]],
) -> str:
    """Return 'delete', 'quarantine', or raise if the proof obligation is unmet."""
    if mem.decay_score >= threshold:
        return "retain"

    live_citations = reverse_index.get(mem.id, [])
    if live_citations:
        raise ProvenanceLiveEviction(
            f"memory {mem.id} is retention-dead (score={mem.decay_score:.4f} < "
            f"{threshold}) but cited by live derivations {live_citations}; "
            f"eviction would sever a provenance chain mid-flight"
        )

    proof = DeletionProof(
        memory_id=mem.id,
        content_hash=mem.content_hash if mem.content_hash else None,
        assertion_summary=mem.assertion_summary,
        cited_by=live_citations,
        decay_score_at_eviction=mem.decay_score,
        lambda_=lambda_,
        neighborhood_size=mem.neighborhood_size,
    )

    singleton = mem.neighborhood_size == 0
    if singleton and not mem.irreplaceable and not proof.is_content_complete():
        raise DeletionProofIncomplete(
            f"memory {mem.id} is a singleton (neighborhood_size=0) with an "
            f"incomplete deletion proof; interference-aware eviction's "
            f"recoverability assumption is falsified and the proof cannot "
            f"reconstruct the lost assertion"
        )

    if not proof.is_content_complete() and not mem.irreplaceable:
        return "quarantine"

    return "delete"
