The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): split _adapter_for_source into intake and delivery seams
teknium1c70565ef30c1
diff --git a/gateway/authz_mixin.py b/gateway/authz_mixin.pyindex dc9a98af876..06996f673fc 100644--- a/gateway/authz_mixin.py+++ b/gateway/authz_mixin.py@@ -253,18 +253,53 @@ class GatewayAuthorizationMixin:except Exception:return False- def _adapter_for_source(self, source: Optional[SessionSource]):- """Resolve the live adapter for an inbound ``SessionSource``."""+ def _intake_adapter_for(self, source: Optional[SessionSource]):+ """The adapter that RECEIVED *source*'s event — the only one whose intake policy (ignored+ channels, relay fronting, re-dispatch of a still-live event) may act on it.++ Live provenance only: the registered transport that built the source, the process-level+ RelayAdapter for relay-delivered events, or the receiving bot named by a pinned+ ``RoutingIdentity`` (``transport_profile``) when its adapter reconnected. A source with none+ of these (restored row, hand-built) fails closed under multiplexing — nothing can say which+ bot admitted it, and guessing the runtime profile's bot is exactly the shared-bot /+ route-override confusion the identity exists to end. A standalone gateway owns one adapter+ per platform, so that adapter is the receiver by construction.+ """if source is None:return Noneowner = self._transport_owner(source)if owner is not None:return owner[0]- # Relay ingress keeps the underlying platform on the source, but delivery must use the one- # process-level RelayAdapter owning the connector socket; a profile-aware lookup would- # silently disable streaming/typing/tool progress.+ # Relay ingress keeps the underlying platform on the source, but the one process-level+ # RelayAdapter owns the connector socket; a profile-aware lookup would silently disable+ # streaming/typing/tool progress.if getattr(source, "delivered_via_upstream_relay", False) is True:return self._primary_adapters().get(Platform.RELAY)+ platform = getattr(source, "platform", None)+ from gateway.session_identity import identity_of+ identity = identity_of(source)+ if identity is not None and identity.multiplexed:+ return self._adapters_for_profile(identity.transport_profile).get(platform) if platform else None+ if not getattr(getattr(self, "config", None), "multiplex_profiles", False):+ return self._primary_adapters().get(platform) if platform else None+ return None++ def _delivery_adapter_for(self, source: Optional[SessionSource]):+ """The adapter that ANSWERS *source*: sends, edits, typing, progress, pickers, pending slots.++ The receiving bot whenever it is known (:meth:`_intake_adapter_for` — a routed event replies+ through the bot that received it, whether the runtime is a shared-bot satellite or a profile+ with a bot of its own). With no live provenance the unique owner of ``(platform,+ runtime_profile)`` delivers — ``_adapters_for_profile`` holds one adapter per platform per+ profile, a shared-bot satellite drains through the primary, and a disconnected secondary+ fails closed to ``{}`` rather than borrowing the default bot. Restored sessions, cron,+ kanban and completion notices all rely on this row. Matrix: gateway/AGENTS.md § Profile scope.+ """+ if source is None:+ return None+ adapter = self._intake_adapter_for(source)+ if adapter is not None:+ return adapter# ``getattr``: test fixtures build bare SimpleNamespace sources without ``profile``.return self._authorization_adapter(getattr(source, "platform", None), getattr(source, "profile", None))@@ -310,7 +345,9 @@ class GatewayAuthorizationMixin:return Path(stamped)if not getattr(getattr(self, "config", None), "multiplex_profiles", False):return None- adapter = self._adapter_for_source(source)+ # A restored/hand-built source is admitted by the bot that will answer it (auto-resume,+ # plugin injection) — the same unique-owner row delivery uses.+ adapter = self._delivery_adapter_for(source)if adapter is None:return None_registered, profile = self._owning_profile(adapter, getattr(source, "platform", None))@@ -390,7 +427,7 @@ class GatewayAuthorizationMixin:return per_profile[profile] if profile and profile in per_profile else getattr(self, "pairing_store", None)def _adapter_extra_for_source(self, source) -> dict:- return _adapter_config_extra(self._adapter_for_source(source))+ return _adapter_config_extra(self._delivery_adapter_for(source))def _own_policy_authorizes(self, source, user_id, is_group, adapter_profile) -> Optional[bool]:"""Own-policy adapter verdict when no env allowlist exists; None = no verdict.@@ -417,7 +454,7 @@ class GatewayAuthorizationMixin:def _adapter_extra_allowlist_authorizes(self, source, user_id, is_group) -> bool:"""Adapters (e.g. Telegram) that gate via config.extra.allow_from / group_allow_fromwithout setting enforces_own_access_policy."""- adapter = self._adapter_for_source(source)+ adapter = self._delivery_adapter_for(source)if adapter is None:return Falseextra = _adapter_config_extra(adapter)@@ -452,7 +489,7 @@ class GatewayAuthorizationMixin:"""adapter = resolved_ids = Nonewith contextlib.suppress(Exception):- adapter = self._adapter_for_source(source)+ adapter = self._delivery_adapter_for(source)resolver = getattr(adapter, "resolved_allowlist_user_ids", None)if callable(resolver):with contextlib.suppress(Exception):diff --git a/tests/gateway/test_multiplex_transport_matrix.py b/tests/gateway/test_multiplex_transport_matrix.pynew file mode 100644index 00000000000..fd2b7b27d27--- /dev/null+++ b/tests/gateway/test_multiplex_transport_matrix.py@@ -0,0 +1,156 @@+"""The intake / delivery transport matrix (#88715 phase 4; gateway/AGENTS.md § Profile scope).++Real ``GatewayRunner`` resolvers against a temp ``HERMES_HOME`` with three served profiles:+``default`` (shared bot), ``ops`` (satellite routed through the shared bot) and ``team_b`` (its own+bot). Every row is asserted in one fixture so the two historical fixes (#69246 shared transport+preserved, #70625 route-override egress) cannot drift apart again.+"""++import weakref+from types import SimpleNamespace+from unittest.mock import patch++import pytest++from gateway.config import GatewayConfig, Platform, PlatformConfig+from gateway.pairing import PairingStore+from gateway.platforms.base import BasePlatformAdapter+from gateway.profile_routing import parse_profile_routes+from gateway.session import SessionSource+from gateway.session_identity import resolve_identity+++class _Stub(BasePlatformAdapter):+ pass+++_Stub.__abstractmethods__ = frozenset()+++def _stub(platform, runner, label):+ adapter = _Stub.__new__(_Stub)+ adapter.platform, adapter.gateway_runner, adapter.label = platform, runner, label+ adapter.config = PlatformConfig(enabled=True, extra={})+ adapter._pending_messages, adapter._active_sessions = {}, {}+ return adapter+++def _runner(*, multiplex, routes=()):+ from gateway.run import GatewayRunner++ runner = object.__new__(GatewayRunner)+ runner.config = GatewayConfig(multiplex_profiles=multiplex)+ runner.config.platforms = {Platform.TELEGRAM: PlatformConfig(enabled=True, extra={})}+ runner.config.profile_routes = parse_profile_routes(list(routes))+ runner.pairing_store = PairingStore(profile="default")+ runner.pairing_stores = {}+ runner._primary_profile_name = "default"+ primary = _stub(Platform.TELEGRAM, runner, "PRIMARY")+ team_b = _stub(Platform.TELEGRAM, runner, "TEAM_B")+ team_b.set_owner_profile("team_b")+ runner.adapters = {Platform.TELEGRAM: primary}+ runner._profile_adapters = {"team_b": {Platform.TELEGRAM: team_b}, "ops": {}}+ return SimpleNamespace(runner=runner, primary=primary, team_b=team_b)+++def mux(tmp_path, monkeypatch):+ home = tmp_path / "hh"+ for name in ("ops", "team_b"):+ (home / "profiles" / name).mkdir(parents=True)+ monkeypatch.setenv("HERMES_HOME", str(home))+ served = [("default", home), ("ops", home / "profiles" / "ops"), ("team_b", home / "profiles" / "team_b")]+ rig = _runner(multiplex=True, routes=[+ # shared credential → satellite (#69246)+ {"name": "ops-dm", "platform": "telegram", "profile": "ops", "chat_id": "72719239"},+ # per-credential source → a profile that owns its own bot (#70625, decision D1)+ {"name": "team-b-via-shared", "platform": "telegram", "profile": "team_b", "chat_id": "555"},+ # secondary-owned transport → default (the inverse row)+ {"name": "default-via-team-b", "platform": "telegram", "profile": "default", "chat_id": "999",+ "bot_profile": "team_b"},+ ])+ rig.home = home+ with patch("hermes_cli.profiles.profiles_to_serve", return_value=served), \+ patch("hermes_cli.profiles.get_profile_dir",+ side_effect=lambda n: home if n == "default" else home / "profiles" / n), \+ patch("hermes_cli.profiles.profile_exists", return_value=True):+ yield rig+++def _live(rig, adapter, chat_id, **resolve_kwargs):+ source = adapter.build_source(chat_id=chat_id, chat_type="dm", user_id=chat_id)+ identity = resolve_identity(source, runner=rig.runner, **resolve_kwargs)+ return source, identity+++def test_live_rows_reply_through_the_receiving_bot_whatever_the_runtime(mux):+ """Rows 1–4 of the matrix: with live provenance, intake and delivery are BOTH the receiving+ adapter; only the runtime (profile, namespace, home) follows the route."""+ r = mux.runner+ # per-credential, no route: owner on every axis+ own, own_id = _live(mux, mux.team_b, "1", transport_profile="team_b")+ assert (own_id.transport_profile, own_id.runtime_profile) == ("team_b", "team_b")+ assert r._intake_adapter_for(own) is r._delivery_adapter_for(own) is mux.team_b++ # shared credential → satellite: runtime ops, transport stays the shared bot (#69246)+ sat, sat_id = _live(mux, mux.primary, "72719239")+ assert (sat_id.transport_profile, sat_id.runtime_profile) == ("default", "ops")+ assert r._intake_adapter_for(sat) is r._delivery_adapter_for(sat) is mux.primary+ assert r._session_key_for_source(sat) == "agent:ops:telegram:dm:72719239"++ # per-credential source routed to a profile that owns a bot: runtime team_b, reply via the+ # bot that received it — conversation continuity (D1), not the routed profile's bot (#70625).+ override, override_id = _live(mux, mux.primary, "555")+ assert (override_id.transport_profile, override_id.runtime_profile) == ("default", "team_b")+ assert override_id.runtime_home == mux.home / "profiles" / "team_b"+ assert r._intake_adapter_for(override) is r._delivery_adapter_for(override) is mux.primary++ # secondary-owned transport → default: agent:main key, default home, reply via the secondary bot+ inverse, inverse_id = _live(mux, mux.team_b, "999", transport_profile="team_b")+ assert (inverse_id.transport_profile, inverse_id.runtime_profile) == ("team_b", "default")+ assert inverse_id.runtime_home == mux.home and inverse_id.namespace == "agent:main"+ assert r._intake_adapter_for(inverse) is r._delivery_adapter_for(inverse) is mux.team_b+ assert mux.team_b._source_session_key(inverse) == "agent:main:telegram:dm:999"++ # Provenance survives a reconnect: the transport ref dies, the identity still names the bot.+ replacement = _stub(Platform.TELEGRAM, r, "PRIMARY2")+ r.adapters = {Platform.TELEGRAM: replacement}+ assert r._intake_adapter_for(sat) is r._delivery_adapter_for(sat) is replacement+++def test_restored_rows_fail_closed_on_intake_and_deliver_only_via_a_unique_owner(mux, tmp_path, monkeypatch):+ """Row 5: no live provenance → intake is ``None`` (nothing may re-dispatch or apply intake+ policy); delivery goes to the unique owner of ``(platform, runtime)`` — the primary for a+ shared-bot satellite, a secondary's own bot, ``None`` when that bot is down (never the default+ bot). Outside multiplexing the one adapter is the receiver by construction."""+ r = mux.runner++ def restored(profile):+ return SessionSource(platform=Platform.TELEGRAM, chat_id="42", chat_type="dm", user_id="42", profile=profile)++ for profile, owner in ((None, mux.primary), ("ops", mux.primary), ("team_b", mux.team_b)):+ assert r._intake_adapter_for(restored(profile)) is None, profile+ assert r._delivery_adapter_for(restored(profile)) is owner, profile+ # The admitting allowlist for a restored source is its delivering bot's (auto-resume, injection).+ assert r._authorization_home_for_source(restored("team_b")) == mux.home / "profiles" / "team_b"+ assert r._authorization_home_for_source(restored("ops")) == mux.home++ # team_b's bot failed to connect and is queued for reconnect: it owns a credential, so the+ # route that also names it through the shared bot must not turn it into a satellite.+ r._profile_adapters["team_b"] = {}+ r._profile_failed_platforms = {"team_b": {Platform.TELEGRAM: {}}}+ assert r._delivery_adapter_for(restored("team_b")) is None+ assert r._authorization_home_for_source(restored("team_b")) is None++ # A dead transport ref with no identity is a restored row too, never a lookup by runtime.+ stale = restored("ops")+ stale._transport_adapter_ref = weakref.ref(_stub(Platform.TELEGRAM, r, "GONE"))+ assert r._intake_adapter_for(stale) is None and r._delivery_adapter_for(stale) is mux.primary++ solo_home = tmp_path / "solo"+ solo_home.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(solo_home))+ solo = _runner(multiplex=False)+ bare = SessionSource(platform=Platform.TELEGRAM, chat_id="42", chat_type="dm", user_id="42")+ assert solo.runner._intake_adapter_for(bare) is solo.runner._delivery_adapter_for(bare) is solo.primary+ assert solo.runner._intake_adapter_for(None) is None and solo.runner._delivery_adapter_for(None) is None