# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-plugin-whose-skill-provenance-attestation-chains-to-a-revoked-but-still-cached-signer.md
# Remnant Warrant:    https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-plugin-whose-skill-provenance-attestation-chains-to-a-revoked-but-still-cached-signer.warrant.json
# Markdown SHA-256:   66cc364945b5de6f392c17c481487c4dee419349c631a68ce656cdfda781613e
# Code AST SHA-256:   3f2e3cce0970e14dbc8255bb9518542761205660444c944322d37bbb6a7cda36
# Verified Classes:   StaleAttestationError, CachedVerdict
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleAttestationError(RuntimeError):
    """Raised when a cached provenance verdict cannot be shown to be
    trust-fresh at instantiation time. Fail-closed: do not load."""
    pass


@dataclass
class CachedVerdict:
    skill: str
    artifact_digest: str
    signer: str
    # Absolute, externally-anchored UTC epoch seconds. Never monotonic.
    verified_at_utc: float
    trust_epoch: str          # e.g. CRL number / log checkpoint at verify time
    ttl_seconds: float = 86400.0


def authorize_instantiation(
    row: CachedVerdict,
    current_trust_epoch: str | None,
    revocation_probe_ok: bool,
    poll_interval_seconds: float = 300.0,
    now_utc: float | None = None,
) -> None:
    """Gate plugin load on a cached verdict. Raises StaleAttestationError
    if the verdict cannot be proven trust-fresh *right now*."""
    now = now_utc if now_utc is not None else time.time()

    # C1: monotonic-derived timestamps are not absolute; reject absurd ages.
    age = now - row.verified_at_utc
    if age < 0:
        raise StaleAttestationError(
            f"{row.skill}: verified_at is in the future "
            f"(age={age:.1f}s); clock basis is not wall-clock-anchored"
        )

    # C4: trust epoch must match the authority's current epoch.
    if current_trust_epoch is None:
        raise StaleAttestationError(
            f"{row.skill}: trust epoch unavailable; cannot confirm signer "
            f"{row.signer} is still trusted"
        )
    if current_trust_epoch != row.trust_epoch:
        raise StaleAttestationError(
            f"{row.skill}: trust epoch moved {row.trust_epoch} -> "
            f"{current_trust_epoch}; signer {row.signer} may be revoked"
        )

    # C2: effective freshness window is min(ttl, poll interval).
    window = min(row.ttl_seconds, poll_interval_seconds)
    if age > window:
        raise StaleAttestationError(
            f"{row.skill}: cached verdict age {age:.0f}s exceeds window "
            f"{window:.0f}s; re-verification required"
        )

    # C3: load-time revocation probe must succeed affirmatively.
    if not revocation_probe_ok:
        raise StaleAttestationError(
            f"{row.skill}: revocation probe failed/unreachable; "
            f"fail-closed on signer {row.signer}"
        )

    # All conditions hold: cached verdict is trust-fresh; safe to load.
    return None
