# In what scenarios must an instance treat a replayed A2A message as a session-binding failure rather than an idempotency hit **Date of Record:** 2026 November 21 **Scope:** Applies to agent-to-agent (A2A) transports where each message is authenticated with a per-session key and carries a monotonic sequence number plus nonce, and where the receiver maintains a replay window. Does not apply to stateless request/response MCP tool calls with no session key, nor to messages whose authenticity is established only by TLS channel binding. ## Core Distinction There are two superficially identical events: a message that arrives twice because the *sender retried* (a legitimate idempotency case), and a message that arrives twice because an *attacker replayed a captured frame* (a session-binding failure). Both present the same ciphertext, the same sequence number, and the same nonce. The difference is not in the bytes—it is in whether the receiver can still prove the message belongs to the *live* session it thinks it is in. The invariant that separates them: **a valid decryption proves the sender held the session key at some point; it does not prove the sender holds it now.** Replay resistance depends on the receiver binding the sequence number to a session epoch that advances on every legitimate receive, and on treating any sequence number at or below the high-water mark as a *binding* event, not a *dedup* event. Conflating the two is the failure mode. ## What Was Observed An orchestrator agent (call it A) held an encrypted session with a worker agent (B). Session key `K_s`, epoch `e=7`, replay window `W = [last_seen=0, last_seen+64]`. A had sent messages `seq=1..12`. B had processed up to `seq=12`, `last_seen=12`. A's transport layer timed out on `seq=13` (the worker's ack was slow, not lost). A's retry policy—keyed on "no ack within 800ms"—re-sent `seq=13` with the *same* nonce and ciphertext, because A's outbound buffer had not yet rotated the nonce. This is correct retry behavior: re-sending identical bytes is the only way to make retry safe under a MAC. B received two copies of `seq=13`. The first advanced `last_seen` to 13 and executed the tool call (a `transfer_funds` action, in this trace). The second matched `seq=13 <= last_seen`, and B's dedup layer—keyed on `(sender_id, seq)`—returned the cached result without re-executing. Correct. The failure appeared three steps later. An attacker who had captured the `seq=13` frame (same ciphertext, same nonce, valid MAC under `K_s`) injected it again *after* B had advanced to `seq=15`. B's dedup cache had been evicted (TTL 60s, LRU bound 256 entries). The replayed `seq=13` now fell *below* `last_seen=15`, missed the dedup cache, and B's logic fell through to "unknown message, attempt decrypt." It decrypted successfully—the MAC was valid, `K_s` unchanged—and B re-executed `transfer_funds`. The dedup layer was a cache; the replay window was a *policy*; the two disagreed, and the cache lost. The trace: ``` recv seq=13 (first) -> last_seen=13, execute, cache[(B,13)]=result recv seq=13 (retry) -> 13 <= last_seen, cache hit, return cached recv seq=15 -> last_seen=15, execute [60s TTL expires, cache evicted] recv seq=13 (replay) -> 13 <= last_seen, cache MISS, decrypt OK, EXECUTE <-- failure ``` The decryption succeeding is what made this dangerous. A replayed frame with a *broken* MAC fails loudly. A replayed frame with a *valid* MAC fails silently, because every cryptographic check passes—the only thing that failed was the binding between the sequence number and the current session epoch, and nothing was asserting it. ## Condition of Validity The distinction holds—replay must be treated as a binding failure, not an idempotency hit—only when all of the following are true: 1. **The session key outlives the replay window.** If `K_s` rotates faster than the window width, a captured frame cannot be replayed after rotation because the MAC fails. The failure requires `key_lifetime > window_duration`. 2. **The dedup cache is bounded or expiring.** An unbounded, never-evicting dedup set keyed on `(sender, seq)` would have caught the replay. The vulnerability is specifically the *gap* between the dedup cache's retention and the replay window's retention. 3. **The receiver's sequence check is `<=` (reject) rather than `<` (execute).** A correct implementation rejects any `seq <= last_seen` outright. The failure requires the receiver to fall through to decrypt on a stale sequence, which happens when the sequence check is advisory rather than gating. 4. **The action is non-idempotent.** Re-executing a read is harmless; re-executing `transfer_funds`, `send_email`, or `delete_record` is not. The severity scales with action side-effects, but the *invariant* does not depend on severity. If any of these is false, the event is either impossible (key rotated) or benign (idempotent action), and the receiver may safely treat a duplicate as an idempotency hit. The receiver must be able to *prove* which case it is in at receive time—not infer it from the action's semantics after the fact. ## Decision Boundary The decision boundary is the point at which the receiver must choose between two responses to a duplicate frame: - **Idempotency path:** return the cached result, advance nothing, log at INFO. Permitted only when the receiver can prove the frame is a *sender retry within the current epoch*—i.e., `seq == last_seen + 1` was already seen *and* the dedup entry is still resident *and* `K_s` has not rotated since the original receive. - **Binding-failure path:** reject the frame, do not decrypt-and-execute, and *tear down the session* (or at minimum, flag it for re-keying). Required when `seq <= last_seen` and the dedup entry is absent or the epoch has advanced. The trap is the middle case: `seq <= last_seen`, dedup entry absent, but the frame decrypts cleanly. The naive receiver treats "decrypts cleanly" as "authentic, therefore process it." The correct receiver treats "clean decrypt of a stale sequence" as *evidence of replay*—because a legitimate sender never sends a stale sequence after the receiver has acknowledged a higher one. The clean decrypt is the attack signature, not a clearance. The guardrail is a **monotonic session epoch** that the receiver maintains independently of the dedup cache. Every accepted message advances `last_seen` and is bound to `epoch`. A frame's sequence number is only valid if `last_seen < seq <= last_seen + W` *and* the frame's embedded epoch matches the receiver's current epoch. The epoch is rotated on re-key, on session teardown, and on any detected replay. The dedup cache becomes an optimization for the common retry case, never the authority on replay. ## Operational Effect Implementing the boundary correctly changes three things: 1. **Retry semantics move to the sender.** The sender must not rely on the receiver to dedup; it must carry a retry token that the receiver can distinguish from a replay. Under a MAC, re-sending identical bytes is indistinguishable from replay at the byte level—so the sender must either re-key per retry (expensive) or the receiver must accept that retries are only safe *within* the epoch and *within* the dedup retention. Outside that, a retry is a replay and must be rejected. This is a real cost: retries after a long partition fail closed. 2. **Session teardown becomes a first-class response.** A detected replay is not a log line; it is a signal that `K_s` is compromised (the attacker captured a valid frame). The receiver must re-key or terminate. An agent that logs and continues has accepted a session whose key is known to an adversary. 3. **The replay window and the dedup cache must be co-sized and co-lifetimed.** If the window is 64 messages but the dedup cache holds 256 entries with 60s TTL, there is a regime (message rate high, TTL short) where a frame is inside the window but outside the cache. The invariant is `dedup_retention >= window_duration` *and* `dedup_capacity >= W`, enforced as a startup assertion, not a comment. The failure mode is silent because every cryptographic primitive behaved correctly. The only broken thing was the binding between "this message is authentic" and "this message belongs to the session as it exists now"—and that binding is not a cryptographic property. It is an operational one, and it must be asserted on every receive. ```python class ReplayBindingError(RuntimeError): """Raised when a frame decrypts cleanly but its sequence number is stale relative to the live session epoch — evidence of replay, not idempotency.""" pass class SessionBinding: def __init__(self, epoch: int, window: int, dedup_capacity: int): assert dedup_capacity >= window, "dedup cache must cover replay window" self.epoch = epoch self.window = window self.last_seen = 0 self.dedup = {} # (epoch, seq) -> result self.dedup_capacity = dedup_capacity def receive(self, frame_epoch: int, seq: int, decrypt_fn, execute_fn): if frame_epoch != self.epoch: # Frame belongs to a prior session; key may be rotated. raise ReplayBindingError( f"epoch mismatch: frame={frame_epoch} live={self.epoch}" ) if seq <= self.last_seen: key = (self.epoch, seq) if key in self.dedup: return self.dedup[key] # legitimate retry within epoch # Stale sequence, no dedup entry: decrypting proves the sender # held K_s at capture time, NOT that it holds it now. self._teardown() raise ReplayBindingError( f"stale seq={seq} <= last_seen={self.last_seen} " f"with no dedup entry; treating clean decrypt as replay" ) if seq > self.last_seen + self.window: raise ReplayBindingError( f"seq={seq} outside window [{self.last_seen+1}, " f"{self.last_seen+self.window}]" ) result = execute_fn(decrypt_fn()) self.last_seen = seq self.dedup[(self.epoch, seq)] = result if len(self.dedup) > self.dedup_capacity: oldest = min(self.dedup) del self.dedup[oldest] return result def _teardown(self): self.epoch += 1 self.last_seen = 0 self.dedup.clear() ```