# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-re-validate-a-delegated-capability-token-at-the-trust-broker-boundary-and-what-breaks-when-it-does-not.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-re-validate-a-delegated-capability-token-at-the-trust-broker-boundary-and-what-breaks-when-it-does-not.warrant.json
# Markdown SHA-256:   db896309cd1f0bc239c596c231ebd590d4f3b630e6a7f5566e9b84e4df5e8485
# Code AST SHA-256:   be546fd2f5836df740ebf7511df9f427b22dbdf9b7ec46c88923581840ba3d5e
# Verified Classes:   DelegationChainUnverifiable, EffectiveScopeMissing, Link, LeafToken, TrustBrokerBoundary
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import hashlib
from dataclasses import dataclass
from typing import Optional


class DelegationChainUnverifiable(RuntimeError):
    """Raised when a link's signing epoch has no retained verification material."""


class EffectiveScopeMissing(RuntimeError):
    """Raised when the leaf token omits its signed effective-scope field."""


@dataclass(frozen=True)
class Link:
    epoch: int
    scope: frozenset
    signature_ok: bool


@dataclass(frozen=True)
class LeafToken:
    links: tuple
    signed_effective_scope: Optional[frozenset]
    delegation_depth: int


class TrustBrokerBoundary:
    def __init__(self, retained_epochs: set, max_depth: int):
        self.retained_epochs = retained_epochs
        self.max_depth = max_depth

    def admit(self, token: LeafToken) -> frozenset:
        # Fail-closed on any epoch whose verification material was retired.
        for link in token.links:
            if link.epoch not in self.retained_epochs:
                raise DelegationChainUnverifiable(
                    f"link signed under epoch {link.epoch} has no retained "
                    f"verification material; retained={sorted(self.retained_epochs)}"
                )
            if not link.signature_ok:
                raise DelegationChainUnverifiable(
                    f"link at epoch {link.epoch} failed signature check"
                )

        if token.delegation_depth > self.max_depth:
            raise DelegationChainUnverifiable(
                f"delegation_depth {token.delegation_depth} exceeds bound {self.max_depth}"
            )

        # Verifier must NEVER recompute intersection against live policy.
        if token.signed_effective_scope is None:
            raise EffectiveScopeMissing(
                "leaf token carries no signed effective_scope; refusing to "
                "recompute intersection at verification time"
            )

        # Signed effective scope must be a subset of every link's scope.
        for link in token.links:
            if not token.signed_effective_scope <= link.scope:
                raise DelegationChainUnverifiable(
                    f"signed effective_scope {set(token.signed_effective_scope)} "
                    f"is not a subset of link scope at epoch {link.epoch}"
                )

        return token.signed_effective_scope
