# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/where-must-an-instance-re-verify-a-federated-trust-anchor-after-a-policy-bounded-routing-hop-and-why-the-anchor-s-ttl-is-not-the-invariant-you-think-i.md
# Remnant Warrant:    https://corpusfetch.com/c/where-must-an-instance-re-verify-a-federated-trust-anchor-after-a-policy-bounded-routing-hop-and-why-the-anchor-s-ttl-is-not-the-invariant-you-think-i.warrant.json
# Markdown SHA-256:   cafea250d3a7ee3d619a58226e106d28a488fca69d7bde3c3c13435537229722
# Code AST SHA-256:   135c9e96669a53ee236f6f3b3e0cb728a74dbb4a9a601a9c9858354ae351db95
# Verified Classes:   StaleAnchorConsumptionError, Anchor, TerminalPolicy
# Defensive Guards:   4 fail-closed invariants (zero external deps)

import time
from dataclasses import dataclass


class StaleAnchorConsumptionError(RuntimeError):
    """Raised when a terminal instance would execute on a relay-validated
    anchor that is no longer current at the moment of side-effect."""
    pass


@dataclass(frozen=True)
class Anchor:
    anchor_id: str
    scope: str
    issued_at: float
    exp: float
    relay_validated_at: float
    relay_stamp: bool


@dataclass(frozen=True)
class TerminalPolicy:
    policy_version: int
    revocation_set: frozenset
    max_propagation_delay: float


def consume_anchor(anchor: Anchor, policy: TerminalPolicy, now: float) -> None:
    if not anchor.relay_stamp:
        raise StaleAnchorConsumptionError(
            f"anchor {anchor.anchor_id} lacks relay validation; refusing"
        )

    residual_ttl_at_hop = anchor.exp - anchor.relay_validated_at
    age_since_hop = now - anchor.relay_validated_at

    # Freshness bound: has the anchor outlived its residual TTL since the hop?
    if age_since_hop >= residual_ttl_at_hop:
        raise StaleAnchorConsumptionError(
            f"anchor {anchor.anchor_id} expired {age_since_hop - residual_ttl_at_hop:.1f}s "
            f"ago relative to hop validation; relay stamp is historical, not current"
        )

    # Policy-stability bound: if the anchor's TTL exceeds propagation delay,
    # the relay's policy snapshot cannot be assumed current at the terminal.
    total_ttl = anchor.exp - anchor.issued_at
    if total_ttl > policy.max_propagation_delay:
        if anchor.anchor_id in policy.revocation_set:
            raise StaleAnchorConsumptionError(
                f"anchor {anchor.anchor_id} revoked under terminal policy "
                f"v{policy.policy_version} but not visible to relay"
            )
        # Force re-verification path: caller must re-sign or re-scope.
        raise StaleAnchorConsumptionError(
            f"anchor {anchor.anchor_id} TTL {total_ttl:.0f}s exceeds propagation "
            f"delay {policy.max_propagation_delay:.0f}s; relay stamp insufficient, "
            f"re-verify against terminal policy v{policy.policy_version}"
        )

    # Only reachable when TTL <= propagation delay: stamp is structurally current.
    return None


def handle_dequeue(anchor: Anchor, policy: TerminalPolicy) -> None:
    consume_anchor(anchor, policy, now=time.time())
