# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-capability-grant-it-cannot-re-verify-after-broker-rotation.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-refuse-a-federated-capability-grant-it-cannot-re-verify-after-broker-rotation.warrant.json
# Markdown SHA-256:   f562168aea0e279115d6964a865ac6c68a99d4c357406aecac6f758bf59ad520
# Code AST SHA-256:   02a51d9f62aa6a24021bc8cc0ad21111d68c65e52656a174b0cf6d3ac5710f29
# Verified Classes:   GrantState, UnverifiableGrantError, CachedGrant, JwksSnapshot, FederatedVerifier
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from enum import Enum


class GrantState(Enum):
    VERIFIED = "verified"
    UNDECIDABLE = "undecidable"
    REVOKED = "revoked"


class UnverifiableGrantError(RuntimeError):
    """Raised when a federated grant cannot be re-verified against live trust material."""
    pass


@dataclass
class CachedGrant:
    grant_id: str
    key_id: str
    expires_at: float


@dataclass
class JwksSnapshot:
    keys: frozenset
    fetched_at: float


class FederatedVerifier:
    def __init__(self, rotation_interval_s: float, overlap_s: float, attest_fn):
        self.rotation_interval_s = rotation_interval_s
        self.overlap_s = overlap_s
        self.max_snapshot_age = rotation_interval_s - overlap_s
        self.attest_fn = attest_fn  # callable(grant_id, key_id) -> "ROTATED"|"REVOKED"|"UNKNOWN"

    def classify(self, grant: CachedGrant, live: JwksSnapshot, now: float) -> GrantState:
        if now >= grant.expires_at:
            return GrantState.REVOKED
        if now - live.fetched_at > self.max_snapshot_age:
            return GrantState.UNDECIDABLE
        if grant.key_id not in live.keys:
            return GrantState.UNDECIDABLE
        return GrantState.VERIFIED

    def authorize(self, grant: CachedGrant, live: JwksSnapshot, now: float) -> None:
        state = self.classify(grant, live, now)
        if state is GrantState.VERIFIED:
            return
        if state is GrantState.REVOKED:
            raise UnverifiableGrantError(
                f"grant {grant.grant_id} expired or revoked; discard and re-auth"
            )
        # UNDECIDABLE: never fall back to a stale key. Attest before use.
        verdict = self.attest_fn(grant.grant_id, grant.key_id)
        if verdict == "REVOKED":
            raise UnverifiableGrantError(
                f"grant {grant.grant_id} revoked by broker on attestation"
            )
        if verdict == "UNKNOWN":
            raise UnverifiableGrantError(
                f"grant {grant.grant_id} attestation unresolved; quarantining, refusing execution"
            )
        # verdict == "ROTATED": caller must re-mint under current key before retrying.
        raise UnverifiableGrantError(
            f"grant {grant.grant_id} key rotated; re-mint required, do not execute under stale key"
        )
