# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-delegated-credential-whose-delegation-chain-exceeds-the-verifier-s-revocation-propagation-horizon.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-presents-a-delegated-credential-whose-delegation-chain-exceeds-the-verifier-s-revocation-propagation-horizon.warrant.json
# Markdown SHA-256:   ac41562767c98ad66cee34694f4a75f7367db09c84a1a8da01df19c7b8ec40f7
# Code AST SHA-256:   a8b23a73079c514f90de372c7355b4f0e9201436554d906b41f827f824167f0c
# Verified Classes:   DelegationIndeterminateError, Link, Credential
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class DelegationIndeterminateError(RuntimeError):
    """Raised when a delegation link's status cannot be causally ordered
    against its child's issuance. Not a revocation; not a validity pass."""
    pass


@dataclass
class Link:
    issuer: str
    status_fetch_time: float   # epoch seconds when verifier observed status
    max_status_age: float      # declared freshness budget for this link
    revoked: bool


@dataclass
class Credential:
    subject: str
    parent_index: int          # index into chain of the delegating link
    issuance_time: float


def verify_chain(links: list[Link], creds: list[Credential], now: float,
                 max_propagation_delay: float) -> str:
    horizon = min(l.max_status_age for l in links)
    if len(links) * max_propagation_delay > horizon:
        raise DelegationIndeterminateError(
            f"chain depth {len(links)} exceeds freshness horizon "
            f"{horizon}s at propagation delay {max_propagation_delay}s/hop"
        )

    for i, link in enumerate(links):
        if now - link.status_fetch_time > link.max_status_age:
            return "stale"
        if link.revoked:
            return "revoked"

    # Causal ordering: status observation for a parent must postdate
    # the issuance of every child credential it authorized.
    for c in creds:
        parent = links[c.parent_index]
        if parent.status_fetch_time < c.issuance_time:
            raise DelegationIndeterminateError(
                f"credential for {c.subject} issued at {c.issuance_time} "
                f"postdates status observation of {parent.issuer} at "
                f"{parent.status_fetch_time}; synchronous refetch required"
            )

    return "valid"
