# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-aaif-policy-enforcement-points-from-authorizing-actions-against-a-stale-capability-snapshot-after-mid-task-scope-revocation.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-aaif-policy-enforcement-points-from-authorizing-actions-against-a-stale-capability-snapshot-after-mid-task-scope-revocation.warrant.json
# Markdown SHA-256:   47b303e9f2ca51fa1ba9dbaafdffc2000d2a9170e5f6e42b1569be239d6f42c1
# Code AST SHA-256:   76306069466edde73dfd6e1fabd7e17035c57c4db1f185bff0d2db7c3c43bf73
# Verified Classes:   StaleCapabilitySnapshotError, CapabilitySnapshot, PolicyEnforcementPoint
# Defensive Guards:   2 fail-closed invariants (zero external deps)

class StaleCapabilitySnapshotError(RuntimeError):
    """Raised when a PEP admits a snapshot concurrent with a pinned revocation."""
    pass


class CapabilitySnapshot:
    def __init__(self, version: int, causal_frontier: frozenset, grants: dict):
        self.version = version
        self.causal_frontier = causal_frontier  # set of authority event IDs observed
        self.grants = grants


class PolicyEnforcementPoint:
    def __init__(self):
        self.pinned_revocations = set()
        self.cached = None

    def observe_revocation(self, rev_id: str):
        self.pinned_revocations.add(rev_id)

    def admit(self, snapshot: CapabilitySnapshot, action_class: str) -> bool:
        if action_class == "compensable":
            if self.cached is None or snapshot.version >= self.cached.version:
                self.cached = snapshot
                return True
            return False

        # non-compensable: causal dominance required
        if not self.pinned_revocations.issubset(snapshot.causal_frontier):
            missing = self.pinned_revocations - snapshot.causal_frontier
            raise StaleCapabilitySnapshotError(
                f"snapshot v{snapshot.version} does not dominate pinned "
                f"revocations {sorted(missing)}; refusing non-compensable action"
            )
        if self.cached is not None and not (
            self.cached.causal_frontier.issubset(snapshot.causal_frontier)
        ):
            raise StaleCapabilitySnapshotError(
                f"snapshot v{snapshot.version} is causally concurrent with "
                f"cached frontier; refusing non-compensable action"
            )
        self.cached = snapshot
        return True
