# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/which-invariants-prevent-a-revoked-aaif-delegation-root-from-remaining-live-in-an-offline-verified-agent-chain.md
# Remnant Warrant:    https://corpusfetch.com/c/which-invariants-prevent-a-revoked-aaif-delegation-root-from-remaining-live-in-an-offline-verified-agent-chain.warrant.json
# Markdown SHA-256:   420210be7ba881b9f564711148c1d30074e2913c0d098daf74beed142dcdd1a5
# Code AST SHA-256:   e3beaeafad9a0a217e7fad0646c01b01a9c32ce10475a44d1ba193345274d304
# Verified Classes:   AAIFChainVerificationError, Warrant, RevocationSnapshot
# Defensive Guards:   9 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Sequence


class AAIFChainVerificationError(RuntimeError):
    """Raised when an AAIF delegation chain cannot be verified to completion."""


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    issuer: str
    holder: str
    scope: frozenset
    expires_at: float
    parent_revocation_epoch: int
    signature_valid: bool


@dataclass(frozen=True)
class RevocationSnapshot:
    issuer: str
    this_update: float
    next_update: float
    epoch: int
    revoked: frozenset


def verify_chain(
    chain: Sequence[Warrant],
    snapshot: RevocationSnapshot,
    now: float,
) -> None:
    if not chain:
        raise AAIFChainVerificationError("empty chain")
    if not (snapshot.this_update <= now <= snapshot.next_update):
        raise AAIFChainVerificationError(
            f"snapshot stale: now={now} outside "
            f"[{snapshot.this_update}, {snapshot.next_update}]"
        )

    horizon = max(w.expires_at for w in chain)
    if snapshot.next_update < horizon:
        # Indeterminate: cannot prove non-revocation across full chain lifetime.
        raise AAIFChainVerificationError(
            f"snapshot horizon {snapshot.next_update} < chain horizon {horizon}; "
            f"downgrade to re-verification lease, do not grant"
        )

    root = chain[0]
    if root.warrant_id in snapshot.revoked:
        raise AAIFChainVerificationError(
            f"root warrant {root.warrant_id} revoked at epoch {snapshot.epoch}"
        )

    for i, w in enumerate(chain):
        if not w.signature_valid:
            raise AAIFChainVerificationError(f"bad signature at hop {i}: {w.warrant_id}")
        if w.expires_at < now:
            raise AAIFChainVerificationError(f"expired at hop {i}: {w.warrant_id}")
        if i > 0:
            parent = chain[i - 1]
            if not w.scope <= parent.scope:
                raise AAIFChainVerificationError(f"scope escalation at hop {i}")
            if w.expires_at > parent.expires_at:
                raise AAIFChainVerificationError(f"ttl escalation at hop {i}")
            if w.parent_revocation_epoch != snapshot.epoch:
                raise AAIFChainVerificationError(
                    f"epoch skew at hop {i}: child saw {w.parent_revocation_epoch}, "
                    f"issuer epoch is {snapshot.epoch}; chain anchored to stale ancestry"
                )
