# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-peer-credential-when-revocation-propagates-through-a-federated-trust-mesh-slower-than-the-peer-s-own-action-leas.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-revoked-peer-credential-when-revocation-propagates-through-a-federated-trust-mesh-slower-than-the-peer-s-own-action-leas.warrant.json
# Markdown SHA-256:   49136557026dfeef23a221ea11d5b63a27f32d1730479081c986f0bdc3e71273
# Code AST SHA-256:   c4c1a6b6cd4712bd904ae6a8d0525d051fd40c7bc2bee5f038e6a62936074618
# Verified Classes:   StaleLeaseError, ActionLease
# Defensive Guards:   2 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleLeaseError(RuntimeError):
    """Raised when an action lease outlives the freshness of its revocation check."""
    pass


@dataclass
class ActionLease:
    peer_did: str
    scope: str
    lease_expiry: float          # monotonic seconds
    verified_at: float           # last origin-confirmed non-revocation
    freshness_budget: float      # max age of verified_at we will trust


def authorize_action(
    lease: ActionLease,
    now: float,
    observed_revocation_p99: float,
) -> None:
    # Freshness deadline: the earlier of lease expiry and the point at which
    # our last origin-confirmed check becomes too old to trust.
    freshness_deadline = lease.verified_at + lease.freshness_budget

    # Structural invariant: a lease must never be usable past the worst-case
    # revocation propagation delay measured from a confirmed-fresh check.
    propagation_deadline = lease.verified_at + observed_revocation_p99

    effective_deadline = min(lease.lease_expiry, freshness_deadline, propagation_deadline)

    if now >= effective_deadline:
        raise StaleLeaseError(
            f"lease for {lease.peer_did} scope={lease.scope} unusable: "
            f"now={now:.1f} effective_deadline={effective_deadline:.1f} "
            f"(lease_expiry={lease.lease_expiry:.1f}, "
            f"freshness_deadline={freshness_deadline:.1f}, "
            f"propagation_deadline={propagation_deadline:.1f}); "
            f"force synchronous status check against origin before acting"
        )


def refresh_lease(
    lease: ActionLease,
    now: float,
    origin_reachable: bool,
    revocation_seen: bool,
    observed_revocation_p99: float,
    new_lease_ttl: float,
) -> ActionLease:
    if revocation_seen:
        # Positive revocation observation: do not extend under any circumstance.
        raise StaleLeaseError(
            f"peer {lease.peer_did} revoked at origin; refusing to refresh lease"
        )

    if not origin_reachable:
        # Cannot confirm non-revocation from origin: clamp to last confirmed
        # freshness, never extend on the basis of a cached negative.
        clamped_expiry = lease.verified_at + observed_revocation_p99
        return ActionLease(
            peer_did=lease.peer_did,
            scope=lease.scope,
            lease_expiry=min(lease.lease_expiry, clamped_expiry),
            verified_at=lease.verified_at,
            freshness_budget=lease.freshness_budget,
        )

    # Origin confirmed non-revocation: safe to extend, and verified_at advances.
    return ActionLease(
        peer_did=lease.peer_did,
        scope=lease.scope,
        lease_expiry=now + new_lease_ttl,
        verified_at=now,
        freshness_budget=lease.freshness_budget,
    )
