# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-halt-when-a-federated-peer-s-trust-anchor-rotation-outpaces-its-policy-bundle-s-delegation-chain-epoch.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-halt-when-a-federated-peer-s-trust-anchor-rotation-outpaces-its-policy-bundle-s-delegation-chain-epoch.warrant.json
# Markdown SHA-256:   c6c5c6f28c9263b0b306814d297cd09ed49311398076106a9737abc1cc1722e9
# Code AST SHA-256:   954603e6c5f3481b2f4f0feb21740d6ed01738aaee13d4f181e29c9068409598
# Verified Classes:   EpochMismatchError, Warrant, PeerPolicy
# Defensive Guards:   4 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class EpochMismatchError(RuntimeError):
    """Raised when a warrant's anchor epoch disagrees with the peer's current policy epoch."""


@dataclass(frozen=True)
class Warrant:
    issuer: str
    anchor_epoch: int
    capabilities: frozenset[str]


@dataclass(frozen=True)
class PeerPolicy:
    anchor_epoch: int
    sub_delegable: frozenset[str]


def authorize_action(
    warrant: Warrant,
    peer_policy: PeerPolicy,
    action: str,
    *,
    signature_valid: bool,
) -> None:
    # Signature validity is necessary but NOT sufficient: it says nothing about epoch.
    if not signature_valid:
        raise EpochMismatchError(
            f"warrant from {warrant.issuer} failed signature verification"
        )

    if warrant.anchor_epoch != peer_policy.anchor_epoch:
        relation = (
            "stale"
            if warrant.anchor_epoch < peer_policy.anchor_epoch
            else "ahead-or-unorderable"
        )
        raise EpochMismatchError(
            f"epoch mismatch ({relation}): warrant epoch={warrant.anchor_epoch} "
            f"peer current epoch={peer_policy.anchor_epoch}; "
            f"action={action!r} must not proceed on stale verification"
        )

    if action not in warrant.capabilities:
        raise EpochMismatchError(
            f"action {action!r} not in warrant capabilities {sorted(warrant.capabilities)}"
        )
    if action not in peer_policy.sub_delegable:
        raise EpochMismatchError(
            f"action {action!r} outside peer sub-delegable scope at epoch "
            f"{peer_policy.anchor_epoch}"
        )


def run_task_step(warrant: Warrant, peer_policy: PeerPolicy, action: str) -> None:
    # Re-fetch peer_policy at execution time; never reuse an intake-time snapshot.
    authorize_action(
        warrant, peer_policy, action, signature_valid=verify(warrant)
    )
    execute(action)


def verify(warrant: Warrant) -> bool:  # placeholder for real crypto check
    return True


def execute(action: str) -> None:  # placeholder for real tool call
    pass
