# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-federated-peer-s-attestation-chain-as-expired-rather-than-merely-unverifiable-and-how-does-clock-skew-a.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-federated-peer-s-attestation-chain-as-expired-rather-than-merely-unverifiable-and-how-does-clock-skew-a.warrant.json
# Markdown SHA-256:   14429b0786b1551c7609e4ce70e52690f9a38a972362b14facf142ff80c3c1f7
# Code AST SHA-256:   bfc97a5d7eb96880c45033e83beb0830242125f3ccf038d83f33d6f70cfa0385
# Verified Classes:   FederationFreshnessError, Attestation, FederationLink
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class FederationFreshnessError(RuntimeError):
    """Raised when a verifier cannot soundly decide freshness of a federated attestation."""
    pass


@dataclass
class Attestation:
    issuer: str
    issued_at: float
    not_after: float
    signature_valid: bool


@dataclass
class FederationLink:
    peer: str
    max_skew: float          # declared at handshake, e.g. 60s
    validity_window: float   # e.g. 300s
    refresh_interval: float  # e.g. 240s


def validate_link_config(link: FederationLink) -> None:
    # Soundness precondition: the indeterminate band must never be entered
    # during normal operation. If refresh + skew >= window, the link is
    # unsound at handshake time, before any agent runs.
    if link.refresh_interval + link.max_skew >= link.validity_window:
        raise FederationFreshnessError(
            f"unsound federation link {link.peer}: "
            f"refresh({link.refresh_interval}) + skew({link.max_skew}) "
            f">= window({link.validity_window}); indeterminate band is steady-state"
        )


def verify_freshness(
    att: Attestation,
    link: FederationLink,
    verifier_clock: float,
    clock_probe=None,
) -> str:
    if not att.signature_valid:
        raise FederationFreshnessError(
            f"attestation from {att.issuer} is UNVERIFIABLE (trust failure, not freshness)"
        )

    remaining = att.not_after - verifier_clock

    if remaining > link.max_skew:
        return "ADMIT"                      # definitely live
    if remaining < -link.max_skew:
        return "REJECT_EXPIRED"             # definitely stale

    # Indeterminate band: verifier cannot soundly decide using its own clock.
    # Must not be classified as a trust failure. Requires a signed clock probe.
    if clock_probe is None:
        raise FederationFreshnessError(
            f"attestation from {att.issuer} INDETERMINATE: remaining={remaining:.1f}s "
            f"within skew bound ±{link.max_skew}s; refusing to misclassify as trust failure"
        )

    issuer_now = clock_probe(att.issuer)    # signed round-trip, returns issuer time
    observed_skew = abs(issuer_now - verifier_clock)
    if observed_skew > link.max_skew:
        raise FederationFreshnessError(
            f"observed skew {observed_skew:.1f}s exceeds declared bound "
            f"{link.max_skew}s for {att.issuer}; link must be renegotiated"
        )

    # With measured skew, re-decide against the issuer's timeline.
    issuer_remaining = att.not_after - issuer_now
    if issuer_remaining <= 0:
        return "REJECT_EXPIRED"
    return "ADMIT"


# --- observed incident replay ---
link = FederationLink(peer="alpha", max_skew=60.0, validity_window=300.0, refresh_interval=240.0)
validate_link_config(link)  # raises: 240 + 60 >= 300, link unsound at handshake
