# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-verify-manifest-expiry-against-its-own-clock-before-trusting-a-federated-capability-claim.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-verify-manifest-expiry-against-its-own-clock-before-trusting-a-federated-capability-claim.warrant.json
# Markdown SHA-256:   243d796b6d9e8aadd1cacd6d83975f480953ef645f56a555110372cc52e9d758
# Code AST SHA-256:   f0b76be37f87f3c8c4db54b094888f05550796ac93a781b32179346dfbfbcb45
# Verified Classes:   ManifestTemporalViolation, SignedManifest
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class ManifestTemporalViolation(RuntimeError):
    """Raised when a federated capability manifest cannot be temporally trusted."""
    pass


@dataclass(frozen=True)
class SignedManifest:
    issuer: str
    capability: str
    not_before: float
    not_after: float
    signature_valid: bool
    relay_fresh_until: float | None = None


def verify_manifest_actionable(
    manifest: SignedManifest,
    consumer_now: float,
    consumer_skew_bound: float,
    expected_use_duration: float,
) -> None:
    if not manifest.signature_valid:
        raise ManifestTemporalViolation(
            f"signature invalid for {manifest.issuer}:{manifest.capability}"
        )

    # Relay advisories may only NARROW the window, never widen it.
    effective_not_after = manifest.not_after
    if manifest.relay_fresh_until is not None:
        effective_not_after = min(effective_not_after, manifest.relay_fresh_until)

    # Consumer-side temporal predicate against consumer-controlled clock.
    earliest_safe = manifest.not_before - consumer_skew_bound
    latest_safe = effective_not_after - consumer_skew_bound

    if consumer_now < earliest_safe:
        raise ManifestTemporalViolation(
            f"manifest not yet valid: now={consumer_now:.2f} "
            f"not_before={manifest.not_before:.2f} skew={consumer_skew_bound:.2f}"
        )

    if consumer_now > latest_safe:
        raise ManifestTemporalViolation(
            f"manifest expired under consumer clock: now={consumer_now:.2f} "
            f"effective_not_after={effective_not_after:.2f} skew={consumer_skew_bound:.2f}"
        )

    remaining_lifetime = latest_safe - consumer_now
    if remaining_lifetime < expected_use_duration:
        raise ManifestTemporalViolation(
            f"remaining lifetime {remaining_lifetime:.2f}s insufficient for "
            f"expected use {expected_use_duration:.2f}s on "
            f"{manifest.issuer}:{manifest.capability}"
        )


def resolve_and_invoke(manifest: SignedManifest, expected_use_duration: float) -> None:
    # consumer_now and skew bound are LOCAL facts; never sourced from the relay.
    verify_manifest_actionable(
        manifest=manifest,
        consumer_now=time.monotonic(),
        consumer_skew_bound=2.0,
        expected_use_duration=expected_use_duration,
    )
    # ... invoke peer with manifest.capability ...
