# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-stale-delegated-warrants-from-surviving-key-rotation-across-federated-trust-domains.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-stale-delegated-warrants-from-surviving-key-rotation-across-federated-trust-domains.warrant.json
# Markdown SHA-256:   5b4de7f329a95781d9415c7c61a884833ebc6f9e06a5e4466820912e613da2b6
# Code AST SHA-256:   d89a0cbd0902d8f5fb20c49697fee082b46a49af5e0c0097597608bb8bdd3875
# Verified Classes:   StaleWarrantError, EpochStatement, Warrant, FederatedVerifier
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleWarrantError(RuntimeError):
    """Raised when a delegated warrant's authority cannot be confirmed current."""


@dataclass(frozen=True)
class EpochStatement:
    domain: str
    key_id: str
    epoch: int
    revoked: bool
    observed_at: float  # monotonic seconds


@dataclass(frozen=True)
class Warrant:
    issuer_domain: str
    issuer_key_id: str
    issuer_epoch: int
    subject: str
    not_after: float


class FederatedVerifier:
    def __init__(self, freshness_bound_s: float = 5.0, max_depth: int = 4):
        self.freshness_bound_s = freshness_bound_s
        self.max_depth = max_depth

    def _resolve_current_epoch(self, domain: str, key_id: str) -> EpochStatement:
        # In production: fetch from authoritative revocation source, fail-closed on error.
        # Here we simulate a resolver that may return a stale cached statement.
        raise NotImplementedError("resolver injected by caller")

    def verify_chain(self, chain: list[Warrant], now: float) -> None:
        if len(chain) > self.max_depth:
            raise StaleWarrantError(
                f"delegation depth {len(chain)} exceeds cap {self.max_depth}"
            )
        for w in chain:
            if now >= w.not_after:
                raise StaleWarrantError(
                    f"warrant for {w.subject} expired at {w.not_after}"
                )
            stmt = self._resolve_current_epoch(w.issuer_domain, w.issuer_key_id)
            age = now - stmt.observed_at
            if age > self.freshness_bound_s:
                # Fail-closed: cannot confirm current epoch within freshness bound.
                raise StaleWarrantError(
                    f"epoch statement for {w.issuer_domain}/{w.issuer_key_id} "
                    f"is {age:.1f}s old, bound is {self.freshness_bound_s:.1f}s"
                )
            if stmt.revoked:
                raise StaleWarrantError(
                    f"issuer key {w.issuer_key_id} revoked in {w.issuer_domain}"
                )
            if stmt.epoch != w.issuer_epoch:
                raise StaleWarrantError(
                    f"warrant epoch {w.issuer_epoch} != current epoch "
                    f"{stmt.epoch} for {w.issuer_domain}/{w.issuer_key_id}"
                )
