# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-re-verify-a-federated-credential-when-the-issuer-s-revocation-epoch-has-advanced-past-the-agent-s-cached-attestation-window.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-re-verify-a-federated-credential-when-the-issuer-s-revocation-epoch-has-advanced-past-the-agent-s-cached-attestation-window.warrant.json
# Markdown SHA-256:   81c1b4f8fd3649faaa06ba66bd07953c9b3a8b73c8abd751da7322b679130a51
# Code AST SHA-256:   b555713d6962d9199ecd3710d3eb159ef98822a27202c69f0d71439b6a628f5d
# Verified Classes:   StaleEpochVerdict, Verdict, IssuerState, FederatedVerifier
# Defensive Guards:   3 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class StaleEpochVerdict(RuntimeError):
    """Raised when a cached credential verdict is honored against an issuer epoch
    that has advanced past the verifier's observation window."""
    pass


@dataclass(frozen=True)
class Verdict:
    jti: str
    issuer_id: str
    epoch_observed: int
    decision: str          # "allow" | "deny"
    decided_at: float      # monotonic seconds


@dataclass(frozen=True)
class IssuerState:
    issuer_id: str
    current_epoch: int
    revoked_jtis: frozenset


class FederatedVerifier:
    def __init__(self, max_staleness: float, scope_sensitive: frozenset):
        self.max_staleness = max_staleness
        self.scope_sensitive = scope_sensitive  # scopes that tolerate zero staleness
        self._cache: dict[str, Verdict] = {}

    def verify(self, jti: str, issuer: IssuerState, scope: str, now: float) -> Verdict:
        cached: Optional[Verdict] = self._cache.get(jti)

        if cached is not None:
            epoch_advanced = issuer.current_epoch > cached.epoch_observed
            staleness = now - cached.decided_at

            # Zero-tolerance scopes: any epoch advance invalidates the cache.
            if scope in self.scope_sensitive and epoch_advanced:
                raise StaleEpochVerdict(
                    f"cached verdict for {jti} observed epoch {cached.epoch_observed}, "
                    f"issuer now at {issuer.current_epoch}; scope {scope} tolerates zero staleness"
                )

            # General scopes: bounded staleness on the epoch, not on exp.
            if epoch_advanced and staleness > self.max_staleness:
                raise StaleEpochVerdict(
                    f"cached verdict for {jti} is {staleness:.1f}s stale against "
                    f"epoch advance {cached.epoch_observed}->{issuer.current_epoch}"
                )

            if jti in issuer.revoked_jtis:
                raise StaleEpochVerdict(
                    f"cached verdict for {jti} is positive but jti appears in "
                    f"revocation set at epoch {issuer.current_epoch}"
                )

            return cached

        decision = "deny" if jti in issuer.revoked_jtis else "allow"
        verdict = Verdict(
            jti=jti,
            issuer_id=issuer.issuer_id,
            epoch_observed=issuer.current_epoch,
            decision=decision,
            decided_at=now,
        )
        self._cache[jti] = verdict
        return verdict
