# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-decay-weighted-memory-retrieval-as-unsafe-after-an-explicit-erasure.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-decay-weighted-memory-retrieval-as-unsafe-after-an-explicit-erasure.warrant.json
# Markdown SHA-256:   68c2cbba5791132193406614a458eafed6595ee4847f8bfc47afe090c4dd525d
# Code AST SHA-256:   810ed1c4e9107827bd3b018f098f5a7fe0a22975aa9477ea21b156b81284574b
# Verified Classes:   MemoryStatusError, Status, ReadResult
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class MemoryStatusError(RuntimeError):
    """Raised when an instance attempts to infer erasure from retrieval absence."""


class Status(Enum):
    PRESENT = "present"
    DECAYED_BELOW_THRESHOLD = "decayed_below_threshold"
    ERASED = "erased"
    NEVER_WRITTEN = "never_written"


@dataclass
class ReadResult:
    results: list
    below_threshold_count: int

    def is_ambiguous_empty(self) -> bool:
        # Empty results with any below-threshold matches means the store
        # HAS records that merely decayed out. Absence is not erasure.
        return len(self.results) == 0 and self.below_threshold_count > 0


def resolve_memory(record_id: str, read: ReadResult, status_probe) -> Status:
    """Resolve true status. Refuses to collapse decay into erasure."""
    if read.is_ambiguous_empty():
        # The read told us nothing about membership. Probe authoritatively.
        status = status_probe(record_id)
        if status is None:
            raise MemoryStatusError(
                f"record {record_id!r} decayed below retrieval threshold but "
                f"status probe returned no membership; cannot distinguish "
                f"decayed from erased — refusing to reconstruct or rewrite"
            )
        return status
    status = status_probe(record_id)
    if status is None:
        raise MemoryStatusError(
            f"record {record_id!r} present in ranking but absent from status "
            f"probe; store is inconsistent, erasure semantics untrustworthy"
        )
    return status


def safe_write(record_id: str, payload: bytes, status: Status, store) -> None:
    """Write must not silently reset decay weight on an existing record."""
    if status is Status.ERASED:
        raise MemoryStatusError(
            f"refusing to write {record_id!r}: record was explicitly erased; "
            f"reconstruction-and-rewrite would resurrect erased state"
        )
    if status is Status.DECAYED_BELOW_THRESHOLD:
        # Require an explicit refresh intent, never an implicit upsert.
        raise MemoryStatusError(
            f"refusing implicit upsert of decayed record {record_id!r}; "
            f"caller must issue refresh() to reset decay weight deliberately"
        )
    store.write(record_id, payload, reset_decay=(status is Status.NEVER_WRITTEN))
