# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-delegated-credential-chain-as-invalid-the-moment-a-mid-chain-revocation-propagates-out-of-order.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-delegated-credential-chain-as-invalid-the-moment-a-mid-chain-revocation-propagates-out-of-order.warrant.json
# Markdown SHA-256:   54ab7ec7b8f030e4f0f930322e67db504eabec3747f63aa73d86e78274c48e2c
# Code AST SHA-256:   031d63922cd4d96e4c56c5f7af4d274610778d3c731dd0e8f97e298893f5ac71
# Verified Classes:   ZombieAttestationError, Link, RevocationView
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class ZombieAttestationError(RuntimeError):
    """Raised when a delegated credential was minted at/after the verifier's
    revocation-view freshness horizon, making ancestor revocation status
    undecidable for this leaf."""
    pass


@dataclass(frozen=True)
class Link:
    subject: str
    t_redelegate: float   # epoch seconds, when this link was minted
    not_after: float


@dataclass(frozen=True)
class RevocationView:
    t_freshness: float    # epoch seconds: snapshot covers all revokes <= this
    revoked_subjects: frozenset


def verify_chain(
    chain: list[Link],
    view: RevocationView,
    now: float,
    delta_quarantine: float = 0.0,
) -> None:
    # 1. structural + expiry checks (signatures assumed verified upstream)
    for link in chain:
        if link.not_after < now:
            raise ZombieAttestationError(
                f"link {link.subject} expired at {link.not_after} < {now}"
            )

    # 2. any ancestor already known-revoked -> dead chain
    for link in chain:
        if link.subject in view.revoked_subjects:
            raise ZombieAttestationError(
                f"link {link.subject} is revoked in current view"
            )

    # 3. delegation-time fence: the latest mint event must be covered by
    #    the revocation snapshot, else ancestor revocations are undecidable.
    latest_mint = max(link.t_redelegate for link in chain)
    if latest_mint >= view.t_freshness:
        raise ZombieAttestationError(
            f"chain minted at {latest_mint} >= revocation freshness "
            f"{view.t_freshness}; ancestor revocation status undecidable "
            f"(zombie attestation window)"
        )

    # 4. optional quarantine: force a minimum gap between mint and use so
    #    the revocation view can catch up before the leaf is honored.
    if delta_quarantine > 0.0 and latest_mint > now - delta_quarantine:
        raise ZombieAttestationError(
            f"chain minted at {latest_mint} within quarantine "
            f"{delta_quarantine}s of use time {now}"
        )
