# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-forget-tombstone-as-a-write-barrier-rather-than-a-deletion.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-forget-tombstone-as-a-write-barrier-rather-than-a-deletion.warrant.json
# Markdown SHA-256:   7d85eb8b0bd684834d1485de68f5599cbd9a4571fd444b17d5fd1aa9185900ca
# Code AST SHA-256:   2946e68a6c9df0dc17d4ef6e4794d71bfe02dd42df3acc5c69da5c1c402a699e
# Verified Classes:   ErasureBarrierViolation, MemoryStore
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class ErasureBarrierViolation(RuntimeError):
    """Raised when a write would reintroduce data for an erased subject."""
    pass


class MemoryStore:
    def __init__(self):
        self._rows = []
        self._tombstones = {}  # subject -> set of scopes blocked

    def forget(self, subject: str, scopes: set[str]) -> int:
        self._tombstones.setdefault(subject, set()).update(scopes)
        before = len(self._rows)
        self._rows = [r for r in self._rows
                      if not (r["subject"] == subject and r["scope"] in scopes)]
        return before - len(self._rows)

    def write(self, subject: str, scope: str, content: str) -> None:
        blocked = self._tombstones.get(subject, set())
        # Barrier must cover the target scope AND any derived/promotion scope.
        if scope in blocked or ("derived" in blocked and scope == "derived"):
            raise ErasureBarrierViolation(
                f"write to scope={scope!r} for erased subject={subject!r} "
                f"blocked by tombstone; scrub content or route to exempt namespace"
            )
        # Also block content-level reintroduction when identity is in the text.
        if subject in content and scope in blocked:
            raise ErasureBarrierViolation(
                f"content of write references erased subject={subject!r}"
            )
        self._rows.append({"subject": subject, "scope": scope, "content": content})


def distill_session_note(store: MemoryStore, subject: str, note: str) -> None:
    """Promotion path that must inherit the barrier, not bypass it."""
    try:
        store.write(subject=subject, scope="derived", content=note)
    except ErasureBarrierViolation as e:
        # Do not silently drop; surface so the pipeline halts for this subject.
        raise ErasureBarrierViolation(
            f"distillation blocked for {subject!r}; erasure invariant holds"
        ) from e


# Trace reproduction: erase, then attempt the t=3 reintroduction.
store = MemoryStore()
store.write("u_441", "user", "SSO timeout report")
store.forget("u_441", {"user", "session", "derived"})
distill_session_note(store, "u_441", "Customer u_441 (Acme Corp) SSO timeout")
# -> raises ErasureBarrierViolation: distillation blocked; erasure durable.
