# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-should-an-instance-detect-and-contain-asymmetric-attestation-window-skew-during-cross-org-federation-negotiation.md
# Remnant Warrant:    https://corpusfetch.com/c/how-should-an-instance-detect-and-contain-asymmetric-attestation-window-skew-during-cross-org-federation-negotiation.warrant.json
# Markdown SHA-256:   25d17a60ed6e747e628701e3d44cd457a71eeb5189ae620b79697b5bd5dd31f6
# Code AST SHA-256:   4efe686e76974ab5d9b9b53ecfff4e6ab4acb79303489b25846494f4899a5564
# Verified Classes:   AttestationWindowError
# Defensive Guards:   3 fail-closed invariants (zero external deps)

class AttestationWindowError(RuntimeError):
    """Raised when an attestation's usable window is smaller than the
    verifier's required safety margin, or when a peer rejection cannot be
    classified as transient with confidence."""
    pass


def evaluate_attestation(
    iat_issuer: float,
    ttl_seconds: float,
    now_verifier: float,
    max_skew_seconds: float,
    rtt_to_issuer_seconds: float,
) -> float:
    """
    Return the usable remaining life in seconds for an attestation, or raise
    AttestationWindowError if the window is too small to act on safely.

    The usable window is the minimum of:
      - the issuer's remaining life, as estimated from the verifier's clock
        plus the worst-case skew (issuer may be ahead of us by max_skew)
      - the verifier's remaining life, as estimated from its own clock
    minus the round-trip time to the issuer, since any call dispatched now
    lands at the issuer after at least one RTT.
    """
    skew = abs(now_verifier - iat_issuer)
    if skew > max_skew_seconds:
        raise AttestationWindowError(
            f"clock skew {skew:.1f}s exceeds max {max_skew_seconds:.1f}s; "
            f"attestation window is unreliable"
        )

    issuer_remaining = (iat_issuer + ttl_seconds) - now_verifier - max_skew_seconds
    verifier_remaining = (iat_issuer + ttl_seconds) - now_verifier
    usable = min(issuer_remaining, verifier_remaining) - rtt_to_issuer_seconds

    if usable <= 0:
        raise AttestationWindowError(
            f"usable window {usable:.1f}s is non-positive after subtracting "
            f"skew {max_skew_seconds:.1f}s and rtt {rtt_to_issuer_seconds:.1f}s"
        )

    # Require at least 2x RTT of margin so a single retry is still safe.
    if usable < 2 * rtt_to_issuer_seconds:
        raise AttestationWindowError(
            f"usable window {usable:.1f}s is less than 2x rtt "
            f"{rtt_to_issuer_seconds:.1f}s; refresh before dispatch"
        )

    return usable


def classify_peer_rejection(error_class: str, retry_after: float | None) -> str:
    """
    Classify a rejection from the issuing peer. Default is TERMINAL.
    Only explicit, well-known transient classes may be retried.
    """
    transient_classes = {"RATE_LIMITED", "SERVICE_UNAVAILABLE"}
    if error_class in transient_classes and retry_after is not None:
        return "TRANSIENT"
    if error_class in {"AUTHORIZATION_EXPIRED", "ATTESTATION_REVOKED"}:
        return "TERMINAL"
    # Unknown class: fail closed. A terminal misclassification costs one task;
    # a transient misclassification costs a retry storm against a peer that
    # has already decided to reject.
    return "TERMINAL"
