Under what conditions must an instance treat a replayed A2A envelope as a live instruction rather than a stale artifact

**Date of Record:** 2026 December 2 **Scope:** Applies to any agent instance that receives task-bearing messages over an A2A channel where envelopes carry a monotonic sequence number, a nonce, a validity window, and a signature, and where the receiver maintains per-sender replay state. Does not apply to stateless request/response RPC where the caller is also the authoritative task owner, nor to channels where the transport layer guarantees exactly-once delivery across restarts.

Core Distinction

The failure mode is not "signature verification failed." Signature verification is the easy case: the envelope is rejected, the sender is notified, the instance continues. The failure mode is the case where the signature verifies, the nonce is fresh *relative to the receiver's persisted replay window*, the validity timestamp is still in the future, and the message is nonetheless a replay of a task the receiver already completed — because the receiver's replay-window state was rolled back, lost, or never durably committed across a restart or a failover.

The boundary that matters is the difference between **envelope freshness** and **task freshness**. A signed envelope proves the sender authorized *this byte sequence at some point*. It does not prove the sender authorizes *executing its effect now*. Those are separable, and the separation is exactly where replay protection leaks. An instance that conflates the two will re-execute a completed task — re-issuing a payment, re-running a destructive tool call, re-broadcasting a delegation — while every cryptographic check passes green.

What Was Observed

A coordinator instance `C` hands tasks to a worker swarm. Each task envelope from `C` carries: a `task_id`, a per-sender monotonic `seq`, a random 128-bit `nonce`, an `issued_at` / `expires_at` window, and an Ed25519 signature over the canonicalized body. Worker `W` persists `(sender_id, high_water_seq, seen_nonce_bloom)` to a local store and rejects any envelope whose `seq <= high_water_seq` or whose `nonce` is in the bloom filter.

The sequence of events that produced the incident:

  1. `W` receives task `seq=4411`, verifies signature, checks `4411 > 4410`, accepts. It begins a multi-tool execution (a database mutation followed by an outbound webhook).
  2. `W` crashes *after* the database mutation commits but *before* it durably writes `high_water_seq=4411` to the replay store. The two writes were not in the same transaction — the replay store was a separate embedded KV with its own flush cadence.
  3. `W` restarts. Its replay store reads back `high_water_seq=4410`. Its nonce bloom is also stale.
  4. `C`, having seen no ack within its retry budget, re-sends the *identical signed envelope* `seq=4411`. It is not malicious; its retry is legitimate at-least-once behavior.
  5. `W` re-verifies the signature (valid), checks `4411 > 4410` (true), checks the nonce (absent), checks `expires_at` (still 90 seconds in the future because the original window was generous). It accepts and re-executes.
  6. The database mutation runs twice. The webhook fires twice. `W`'s downstream auditor, comparing effect logs against task intents, flags a divergence it cannot reconcile because both executions are cryptographically attributable to a single signed envelope.

The signature was never wrong. The nonce was never reused by the sender. The validity window was never violated. Every individual guard held. The composition failed because **replay state and effect state were not co-committed**, and the envelope's freshness window was wide enough to outlive the crash-recovery gap.

Condition of Validity

The invariant that must hold for replay protection to be sound is stronger than "the receiver tracks a high-water mark." It is:

> For any envelope `E` whose effect has been applied, the receiver's durable state must reflect *both* the effect and the advancement of the replay window, atomically, before any further envelope from the same sender is evaluated.

Formally, acceptance of `E` requires that the tuple `(effect_committed, high_water_seq >= E.seq, E.nonce ∈ seen)` is written as one durable unit. If effect and replay-state can diverge across a crash boundary, then there exists a recovery state in which a valid envelope is both fresh (passes replay checks) and stale (its effect is already applied). That state is the failure condition.

Two corollaries bound it:

Decision Boundary

Given a verified envelope `E` from sender `S`, the receiver must resolve exactly one of four states, and the boundary between "accept" and "reject-as-replay" is defined by the *durable* replay state, never by an in-memory or best-effort one:

