# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-attenuate-an-aaif-delegation-warrant-when-a-sub-agent-s-declared-capability-set-is-a-strict-superset.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-attenuate-an-aaif-delegation-warrant-when-a-sub-agent-s-declared-capability-set-is-a-strict-superset.warrant.json
# Markdown SHA-256:   4e503118120adcaa09d23b95c39949790f1ec7594377627d17a13c62859e0a1f
# Code AST SHA-256:   79b96b5b4eeec8cc8e148616df22b2234a40dafcfc37e3f24cc19f99506f0be1
# Verified Classes:   DelegationChainUnresolvable, CapabilitySupersetViolation, Warrant
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import FrozenSet


class DelegationChainUnresolvable(RuntimeError):
    """Raised when a minter cannot prove a requested capability set is a
    subset of the parent's resolved grant chain."""


class CapabilitySupersetViolation(DelegationChainUnresolvable):
    """Raised when the requested set is not a subset of the resolved chain."""


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    issuer: str
    payload_caps: FrozenSet[str]
    parent_id: str | None
    chain_hash: str | None  # None => opaque, cannot resolve ancestors
    not_after: float


def resolve_chain(w: Warrant, registry: dict[str, Warrant]) -> FrozenSet[str]:
    """Recursively intersect payload_caps across the full ancestor chain."""
    resolved = set(w.payload_caps)
    cursor = w
    seen = set()
    while cursor.parent_id is not None:
        if cursor.parent_id in seen:
            raise DelegationChainUnresolvable(
                f"cycle detected at {cursor.parent_id}"
            )
        seen.add(cursor.parent_id)
        parent = registry.get(cursor.parent_id)
        if parent is None:
            raise DelegationChainUnresolvable(
                f"ancestor {cursor.parent_id} not in registry; cannot resolve"
            )
        if parent.chain_hash is None and parent.parent_id is not None:
            raise DelegationChainUnresolvable(
                f"ancestor {parent.warrant_id} is opaque; chain unprovable"
            )
        resolved &= parent.payload_caps
        cursor = parent
    return frozenset(resolved)


def mint_child(
    parent: Warrant,
    requested_caps: FrozenSet[str],
    registry: dict[str, Warrant],
    child_id: str,
    now: float,
) -> Warrant:
    if now >= parent.not_after:
        raise DelegationChainUnresolvable(
            f"parent {parent.warrant_id} expired at {parent.not_after}"
        )
    resolved = resolve_chain(parent, registry)
    if not requested_caps.issubset(resolved):
        over = requested_caps - resolved
        raise CapabilitySupersetViolation(
            f"requested {sorted(over)} not provable from resolved chain of "
            f"{parent.warrant_id}; resolved={sorted(resolved)}"
        )
    return Warrant(
        warrant_id=child_id,
        issuer=parent.warrant_id,
        payload_caps=requested_caps,
        parent_id=parent.warrant_id,
        chain_hash=parent.chain_hash,
        not_after=min(parent.not_after, now + 3600.0),
    )
