Skip to content

The agent that grows with you

fix(a2a): wait for final replies before resolving RPCs

David Robertson38318cec1ece
diff --git a/plugins/platforms/a2a/adapter.py b/plugins/platforms/a2a/adapter.py
index 9c5c5751c84..31ab0cc9ce1 100644
--- a/plugins/platforms/a2a/adapter.py
+++ b/plugins/platforms/a2a/adapter.py
@@ -85,7 +85,10 @@ class A2AAdapter(BasePlatformAdapter):
# ── Lifecycle ─────────────────────────────────────────────────────────
- async def connect(self) -> bool:
+ async def connect(self, **_kwargs) -> bool:
+ # Gateway reconnection plumbing passes adapter-agnostic kwargs such as
+ # ``is_reconnect``. A2A does not need them, but accepting them keeps the
+ # plugin compatible with the BasePlatformAdapter lifecycle contract.
# Capture the running gateway loop so the HTTP thread can marshal
# events onto it via run_coroutine_threadsafe.
try:
@@ -280,12 +283,21 @@ class A2AAdapter(BasePlatformAdapter):
``chat_id`` is the A2A context id we set as the source chat_id, so it
keys straight back to the blocked HTTP request.
+
+ The gateway marks final user-visible replies with ``metadata['notify']``.
+ Progress, status, and editable preview sends intentionally lack that
+ marker; those must not satisfy the JSON-RPC caller, or the caller sees
+ a banner/status update instead of the agent's actual answer.
"""
+ is_final_reply = bool((metadata or {}).get("notify"))
with self._pending_lock:
fut = self._pending_replies.get(chat_id)
- if fut is not None and not fut.done():
- fut.set_result(content or "")
- return SendResult(success=True, message_id=str(int(time.time() * 1000)))
+ if fut is not None and not fut.done():
+ if not is_final_reply:
+ logger.debug("A2A: ignoring non-final send for context %s", chat_id)
+ return SendResult(success=True, message_id=str(int(time.time() * 1000)))
+ fut.set_result(content or "")
+ return SendResult(success=True, message_id=str(int(time.time() * 1000)))
# No waiter (e.g. a late streamed chunk or out-of-band send) — drop it.
logger.debug("A2A: send() for context %s had no pending waiter", chat_id)
return SendResult(success=True, message_id=str(int(time.time() * 1000)))
diff --git a/tests/plugins/test_a2a_plugin.py b/tests/plugins/test_a2a_plugin.py
index 6567cb36a78..1e998ba45d8 100644
--- a/tests/plugins/test_a2a_plugin.py
+++ b/tests/plugins/test_a2a_plugin.py
@@ -8,6 +8,7 @@ http.server with a mocked agent handler.
from __future__ import annotations
import asyncio
+from concurrent.futures import Future
import json
import os
import tempfile
@@ -310,6 +311,45 @@ class TestRegistryDispatchConvention:
assert "PONG" in out
+# --------------------------------------------------------------------------
+# A2A reply capture
+# --------------------------------------------------------------------------
+
+class TestReplyCapture:
+ def test_send_waits_for_notify_marked_final_reply(self):
+ """Interim/editable sends must not satisfy the blocked A2A RPC future."""
+ from plugins.platforms.a2a.adapter import A2AAdapter
+ from gateway.config import PlatformConfig
+
+ adapter = A2AAdapter(PlatformConfig(enabled=True))
+ fut = Future()
+ with adapter._pending_lock:
+ adapter._pending_replies["ctx-final"] = fut
+
+ async def run():
+ interim = await adapter.send(
+ "ctx-final",
+ "⏩ Steered into current run (iteration 1/200).",
+ metadata={"expect_edits": True},
+ )
+ assert interim.success is True
+ assert fut.done() is False
+
+ final = await adapter.send(
+ "ctx-final",
+ "FINAL_PROOF_PAYLOAD",
+ metadata={"notify": True},
+ )
+ assert final.success is True
+ assert fut.result(timeout=0) == "FINAL_PROOF_PAYLOAD"
+
+ try:
+ asyncio.run(run())
+ finally:
+ with adapter._pending_lock:
+ adapter._pending_replies.pop("ctx-final", None)
+
+
# --------------------------------------------------------------------------
# End-to-end inbound round-trip (real http.server + mocked agent)
# --------------------------------------------------------------------------
@@ -340,7 +380,7 @@ class TestInboundRoundTrip:
# by resolving the pending future via the real send() path.
async def fake_handle_message(event):
# The reply path the gateway would normally drive.
- await adapter.send(event.source.chat_id, "ECHO: " + event.text)
+ await adapter.send(event.source.chat_id, "ECHO: " + event.text, metadata={"notify": True})
adapter.handle_message = fake_handle_message # type: ignore
adapter._message_handler = object() # non-None so dispatch proceeds
@@ -387,6 +427,29 @@ class TestInboundRoundTrip:
asyncio.run(run())
+ def test_connect_accepts_gateway_reconnect_kwarg(self, monkeypatch):
+ """Gateway reconnection passes is_reconnect=... to every adapter connect()."""
+ monkeypatch.setenv("A2A_BEARER_TOKEN", "topsecret")
+ monkeypatch.setenv("A2A_HOST", "127.0.0.1")
+
+ from plugins.platforms.a2a.adapter import A2AAdapter
+ from gateway.config import PlatformConfig
+ import socket
+
+ s = socket.socket()
+ s.bind(("127.0.0.1", 0))
+ port = s.getsockname()[1]
+ s.close()
+ monkeypatch.setenv("A2A_PORT", str(port))
+
+ adapter = A2AAdapter(PlatformConfig(enabled=True))
+
+ async def run():
+ assert await adapter.connect(is_reconnect=True) is True
+ await adapter.disconnect()
+
+ asyncio.run(run())
+
def test_auth_required_when_token_set(self, monkeypatch):
monkeypatch.setenv("A2A_BEARER_TOKEN", "topsecret")