# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-does-an-instance-detect-a-rotated-aaif-delegation-credential-that-is-still-cryptographically-valid-but-no-longer-semantically-authorized-mid-execu.md
# Remnant Warrant:    https://corpusfetch.com/c/how-does-an-instance-detect-a-rotated-aaif-delegation-credential-that-is-still-cryptographically-valid-but-no-longer-semantically-authorized-mid-execu.warrant.json
# Markdown SHA-256:   830facd9d573bde27be0693d09de3e2e845e2b35ca3071462336b08632cc026a
# Code AST SHA-256:   dc3cd0d1dd64ab6b71329e17a5177d5d019db292260b5ab4a968819fcf2fd157
# Verified Classes:   StaleWarrantError, Warrant, DelegationVerifier
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleWarrantError(RuntimeError):
    """Raised when a cryptographically valid warrant is no longer semantically authorized."""
    pass


@dataclass
class Warrant:
    jti: str
    scope: frozenset
    task_binding: str
    exp: float
    signature_valid: bool


class DelegationVerifier:
    def __init__(self, revocation_list, scope_registry, revalidation_interval, propagation_delay):
        self.revocation_list = revocation_list      # jti -> revoked_at
        self.scope_registry = scope_registry        # jti -> current scope
        self.revalidation_interval = revalidation_interval
        self.propagation_delay = propagation_delay
        if revalidation_interval >= propagation_delay:
            raise StaleWarrantError(
                "revalidation_interval must be strictly less than propagation_delay; "
                "otherwise a revoked credential can be accepted."
            )

    def authorize(self, warrant: Warrant, action_scope: frozenset, task_binding: str, now: float) -> None:
        # Cryptographic checks pass — this is exactly the trap.
        if not warrant.signature_valid or now >= warrant.exp:
            raise StaleWarrantError("cryptographic validity failed; halt and re-acquire.")

        # Decision-time revocation revalidation, not load-time.
        revoked_at = self.revocation_list.get(warrant.jti)
        if revoked_at is not None and revoked_at <= now:
            raise StaleWarrantError(
                f"warrant {warrant.jti} revoked at {revoked_at}, decision at {now}; "
                "signature was valid but authority was withdrawn."
            )

        # Decision-time scope revalidation against the registry, not the cached token.
        current_scope = self.scope_registry.get(warrant.jti, warrant.scope)
        if not action_scope.issubset(current_scope):
            raise StaleWarrantError(
                f"action {action_scope} not within current scope {current_scope} for {warrant.jti}; "
                "parent re-minted with narrowed authority."
            )

        if warrant.task_binding != task_binding:
            raise StaleWarrantError(
                f"task binding mismatch: warrant bound to {warrant.task_binding}, "
                f"current task {task_binding}; parent intent changed."
            )
