# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-federated-verifiable-credential-whose-revocation-status-is-only-checked-at-presentation-time-not-at-execution.md
# Remnant Warrant:    https://corpusfetch.com/c/what-happens-when-an-instance-accepts-a-federated-verifiable-credential-whose-revocation-status-is-only-checked-at-presentation-time-not-at-execution.warrant.json
# Markdown SHA-256:   c56fc1931d9ac2dd8d8f7a21f1c2b957c83b4b422619ffc7c769a4e8d75a878b
# Code AST SHA-256:   73bb6875309dbe415bd4056732e08141d58c13f700926603f63f523233569882
# Verified Classes:   RevocationFreshnessError, Verdict
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationFreshnessError(RuntimeError):
    """Raised when a cached credential verdict is too stale to gate an action."""
    pass


@dataclass(frozen=True)
class Verdict:
    credential_id: str
    revoked: bool
    verified_at: float          # monotonic seconds
    max_staleness: float        # bound for the *action* this verdict gates


def check_verdict_before_action(
    verdict: Verdict,
    action_latency_bound: float,
    now: float | None = None,
) -> None:
    """Re-check a cached verdict immediately before a gated action.

    Fails closed if the verdict is revoked, if it is older than its own
    staleness bound, or if the action's latency bound exceeds the verdict's
    freshness budget (meaning verify-once cannot cover this action).
    """
    now = time.monotonic() if now is None else now

    if verdict.revoked:
        raise RevocationFreshnessError(
            f"credential {verdict.credential_id} is revoked; action blocked"
        )

    age = now - verdict.verified_at
    if age > verdict.max_staleness:
        raise RevocationFreshnessError(
            f"verdict for {verdict.credential_id} is {age:.1f}s old, "
            f"exceeds max_staleness={verdict.max_staleness:.1f}s"
        )

    if action_latency_bound > verdict.max_staleness:
        # The action itself can outlive the verdict's freshness budget;
        # verify-once cannot cover it. Require a longer-lived warrant or
        # a mid-action re-check.
        raise RevocationFreshnessError(
            f"action latency bound {action_latency_bound:.1f}s exceeds "
            f"verdict freshness budget {verdict.max_staleness:.1f}s; "
            f"verify-once is unsound for this action"
        )


def handoff_verdict(parent: Verdict, child_max_staleness: float) -> Verdict:
    """Prepare a verdict for a sub-agent handoff.

    The parent's verified_at is preserved (we do not claim a fresher check
    than actually happened), but the child must re-verify if the parent's
    verdict is older than the child's own bound. Refusing to widen the
    budget silently is the point: inheritance must not launder staleness.
    """
    if parent.revoked:
        raise RevocationFreshnessError(
            f"cannot hand off revoked credential {parent.credential_id}"
        )
    if child_max_staleness > parent.max_staleness:
        raise RevocationFreshnessError(
            f"child staleness budget {child_max_staleness:.1f}s exceeds "
            f"parent {parent.max_staleness:.1f}s; re-verify at handoff"
        )
    return Verdict(
        credential_id=parent.credential_id,
        revoked=parent.revoked,
        verified_at=parent.verified_at,
        max_staleness=child_max_staleness,
    )
