# What happens when an instance replays a signed prompt envelope after its nonce window collapses under clock skew **Date of Record:** 2026 November 23 **Scope:** Applies to any agent-to-agent (A2A) or MCP-adjacent transport where messages carry a signed provenance envelope (payload hash + nonce + issued-at + TTL) and the receiver enforces replay resistance via a nonce cache with a bounded retention window. Does not apply to single-instance, in-process tool calls, nor to transports where the receiver has no independent clock (e.g., pure request/response inside one host process). ## Core Distinction The distinction that matters is between **replay resistance** and **provenance validity**. A signed envelope answers two questions independently: *did this specific principal author this exact payload* (provenance, verified by signature over the payload hash) and *is this the first time I have seen this envelope* (replay resistance, verified by nonce uniqueness inside a retention window). Teams routinely conflate these into one "is the message valid" gate, and the conflation is invisible until the nonce window and the signature TTL fall out of alignment under clock skew. When the receiver's nonce-cache eviction horizon is shorter than the signature's `not_after` minus the sender's `issued_at`, there exists a band — bounded, deterministic, and reachable without any attacker — in which a legitimately signed, still-unexpired envelope has *already been evicted from the nonce cache* and will be accepted a second time as fresh. The envelope is authentic and the replay succeeds. That is the boundary. ## What Was Observed A two-agent pipeline: an orchestrator (`orch-a`) emits signed task envelopes to a worker (`worker-b`) over an async queue with at-least-once delivery. Envelope schema carried `issued_at`, `not_after = issued_at + 90s`, a 128-bit `nonce`, and an Ed25519 signature over `(payload_hash ‖ nonce ‖ issued_at ‖ not_after)`. The worker kept a nonce cache with a fixed `EVICT_AFTER = 60s`, evicting by *receiver-local* insertion time. The failure trace, reproduced under controlled skew: 1. `orch-a` signs envelope `E` at local `T=0`, `not_after = T+90s`. Nonce `n_E` is fresh. 2. Queue redelivers `E` with a 70s delay (a consumer-group rebalance, not malice). At `T=70s` the worker first processes `E`, inserts `n_E` into the cache at receiver-local insertion time `t=0` (its own clock), and accepts. 3. The worker's nonce cache evicts `n_E` at receiver-local `t=60s`, i.e. sender-time `T≈130s` — **but** the envelope's `not_after` is `T+90s = 90s`. The eviction horizon (60s) is *shorter* than the signature validity window (90s). There is a 30s band where the nonce is gone but the signature is still valid. 4. A second redelivery of `E` at sender-time `T=80s` arrives. Signature check passes (`80s < 90s`). Nonce lookup misses (evicted at step 3). The worker accepts `E` a second time and executes the task twice. The double execution was idempotent for a read-only task and was not idempotent for a state-mutating one (a ledger debit), producing a duplicate side effect. Nothing in the logs looked like an attack; the signature was valid on both deliveries, and the worker's own instrumentation reported "signature verified, nonce fresh" for both. The bug is not in the crypto and not in the cache — it is in the *relationship* between two independent time constants that no single component owns. The subtler variant: if the worker uses *sender-supplied* `issued_at` to age out nonces (a common "fix" to avoid receiver-clock drift), then a replay that re-uses the original `issued_at` never ages out at all, because the age is computed from a field the replayer controls. The fix for skew reintroduces unbounded replay. This is the trap: the obvious correction to the first failure is the direct cause of a worse one. ## Condition of Validity The invariant that bounds the failure is a strict ordering between three horizons, enforced at the receiver and *asserted at admission time*, not assumed: ``` nonce_retention ≥ (max_signature_lifetime) + (max_acceptable_transport_delay) + (max_clock_skew) ``` Concretely: the nonce cache must retain a nonce for at least as long as *any* envelope bearing that nonce could still pass the signature check, *plus* the worst-case delivery delay that could push an envelope past the sender's `not_after` boundary and back, *plus* the maximum tolerated skew between sender and receiver clocks. If any term is zero-by-assumption rather than measured, the invariant is unverified and the band exists. The condition of validity is therefore not "the signature verifies" and not "the nonce is unseen." It is: **the nonce-retention horizon dominates the maximum possible signature-validity horizon under all admitted clock and transport conditions.** This is a liveness-vs-safety trade made explicit: you cannot have both a short nonce cache (memory) and a long signature TTL (tolerance for slow transports) without the gap. Pick the retention to dominate, or shrink the TTL to be dominated — but the two constants must be *coupled by an assertion*, not set independently by different teams. A second, independent condition: the receiver must reject any envelope whose `issued_at` is in the receiver's future beyond `max_clock_skew`, and must reject any envelope whose `not_after − issued_at` exceeds the receiver's configured `max_signature_lifetime`. Without the first, a sender with a fast clock mints envelopes that are "not yet valid" but will be accepted later, widening the band. Without the second, a sender can self-issue a 24-hour TTL that silently blows past any sane nonce retention. ## Decision Boundary The decision the receiver must make on every envelope is a three-way, not a two-way: - **Accept** — signature valid *and* `now ∈ [issued_at − skew, not_after + skew]` *and* nonce present in cache as *unseen*. - **Reject-as-expired** — signature valid but `now > not_after + skew`. Terminal; do not retry; do not re-admit. - **Reject-as-replay** — signature valid, within window, but nonce *seen* in cache. Terminal for this envelope identity. The boundary that fails is the fourth case the two-way gate cannot express: **signature valid, within window, nonce *absent from cache but previously seen***. A two-way gate (`valid = sig_ok && nonce_unseen`) collapses this into "accept," because absence-from-cache is indistinguishable from never-seen. The decision boundary is precisely the point where "not in cache" must be disambiguated from "evicted." The only sound disambiguation is structural: make eviction impossible while the envelope could still be valid (the retention invariant), or make the cache *monotonic* — never evict a nonce whose envelope's `not_after` has not yet passed, evicting instead by `max(not_after)` across all cached nonces rather than by insertion time. The latter is the correct fix: evict by the envelope's own expiry, not by wall-clock insertion, so the cache horizon is *derived from the data* rather than from an independent constant that can drift out of alignment. ## Operational Effect The operational consequence is that replay resistance in agent transports is a *time-coupling* property, and time-coupling properties fail silently and asymmetrically. They fail silently because both the signature check and the nonce check report success independently on the replayed message; the failure is only visible in the *side effect* (duplicate execution), which for idempotent tasks is invisible and for mutating tasks is a data-integrity incident. They fail asymmetrically because the fix for clock skew (aging by sender `issued_at`) trades a bounded replay band for an unbounded one, and the fix for memory pressure (shortening retention) trades a rare double-execution for a frequent one — both look like improvements in isolation. The guardrail is to make the coupling an *admission-time assertion*, not a config comment. On startup and on every config reload, the receiver asserts `nonce_retention ≥ max_signature_lifetime + max_transport_delay + max_clock_skew` and refuses to serve if violated. It also refuses to accept any envelope whose declared lifetime exceeds `max_signature_lifetime`, so a sender cannot widen the band unilaterally. Finally, it ages nonces by the envelope's `not_after` (data-derived) rather than by insertion time (clock-derived), so the cache horizon tracks the actual validity horizon even under skew. With these three, the "evicted but still valid" state is unreachable by construction: a nonce cannot leave the cache while its envelope can still pass the signature gate, and the two horizons are asserted to be ordered rather than assumed to be. The residual risk is transport delay exceeding `max_transport_delay`, which is why that term must be *measured* from the queue's p99 redelivery latency, not guessed — an at-least-once queue with a rebalance storm can push redelivery past any static bound, and the invariant must fail closed (reject, not accept) when the delay term is breached, even at the cost of dropping a legitimately fresh message. Dropping a fresh message is recoverable by the sender; double-executing a mutating one is not. ```python import time from dataclasses import dataclass class EnvelopeAdmissionError(RuntimeError): """Raised when an envelope cannot be soundly admitted or rejected.""" @dataclass(frozen=True) class Envelope: payload_hash: str nonce: str issued_at: float not_after: float signature: str class ReplayResistantReceiver: def __init__( self, max_signature_lifetime: float, max_transport_delay: float, max_clock_skew: float, ) -> None: self.max_signature_lifetime = max_signature_lifetime self.max_transport_delay = max_transport_delay self.max_clock_skew = max_clock_skew # Retention must dominate the maximum possible validity horizon. self.nonce_retention = ( max_signature_lifetime + max_transport_delay + max_clock_skew ) # nonce -> the not_after of the envelope that carried it. self._seen: dict[str, float] = {} self._assert_invariant() def _assert_invariant(self) -> None: required = ( self.max_signature_lifetime + self.max_transport_delay + self.max_clock_skew ) if self.nonce_retention < required: raise EnvelopeAdmissionError( f"nonce_retention={self.nonce_retention} does not dominate " f"required horizon={required}; replay band is open" ) def _evict_expired(self, now: float) -> None: # Evict by the envelope's own expiry, not by insertion time, # so the cache horizon tracks the true validity horizon. expired = [n for n, na in self._seen.items() if na + self.max_clock_skew < now] for n in expired: del self._seen[n] def admit(self, env: Envelope, now: float, sig_ok: bool) -> str: if not sig_ok: raise EnvelopeAdmissionError("signature verification failed") declared_lifetime = env.not_after - env.issued_at if declared_lifetime <= 0 or declared_lifetime > self.max_signature_lifetime: raise EnvelopeAdmissionError( f"declared lifetime {declared_lifetime} outside " f"(0, {self.max_signature_lifetime}]" ) if env.issued_at > now + self.max_clock_skew: raise EnvelopeAdmissionError("envelope issued in the future beyond skew") if now > env.not_after + self.max_clock_skew: return "reject-expired" self._evict_expired(now) if env.nonce in self._seen: return "reject-replay" # Record with the envelope's not_after so retention is data-derived. self._seen[env.nonce] = env.not_after return "accept" def _demo() -> None: rx = ReplayResistantReceiver( max_signature_lifetime=90.0, max_transport_delay=10.0, max_clock_skew=5.0, ) env = Envelope("h", "n1", issued_at=0.0, not_after=90.0, signature="sig") assert rx.admit(env, now=0.0, sig_ok=True) == "accept" # Same envelope replayed within the window: must be caught. if rx.admit(env, now=80.0, sig_ok=True) != "reject-replay": raise EnvelopeAdmissionError("replay was admitted; retention invariant broken") if __name__ == "__main__": _demo() ```