# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-did-based-agent-passport-whose-trust-anchor-is-valid-but-whose-delegation-chain-crosses-an-organizational.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-did-based-agent-passport-whose-trust-anchor-is-valid-but-whose-delegation-chain-crosses-an-organizational.warrant.json
# Markdown SHA-256:   bffc29ae3ab988f3cd20ca245f7625c8613883c8c64c4316c8474fd8f39704e5
# Code AST SHA-256:   58f2b51b87a796367345e76fdb9cd88547322323ea639e70dcabe3992a8dcd7b
# Verified Classes:   FederationTrustHorizonError, Hop, Passport
# Defensive Guards:   8 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Sequence


class FederationTrustHorizonError(RuntimeError):
    """Raised when a cross-org agent passport cannot be soundly accepted
    because the relying party cannot bound revocation propagation across
    every domain in the delegation chain."""


@dataclass(frozen=True)
class Hop:
    domain: str
    declared_revocation_latency_s: int
    status_next_update_epoch: int
    revoked: bool


@dataclass(frozen=True)
class Passport:
    anchor_did: str
    chain: Sequence[Hop]
    valid_until_epoch: int


def admit_passport(
    passport: Passport,
    *,
    now_epoch: int,
    relying_party_max_staleness_s: int,
    allowlisted_domains: frozenset[str],
    max_delegation_depth: int,
    anchor_doc_cache_age_s: int,
    anchor_doc_rotation_bound_s: int,
) -> None:
    if not passport.chain:
        raise FederationTrustHorizonError("empty delegation chain")

    if len(passport.chain) > max_delegation_depth:
        raise FederationTrustHorizonError(
            f"chain depth {len(passport.chain)} exceeds max {max_delegation_depth}; "
            "revocation horizon unprovable across hops"
        )

    if passport.valid_until_epoch <= now_epoch:
        raise FederationTrustHorizonError("passport expired")

    if anchor_doc_cache_age_s > anchor_doc_rotation_bound_s:
        raise FederationTrustHorizonError(
            "anchor DID document served from stale cache; rotation bound exceeded"
        )

    worst_latency = 0
    for hop in passport.chain:
        if hop.domain not in allowlisted_domains:
            raise FederationTrustHorizonError(
                f"domain {hop.domain!r} not in federation allowlist with bounded latency"
            )
        if hop.revoked:
            raise FederationTrustHorizonError(f"hop {hop.domain!r} is revoked")
        if hop.status_next_update_epoch < now_epoch:
            raise FederationTrustHorizonError(
                f"hop {hop.domain!r} status list stale (nextUpdate passed); "
                "stale status is indistinguishable from forged"
            )
        worst_latency = max(worst_latency, hop.declared_revocation_latency_s)

    if relying_party_max_staleness_s < worst_latency:
        raise FederationTrustHorizonError(
            f"cannot dominate chain revocation horizon: "
            f"max_staleness={relying_party_max_staleness_s}s < "
            f"worst_hop_latency={worst_latency}s"
        )
