# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-to-re-delegate-a-downstream-agent-s-attenuated-warrant-mid-chain.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-to-re-delegate-a-downstream-agent-s-attenuated-warrant-mid-chain.warrant.json
# Markdown SHA-256:   6562163e695a02d0f0bc52a1553230bd72d5a51a300cd096c7817b18540a7097
# Code AST SHA-256:   27130444ebc43fd13c2df1a11e7c94d0d1a6acb9737075bdb27b43cc9377cfc7
# Verified Classes:   DelegationWindowExhausted, Warrant
# Defensive Guards:   8 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class DelegationWindowExhausted(RuntimeError):
    """Raised when a derivative credential cannot be safely minted or accepted
    because the remaining parent authority window is smaller than the
    measurable clock uncertainty, or because absolute epochs cannot be compared."""
    pass


@dataclass(frozen=True)
class Warrant:
    subject: str
    scope: frozenset
    not_before: float      # absolute epoch, root-signed
    exp: float             # absolute epoch, root-signed
    parent_exp: float      # absolute epoch of immediate parent (signed)
    chain_min_exp: float   # running min over ancestors (signed)
    depth: int
    max_depth: int
    skew_bound: float      # declared epsilon for this issuer


def mint_child(parent: Warrant, subject: str, scope: frozenset,
               requested_ttl: float, now: float, skew_bound: float) -> Warrant:
    # 1. Scope attenuation must be a subset.
    if not scope.issubset(parent.scope):
        raise DelegationWindowExhausted(
            f"scope escalation: {scope - parent.scope} not in parent scope")

    # 2. Depth bound must be declared and respected.
    if parent.max_depth <= 0 or parent.depth + 1 > parent.max_depth:
        raise DelegationWindowExhausted(
            f"depth {parent.depth + 1} exceeds max {parent.max_depth}")

    # 3. Skew must be measurable; unknown skew forbids delegation.
    if skew_bound <= 0 or skew_bound != parent.skew_bound:
        raise DelegationWindowExhausted(
            "skew bound undeclared or mismatched; cannot safely attenuate")

    # 4. Worst-case absolute now, and the effective parent ceiling.
    now_worst = now + skew_bound
    parent_ceiling = min(parent.exp, parent.chain_min_exp)

    # 5. Remaining window must exceed the uncertainty margin.
    if parent_ceiling - now_worst <= 2 * skew_bound:
        raise DelegationWindowExhausted(
            f"remaining window {parent_ceiling - now_worst:.3f}s <= "
            f"2*skew {2 * skew_bound:.3f}s; treat parent as expired")

    # 6. Child expiry is the intersection, never the local re-anchor.
    child_exp = min(now_worst + requested_ttl, parent_ceiling - skew_bound)

    # 7. Absolute comparison, not duration comparison.
    if child_exp > parent_ceiling:
        raise DelegationWindowExhausted(
            "child expiry exceeds parent ceiling in absolute time")

    return Warrant(
        subject=subject,
        scope=scope,
        not_before=now_worst,
        exp=child_exp,
        parent_exp=parent.exp,
        chain_min_exp=parent_ceiling,
        depth=parent.depth + 1,
        max_depth=parent.max_depth,
        skew_bound=skew_bound,
    )


def verify_for_execution(w: Warrant, now: float, skew_bound: float) -> None:
    if skew_bound <= 0:
        raise DelegationWindowExhausted("verifier has no skew bound")
    now_worst = now + skew_bound
    ceiling = min(w.exp, w.chain_min_exp)
    if now_worst >= ceiling:
        raise DelegationWindowExhausted(
            f"authority window exhausted: now+eps={now_worst:.3f} >= "
            f"ceiling={ceiling:.3f}")
    if w.parent_exp < w.chain_min_exp:
        raise DelegationWindowExhausted(
            "chain_min_exp inconsistent with parent_exp; reject as malformed")
