# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-memory-tool-erasure-receipt-as-insufficient-evidence-of-forgetting-and-re-verify-against-the-semantic-disti.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-memory-tool-erasure-receipt-as-insufficient-evidence-of-forgetting-and-re-verify-against-the-semantic-disti.warrant.json
# Markdown SHA-256:   731f5ee7810456c2221561cfb99154b3e0345a3f63720a43f3cef9136ca69c85
# Code AST SHA-256:   97bbf5fce7a847e9c8c518ed31537e545ca3403279b983d05e9f68ba004a1f07
# Verified Classes:   IncompleteErasureError, ForgetReceipt, MemorySurface
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Iterable


class IncompleteErasureError(RuntimeError):
    """Raised when a forget receipt is not corroborated by cross-surface verification."""


@dataclass
class ForgetReceipt:
    subject_id: str
    stores_covered: list[str]
    records_removed: int
    propagation: str  # "synchronous" | "recursive" | "none"


@dataclass
class MemorySurface:
    """One physical store the agent's retrieval path can reach."""
    name: str
    retention_days: int
    _rows: dict[str, list[str]] = field(default_factory=dict)

    def read(self, subject_id: str) -> list[str]:
        return list(self._rows.get(subject_id, []))

    def forget(self, subject_id: str) -> int:
        removed = len(self._rows.pop(subject_id, []))
        return removed


def erase_and_verify(
    subject_id: str,
    forget_tool_surface: MemorySurface,
    all_surfaces: Iterable[MemorySurface],
    max_retention_window_days: int,
) -> ForgetReceipt:
    surfaces = list(all_surfaces)
    receipt = ForgetReceipt(
        subject_id=subject_id,
        stores_covered=[forget_tool_surface.name],
        records_removed=forget_tool_surface.forget(subject_id),
        propagation="synchronous",
    )

    # Invariant 1: the forget tool must have covered every reachable surface.
    uncovered = [s.name for s in surfaces if s.name not in receipt.stores_covered]
    if uncovered:
        raise IncompleteErasureError(
            f"forget receipt for {subject_id!r} covers {receipt.stores_covered} "
            f"but retrieval path reaches uncovered stores: {uncovered}"
        )

    # Invariant 2: derived data must not outlive its source by construction.
    longest_ttl = max(s.retention_days for s in surfaces)
    if longest_ttl > max_retention_window_days:
        raise IncompleteErasureError(
            f"surface retention {longest_ttl}d exceeds window "
            f"{max_retention_window_days}d; derived copies may survive source deletion"
        )

    # Invariant 3: verification must be cross-surface, not receipt-bound.
    for surface in surfaces:
        residual = surface.read(subject_id)
        if residual:
            raise IncompleteErasureError(
                f"post-forget read of {surface.name!r} still returns "
                f"{len(residual)} record(s) for {subject_id!r}; "
                f"receipt is not evidence of forgetting"
            )

    return receipt
