# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-signed-delegation-chain-as-revoked-before-the-revocation-event-itself-propagates.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-signed-delegation-chain-as-revoked-before-the-revocation-event-itself-propagates.warrant.json
# Markdown SHA-256:   80133f2613e7b827ab39116e9c7408b615b4f9f3797da05e69bd82b7b17d3348
# Code AST SHA-256:   8901c77016d6b657860b41a3ba8d56094eae383868917f89fb9b19b39e4d3e64
# Verified Classes:   RevocationPropagationError, ChainLink
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime, timedelta


class RevocationPropagationError(RuntimeError):
    """Raised when a delegation chain cannot be trusted because status
    freshness exceeds the originating issuer's propagation bound."""


@dataclass(frozen=True)
class ChainLink:
    issuer: str
    propagation_bound_s: int          # signed by issuer; must be present
    last_confirmed_ok: datetime | None  # last positive status check
    revoked_seen: bool = False


def authorize(chain: list[ChainLink], now: datetime) -> None:
    if not chain:
        raise RevocationPropagationError("empty delegation chain")

    # The governing bound is the minimum across all issuers in the chain.
    bounds = []
    for link in chain:
        if link.propagation_bound_s is None or link.propagation_bound_s <= 0:
            raise RevocationPropagationError(
                f"issuer {link.issuer!r} declares no valid propagation bound; "
                "unbounded issuers are un-verifiable in a federation"
            )
        bounds.append(link.propagation_bound_s)
    governing_bound = timedelta(seconds=min(bounds))

    for link in chain:
        if link.revoked_seen:
            raise RevocationPropagationError(
                f"issuer {link.issuer!r}: explicit revocation observed"
            )
        if link.last_confirmed_ok is None:
            raise RevocationPropagationError(
                f"issuer {link.issuer!r}: no confirmed non-revocation; "
                "treating unknown status as revoked (fail-closed)"
            )
        age = now - link.last_confirmed_ok
        if age > governing_bound:
            # No revocation event seen, but freshness exceeded the bound:
            # operationally equivalent to revoked.
            raise RevocationPropagationError(
                f"issuer {link.issuer!r}: status stale by {age.total_seconds():.0f}s "
                f"> governing bound {governing_bound.total_seconds():.0f}s; "
                "status_stale treated as revoked"
            )


if __name__ == "__main__":
    t0 = datetime(2026, 11, 14, 12, 0, 0)
    good = [ChainLink("org-a", 120, t0 - timedelta(seconds=30))]
    authorize(good, t0)  # passes

    stale = [ChainLink("org-a", 120, t0 - timedelta(seconds=260))]
    try:
        authorize(stale, t0)
    except RevocationPropagationError as exc:
        print("denied:", exc)
