# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-mint-a-did-delegation-warrant-when-the-parent-capability-s-caveat-set-is-only-partially-resolvable-at.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-mint-a-did-delegation-warrant-when-the-parent-capability-s-caveat-set-is-only-partially-resolvable-at.warrant.json
# Markdown SHA-256:   10e95db2fe775912889e08a7e64b15968e7ed272f6adef2a220f6fbdc82801e7
# Code AST SHA-256:   64f3f49ab08b8077a058d6b3acbd01c89abbdb2b7987a95c7e9f334a8bbe70a6
# Verified Classes:   OrphanedCaveatError, StaleResolutionError, Caveat, PeerCapabilities
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Callable, Optional


class OrphanedCaveatError(RuntimeError):
    """Raised when a child warrant would carry a caveat no reachable party can evaluate."""
    pass


class StaleResolutionError(RuntimeError):
    """Raised when a caveat's resolution witness exceeds its freshness TTL."""
    pass


@dataclass
class Caveat:
    name: str
    dialect: str
    resolve: Optional[Callable[[], bool]] = None  # None => issuer cannot evaluate
    freshness_ttl_s: int = 0
    resolved_at: Optional[float] = None


@dataclass
class PeerCapabilities:
    did: str
    supported_dialects: set[str] = field(default_factory=set)


def mint_child_warrant(
    parent_caveats: list[Caveat],
    child_authority_domain: set[str],
    reachable_peers: dict[str, PeerCapabilities],
    now: float,
) -> list[Caveat]:
    """
    Mint a child warrant iff every parent caveat is decidable-and-true by the issuer,
    bound to a reachable peer that advertises the caveat's dialect, or provably vacuous.
    Otherwise refuse: an unresolved caveat carried forward is a silent authority widening.
    """
    child: list[Caveat] = []
    for c in parent_caveats:
        # Case 3: provably vacuous under attenuation.
        if c.name not in child_authority_domain:
            continue

        # Case 1: issuer-decidable and true.
        if c.resolve is not None:
            if c.resolved_at is not None and (now - c.resolved_at) > c.freshness_ttl_s:
                raise StaleResolutionError(
                    f"caveat {c.name!r} resolution witness stale: "
                    f"age={now - c.resolved_at:.1f}s ttl={c.freshness_ttl_s}s"
                )
            if not c.resolve():
                raise OrphanedCaveatError(
                    f"caveat {c.name!r} resolved false at mint time; refuse or tighten"
                )
            child.append(c)
            continue

        # Case 2: bound to a reachable peer that advertises the dialect.
        bound = [
            p for p in reachable_peers.values()
            if c.dialect in p.supported_dialects
        ]
        if not bound:
            raise OrphanedCaveatError(
                f"caveat {c.name!r} (dialect={c.dialect!r}) is orphaned: "
                f"no reachable peer advertises the dialect and issuer cannot evaluate it"
            )
        child.append(c)

    return child