The decision boundary is therefore not "is this message authentic?" but "can I prove, from durable state alone, that this message's effect has not already been applied?" If the answer is no, the correct action is rejection, not optimistic re-execution.

Operational Effect

When the invariant holds — effect and replay-window co-committed, validity window bounded by recovery latency, fail-closed on state divergence — the operational consequence is that at-least-once delivery from the sender degrades safely to at-most-once effect at the receiver. Retries become idempotent by construction rather than by the receiver's hopeful guesswork. The cost is that a receiver recovering from a crash may reject legitimate in-flight envelopes whose replay state it cannot reconstruct, forcing the sender to re-issue with a fresh `seq` and nonce after the receiver signals readiness. That is a liveness cost paid deliberately to buy effect safety.

When the invariant is violated, the operational consequence is silent double-execution: effects that are individually signed, individually fresh, and individually valid, but collectively duplicated. Auditors comparing intent logs to effect logs will see two effects attributable to one envelope and have no protocol-level way to attribute the second to a replay versus a sender bug versus a receiver bug — because the envelope itself carries no evidence of which execution it authorized. The failure is invisible to every cryptographic check and visible only in the effect ledger, which is exactly the wrong place to discover it.

The guardrail is not a better signature scheme. It is the co-commitment of effect and replay state, and a fail-closed posture when that co-commitment cannot be proven.

import time
from dataclasses import dataclass


class ReplayStateInconsistent(RuntimeError):
    """Raised when durable replay state cannot be reconciled with the effect log."""
    pass


class EnvelopeReplayError(RuntimeError):
    """Raised when a verified envelope is a replay or has expired."""
    pass


@dataclass(frozen=True)
class Envelope:
    sender_id: str
    seq: int
    nonce: bytes
    issued_at: float
    expires_at: float
    signature_valid: bool


class DurableReplayStore:
    """Replay state must be co-committed with effects; here we model the read side."""

    def __init__(self, high_water: dict, seen_nonces: dict, effect_log: dict):
        self.high_water = high_water          # sender_id -> int
        self.seen_nonces = seen_nonces        # sender_id -> set[bytes]
        self.effect_log = effect_log          # (sender_id, seq) -> effect_id

    def durable_high_water(self, sender_id: str) -> int:
        return self.high_water.get(sender_id, 0)

    def nonce_seen(self, sender_id: str, nonce: bytes) -> bool:
        return nonce in self.seen_nonces.get(sender_id, set())

    def effect_applied(self, sender_id: str, seq: int) -> bool:
        return (sender_id, seq) in self.effect_log

    def consistent(self, sender_id: str) -> bool:
        # Invariant: every applied effect must be at or below the high-water mark.
        hw = self.durable_high_water(sender_id)
        for (sid, seq) in self.effect_log:
            if sid == sender_id and seq > hw:
                return False
        return True


def admit_envelope(env: Envelope, store: DurableReplayStore, now: float | None = None) -> str:
    """Return 'accept' or raise. Fails closed on any state uncertainty."""
    now = time.time() if now is None else now

    if not env.signature_valid:
        raise EnvelopeReplayError(f"signature invalid for seq={env.seq}")

    if not store.consistent(env.sender_id):
        # Cannot prove effect/replay co-commitment: refuse to execute anything.
        raise ReplayStateInconsistent(
            f"replay state diverges from effect log for sender={env.sender_id}"
        )

    if now >= env.expires_at:
        # Expiry is the only safe fallback when freshness is in doubt.
        raise EnvelopeReplayError(f"envelope seq={env.seq} expired at {env.expires_at}")

    if env.seq <= store.durable_high_water(env.sender_id):
        raise EnvelopeReplayError(
            f"seq={env.seq} <= high_water={store.durable_high_water(env.sender_id)}"
        )

    if store.nonce_seen(env.sender_id, env.nonce):
        raise EnvelopeReplayError(f"nonce already seen for seq={env.seq}")

    if store.effect_applied(env.sender_id, env.seq):
        # Effect exists but replay window did not advance: the exact failure state.
        raise ReplayStateInconsistent(
            f"effect for seq={env.seq} already applied but high_water not advanced"
        )

    return "accept"