# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-forget-a-scope-boundary-it-never-re-reads-tombstone-laundering-across-memory-tool-contracts.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-forget-a-scope-boundary-it-never-re-reads-tombstone-laundering-across-memory-tool-contracts.warrant.json
# Markdown SHA-256:   b3350148f6d15c98cda5e6567910214a07b896a5a55dbd5f357948533abbb93c
# Code AST SHA-256:   d8b68ba845ef577872a64b291a39d17d7c42766f6d5042f4a394ee360951ed5b
# Verified Classes:   TombstoneLaunderingError, MemoryStore
# Defensive Guards:   1 fail-closed invariants (zero external deps)

class TombstoneLaunderingError(RuntimeError):
    """Raised when a write to a scope carries content derived from a pre-forget epoch."""
    pass


class MemoryStore:
    def __init__(self):
        self._epoch = {}          # scope -> current epoch
        self._records = {}        # scope -> {id: content}

    def forget(self, scope: str) -> dict:
        removed = list(self._records.pop(scope, {}).keys())
        self._epoch[scope] = self._epoch.get(scope, 0) + 1
        # Receipt carries the fence; callers MUST thread it into later writes.
        return {"scope": scope, "deleted": len(removed),
                "removed_ids": removed, "epoch": self._epoch[scope]}

    def write(self, scope: str, content: str, provenance: str,
              derived_from_epoch: int | None) -> None:
        current = self._epoch.get(scope, 0)
        trusted = provenance.startswith("self:") or provenance in {"auditor:verified"}
        if trusted and derived_from_epoch != current:
            # Trusted provenance at a stale (or undeclared) epoch is laundering.
            raise TombstoneLaunderingError(
                f"write to {scope!r} from {provenance!r} declares epoch "
                f"{derived_from_epoch!r} but scope is at epoch {current!r}; "
                f"content may re-assert tombstoned records — route to quarantine"
            )
        self._records.setdefault(scope, {})[content[:16]] = content


def rebuild_summary(store: MemoryStore, scope: str, context_epoch: int,
                    summary: str) -> None:
    # Agent summarizes from context it read at context_epoch, then writes back.
    store.write(scope, summary, provenance="self:agent-7",
                derived_from_epoch=context_epoch)


# Trace: read at epoch 0, forget bumps to 1, agent writes summary it built at epoch 0.
store = MemoryStore()
store.write("tenant:acme", "refund policy: 30 days", "scrape:web", derived_from_epoch=0)
receipt = store.forget("tenant:acme")          # epoch -> 1, 1 record removed
try:
    rebuild_summary(store, "tenant:acme", context_epoch=0,
                    summary="refund policy: 30 days (per acme docs)")
except TombstoneLaunderingError as e:
    # Correct outcome: the ghost is caught at the tool boundary, not by the auditor's judgment.
    print(f"blocked: {e}")
