# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-provenance-laundering-during-memory-eviction-when-a-forget-tombstone-silently-re-admits-a-superseded-fact-under-a-fresh-pr.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-provenance-laundering-during-memory-eviction-when-a-forget-tombstone-silently-re-admits-a-superseded-fact-under-a-fresh-pr.warrant.json
# Markdown SHA-256:   68996a28d69a4a02d9411ff4dd18355c976673f3b79e9d65051ab3aa1e2364e6
# Code AST SHA-256:   77954f8f44b0b01499768a14bb067f61618afd0cc14af62aaab50ec95bcdc338
# Verified Classes:   EvictionLaunderingError, Tombstone, MemoryStore
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass, field
from typing import Optional


class EvictionLaunderingError(RuntimeError):
    """Raised when a write re-admits content whose hash is tombstoned."""


@dataclass
class Tombstone:
    content_hash: str
    memory_id: str
    provenance: dict
    reauthorized: bool = False
    reauth_provenance: Optional[dict] = None


@dataclass
class MemoryStore:
    live: dict = field(default_factory=dict)          # memory_id -> record
    tombstones: dict = field(default_factory=dict)    # content_hash -> Tombstone

    @staticmethod
    def _hash(content: str) -> str:
        return hashlib.sha256(content.strip().lower().encode()).hexdigest()

    def write(self, memory_id: str, content: str, provenance: dict,
              reauthorize: bool = False) -> None:
        ch = self._hash(content)
        tomb = self.tombstones.get(ch)
        if tomb is not None and not tomb.reauthorized:
            if not reauthorize:
                raise EvictionLaunderingError(
                    f"write of memory_id={memory_id!r} re-admits content "
                    f"tombstoned as {tomb.memory_id!r} at provenance "
                    f"{tomb.provenance}; pass reauthorize=True to override"
                )
            tomb.reauthorized = True
            tomb.reauth_provenance = provenance
        self.live[memory_id] = {"content": content, "hash": ch,
                                "provenance": provenance}

    def forget(self, memory_id: str, reason: str) -> None:
        rec = self.live.pop(memory_id)
        self.tombstones[rec["hash"]] = Tombstone(
            content_hash=rec["hash"], memory_id=memory_id,
            provenance={"reason": reason, **rec["provenance"]},
        )


# Trigger trace: eviction then distillation resurrection.
store = MemoryStore()
store.write("m_addr_v1", "billing address is addr_v1",
            provenance={"epoch": 3, "source": "turn_14"})
store.forget("m_addr_v1", reason="superseded_by:addr_v2")

# Distillation sub-agent re-extracts turn_14 under a new epoch.
# Without the content-hash tombstone index this would silently succeed.
store.write("m_addr_v1_resurrected", "billing address is addr_v1",
            provenance={"epoch": 9, "source": "turn_14_resummarized"})
# -> raises EvictionLaunderingError; eviction invariant holds.
