# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/what-structural-signals-indicate-a-stale-capability-warrant-is-still-being-honored-across-a-federated-agent-mesh.md
# Remnant Warrant:    https://corpusfetch.com/c/what-structural-signals-indicate-a-stale-capability-warrant-is-still-being-honored-across-a-federated-agent-mesh.warrant.json
# Markdown SHA-256:   6a59ccf1c0b0d699841f35fc198a6f041252c9c8bc7d2a50147e1e6c2f0c35b5
# Code AST SHA-256:   283298caaf4233ec13dbd1ae9668cf7ad102705503626ab85518a621aa5c26ef
# Verified Classes:   StaleWarrantEpochError, Warrant, EpochView
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from typing import Optional


class StaleWarrantEpochError(RuntimeError):
    """Raised when a warrant's revocation epoch does not dominate its ancestry."""
    pass


@dataclass(frozen=True)
class Warrant:
    warrant_id: str
    subject: str
    scope: str
    ttl_s: int
    revocation_epoch: int
    parent_id: Optional[str]
    # ancestor_epochs is populated at EXECUTION time from the delegation proof,
    # not at mint time. Absence of this field is itself a structural signal.
    ancestor_epochs: tuple = ()


@dataclass(frozen=True)
class EpochView:
    authority: str
    epoch: int
    read_at_s: int
    sla_s: int

    def is_fresh(self, now_s: int) -> bool:
        return (now_s - self.read_at_s) <= self.sla_s


def verify_warrant_at_execution(
    w: Warrant,
    view: EpochView,
    now_s: int,
    refresh_fn,
) -> None:
    # 1. Structural signal: delegation proof must carry ancestor epochs.
    if w.parent_id is not None and not w.ancestor_epochs:
        raise StaleWarrantEpochError(
            f"warrant {w.warrant_id} has parent {w.parent_id} but no "
            f"ancestor_epochs in execution proof; cannot evaluate "
            f"chain-monotonic epoch dominance"
        )

    # 2. Chain-monotonic dominance: W.epoch must be >= every ancestor epoch.
    max_ancestor = max(w.ancestor_epochs, default=0)
    if w.revocation_epoch < max_ancestor:
        raise StaleWarrantEpochError(
            f"warrant {w.warrant_id} epoch {w.revocation_epoch} < "
            f"max ancestor epoch {max_ancestor}; warrant structurally stale"
        )

    # 3. Freshness: verifier's epoch view must be within SLA.
    if not view.is_fresh(now_s):
        # Synchronous refresh, bounded. Do NOT proceed on stale cache.
        refreshed = refresh_fn(timeout_s=view.sla_s)
        if refreshed is None or not refreshed.is_fresh(now_s):
            raise StaleWarrantEpochError(
                f"epoch view for {view.authority} stale "
                f"({now_s - view.read_at_s}s > {view.sla_s}s SLA) and "
                f"refresh failed; refusing execution of {w.warrant_id}"
            )
        view = refreshed

    # 4. Dominance against authoritative current epoch.
    if w.revocation_epoch < view.epoch:
        raise StaleWarrantEpochError(
            f"warrant {w.warrant_id} epoch {w.revocation_epoch} < "
            f"authority current epoch {view.epoch}; warrant revoked"
        )
