# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-warrant-whose-policy-scope-it-cannot-re-derive.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-accept-a-federated-warrant-whose-policy-scope-it-cannot-re-derive.warrant.json
# Markdown SHA-256:   64e6af645d49865ff8ba5b821ce70815206ac616065dfc106d94a9ad0a12f29c
# Code AST SHA-256:   d17314019a371b097a5bf30fb8c6ea3424e5496aba3f4243623dd276ad9181b9
# Verified Classes:   FederatedScopeError, Warrant, LocalPolicy
# Defensive Guards:   7 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional
import hashlib


class FederatedScopeError(RuntimeError):
    """Raised when an ingress agent cannot establish local admissibility of a federated warrant."""
    pass


@dataclass
class Warrant:
    issuer: str
    scope_referent: str          # WHICH policy set this scope claim is about
    scope_hash: str              # hash of the claimed scope set
    issued_at: int               # monotonic epoch
    scope_verified: bool         # honest, but referent-relative


@dataclass
class LocalPolicy:
    org_id: str
    snapshot_epoch: int
    last_revocation_epoch: int
    intersection_hash_with: dict  # peer_org -> hash of S_local ∩ S_peer


def admit(warrant: Warrant, local: LocalPolicy, peer_org: str) -> None:
    # A boolean without a referent is uninterpretable: treat as absent.
    if warrant.scope_referent is None:
        raise FederatedScopeError(
            f"warrant from {warrant.issuer} has no scope_referent; "
            f"scope_verified={warrant.scope_verified} cannot be trusted"
        )

    # Case 1: scope claim is about the ORIGIN's policy set -> must re-derive.
    if warrant.scope_referent == f"policy:{warrant.issuer}":
        if local.snapshot_epoch < warrant.issued_at:
            raise FederatedScopeError(
                f"stale snapshot: local={local.snapshot_epoch} < warrant={warrant.issued_at}"
            )
        if local.last_revocation_epoch > local.snapshot_epoch:
            # Revocation advanced since snapshot: refuse rather than admit.
            raise FederatedScopeError(
                f"revocation epoch {local.last_revocation_epoch} advanced past "
                f"snapshot {local.snapshot_epoch}; re-derivation required"
            )
        # Caller must run local scope check here; we cannot skip it.
        raise FederatedScopeError(
            "origin-scoped warrant requires local re-derivation; "
            "signature validity is not admissibility"
        )

    # Case 2: scope claim is about a MUTUAL intersection set -> admit only on hash match.
    if warrant.scope_referent == "policy:intersection":
        expected = local.intersection_hash_with.get(peer_org)
        if expected is None:
            raise FederatedScopeError(
                f"no intersection hash recorded for peer {peer_org}"
            )
        if expected != warrant.scope_hash:
            raise FederatedScopeError(
                f"intersection hash mismatch for {peer_org}: "
                f"local={expected} warrant={warrant.scope_hash} (policy divergence)"
            )
        return  # admissible: intersection verified against local store

    raise FederatedScopeError(
        f"unknown scope_referent {warrant.scope_referent!r}"
    )
