# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-honor-a-delegated-aaif-credential-whose-delegation-chain-crosses-a-revocation-epoch-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-honor-a-delegated-aaif-credential-whose-delegation-chain-crosses-a-revocation-epoch-boundary.warrant.json
# Markdown SHA-256:   91689d9b6b15b7ca50823da5ae2c945be63ab809b2101cdf95811ec018507455
# Code AST SHA-256:   8f49674169908890b284ac91161c08765b2a4676c0b38108c80970958519286b
# Verified Classes:   StaleAncestryError, IssuerAttestation, DelegatedCredential
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class StaleAncestryError(RuntimeError):
    """Raised when a delegated credential's ancestry cannot be attested fresh."""


@dataclass
class IssuerAttestation:
    issuer_id: str
    non_revoked: bool
    attested_at: float      # epoch seconds
    ttl: float              # issuer-published freshness bound


@dataclass
class DelegatedCredential:
    leaf_issuer: str
    ancestry: list[str]     # issuer ids from root to leaf issuer, inclusive
    expires_at: float


def validate_delegated_credential(
    cred: DelegatedCredential,
    attestations: dict[str, Optional[IssuerAttestation]],
    now: float,
) -> None:
    if now >= cred.expires_at:
        raise StaleAncestryError(f"credential expired at {cred.expires_at}")

    for issuer in cred.ancestry:
        att = attestations.get(issuer)
        if att is None:
            raise StaleAncestryError(
                f"no non-revocation attestation for ancestor issuer {issuer!r}; "
                f"chain is indeterminate and must fail closed"
            )
        if not att.non_revoked:
            raise StaleAncestryError(
                f"ancestor issuer {issuer!r} revoked its credential at "
                f"{att.attested_at}"
            )
        age = now - att.attested_at
        if age > att.ttl:
            raise StaleAncestryError(
                f"attestation for ancestor {issuer!r} is stale: "
                f"age {age:.1f}s exceeds issuer TTL {att.ttl:.1f}s"
            )
    # All ancestors freshly attested; credential is honor-able.
