# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-stop-trusting-a-peer-s-revocation-gossip-the-quorum-signed-revocation-horizon-in-cross-org-federated-agent-reputation.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-stop-trusting-a-peer-s-revocation-gossip-the-quorum-signed-revocation-horizon-in-cross-org-federated-agent-reputation.warrant.json
# Markdown SHA-256:   7ba5de5ad2fe811bfd1032e503570978dfa0b2231bff75364249aa5999c17c0a
# Code AST SHA-256:   c1adb4987c24353357e51f2d361cc0a7660402fdba582d654e033e7bc3558636
# Verified Classes:   RevocationHorizonError, FreshnessAssertion, VerifierState
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass, field
from typing import Dict


class RevocationHorizonError(RuntimeError):
    """Raised when an admission decision is attempted against a per-issuer
    revocation horizon that is absent or stale. Fail closed: the caller must
    escalate to a synchronous freshness query, never fall back to a scalar
    horizon."""


@dataclass
class FreshnessAssertion:
    issuer: str
    epoch: int          # issuer's stated revocation epoch at assertion time
    signed_at: float    # wall-clock seconds, issuer-signed


@dataclass
class VerifierState:
    per_issuer_horizon: Dict[str, int] = field(default_factory=dict)
    freshness: Dict[str, FreshnessAssertion] = field(default_factory=dict)
    staleness_bound: Dict[str, float] = field(default_factory=dict)


def admit_agent(
    state: VerifierState,
    agent_id: str,
    issuer: str,
    agent_revocation_epoch: int,
    now: float,
) -> bool:
    horizon = state.per_issuer_horizon.get(issuer)
    assertion = state.freshness.get(issuer)
    delta = state.staleness_bound.get(issuer)

    if horizon is None or assertion is None or delta is None:
        raise RevocationHorizonError(
            f"no per-issuer horizon/freshness for issuer={issuer!r}; "
            f"refusing to evaluate agent={agent_id!r} against a scalar horizon"
        )

    if now - assertion.signed_at > delta:
        raise RevocationHorizonError(
            f"stale freshness assertion for issuer={issuer!r} "
            f"(age={now - assertion.signed_at:.1f}s > delta={delta:.1f}s); "
            f"agent={agent_id!r} requires synchronous query"
        )

    # Sound rule: compare against the issuing authority's own horizon,
    # never against max(per_issuer_horizon.values()).
    if horizon < agent_revocation_epoch:
        return False
    return True
