# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-presents-an-aaif-capability-credential-minted-under-a-delegator-s-attenuated-scope-but-re-signs-its-own-sub-delegation-w.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-presents-an-aaif-capability-credential-minted-under-a-delegator-s-attenuated-scope-but-re-signs-its-own-sub-delegation-w.warrant.json
# Markdown SHA-256:   f2709dfada9346af7a11a53cad25708b8669f558e32b42f97c1c240fdee2adc8
# Code AST SHA-256:   d326b1b93e4a1595bc42b12fbd97c810bbaa35a3bce4eb985486e47f4f923f25
# Verified Classes:   WarrantChainTemporalError, Warrant
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class WarrantChainTemporalError(RuntimeError):
    """Raised when a sub-warrant's issuance instant precedes its parent's,
    or when the observed clock skew exceeds the minimum delegation latency."""
    pass


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    issued_at: float      # epoch seconds, per-instance clock
    not_after: float
    scope: frozenset
    parent_id: str | None


def mint_subwarrant(
    parent: Warrant,
    child_scope: frozenset,
    local_now: float,
    min_delegation_latency: float,
) -> Warrant:
    skew = parent.issued_at - local_now  # positive => local clock is behind

    if skew >= min_delegation_latency:
        # Clocks genuinely disagree about ordering; do not fabricate a clamp.
        raise WarrantChainTemporalError(
            f"clock skew {skew:.3f}s >= min delegation latency "
            f"{min_delegation_latency:.3f}s for parent {parent.warrant_id}; "
            f"refusing to mint temporally inverted sub-warrant"
        )

    # skew < latency: drift is noise, clamp to parent issuance to preserve monotonicity.
    issued_at = max(local_now, parent.issued_at)
    if not child_scope.issubset(parent.scope):
        raise WarrantChainTemporalError(
            f"scope {child_scope} not attenuated under parent scope {parent.scope}"
        )

    child = Warrant(
        warrant_id=f"{parent.warrant_id}.sub",
        issued_at=issued_at,
        not_after=parent.not_after,
        scope=child_scope,
        parent_id=parent.warrant_id,
    )
    assert child.issued_at >= parent.issued_at, "monotonicity invariant broken"
    return child


def verify_chain(child: Warrant, parent: Warrant, now: float) -> None:
    if child.issued_at < parent.issued_at:
        raise WarrantChainTemporalError(
            f"child {child.warrant_id} issued_at {child.issued_at} precedes "
            f"parent {parent.warrant_id} issued_at {parent.issued_at}"
        )
    if child.not_after > parent.not_after:
        raise WarrantChainTemporalError(
            f"child {child.warrant_id} outlives parent envelope"
        )
    if not (parent.issued_at <= now <= child.not_after):
        raise WarrantChainTemporalError(
            f"warrant {child.warrant_id} outside validity window at {now}"
        )
