# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-delegated-macaroon-whose-caveats-are-individually-satisfied-but-whose-revocation-set-was-computed-agai.md
# Remnant Warrant:    https://corpusfetch.com/c/under-what-conditions-must-an-instance-reject-a-delegated-macaroon-whose-caveats-are-individually-satisfied-but-whose-revocation-set-was-computed-agai.warrant.json
# Markdown SHA-256:   5d3812952e99f05476ee56c4c5be70fa11cfe8e43e73b90aaf47f268adb89906
# Code AST SHA-256:   2218bf6e77398116bf46266d9b92651075a9c220cf9b89c53772eeb14fbe61b0
# Verified Classes:   RevocationIndeterminate, RevocationRollback, Caveat, RevocationSet
# Defensive Guards:   6 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass
from typing import Sequence

class RevocationIndeterminate(RuntimeError):
    """Raised when the revocation set cannot prove the root's status at minting."""
    pass

class RevocationRollback(RevocationIndeterminate):
    """Raised when a fetched set's as_of regresses below the last seen value."""
    pass

@dataclass(frozen=True)
class Caveat:
    issued_at: float
    not_after: float

@dataclass(frozen=True)
class RevocationSet:
    as_of: float
    revoked_roots: frozenset[str]
    signature_valid: bool

def verify_delegated(
    root_id: str,
    caveats: Sequence[Caveat],
    revset: RevocationSet,
    first_observed_use: float,
    last_seen_as_of: float,
    now: float,
) -> None:
    if not revset.signature_valid:
        raise RevocationIndeterminate("revocation set signature invalid")
    if revset.as_of < last_seen_as_of:
        raise RevocationRollback(
            f"revocation as_of {revset.as_of} regressed below {last_seen_as_of}"
        )
    issuance_floor = max(c.issued_at for c in caveats)
    expiry_ceiling = min(c.not_after for c in caveats)
    if not (issuance_floor <= now <= expiry_ceiling):
        raise RevocationIndeterminate(
            f"validity window empty or now={now} outside "
            f"[{issuance_floor},{expiry_ceiling}]"
        )
    if revset.as_of < issuance_floor:
        raise RevocationIndeterminate(
            f"revocation as_of {revset.as_of} predates issuance floor "
            f"{issuance_floor}; root status unknown at minting"
        )
    if root_id in revset.revoked_roots:
        raise RevocationIndeterminate(f"root {root_id} revoked")
    if revset.as_of < first_observed_use:
        # Valid at issuance but possibly revoked since first use.
        # Caller must decide: reject, or allow only idempotent ops.
        raise RevocationIndeterminate(
            f"revocation as_of {revset.as_of} predates first observed use "
            f"{first_observed_use}; degraded state"
        )
