# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-federated-a2a-peer-s-cached-trust-anchor-as-revoked-before-the-revocation-event-itself-has-propagated.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-a-federated-a2a-peer-s-cached-trust-anchor-as-revoked-before-the-revocation-event-itself-has-propagated.warrant.json
# Markdown SHA-256:   1e65cd2fca76aab10ffe7c41801f5139ba299ed428d3e75f7c40c018c8387b44
# Code AST SHA-256:   1457d4d3d0349a52630386aa6c65a04480137f215ea102c0c859faa0d1dbe876
# Verified Classes:   StaleTrustAnchorError, CachedAnchor
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleTrustAnchorError(RuntimeError):
    """Raised when an artifact-minting action would proceed under a trust
    anchor whose revocation status was not confirmed in the current cycle."""
    pass


@dataclass
class CachedAnchor:
    anchor_id: str
    cached_at: float          # epoch seconds
    ttl: float                # freshness TTL, NOT a revocation bound
    revocation_confirmed_at: float | None  # last successful revocation poll


def verify_for_action(
    anchor: CachedAnchor,
    action_class: str,           # "ephemeral" | "artifact_minting"
    revocation_endpoint_reachable: bool,
    now: float | None = None,
) -> str:
    now = now if now is not None else time.time()

    if action_class == "ephemeral":
        if now - anchor.cached_at > anchor.ttl:
            raise StaleTrustAnchorError(
                f"anchor {anchor.anchor_id} past TTL for ephemeral action"
            )
        return "cached"

    if action_class == "artifact_minting":
        # TTL is irrelevant here; only a synchronous confirmation counts.
        if not revocation_endpoint_reachable:
            raise StaleTrustAnchorError(
                f"cannot confirm revocation for {anchor.anchor_id} before "
                f"minting; endpoint unreachable — failing closed"
            )
        confirmed = anchor.revocation_confirmed_at
        if confirmed is None or confirmed < now - 1.0:
            raise StaleTrustAnchorError(
                f"anchor {anchor.anchor_id} revocation not confirmed in "
                f"current decision cycle; refusing to mint durable artifact"
            )
        return "synchronous"

    raise StaleTrustAnchorError(f"unknown action_class {action_class!r}")
