# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-agent-identity-attestation-whose-trust-anchor-chain-terminates-in-a-valid-but-stale-cross-or.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-federated-agent-identity-attestation-whose-trust-anchor-chain-terminates-in-a-valid-but-stale-cross-or.warrant.json
# Markdown SHA-256:   0fa4472bb6e12b5f382525806762c06737dc2c3b28a4f6c276ca0c1721a0f7d0
# Code AST SHA-256:   e8d708a6767621a23d5808af0842bd07f3709cdcfb2d529232364fc7ffe975ea
# Verified Classes:   StaleTrustAnchorError, AnchorCheck, FederatedAttestation
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleTrustAnchorError(RuntimeError):
    """Raised when a federated attestation's terminal trust anchor cannot be
    confirmed fresh enough to justify admission."""
    pass


@dataclass(frozen=True)
class AnchorCheck:
    root_id: str
    checked_at: float          # epoch seconds of last authoritative registry confirmation
    status: str                # "good" | "revoked" | "unknown"


@dataclass(frozen=True)
class FederatedAttestation:
    leaf_agent: str
    chain: tuple               # (root_id, intermediate_id, ..., leaf_id), root-first
    exp: float                 # attestation's own expiry, epoch seconds


def admit_federated_agent(
    attestation: FederatedAttestation,
    anchor_checks: dict,       # root_id -> AnchorCheck
    now: float,
    freshness_bound: float,    # verifier-declared, must be < issuer rotation latency
) -> bool:
    if now >= attestation.exp:
        raise StaleTrustAnchorError(
            f"attestation for {attestation.leaf_agent} already expired"
        )

    root_id = attestation.chain[0]
    check = anchor_checks.get(root_id)

    if check is None:
        raise StaleTrustAnchorError(
            f"no anchor check on record for root {root_id}; fail closed"
        )

    if check.status == "revoked":
        raise StaleTrustAnchorError(
            f"root {root_id} revoked; entire chain below it is invalid"
        )

    if check.status != "good":
        raise StaleTrustAnchorError(
            f"root {root_id} status '{check.status}' is not confirmed-good"
        )

    age = now - check.checked_at
    if age >= freshness_bound:
        # The root is unexpired and was once good, but the confirmation is
        # too old to bound the revocation blind window. Reject.
        raise StaleTrustAnchorError(
            f"root {root_id} last confirmed {age:.0f}s ago, "
            f"exceeds freshness_bound {freshness_bound:.0f}s"
        )

    # Chain-level check: any ancestor revoked/unknown invalidates the leaf,
    # even if the leaf itself is current.
    for node_id in attestation.chain[1:]:
        node_check = anchor_checks.get(node_id)
        if node_check is None or node_check.status != "good":
            raise StaleTrustAnchorError(
                f"ancestor {node_id} in chain to {attestation.leaf_agent} "
                f"is not confirmed-good"
            )

    return True
