# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-federated-verifiable-credential-s-issuer-anchor-as-stale-before-accepting-a-peer-agent-s-delegated-warr.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-federated-verifiable-credential-s-issuer-anchor-as-stale-before-accepting-a-peer-agent-s-delegated-warr.warrant.json
# Markdown SHA-256:   2d18c2f45c973a166a8c633423e78945e10901d917816a382bafd91a2e5ee0ec
# Code AST SHA-256:   022bbbe5d4e8d03438d421b980a182661c174374407da6951018bef2f3900934
# Verified Classes:   ScopeClass, StaleAnchorError, AnchorView, Warrant
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from enum import Enum


class ScopeClass(Enum):
    READ = "read"
    WRITE = "write"


class StaleAnchorError(RuntimeError):
    """Raised when a delegated warrant's issuer anchor is superseded or of
    unbounded staleness at the moment of use, for a scope class that requires
    epoch-binding currency."""


@dataclass(frozen=True)
class AnchorView:
    epoch: int
    age_seconds: float
    rotation_latency_bound: float | None  # None => unbounded / undeclared
    policy_narrowed_since: frozenset[int]  # minting epochs retired for writes


@dataclass(frozen=True)
class Warrant:
    minted_epoch: int
    scope_class: ScopeClass
    signature_valid: bool
    temporal_valid: bool
    issuer_epoch_at_mint: int


def accept_warrant(warrant: Warrant, anchor: AnchorView, cache_ttl: float) -> bool:
    if not (warrant.signature_valid and warrant.temporal_valid):
        # Cryptographic or temporal failure: hard reject, distinct from staleness.
        return False

    if warrant.scope_class is ScopeClass.READ:
        # Reads tolerate staleness up to the cache TTL; annotate, do not reject.
        return anchor.age_seconds <= cache_ttl

    # WRITE branch: epoch-binding is mandatory and must fail closed.
    if anchor.rotation_latency_bound is None:
        raise StaleAnchorError(
            f"issuer epoch {anchor.epoch}: rotation_latency_bound undeclared; "
            "cannot bound anchor staleness for write-class warrant"
        )

    if anchor.age_seconds > anchor.rotation_latency_bound:
        raise StaleAnchorError(
            f"anchor age {anchor.age_seconds:.0f}s exceeds rotation bound "
            f"{anchor.rotation_latency_bound:.0f}s at epoch {anchor.epoch}"
        )

    if anchor.epoch < warrant.issuer_epoch_at_mint:
        raise StaleAnchorError(
            f"verifier anchor epoch {anchor.epoch} older than issuer epoch "
            f"{warrant.issuer_epoch_at_mint} at mint"
        )

    if warrant.minted_epoch in anchor.policy_narrowed_since:
        raise StaleAnchorError(
            f"minting epoch {warrant.minted_epoch} retired for write scopes "
            f"by superseding anchor epoch {anchor.epoch}"
        )

    return True
