# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-cryptographically-valid-aaif-attestation-as-revoked-when-the-revocation-proof-has-not-yet-propagated-ac.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-cryptographically-valid-aaif-attestation-as-revoked-when-the-revocation-proof-has-not-yet-propagated-ac.warrant.json
# Markdown SHA-256:   41885b5fd70dab73a9a18a6e36e43feb15227e929866c608fc107744e8aeef47
# Code AST SHA-256:   612f397cf9ec0d7c7f596de90421d3a2c38b1e4cf499ec24a414ff7af65302b8
# Verified Classes:   RevocationFrontierLag, Frontier, Obligation
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class RevocationFrontierLag(RuntimeError):
    """Raised when an attestation chain's revocation provenance cannot be
    established at or beyond the required epoch for some issuer in the chain."""


@dataclass(frozen=True)
class Frontier:
    issuer: str
    epoch: int


@dataclass(frozen=True)
class Obligation:
    """A revocation obligation co-propagated in an attenuated attestation."""
    issuer: str
    required_epoch: int
    delegation_verifiable: bool  # can this instance verify this issuer's RL?


def admit_attestation(
    local_frontiers: dict[str, int],
    obligations: list[Obligation],
    max_staleness_epochs: int,
    current_epoch: int,
) -> bool:
    """Admission gate. Returns True only if every revocation obligation is
    satisfied by a locally verifiable frontier at or beyond the required epoch."""
    for ob in obligations:
        if not ob.delegation_verifiable:
            # signature-valid chain with unverifiable revocation provenance
            raise RevocationFrontierLag(
                f"issuer {ob.issuer} not in trust anchors; "
                f"no delegation proof to a verifiable revocation-checker"
            )

        local = local_frontiers.get(ob.issuer)
        if local is None:
            raise RevocationFrontierLag(
                f"no local frontier for required issuer {ob.issuer} "
                f"(required epoch {ob.required_epoch})"
            )

        if local < ob.required_epoch:
            lag = ob.required_epoch - local
            if lag > max_staleness_epochs:
                raise RevocationFrontierLag(
                    f"frontier for {ob.issuer} is {lag} epochs behind required "
                    f"{ob.required_epoch} (max_staleness {max_staleness_epochs}); "
                    f"failing closed"
                )
            # within bound: caller must HOLD, not admit and not hard-reject
            raise RevocationFrontierLag(
                f"frontier for {ob.issuer} lags required epoch by {lag}; "
                f"bounded hold required before admission"
            )

    return True
