# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-treat-an-aaif-status-list-entry-as-authoritative-before-acting-on-a-delegated-warrant.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-treat-an-aaif-status-list-entry-as-authoritative-before-acting-on-a-delegated-warrant.warrant.json
# Markdown SHA-256:   b11efad46cd327a16b041297a4a918c75d782cb28d3a204d6bd2137b37018c31
# Code AST SHA-256:   e1823850d7cef65205444058e34adfa4baaf964dc006c63911f1366fa707d913
# Verified Classes:   StaleStatusListError, StatusView, Warrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from datetime import datetime, timedelta


class StaleStatusListError(RuntimeError):
    """Raised when a status-list view cannot be proven fresh enough to clear a warrant."""


@dataclass(frozen=True)
class StatusView:
    list_uri: str
    list_version: int          # monotonic epoch published by issuer; None-equivalent = 0
    fetched_at: datetime
    max_age: timedelta         # from Cache-Control / document ttl
    issuer_propagation_bound: timedelta  # worst-case batch lag, published or estimated


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    status_list_uri: str
    status_list_index: int
    parent_epoch: int | None   # epoch inherited from parent warrant, if sub-delegated
    expires_at: datetime


def assert_status_authoritative(view: StatusView, warrant: Warrant, now: datetime) -> None:
    if view.list_uri != warrant.status_list_uri:
        raise StaleStatusListError(
            f"status list mismatch for {warrant.warrant_id}: "
            f"view={view.list_uri} warrant={warrant.status_list_uri}"
        )

    remaining = warrant.expires_at - now
    if remaining <= timedelta(0):
        raise StaleStatusListError(f"warrant {warrant.warrant_id} already expired at {now.isoformat()}")

    staleness = (now - view.fetched_at) + view.issuer_propagation_bound
    horizon = min(remaining, view.max_age)
    if staleness >= horizon:
        raise StaleStatusListError(
            f"status view for {warrant.warrant_id} is not authoritative: "
            f"staleness={staleness} >= horizon={horizon} "
            f"(remaining={remaining}, max_age={view.max_age}, "
            f"propagation_bound={view.issuer_propagation_bound})"
        )

    if warrant.parent_epoch is not None and view.list_version < warrant.parent_epoch:
        raise StaleStatusListError(
            f"sub-delegated warrant {warrant.warrant_id} references list epoch "
            f"{view.list_version} older than inherited parent epoch {warrant.parent_epoch}; "
            f"revocation of parent is unobservable in this view"
        )


def authorize_delegated_action(view: StatusView, warrant: Warrant, now: datetime) -> bool:
    # A clear bit is only meaningful after the freshness invariant holds.
    assert_status_authoritative(view, warrant, now)
    return True
