Skip to content

The agent that grows with you

fix(a2a): preserve live waiters during orphan cleanup

fangliquanflqd4417088866c
diff --git a/plugins/platforms/a2a/adapter.py b/plugins/platforms/a2a/adapter.py
index 570aa6086cf..b8fc530fc6f 100644
--- a/plugins/platforms/a2a/adapter.py
+++ b/plugins/platforms/a2a/adapter.py
@@ -33,7 +33,7 @@ from . import protocol, security
logger = logging.getLogger(__name__)
_DEFAULT_PORT = 9900
-_ORPHAN_TIMEOUT, _WATCHDOG_INTERVAL = 300, 60 # seconds: pending task considered orphaned / watchdog period
+_MIN_ORPHAN_TIMEOUT, _WATCHDOG_INTERVAL = 300, 60 # seconds: orphan grace floor / watchdog period
_MAX_BODY = 1_048_576 # 1MB max request body — prevents DoS via memory exhaustion
_SSE_KEEPALIVE = 5 # seconds between SSE keepalive comments
_DEFAULT_DESCRIPTION = "Hermes Agent — a general-purpose agent reachable over A2A."
@@ -69,6 +69,11 @@ def _reply_timeout() -> float:
return 300.0
+def _orphan_timeout() -> float:
+ """Orphan grace must never expire before a configured reply window."""
+ return max(float(_MIN_ORPHAN_TIMEOUT), _reply_timeout())
+
+
def _default_agent_name() -> str:
# Scope-aware: a secondary multiplex profile must not borrow the default profile's A2A_AGENT_NAME.
name = _get_scoped_secret("A2A_AGENT_NAME", "").strip()
@@ -334,12 +339,21 @@ class A2AAdapter(BasePlatformAdapter):
"""Background thread that fails orphaned tasks (keeps them queryable)."""
while not self._watchdog_stop.wait(_WATCHDOG_INTERVAL):
try:
- for tid in self.tasks.fail_orphans(_ORPHAN_TIMEOUT):
- logger.warning("A2A: orphaned task %s marked failed (timeout %ds)", tid, _ORPHAN_TIMEOUT)
- protocol.metrics.tasks_failed += 1
+ self._fail_orphans_once()
except Exception:
logger.debug("A2A: watchdog error", exc_info=True)
+ def _fail_orphans_once(self) -> list[str]:
+ """Fail stale tasks that no HTTP/SSE request is still waiting for."""
+ with self._pending_lock:
+ live_waiters = set(self._pending)
+ timeout = _orphan_timeout()
+ failed = self.tasks.fail_orphans(timeout, exclude=live_waiters)
+ for tid in failed:
+ logger.warning("A2A: orphaned task %s marked failed (timeout %gs)", tid, timeout)
+ protocol.metrics.tasks_failed += 1
+ return failed
+
def _load_served_agents(self, extra: dict) -> dict[str, dict]:
"""Served-agent routing from ``platforms.a2a.extra.agents`` (top-level ``a2a_served_agents``
fallback for scripts/tests). Root/default always maps to the live gateway session."""
diff --git a/plugins/platforms/a2a/protocol.py b/plugins/platforms/a2a/protocol.py
index 707527a9a80..a4cbcfe083f 100644
--- a/plugins/platforms/a2a/protocol.py
+++ b/plugins/platforms/a2a/protocol.py
@@ -407,10 +407,12 @@ class TaskStore:
next_offset = offset + page_size if offset + page_size < total else 0
return (page, next_offset, total) if with_total else (page, next_offset)
- def fail_orphans(self, timeout_seconds: int = 300) -> list[str]:
+ def fail_orphans(self, timeout_seconds: float = 300, *, exclude: set[str] | None = None) -> list[str]:
+ excluded = exclude or set()
with self._lock:
stale = [tid for tid, rec in self._tasks.items()
- if rec["state"] not in TERMINAL_STATES and time.time() - rec["created_at"] > timeout_seconds]
+ if tid not in excluded and rec["state"] not in TERMINAL_STATES
+ and time.time() - rec["created_at"] > timeout_seconds]
return [tid for tid in stale if self.complete(tid, STATE_FAILED, "[task orphaned — no reply produced]")]
def _trim_locked(self) -> None:
diff --git a/tests/plugins/test_a2a_phase23.py b/tests/plugins/test_a2a_phase23.py
index 6453eef21b5..582f786cf75 100644
--- a/tests/plugins/test_a2a_phase23.py
+++ b/tests/plugins/test_a2a_phase23.py
@@ -499,6 +499,23 @@ class TestTaskStore:
# Second sweep does nothing (already terminal).
assert store.fail_orphans(timeout_seconds=300) == []
+ def test_watchdog_preserves_live_waiters_and_reply_window(self, monkeypatch):
+ monkeypatch.setenv("A2A_REPLY_TIMEOUT", "600")
+ adapter, _base = _make_live_adapter(monkeypatch)
+ now = time.time()
+ for task_id, age in (("t-live", 700), ("t-orphan", 700), ("t-within-reply-window", 400)):
+ adapter.tasks.create(task_id, "c1", "p")
+ adapter.tasks.set_state(task_id, protocol.STATE_WORKING)
+ adapter.tasks._tasks[task_id]["created_at"] = now - age
+
+ adapter._add_pending("t-live", "c1")
+ assert adapter._fail_orphans_once() == ["t-orphan"]
+ assert adapter.tasks.get("t-live")["state"] == protocol.STATE_WORKING
+ assert adapter.tasks.get("t-within-reply-window")["state"] == protocol.STATE_WORKING
+
+ adapter._pop_pending("t-live")
+ assert adapter._fail_orphans_once() == ["t-live"]
+
def test_list_newest_first_with_filters(self):
store = protocol.TaskStore()
store.create("t1", "c1", "p")