# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revoked-aaif-capability-warrant-as-still-live-during-a-cross-agent-handoff-and-how-does-attestation-lag-pro.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-revoked-aaif-capability-warrant-as-still-live-during-a-cross-agent-handoff-and-how-does-attestation-lag-pro.warrant.json
# Markdown SHA-256:   0eed895f808b80b5ae7e7269498a7a43e367abcc205a79a93e6b6764044b3d6b
# Code AST SHA-256:   f9aa1ecd17dc522e182c300cad2eb4c29fc9032323b86571b2185aaacb16b55f
# Verified Classes:   StaleDelegationError, Warrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleDelegationError(RuntimeError):
    """Raised when a child warrant would outlive its parent's revocation freshness proof."""
    pass


@dataclass
class Warrant:
    subject: str
    capability: str
    expiry: float          # absolute unix time
    freshness_bound: float # latest time parent non-revocation was proven
    parent: "Warrant | None" = None


PROPAGATION_BOUND = 0.100  # seconds; max status-list propagation delay


def attenuate(parent: Warrant, child_subject: str, requested_expiry: float) -> Warrant:
    """Re-delegate a parent warrant to a child, enforcing the freshness invariant."""
    # Invariant: child must not outlive parent's freshness proof + propagation bound.
    max_safe_expiry = parent.freshness_bound + PROPAGATION_BOUND

    if requested_expiry > max_safe_expiry:
        raise StaleDelegationError(
            f"refusing to delegate '{parent.capability}' to {child_subject}: "
            f"requested expiry {requested_expiry:.3f} exceeds freshness bound "
            f"{max_safe_expiry:.3f} (parent checked at {parent.freshness_bound:.3f}, "
            f"propagation bound {PROPAGATION_BOUND:.3f}). "
            f"Perform a synchronous status check or shorten the child expiry."
        )

    # Also refuse if the parent's own freshness proof is already older than
    # the propagation bound at the moment of delegation.
    if time.time() - parent.freshness_bound > PROPAGATION_BOUND:
        raise StaleDelegationError(
            f"parent warrant freshness proof is stale by "
            f"{time.time() - parent.freshness_bound:.3f}s; re-validate before delegating"
        )

    return Warrant(
        subject=child_subject,
        capability=parent.capability,
        expiry=requested_expiry,
        freshness_bound=parent.freshness_bound,
        parent=parent,
    )


def verify_at_use(warrant: Warrant, now: float) -> None:
    """Verifier-side check: reject warrants whose authority may have outlived revocation."""
    if now > warrant.expiry:
        raise StaleDelegationError(f"warrant expired at {warrant.expiry:.3f}")
    if warrant.expiry > warrant.freshness_bound + PROPAGATION_BOUND:
        raise StaleDelegationError(
            f"warrant authority outlives freshness proof: expiry {warrant.expiry:.3f} "
            f"> freshness {warrant.freshness_bound:.3f} + bound {PROPAGATION_BOUND:.3f}"
        )
