Skip to main content

Module idempotency

Module idempotency 

Source
Expand description

Client-supplied idempotency keys for message/send, as a declared extension.

§The gap this closes

A send that fails ambiguously — the connection drops after the request bytes are on the wire — leaves the caller unable to tell whether a task exists. The client is right to refuse to retry: SendMessage is not idempotent, and is_idempotent_method in the client crate treats it as such, retrying only where the error proves the server rejected the request. The correctness of that rule is exactly what makes the gap bite, because the only recovery left is to list the tasks on a context and pattern-match message content to guess whether the send landed — heuristic, racy, and worse the more concurrent delegation a caller does.

With a key the server dedupes on, a retry is safe by construction: the second send returns the task the first one created, in whatever state it has reached, and “did that actually start?” stops being a class of bug.

§Wire shape

This is not part of A2A v1.0. It ships as an extension, identified by IDEMPOTENCY_EXTENSION_URI, so a server that implements it stays conformant and the TCK is unaffected.

Message::extensions is a list of extension URIs — it declares which extensions are in play and cannot carry a value. The key itself therefore travels in Message::metadata, under IDEMPOTENCY_METADATA_KEY:

{
  "extensions": ["https://a2a-rust.com/extensions/idempotency/v1"],
  "metadata": { "a2a-rust.com/idempotency-key": "8f14e45fceea167a..." }
}

§Choosing a key

A key must be unguessable. Within a tenant it is the handle to a task: anyone who can present it can reach the task it created. Generate it the way you would a session token — at least 16 bytes from a CSPRNG, rendered as hex or URL-safe base64 — and never derive it from message content, which would let one caller collide with another’s task by sending the same text. MIN_KEY_LEN rejects the shortest mistakes but cannot detect a low-entropy key that happens to be long.

A key is scoped to the tenant, not to the context: a retry of a send whose context_id the server assigned would otherwise mint a fresh context and miss the dedupe entirely, which is the very case this exists for.

Enums§

KeyError
Why a candidate key was refused.

Constants§

IDEMPOTENCY_EXTENSION_URI
URI identifying this extension on an agent card and in Message.extensions.
IDEMPOTENCY_METADATA_KEY
The Message.metadata key the idempotency key travels under.
KEYS_UNSUPPORTED_REASON
The ErrorInfo reason of the refusal.
MAX_KEY_LEN
Longest accepted key, in bytes, so a key cannot become an upload channel.
MIN_KEY_LEN
Shortest accepted key, in bytes.

Functions§

declares_extension
Whether the message declares this extension in Message.extensions.
key_of
Reads the idempotency key from a message, if it carries one.
keys_unsupported
The refusal: UnsupportedOperation, message as its text, and the typed detail.
refuses_keys
Whether error is that refusal: an UnsupportedOperation whose data holds the typed detail, as a list of details or as the one detail.
remove_key
Takes the key off message: its metadata entry, and the extension URI from extensions (an emptied extensions or metadata is left as None, as a message that never carried a key has it).
set_key
Sets the key on a message, declaring the extension URI alongside it.
validate_key
Checks a candidate key without needing a Message.