# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-delegated-authorization-chain-has-silently-widened-the-depth-truncation-invariant-in-aaif-verifiable-credential-att.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-delegated-authorization-chain-has-silently-widened-the-depth-truncation-invariant-in-aaif-verifiable-credential-att.warrant.json
# Markdown SHA-256:   86a8f47c47e54b9dc47518c8d36b7b622b902f900d2527432fc2ad741abd326c
# Code AST SHA-256:   cfcff3bc17d1711e4d5a339a08c80979de76eff73f3c2e2ac8d27b38ae236552
# Verified Classes:   DelegationWideningError, Hop
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import FrozenSet


class DelegationWideningError(RuntimeError):
    """Raised when a delegated VC chain's leaf scope is not a subset of the
    intersection of all ancestor scopes. Signature validity is irrelevant to
    this check; the chain may be cryptographically pristine and still widen."""


@dataclass(frozen=True)
class Hop:
    issuer: str
    tools: FrozenSet[str]
    max_spend: int
    constraints_present: bool = True


def verify_chain(chain: list[Hop]) -> FrozenSet[str]:
    if not chain:
        raise DelegationWideningError("empty chain: no authority to verify")

    # Absent constraints fail closed: contribute the empty set, not 'inherit'.
    effective_tools: FrozenSet[str] | None = None
    effective_spend: int | None = None

    for hop in chain:
        if not hop.constraints_present:
            raise DelegationWideningError(
                f"hop issued by {hop.issuer!r} omits constraints; "
                f"absent constraints must fail closed, not default to inherit"
            )
        effective_tools = hop.tools if effective_tools is None else effective_tools & hop.tools
        effective_spend = hop.max_spend if effective_spend is None else min(effective_spend, hop.max_spend)

    leaf = chain[-1]
    if not leaf.tools <= effective_tools:
        widened = leaf.tools - effective_tools
        raise DelegationWideningError(
            f"leaf {leaf.issuer!r} claims tools {sorted(widened)} not granted by "
            f"ancestor intersection {sorted(effective_tools)}; chain widened silently"
        )
    if leaf.max_spend > effective_spend:
        raise DelegationWideningError(
            f"leaf {leaf.issuer!r} claims max_spend {leaf.max_spend} > "
            f"effective {effective_spend}; chain widened silently"
        )

    return effective_tools


def mint_downstream(parent: Hop, child_issuer: str, requested_tools: FrozenSet[str], requested_spend: int) -> Hop:
    # Correct minting reads from the received credential, not runtime state.
    granted_tools = requested_tools & parent.tools
    if granted_tools != requested_tools:
        raise DelegationWideningError(
            f"child {child_issuer!r} requested {sorted(requested_tools - parent.tools)} "
            f"outside parent {parent.issuer!r} scope; refusing to mint widened credential"
        )
    return Hop(
        issuer=child_issuer,
        tools=granted_tools,
        max_spend=min(requested_spend, parent.max_spend),
        constraints_present=True,
    )
