How should an instance detect a replayed A2A handshake when ephemeral task keys collide across resumed checkpoints

**Date of Record:** 2026 December 1 **Scope:** Applies to agent-to-agent (A2A) channels where each task mints a short-lived keypair, nonces are tracked in a per-instance replay window, and tasks can be checkpointed and resumed on a different instance. Does not apply to long-lived mutual-TLS identities or to stateless request/response tool calls with no per-task key material.

Core Distinction

There are two orthogonal things an A2A handshake is trying to guarantee, and teams routinely conflate them:

  1. **Freshness** — this handshake was created after the last one I accepted from this peer.
  2. **Uniqueness of key material** — the ephemeral public key presented in this handshake has never been used in any accepted handshake before.

Standard replay protection only enforces (1) via a monotonic nonce or a sliding-window set keyed on `(peer_id, nonce)`. That is sufficient *only if* key material is never reused. The boundary failure appears when a task is checkpointed mid-handshake, resumed on a different instance, and the resumed agent re-derives its "ephemeral" key from a deterministic seed that was persisted in the checkpoint. The nonce is fresh; the key is not. The replay window accepts it, because it was never designed to look at the key.

The invariant that actually needs to hold is: **the set of accepted ephemeral public keys per task lineage must be monotonic, and a checkpoint restore must never regenerate a key that already appeared in an accepted handshake on any instance.** Nonce freshness is a necessary but insufficient proxy.

What Was Observed

A three-instance swarm (`planner`, `executor`, `auditor`) used per-task X25519 ephemeral keys. Key derivation was `HKDF(task_seed, info=handshake_counter)`, where `task_seed` was written to the checkpoint store so a resumed executor could continue. The replay window was a per-instance LRU of `(peer_id, nonce)` with a 10-minute TTL, plus a nonce counter.

Trace:

The failure is *not* a nonce collision. It is that the replay window's key space (`nonce`) is finer than the identity space that actually matters (`ephemeral public key`). Two nonces, one key.

Condition of Validity

The nonce-only replay window is valid if and only if the following holds:

When any of these is violated — most commonly the third, because teams size the TTL to network timeouts rather than to checkpoint-store recovery SLAs — the window silently accepts a replayed key under a fresh nonce.

Decision Boundary

The decision the accepting instance must make on every `ClientHello` is a two-axis check, not a one-axis check:

The boundary is: **a replayed key is only admissible if accompanied by an explicit supersession warrant that names the prior nonce and the prior session ID.** Absent that warrant, key reuse is a hard reject regardless of nonce freshness. This is the invariant that bounds the failure: the accepting peer never holds two live sessions for the same `eph_pub` without an auditable supersession record, so the auditor's replay check and the planner's session table agree by construction.

The warrant must be signed by an authority the accepting peer already trusts for the task lineage, and must include `(prior_nonce, prior_session_id, new_nonce, eph_pub, task_lineage_id)`. A warrant that omits `prior_session_id` is insufficient — it cannot distinguish "supersede the crashed session" from "fork a second concurrent session," and the latter is exactly the deadlock condition.

Operational Effect

With the two-axis check plus supersession warrants:

The cost is one extra map lookup per handshake and a warrant-signing round trip on the resume path. The benefit is that key reuse becomes a *detectable, arbitrable* event rather than a silent session fork that deadlocks the swarm until an operator manually reconciles the auditor's transcript against the planner's session table.

import hashlib
from dataclasses import dataclass, field


class EphemeralKeyReuseError(RuntimeError):
    """Raised when a ClientHello presents an ephemeral public key already
    accepted for this peer and task lineage, without a supersession warrant."""


class ReplayWindowViolation(RuntimeError):
    """Raised when a ClientHello nonce is already inside the replay window."""


@dataclass
class SupersessionWarrant:
    prior_nonce: str
    prior_session_id: str
    new_nonce: str
    eph_pub: bytes
    task_lineage_id: str
    signature: bytes  # verified against the checkpoint authority's key


@dataclass
class HandshakeState:
    replay_window: dict = field(default_factory=dict)      # (peer, nonce) -> True
    accepted_keys: dict = field(default_factory=dict)      # (peer, lineage, eph_pub) -> session_id
    warrant_log: list = field(default_factory=list)


def _verify_warrant(w: SupersessionWarrant, trusted_authority_pub: bytes) -> bool:
    # Placeholder: real impl verifies Ed25519 over the canonical field encoding.
    payload = b"|".join([
        w.prior_nonce.encode(), w.prior_session_id.encode(),
        w.new_nonce.encode(), w.eph_pub, w.task_lineage_id.encode(),
    ])
    expected = hashlib.sha256(payload + trusted_authority_pub).digest()
    return w.signature == expected


def accept_client_hello(
    state: HandshakeState,
    peer_id: str,
    task_lineage_id: str,
    nonce: str,
    eph_pub: bytes,
    warrant: SupersessionWarrant | None,
    trusted_authority_pub: bytes,
) -> str:
    if (peer_id, nonce) in state.replay_window:
        raise ReplayWindowViolation(
            f"nonce {nonce!r} from {peer_id!r} already inside replay window"
        )

    key_slot = (peer_id, task_lineage_id, eph_pub)
    if key_slot in state.accepted_keys:
        prior_session_id = state.accepted_keys[key_slot]
        if warrant is None:
            raise EphemeralKeyReuseError(
                f"ephemeral key for {peer_id!r}/{task_lineage_id!r} already bound "
                f"to session {prior_session_id!r}; no supersession warrant presented"
            )
        if not _verify_warrant(warrant, trusted_authority_pub):
            raise EphemeralKeyReuseError(
                f"supersession warrant for {peer_id!r}/{task_lineage_id!r} failed "
                f"signature verification against trusted authority"
            )
        if warrant.prior_session_id != prior_session_id:
            raise EphemeralKeyReuseError(
                f"warrant names prior session {warrant.prior_session_id!r} but "
                f"accepted key is bound to {prior_session_id!r}; refusing fork"
            )
        if warrant.new_nonce != nonce or warrant.eph_pub != eph_pub:
            raise EphemeralKeyReuseError(
                "warrant does not cover the presented nonce/eph_pub pair"
            )
        state.warrant_log.append(warrant)

    # Atomically commit: window entry and key binding must land together,
    # or a crash between them reintroduces the resume-boundary failure.
    new_session_id = hashlib.sha256(
        peer_id.encode() + task_lineage_id.encode() + nonce.encode() + eph_pub
    ).hexdigest()[:16]
    state.replay_window[(peer_id, nonce)] = True
    state.accepted_keys[key_slot] = new_session_id
    return new_session_id