# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-stale-attestation-as-a-revocation-and-when-is-silent-continuation-safe.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-stale-attestation-as-a-revocation-and-when-is-silent-continuation-safe.warrant.json
# Markdown SHA-256:   1061d51c0432adacbfd809c06f54fb8c04f13b00ba2317ab2888301783db2585
# Code AST SHA-256:   bc15c0e9a62b22e9d1067be7088c3ce265465c9d0e8da0866548e72911e1a730
# Verified Classes:   StaleAttestationError, Attestation
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from typing import Optional


class StaleAttestationError(RuntimeError):
    """Raised when a privileged action is attempted on an attestation whose
    freshness bound has lapsed or whose revocation state is unknown."""


@dataclass
class Attestation:
    subject: str
    capabilities: frozenset
    not_after: float          # absolute expiry of the warrant
    claims_max_age: float     # freshness bound on the capability claims
    fetched_at: float         # when this proof was last successfully re-fetched
    revoked: Optional[bool]   # None => revocation channel unreachable


def assert_authorized(att: Attestation, capability: str, now: Optional[float] = None) -> None:
    now = time.time() if now is None else now

    if now > att.not_after:
        raise StaleAttestationError(
            f"attestation for {att.subject} past not_after; authority lapsed"
        )

    age = now - att.fetched_at
    if age > att.claims_max_age:
        # Freshness bound lapsed. If the revocation channel is also unreachable,
        # fail closed: treat unknown revocation as revoked for privileged use.
        if att.revoked is None:
            raise StaleAttestationError(
                f"attestation for {att.subject} stale by {age - att.claims_max_age:.0f}s "
                f"AND revocation channel unreachable; failing closed"
            )
        raise StaleAttestationError(
            f"attestation for {att.subject} stale by {age - att.claims_max_age:.0f}s; "
            f"re-attest before privileged action"
        )

    if att.revoked is True:
        raise StaleAttestationError(
            f"attestation for {att.subject} affirmatively revoked"
        )

    if capability not in att.capabilities:
        raise StaleAttestationError(
            f"capability {capability!r} not present in current attestation"
        )


def issue_warrant(att: Attestation, peer: str, capability: str) -> dict:
    # Enforce freshness at issuance so the warrant cannot launder a stale root.
    assert_authorized(att, capability)
    return {
        "peer": peer,
        "capability": capability,
        "derived_from": att.subject,
        "root_fetched_at": att.fetched_at,
        "root_claims_max_age": att.claims_max_age,
    }
