# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-delegated-capability-chain-whose-attenuation-lattice-admits-a-widening-path-through-a-re-delegation-hop.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-delegated-capability-chain-whose-attenuation-lattice-admits-a-widening-path-through-a-re-delegation-hop.warrant.json
# Markdown SHA-256:   849c673d9e51e9d448d66154e0c6aeabe4903ee3cbadb9474f945b0ef51139d6
# Code AST SHA-256:   6330dbd8552dc6054cd273101a2a6c93159bec93910c6c1bb38ca4e00d8727fc
# Verified Classes:   DelegationWideningError, Warrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet

class DelegationWideningError(RuntimeError):
    """Raised when a composed capability chain exceeds the intersection of its roots' intents."""
    pass

@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    root_id: str
    scope: FrozenSet[str]
    may_delegate: bool
    delegation_scope: FrozenSet[str] = field(default_factory=frozenset)
    parent_id: str | None = None

def attenuate(parent: Warrant, child_scope: FrozenSet[str], child_delegation_scope: FrozenSet[str]) -> Warrant:
    if not child_scope <= parent.scope:
        raise DelegationWideningError(f"scope {child_scope} not subset of parent {parent.scope}")
    if child_delegation_scope - parent.delegation_scope:
        raise DelegationWideningError(
            f"delegation scope {child_delegation_scope} exceeds parent {parent.delegation_scope}"
        )
    return Warrant(
        warrant_id=f"{parent.warrant_id}+{len(child_scope)}",
        root_id=parent.root_id,
        scope=child_scope,
        may_delegate=bool(child_delegation_scope),
        delegation_scope=child_delegation_scope,
        parent_id=parent.warrant_id,
    )

def effective_authority(inbound: list[Warrant], roots: dict[str, Warrant]) -> FrozenSet[str]:
    """Effective authority = intersection over the narrowest root reachable by any chain."""
    if not inbound:
        return frozenset()
    # Bound by the intersection of every root that fed any inbound chain.
    root_scopes = [roots[w.root_id].scope for w in inbound]
    bound = frozenset.intersection(*root_scopes)
    union = frozenset().union(*(w.scope for w in inbound))
    if not union <= bound:
        # Union exceeds the root-intent bound: a widening hop laundered authority across chains.
        raise DelegationWideningError(
            f"composed authority {union} exceeds root-intent bound {bound}; "
            f"widening hop present in chains {[w.warrant_id for w in inbound]}"
        )
    return union

def exercise(agent_id: str, inbound: list[Warrant], roots: dict[str, Warrant], requested: FrozenSet[str]) -> None:
    authority = effective_authority(inbound, roots)
    if not requested <= authority:
        raise DelegationWideningError(
            f"agent {agent_id} requested {requested} outside effective authority {authority}"
        )
