# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-treat-a-delegated-aaif-credential-chain-as-unverifiable-rather-than-merely-expired-the-revocation-propagation-gap-across-scoped.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-treat-a-delegated-aaif-credential-chain-as-unverifiable-rather-than-merely-expired-the-revocation-propagation-gap-across-scoped.warrant.json
# Markdown SHA-256:   f1eb7c3b95ace60ab203c7ee8710270d7e81ca5a72fdec955d479d4267ed3451
# Code AST SHA-256:   541b60189c7e3c5b7a0450810f965f729e73022ad86805b300640d045f43fe4f
# Verified Classes:   UnverifiableChain, Hop, Chain
# Defensive Guards:   3 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class UnverifiableChain(RuntimeError):
    """Raised when a delegated AAIF chain cannot be proven current at the leaf."""
    pass


@dataclass
class Hop:
    issuer: str
    epoch: int
    not_after: float


@dataclass
class Chain:
    hops: list[Hop]
    max_attestation_age: float  # seconds; declared by the root issuer


def verify_chain(chain: Chain, cached_epochs: dict[str, int],
                 last_sync: dict[str, float], now: float | None = None) -> bool:
    now = time.time() if now is None else now
    for hop in chain.hops:
        if hop.not_after <= now:
            return False  # expired: clean deny
        if hop.issuer not in cached_epochs:
            raise UnverifiableChain(
                f"no revocation view for issuer {hop.issuer}; cannot bound staleness"
            )
        age = now - last_sync.get(hop.issuer, 0.0)
        if age > chain.max_attestation_age:
            # The epoch-equality check below would compare against a stale
            # reference and silently pass. Refuse instead.
            raise UnverifiableChain(
                f"issuer {hop.issuer} sync age {age:.1f}s exceeds "
                f"max_attestation_age {chain.max_attestation_age:.1f}s; "
                f"epoch equality is unsound"
            )
        if hop.epoch < cached_epochs[hop.issuer]:
            return False  # provably revoked: verifier has seen newer epoch
        if hop.epoch > cached_epochs[hop.issuer]:
            raise UnverifiableChain(
                f"hop epoch {hop.epoch} ahead of cached {cached_epochs[hop.issuer]} "
                f"for {hop.issuer}; chain references a revocation view we lack"
            )
    return True
