# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-delegated-credentials-whose-attestation-chain-outlives-the-verifier-s-clock-skew-budget.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-delegated-credentials-whose-attestation-chain-outlives-the-verifier-s-clock-skew-budget.warrant.json
# Markdown SHA-256:   117fd692383a1418230bf716aeb0508b87de7a52ce2aceb9a0d71276382983db
# Code AST SHA-256:   49a93b84d5041a5e143a99a15fb96b9e14b6c5ca5d71e03a17ac3002a781dd4a
# Verified Classes:   DelegationWindowUnsound, Hop
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class DelegationWindowUnsound(RuntimeError):
    """Raised when a delegated credential's validity window cannot survive
    cross-domain clock skew plus presentation latency."""
    pass


@dataclass
class Hop:
    domain: str
    nbf: float          # issuer-clock not-before
    exp: float          # issuer-clock expiry
    skew_to_verifier: float  # signed seconds: hop_clock - verifier_clock


def effective_window(hop: Hop, p99_present_latency: float) -> float:
    # Usable lifetime after accounting for skew on both ends and latency.
    return (hop.exp - hop.nbf) - abs(hop.skew_to_verifier) - p99_present_latency


def assert_chain_sound(
    hops: list[Hop],
    p99_present_latency: float,
    revocation_propagation_latency: float,
) -> None:
    for h in hops:
        usable = effective_window(h, p99_present_latency)
        if usable <= 0:
            raise DelegationWindowUnsound(
                f"hop {h.domain}: usable window {usable:.2f}s <= 0 "
                f"(raw {h.exp - h.nbf:.2f}s, skew {h.skew_to_verifier:.2f}s, "
                f"latency {p99_present_latency:.2f}s)"
            )

    span = max(h.exp for h in hops) - min(h.nbf for h in hops)
    if span > revocation_propagation_latency:
        raise DelegationWindowUnsound(
            f"chain span {span:.2f}s exceeds revocation propagation "
            f"{revocation_propagation_latency:.2f}s; revocation is advisory"
        )


def mint_delegation(
    hops: list[Hop],
    requested_ttl: float,
    p99_present_latency: float,
    revocation_propagation_latency: float,
) -> Hop:
    worst_skew = max(abs(h.skew_to_verifier) for h in hops) if hops else 0.0
    minimum_ttl = 2 * worst_skew + p99_present_latency
    if requested_ttl <= minimum_ttl:
        raise DelegationWindowUnsound(
            f"requested ttl {requested_ttl:.2f}s <= skew budget "
            f"{minimum_ttl:.2f}s; credential would be void by construction"
        )
    if requested_ttl > revocation_propagation_latency:
        raise DelegationWindowUnsound(
            f"requested ttl {requested_ttl:.2f}s exceeds revocation "
            f"propagation {revocation_propagation_latency:.2f}s"
        )
    now = time.time()
    return Hop(domain="issuer", nbf=now, exp=now + requested_ttl, skew_to_verifier=0.0)
