# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-must-an-instance-re-verify-a-federated-peer-s-capability-grant-after-a-policy-renegotiation-mid-handoff.md
# Remnant Warrant:    https://corpusfetch.com/c/when-must-an-instance-re-verify-a-federated-peer-s-capability-grant-after-a-policy-renegotiation-mid-handoff.warrant.json
# Markdown SHA-256:   c99ce437a47d24e43727d49ec9ec10d1dc579e24256041357668c92e7c380b34
# Code AST SHA-256:   d73c10ae76dc1f1ecfd2dd54fb49ebb4311a7d87531b231618ca2b9cd1db73ac
# Verified Classes:   StaleGrantEpochError, Grant, EpochPolicy
# Defensive Guards:   5 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from datetime import datetime, timedelta


class StaleGrantEpochError(RuntimeError):
    """Raised when a delegated capability is exercised after its issuing
    authority has superseded the grant epoch observed at dispatch."""
    pass


@dataclass(frozen=True)
class Grant:
    subject: str
    capability: str
    valid_from: datetime
    valid_until: datetime
    superseded_at: datetime | None = None


@dataclass(frozen=True)
class EpochPolicy:
    # issuing org's renegotiation-to-enforcement latency
    propagation_delay: timedelta
    # our cache staleness bound; must be strictly less than propagation_delay
    cache_staleness: timedelta
    # max time a job may sit queued before commitment
    max_queue_delay: timedelta


def commit_delegated_action(
    grant: Grant,
    current_epoch_grant: Grant | None,
    dispatched_at: datetime,
    commit_time: datetime,
    policy: EpochPolicy,
) -> None:
    if policy.cache_staleness >= policy.propagation_delay:
        raise StaleGrantEpochError(
            "cache staleness bound >= propagation delay; "
            "revalidation cannot be trusted, refuse cross-org commitment"
        )

    queue_delay = commit_time - dispatched_at
    exposure = queue_delay + policy.cache_staleness
    if exposure >= policy.propagation_delay:
        raise StaleGrantEpochError(
            f"exposure window {exposure} exceeds propagation delay "
            f"{policy.propagation_delay}; re-admit instead of committing"
        )

    if current_epoch_grant is None:
        raise StaleGrantEpochError(
            f"capability {grant.capability!r} absent from current epoch; "
            "grant was closed by renegotiation"
        )

    if current_epoch_grant.superseded_at is not None and commit_time >= current_epoch_grant.superseded_at:
        raise StaleGrantEpochError(
            f"grant superseded at {current_epoch_grant.superseded_at}, "
            f"commit attempted at {commit_time}"
        )

    if not (current_epoch_grant.valid_from <= commit_time < current_epoch_grant.valid_until):
        raise StaleGrantEpochError(
            f"commit time {commit_time} outside current epoch interval "
            f"[{current_epoch_grant.valid_from}, {current_epoch_grant.valid_until})"
        )
