# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-honor-a-federated-capability-attestation-whose-issuer-s-policy-epoch-has-advanced-beyond-the-epoch-bo.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-honor-a-federated-capability-attestation-whose-issuer-s-policy-epoch-has-advanced-beyond-the-epoch-bo.warrant.json
# Markdown SHA-256:   cf90a6a9ad5617dca1a1f1a9ba7cb1e6ab262c02e502c89fe7d2cc424c9ce082
# Code AST SHA-256:   3f816f9137ccd875564ea4bac07fc9bc4381b6507e9b00ef48d4b9abfc8c5854
# Verified Classes:   EpochSupersededError, EpochRegressionError, EpochUnverifiableError, Attestation, EpochBeacon
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class EpochSupersededError(RuntimeError):
    """Attestation's epoch is behind the issuer's current epoch beyond tolerance."""


class EpochRegressionError(RuntimeError):
    """Issuer epoch counter moved backwards; federation monotonicity violated."""


class EpochUnverifiableError(RuntimeError):
    """Cannot establish issuer current epoch within staleness bound; deny by default."""


@dataclass(frozen=True)
class Attestation:
    issuer: str
    subject: str
    action: str
    resource: str
    epoch: int
    not_after: float


@dataclass(frozen=True)
class EpochBeacon:
    issuer: str
    current_epoch: int
    issued_at: float


def honor_attestation(
    att: Attestation,
    beacon: EpochBeacon | None,
    skew_tolerance: int,
    max_epoch_staleness: float,
    now: float | None = None,
) -> str:
    now = time.time() if now is None else now

    if now > att.not_after:
        raise EpochSupersededError(f"attestation expired at {att.not_after}")

    if beacon is None:
        raise EpochUnverifiableError(
            f"no epoch beacon for issuer {att.issuer}; cannot establish current epoch"
        )

    if beacon.issuer != att.issuer:
        raise EpochUnverifiableError(
            f"beacon issuer {beacon.issuer} does not match attestation issuer {att.issuer}"
        )

    if beacon.current_epoch < att.epoch:
        raise EpochRegressionError(
            f"issuer {att.issuer} epoch regressed: beacon={beacon.current_epoch} "
            f"< attestation={att.epoch}"
        )

    age = now - beacon.issued_at
    stale = age > max_epoch_staleness

    if beacon.current_epoch > att.epoch + skew_tolerance:
        raise EpochSupersededError(
            f"attestation epoch {att.epoch} superseded by issuer epoch "
            f"{beacon.current_epoch} (tolerance={skew_tolerance})"
        )

    if stale:
        return "provisional"  # honor under post-hoc audit reconciliation

    return "honored"


def resume_from_checkpoint(summary: dict, beacon: EpochBeacon | None) -> str:
    # Distillation-loss guard: a checkpoint that dropped the epoch bound is corrupt.
    if "epoch" not in summary or summary["epoch"] is None:
        raise EpochSupersededError(
            "checkpoint lost attestation epoch bound during compaction; "
            "delegation is corrupt and must be renegotiated"
        )
    att = Attestation(
        issuer=summary["issuer"],
        subject=summary["subject"],
        action=summary["action"],
        resource=summary["resource"],
        epoch=summary["epoch"],
        not_after=summary["not_after"],
    )
    return honor_attestation(
        att, beacon, skew_tolerance=1, max_epoch_staleness=30.0
    )
