# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-re-verify-a-federated-agent-manifest-when-the-delegated-warrant-outlives-the-signing-key.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-re-verify-a-federated-agent-manifest-when-the-delegated-warrant-outlives-the-signing-key.warrant.json
# Markdown SHA-256:   2462752d5cbcea42531f554c15c237977d20f0af53f12390231929d98d689360
# Code AST SHA-256:   16a2414ac594728636903229d11f03ebd78b393fbf78f33e91fc3f7d8343cf39
# Verified Classes:   StaleWarrantError, SignedManifest, FederationConsumer
# Defensive Guards:   5 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass, field
from typing import Optional


class StaleWarrantError(RuntimeError):
    """Raised when a consequential action is attempted under a warrant whose
    issuer liveness has not been re-verified within the admissible horizon."""


@dataclass
class SignedManifest:
    agent_id: str
    issuer_key_id: str
    warrant_issued_at: float
    warrant_expires_at: float
    capabilities: tuple[str, ...]
    signature_valid: bool
    last_liveness_check: Optional[float] = None
    issuer_key_revoked: bool = False


@dataclass
class FederationConsumer:
    revocation_poll_interval: float
    trust_roots: set[str] = field(default_factory=set)

    def _poll_revocation(self, issuer_key_id: str) -> bool:
        # In production: query issuer CRL / OCSP-equivalent endpoint.
        # Returns True if the key is currently revoked.
        return False

    def authorize_action(self, manifest: SignedManifest, action: str) -> None:
        now = time.time()

        # Ingress invariant: signature and chain must be valid.
        if not manifest.signature_valid:
            raise StaleWarrantError(
                f"manifest for {manifest.agent_id} has invalid signature"
            )
        if manifest.issuer_key_id not in self.trust_roots:
            raise StaleWarrantError(
                f"issuer key {manifest.issuer_key_id} not in current trust roots"
            )

        # Warrant must not be expired.
        if now >= manifest.warrant_expires_at:
            raise StaleWarrantError(
                f"warrant for {manifest.agent_id} expired at "
                f"{manifest.warrant_expires_at}"
            )

        # Action-time invariant: liveness must be fresh within the
        # min(remaining warrant, poll interval) horizon.
        remaining = manifest.warrant_expires_at - now
        horizon = min(remaining, self.revocation_poll_interval)
        last = manifest.last_liveness_check
        if last is None or (now - last) > horizon:
            revoked = self._poll_revocation(manifest.issuer_key_id)
            manifest.issuer_key_revoked = revoked
            manifest.last_liveness_check = now
            if revoked:
                raise StaleWarrantError(
                    f"issuer key {manifest.issuer_key_id} revoked; "
                    f"cached warrant for {manifest.agent_id} is void"
                )

        # Capability must cover the action.
        if action not in manifest.capabilities:
            raise StaleWarrantError(
                f"agent {manifest.agent_id} lacks capability {action!r}"
            )


def commit_consequential_action(
    consumer: FederationConsumer,
    manifest: SignedManifest,
    action: str,
) -> None:
    consumer.authorize_action(manifest, action)
    # ... commit the effect under the now-verified-live warrant ...
