# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-brokered-capability-warrant-whose-issuer-chain-crosses-an-administrative-trust-boundary-mid-task.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-a-brokered-capability-warrant-whose-issuer-chain-crosses-an-administrative-trust-boundary-mid-task.warrant.json
# Markdown SHA-256:   73f6e95cf0e871106ccea5ee84a037b13b1b46f844892a946d15c4056f9200df
# Code AST SHA-256:   fdb083c4526e8dc3f4ab19e12e544da6edf48e6190ae576215d38a44ddfd5873
# Verified Classes:   ChainBoundaryViolation, Warrant, TrustSnapshot
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class ChainBoundaryViolation(RuntimeError):
    """Raised when a brokered warrant's issuer chain crosses an administrative
    trust boundary not present in the task-admission trust snapshot."""


@dataclass(frozen=True)
class Warrant:
    chain: tuple            # leaf -> ... -> root, each node an issuer id
    leaf_scope: FrozenSet[str]
    issued_at: float


@dataclass
class TrustSnapshot:
    members: FrozenSet[str]
    revocation_namespaces: FrozenSet[str]   # namespaces whose re-root events we observe
    admitted_at: float
    rerooted_since_admission: set = field(default_factory=set)


# Receiver-side scope mapping: what a foreign scope attribute means *here*.
# Widening across a boundary is a violation, not a grant.
RECEIVER_SCOPE_MAP = {
    "records:read": {"records:read"},
    # a foreign issuer claiming only read must not be mapped to export locally
}


def effective_scope(leaf_scope: FrozenSet[str]) -> FrozenSet[str]:
    out: set = set()
    for s in leaf_scope:
        out |= RECEIVER_SCOPE_MAP.get(s, set())
    return frozenset(out)


def exercise_capability(warrant: Warrant, snap: TrustSnapshot, required: str) -> None:
    # 1. transitive chain walk: every node must be a frozen-snapshot member
    for node in warrant.chain:
        if node not in snap.members:
            raise ChainBoundaryViolation(
                f"issuer '{node}' in chain {warrant.chain} is not in admission "
                f"trust snapshot; mid-task re-root across administrative boundary"
            )

    # 2. revocation closure: any re-root touching a trusted node invalidates
    if snap.rerooted_since_admission & snap.members:
        raise ChainBoundaryViolation(
            f"trusted node(s) {snap.rerooted_since_admission & snap.members} "
            f"re-rooted after admission at {snap.admitted_at}; derived capability stale"
        )

    # 3. revocation coverage must span every intermediate in the presented chain
    for node in warrant.chain:
        if node not in snap.revocation_namespaces and node != warrant.chain[-1]:
            raise ChainBoundaryViolation(
                f"no revocation coverage for intermediate '{node}'; "
                f"treat as revoked and renegotiate"
            )

    # 4. receiver-applied scope mapping must satisfy the requirement without widening
    granted = effective_scope(warrant.leaf_scope)
    if required not in granted:
        raise ChainBoundaryViolation(
            f"receiver-mapped scope {granted} does not satisfy '{required}'"
        )
