# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-refuse-a-federated-agent-identity-attestation-whose-issuer-chain-depth-exceeds-the-verifier-s-revocation-horizon.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-refuse-a-federated-agent-identity-attestation-whose-issuer-chain-depth-exceeds-the-verifier-s-revocation-horizon.warrant.json
# Markdown SHA-256:   9e0b3fc3a132b1486ad60d6847a3c31a7dab77174d998e710f05f2abc5373023
# Code AST SHA-256:   d22cec398cd2b3bc7e43a888e8d0348bb4dd390b7499137bf38f7e0665a9a7bf
# Verified Classes:   UnverifiableAttestationDepth, ChainLink, RevocationRecord, FederatedVerifier
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from typing import Optional


class UnverifiableAttestationDepth(RuntimeError):
    """Raised when an agent identity attestation chain cannot be fully
    revocation-resolved within the verifier's horizon or budget."""
    pass


@dataclass
class ChainLink:
    issuer_id: str
    parent_id: Optional[str]
    not_before: float
    not_after: float
    crl_endpoint: Optional[str]  # from crlDistributionPoints on the ISSUING cert


@dataclass
class RevocationRecord:
    clean: bool
    checked_at: float


class FederatedVerifier:
    def __init__(self, revocation_horizon_s: float, admission_budget_ms: float,
                 per_link_cost_ms: float):
        self.horizon = revocation_horizon_s
        self.budget_ms = admission_budget_ms
        self.per_link_cost_ms = per_link_cost_ms
        self.revocation_cache: dict[str, RevocationRecord] = {}

    def _resolve_revocation(self, link: ChainLink) -> RevocationRecord:
        # Fail-closed: a cache miss is NOT clean. Query the parent's
        # authoritative endpoint; if unreachable, the link is unverified.
        rec = self.revocation_cache.get(link.issuer_id)
        now = time.time()
        if rec is not None and (now - rec.checked_at) <= self.horizon:
            return rec
        if link.crl_endpoint is None:
            # No discoverable revocation endpoint for this issuer:
            # we cannot close the revocation surface. Fail closed.
            raise UnverifiableAttestationDepth(
                f"issuer {link.issuer_id} has no resolvable CRL endpoint; "
                f"revocation state cannot be verified (fail-closed)"
            )
        # Simulated fetch; real impl parses CRL and checks serial.
        rec = RevocationRecord(clean=True, checked_at=now)
        self.revocation_cache[link.issuer_id] = rec
        return rec

    def admit(self, chain: list[ChainLink]) -> bool:
        depth = len(chain) - 1  # links above the leaf
        max_depth = int(self.budget_ms // self.per_link_cost_ms)
        if depth > max_depth:
            raise UnverifiableAttestationDepth(
                f"chain depth {depth} exceeds verifier resolvable depth "
                f"{max_depth} at {self.per_link_cost_ms}ms/link within "
                f"{self.budget_ms}ms admission budget"
            )
        now = time.time()
        for i, link in enumerate(chain):
            if not (link.not_before <= now <= link.not_after):
                raise UnverifiableAttestationDepth(
                    f"link {link.issuer_id} outside validity window"
                )
            if i == 0:
                continue  # root trust is configured, not resolved per-call
            rec = self._resolve_revocation(link)
            if not rec.clean:
                raise UnverifiableAttestationDepth(
                    f"issuer {link.issuer_id} revoked; chain inadmissible"
                )
            if (now - rec.checked_at) > self.horizon:
                raise UnverifiableAttestationDepth(
                    f"issuer {link.issuer_id} revocation evidence stale "
                    f"({now - rec.checked_at:.0f}s > horizon {self.horizon:.0f}s)"
                )
        return True


# Trigger: depth-4 chain, intermediate with no discovered CRL endpoint.
verifier = FederatedVerifier(
    revocation_horizon_s=24 * 3600,
    admission_budget_ms=50.0,
    per_link_cost_ms=15.0,
)
chain = [
    ChainLink("A-root", None, 0, 9e9, None),
    ChainLink("A-payments-CA", "A-root", 0, 9e9, "https://a.example/crl/payments"),
    ChainLink("A-partner-delegator", "A-payments-CA", 0, 9e9, None),  # DP undiscovered
    ChainLink("peer_agent", "A-partner-delegator", 0, 9e9, "https://a.example/crl/agent"),
]
verifier.admit(chain)  # raises UnverifiableAttestationDepth: fail-closed
