# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-peer-s-cached-capability-token-as-revoked-when-the-revocation-event-never-crosses-the-org-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-peer-s-cached-capability-token-as-revoked-when-the-revocation-event-never-crosses-the-org-boundary.warrant.json
# Markdown SHA-256:   c63dde38ef3720dc4338c5a49a17538f11b15a0ff9ed7b595979830249f3e924
# Code AST SHA-256:   4b38312b17461fc1df3763a439e1d70177d65ded2008dc4329f6f5c5bbfe54fb
# Verified Classes:   RevocationFloorViolation, Warrant, TrustState
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class RevocationFloorViolation(RuntimeError):
    """Raised when a cached warrant is honored past its revocation epoch floor."""
    pass


@dataclass(frozen=True)
class Warrant:
    agent_id: str
    authority: str
    version: int          # monotonic warrant_version / revocation_epoch
    not_after: float      # issuer-clock epoch seconds
    signature_ok: bool


@dataclass
class TrustState:
    grant_trust: float          # decayed, gate for liveness/grants
    revocation_floor: float     # floor; decay cannot cross this
    highest_epoch_seen: int     # monotonic, never decreases


REVOCATION_ADMISSIBLE = 0.0  # revocations honored at any trust >= floor


def admit_revocation(trust: TrustState, revoked_version: int) -> bool:
    # Revocation admissibility is governed by the FLOOR, not grant_trust.
    if trust.revocation_floor <= REVOCATION_ADMISSIBLE:
        return False
    return revoked_version > trust.highest_epoch_seen


def validate_on_resume(
    warrant: Warrant,
    trust: TrustState,
    consumer_now: float,
    max_skew: float,
) -> None:
    if not warrant.signature_ok:
        raise RevocationFloorViolation(
            f"warrant for {warrant.agent_id} failed signature; cannot honor"
        )
    # 1. epoch floor: never honor a version below the highest seen
    if warrant.version < trust.highest_epoch_seen:
        raise RevocationFloorViolation(
            f"{warrant.agent_id} version {warrant.version} "
            f"< epoch floor {trust.highest_epoch_seen}"
        )
    # 2. clock-bounded expiry under consumer clock minus skew
    if consumer_now - max_skew > warrant.not_after:
        raise RevocationFloorViolation(
            f"{warrant.agent_id} expired at {warrant.not_after} "
            f"(consumer {consumer_now}, skew {max_skew})"
        )
    # 3. high grant_trust does NOT rescue a stale epoch
    if trust.grant_trust >= 0.6 and warrant.version < trust.highest_epoch_seen:
        raise RevocationFloorViolation(
            f"grant_trust {trust.grant_trust} cannot override epoch floor"
        )
    # 4. a dropped delta is UNKNOWN -> fail closed on resume
    if trust.revocation_floor < 0.6 and warrant.version == trust.highest_epoch_seen:
        raise RevocationFloorViolation(
            f"{warrant.agent_id}: revocation channel degraded "
            f"(floor={trust.revocation_floor}); epoch unconfirmed, failing closed"
        )
