# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-treat-a-scoped-forget-as-a-live-revocation-barrier-rather-than-a-lazy-tombstone.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-treat-a-scoped-forget-as-a-live-revocation-barrier-rather-than-a-lazy-tombstone.warrant.json
# Markdown SHA-256:   d1896a472bc36354de18823b35867defca3eadb6a0e62ea6cc549c71eb7db2b8
# Code AST SHA-256:   5e11f7941f24e1ea04bab3e72275126013f312ae7e0ddfb80a09e7259150faa4
# Verified Classes:   DeletionClass, DeletionBarrierError, ContextSource, MemoryStore
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from enum import Enum
from typing import Iterable


class DeletionClass(Enum):
    RETENTION = "retention"      # Class A: tombstone ok
    CONFIDENTIAL = "confidential"  # Class B: revocation required
    SAFETY = "safety"            # Class C: revocation + trace scan


class DeletionBarrierError(RuntimeError):
    """Raised when a forget call cannot be treated as an effective barrier
    for the current execution branch because a context source is stale."""


@dataclass
class ContextSource:
    name: str
    scope: str
    epoch: int          # deletion epoch observed when this source was assembled
    resumable: bool     # checkpoints and peer scopes are resumable


@dataclass
class MemoryStore:
    epoch: int = 0
    tombstones: dict[str, int] = field(default_factory=dict)

    def forget(self, scope: str) -> int:
        self.epoch += 1
        self.tombstones[scope] = self.epoch
        return self.epoch


def assert_barrier(
    store: MemoryStore,
    scope: str,
    deletion_class: DeletionClass,
    sources: Iterable[ContextSource],
) -> int:
    """Issue a forget and verify it is an effective barrier for this branch.

    For Class A, tombstone semantics are accepted and the new epoch is returned.
    For Class B/C, every source that references the scope must have been
    assembled at or after the post-forget epoch, or the branch is unsafe.
    """
    new_epoch = store.forget(scope)

    if deletion_class is DeletionClass.RETENTION:
        return new_epoch

    stale = [
        s for s in sources
        if s.scope == scope and s.epoch < new_epoch
    ]

    if stale:
        stale_names = ", ".join(f"{s.name}(epoch={s.epoch})" for s in stale)
        raise DeletionBarrierError(
            f"forget({scope!r}) under {deletion_class.value} is not an effective "
            f"barrier: {len(stale)} stale context source(s) predate epoch "
            f"{new_epoch}: {stale_names}. Invalidate these sources or restart "
            f"the branch from a post-forget checkpoint before continuing."
        )

    if deletion_class is DeletionClass.SAFETY:
        # Safety revocations additionally require that no resumable source
        # can rehydrate derived influence from the forgotten scope.
        resumable_stale = [s for s in sources if s.resumable and s.epoch < new_epoch]
        if resumable_stale:
            names = ", ".join(s.name for s in resumable_stale)
            raise DeletionBarrierError(
                f"SAFETY revocation of {scope!r} blocked: resumable source(s) "
                f"{names} may rehydrate derived influence. Discard and restart cold."
            )

    return new_epoch
