# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/why-does-an-instance-keep-honoring-a-revoked-peer-warrant-mid-delegation-in-a2a-handoffs.md
# Remnant Warrant:    https://corpusfetch.com/c/why-does-an-instance-keep-honoring-a-revoked-peer-warrant-mid-delegation-in-a2a-handoffs.warrant.json
# Markdown SHA-256:   adba4fb8df4013b6c6dc1261b3420bfcc22fa152db2d343cd3ba8b4c81a9fc19
# Code AST SHA-256:   839ee7f9c8b3e2fc3a0182c579ba82c4bee024267ded96c92efbcf4c652ad2ee
# Verified Classes:   RevocationClockViolation, Warrant, RevocationFeed
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from typing import Optional


class RevocationClockViolation(RuntimeError):
    """Raised when a receiver cannot bound its stale-authority window."""


@dataclass
class Warrant:
    jti: str
    subject: str
    scope: str
    exp: float          # absolute epoch seconds
    issued_at: float


@dataclass
class RevocationFeed:
    poll_interval: float        # seconds between revocation polls
    sla_rev: float              # issuer guarantee: revocation effective within this
    last_poll_at: float
    revoked_jtis: set


def assert_exposure_bounded(
    warrant: Warrant,
    feed: RevocationFeed,
    max_step_duration: float,
    now: Optional[float] = None,
) -> None:
    """
    Enforce: poll_interval + max_step_duration <= sla_rev.
    If violated, the receiver cannot bound stale-authority exposure and must
    refuse to exercise the warrant rather than silently overrun.
    """
    now = time.time() if now is None else now

    if feed.poll_interval + max_step_duration > feed.sla_rev:
        raise RevocationClockViolation(
            f"stale-authority window unbounded: poll_interval={feed.poll_interval}s "
            f"+ max_step_duration={max_step_duration}s "
            f"> sla_rev={feed.sla_rev}s for jti={warrant.jti}"
        )

    if now - feed.last_poll_at > feed.poll_interval:
        raise RevocationClockViolation(
            f"revocation feed stale: last_poll={now - feed.last_poll_at:.1f}s ago, "
            f"poll_interval={feed.poll_interval}s, jti={warrant.jti}"
        )

    if warrant.jti in feed.revoked_jtis:
        raise RevocationClockViolation(
            f"warrant jti={warrant.jti} is revoked; refusing to exercise authority"
        )

    if now >= warrant.exp:
        raise RevocationClockViolation(
            f"warrant jti={warrant.jti} expired at {warrant.exp}"
        )


def exercise_authority(
    warrant: Warrant,
    feed: RevocationFeed,
    max_step_duration: float,
    action: str,
) -> str:
    """
    Called before every durable or authority-widening action.
    Re-verifies revocation status under the bounded-exposure invariant.
    """
    assert_exposure_bounded(warrant, feed, max_step_duration)
    # ... perform the scoped action ...
    return f"exercised {action} under {warrant.jti}"


# Example: a receiver whose poll interval plus step duration exceeds the
# issuer's revocation SLA must fail closed, not proceed.
if __name__ == "__main__":
    w = Warrant(jti="w-42", subject="agent-B", scope="task:read",
                exp=time.time() + 3600, issued_at=time.time())
    feed = RevocationFeed(poll_interval=120.0, sla_rev=90.0,
                          last_poll_at=time.time(), revoked_jtis=set())
    try:
        exercise_authority(w, feed, max_step_duration=45.0, action="read:resource-7")
    except RevocationClockViolation as e:
        print(f"fail-closed: {e}")
