The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(a2a): scope the client-tools A2A_PORT gate and prove A→B→A under multiplex (#122126, salvage #122128)
teknium175b6d946cd27
diff --git a/plugins/platforms/a2a/__init__.py b/plugins/platforms/a2a/__init__.pyindex 9b8213db7fc..14b62553eac 100644--- a/plugins/platforms/a2a/__init__.py+++ b/plugins/platforms/a2a/__init__.py@@ -33,11 +33,11 @@ def validate_config(config) -> bool:def is_connected(config) -> bool:- """'Connected' when explicitly enabled (the gateway only instantiates enabled platforms).+ """'Connected' when explicitly enabled or the profile's OWN scope carries ``A2A_PORT``.- ``A2A_PORT`` resolves through the shared scope-aware reader. A bare ``os.getenv`` hands every- secondary profile the DEFAULT profile's port under multiplexing, so each of them instantiates- an inbound server, the first one binds and every other profile dies with ``bind_failed``.+ Scoped read, never ``os.getenv``: under multiplexing os.environ is the launch profile's, so a raw+ read enabled an inbound server in every secondary; all fell back to the default port and N-1 died+ ``bind_failed`` (#122126)."""extra = getattr(config, "extra", {}) or {}if extra.get("enabled"):diff --git a/plugins/platforms/a2a/tools.py b/plugins/platforms/a2a/tools.pyindex c3c8512edba..923edfeb158 100644--- a/plugins/platforms/a2a/tools.py+++ b/plugins/platforms/a2a/tools.py@@ -7,13 +7,12 @@ from __future__ import annotationsimport contextlibimport jsonimport logging-import osimport urllib.errorimport urllib.requestfrom concurrent.futures import ThreadPoolExecutor, as_completedfrom typing import Any, Optional-from gateway.platforms._shared import coerce_port as _coerce_int+from gateway.platforms._shared import coerce_port as _coerce_int, get_scoped_secret as _get_scoped_secretfrom . import protocol, security@@ -342,7 +341,8 @@ def _a2a_tools_available() -> bool:if cfg.get("a2a_agents"):return Truetry:- if os.getenv("A2A_PORT"):+ # Scoped like the platform gate: os.environ is the launch profile's under multiplexing (#122126).+ if _get_scoped_secret("A2A_PORT"):return Truea2a_cfg = (cfg.get("platforms") or {}).get("a2a") or {}return bool(isinstance(a2a_cfg, dict) and a2a_cfg.get("enabled"))diff --git a/tests/plugins/test_a2a_plugin.py b/tests/plugins/test_a2a_plugin.pyindex ea66510c0f2..7e715908798 100644--- a/tests/plugins/test_a2a_plugin.py+++ b/tests/plugins/test_a2a_plugin.py@@ -40,39 +40,52 @@ def _free_port() -> int:# --------------------------------------------------------------------------class TestConnectionGateScope:- """``A2A_PORT`` decides whether the inbound server starts, so it must resolve through the- profile-scoped reader. A bare ``os.getenv`` hands every secondary profile the DEFAULT- profile's port under multiplexing: all of them instantiate, the first binds and the rest- die with ``bind_failed``."""-- def test_missing_scoped_port_is_not_connected(self, monkeypatch):- import hermes_cli.gateway as gw- from plugins.platforms import a2a-- seen: list[str] = []-- def fake_get_env_value(key: str):- seen.append(key)- return None # a secondary profile's own scope carries no A2A_PORT-- monkeypatch.setattr(gw, "get_env_value", fake_get_env_value)- monkeypatch.setattr(os, "getenv", lambda *a, **k: pytest.fail("raw os.getenv during enablement"))- assert a2a.is_connected(SimpleNamespace(extra={})) is False- assert seen == ["A2A_PORT"]-- def test_scoped_port_connects(self, monkeypatch):- import hermes_cli.gateway as gw+ """``A2A_PORT`` decides whether the inbound server starts, so it must resolve through the profile's+ own scope. A bare ``os.getenv`` handed every secondary profile the DEFAULT profile's port under+ multiplexing: all instantiated, the first bound, the rest died ``bind_failed`` (#122126)."""++ @staticmethod+ def _home(tmp_path, name: str, env: str):+ home = tmp_path / name+ home.mkdir()+ (home / ".env").write_text(env)+ return home++ def test_secondary_profile_never_borrows_launch_env_port(self, tmp_path, monkeypatch):+ """A→B→A under multiplex: profile A (own A2A_PORT) connects, profile B (none) does not even though+ os.environ carries the launch value, then A again. The tools gate follows the same scope."""+ import agent.secret_scope as ss+ import hermes_constantsfrom plugins.platforms import a2a- monkeypatch.setattr(gw, "get_env_value", lambda key: "9902" if key == "A2A_PORT" else None)- assert a2a.is_connected(SimpleNamespace(extra={})) is True+ monkeypatch.setenv("A2A_PORT", "9902") # the launch profile's value, bridged into os.environ+ home_a = self._home(tmp_path, "s6probe-a", "A2A_PORT=9911\n")+ home_b = self._home(tmp_path, "s6probe-b", "OTHER=1\n")+ prev = ss._MULTIPLEX_ACTIVE+ ss.set_multiplex_active(True)+ try:+ seen = []+ for home in (home_a, home_b, home_a):+ home_tok = hermes_constants.set_hermes_home_override(str(home))+ tok = ss.set_secret_scope(ss.build_profile_secret_scope(home), profile_home=str(home))+ try:+ seen.append((a2a.is_connected(SimpleNamespace(extra={})), tools._a2a_tools_available()))+ finally:+ ss.reset_secret_scope(tok)+ hermes_constants.reset_hermes_home_override(home_tok)+ finally:+ ss.set_multiplex_active(prev)+ assert seen == [(True, True), (False, False), (True, True)]- def test_explicit_extra_enabled_wins(self, monkeypatch):- import hermes_cli.gateway as gw+ def test_standalone_profile_reads_its_own_environ(self, monkeypatch):+ """T1 (no multiplex): os.environ IS the profile — A2A_PORT there enables; ``extra.enabled`` always wins."""from plugins.platforms import a2a- monkeypatch.setattr(gw, "get_env_value", lambda key: None)+ monkeypatch.delenv("A2A_PORT", raising=False)+ assert a2a.is_connected(SimpleNamespace(extra={})) is Falseassert a2a.is_connected(SimpleNamespace(extra={"enabled": True})) is True+ monkeypatch.setenv("A2A_PORT", "9902")+ assert a2a.is_connected(SimpleNamespace(extra={})) is True# --------------------------------------------------------------------------