The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(relay): admit connector-stamped profiles instead of acking and dropping them
unsupportedpastels9cf51056c52f
diff --git a/gateway/relay/inbound_admission.py b/gateway/relay/inbound_admission.pyindex 95c71a4f154..b1c631843df 100644--- a/gateway/relay/inbound_admission.py+++ b/gateway/relay/inbound_admission.py@@ -45,6 +45,10 @@ async def _admit(adapter, runner, event):from gateway.parked_route_refusal import ParkedProfileRefusal, refuse_parked_routefrom gateway.platforms.webhook_ingress import admit_producerfrom gateway.relay.adapter import _PERMANENT_REFUSALS+ if event.source.profile and not getattr(runner.config, 'multiplex_profiles', False):+ # A single-profile gateway serves the connector's profile route from its one profile;+ # ``_capture_scope`` already kept the stamp for egress (persona attribution).+ event.source.profile = Nonetry:receipt = await admit_producer(adapter, event)except ParkedProfileRefusal as exc:diff --git a/gateway/session_ingress_context.py b/gateway/session_ingress_context.pyindex f75f9edfb8f..5c5b52d06c4 100644--- a/gateway/session_ingress_context.py+++ b/gateway/session_ingress_context.py@@ -102,6 +102,33 @@ def carry_callback(event, rewritten):_PREFIX_PRODUCERS = ('webhook', 'api_server', 'msgraph_webhook')+def _runtime_profile(runner, source, profile, owner_name, homes, relay):+ """The multiplexed runtime profile *source* routes to; ``_binding`` checks it is served."""+ # Same matcher as ingress stamping, ``user_id`` included: a ``user_id`` route stamps the+ # source upstream, so it must resolve here too. A route applies only to its bot_profile's+ # bot (#104933), so the receiving adapter's owner is part of the match.+ matches = [route for route in runner.config.profile_routes if route.matches(+ source.platform.value, guild_id=source.scope_id, chat_id=source.chat_id,+ thread_id=source.thread_id, parent_chat_id=source.parent_chat_id,+ adapter_profile=profile, user_id=source.user_id)]+ if source.profile and source.platform.value in _PREFIX_PRODUCERS:+ # ``/p/<profile>/`` producers address a profile explicitly on the HOST's listener; that+ # URL prefix names the profile and wins over ``profile_routes`` (webhook.py /+ # api_server.py stamp it; a bare producer source carries the routed name itself).+ return source.profile+ if matches:+ rank = max(route.specificity for route in matches)+ best = [route for route in matches if route.specificity == rank]+ if len(best) != 1:+ raise RuntimeStoreError('admission_conflict')+ return best[0].profile+ if relay and profile is None and source.profile in homes:+ # The instance's one authenticated connector (relay is process-level) routes scopes to+ # served profiles (Team Gateway); the relay seal covers ``source.profile``.+ return source.profile+ return owner_name++def _binding(runner, source, profile):registries = [(None, runner.adapters), *getattr(runner, '_profile_adapters', {}).items()]from gateway.config import Platform@@ -140,26 +167,7 @@ def _binding(runner, source, profile):owner_name = profile or primaryif homes.get(owner_name) != home:raise RuntimeStoreError('profile_mismatch')- # Same matcher as ingress stamping, ``user_id`` included: a ``user_id`` route stamps the- # source upstream, so it must resolve here too. A route applies only to its bot_profile's- # bot (#104933), so the receiving adapter's owner is part of the match.- matches = [route for route in runner.config.profile_routes if route.matches(- source.platform.value, guild_id=source.scope_id, chat_id=source.chat_id,- thread_id=source.thread_id, parent_chat_id=source.parent_chat_id,- adapter_profile=profile, user_id=source.user_id)]- if source.profile and source.platform.value in _PREFIX_PRODUCERS:- # ``/p/<profile>/`` producers address a profile explicitly on the HOST's listener; that- # URL prefix names the profile and wins over ``profile_routes`` (webhook.py /- # api_server.py stamp it; a bare producer source carries the routed name itself).- runtime_profile = source.profile- elif matches:- rank = max(route.specificity for route in matches)- best = [route for route in matches if route.specificity == rank]- if len(best) != 1:- raise RuntimeStoreError('admission_conflict')- runtime_profile = best[0].profile- else:- runtime_profile = owner_name+ runtime_profile = _runtime_profile(runner, source, profile, owner_name, homes, relay)if (source.profile or primary) != runtime_profile or runtime_profile not in homes:raise RuntimeStoreError('profile_mismatch')runtime_home = homes[runtime_profile]diff --git a/tests/gateway/relay/test_relay_connector_profile_admission.py b/tests/gateway/relay/test_relay_connector_profile_admission.pynew file mode 100644index 00000000000..9a97cb6b6a2--- /dev/null+++ b/tests/gateway/relay/test_relay_connector_profile_admission.py@@ -0,0 +1,212 @@+"""A connector-stamped ``source.profile`` is a route, on single-profile and multiplexed gateways.++The connector (Team Gateway per-scope profile routing) stamps the Hermes profile that serves an+inbound event. A single-profile gateway serves it from its one profile (the stamp is not a route+there, as before shared session authority, and never reaches durable state); a multiplexed gateway admits it into that profile's+own ledger. Neither may acknowledge the frame and drop it. An unserved stamped profile is refused+the way an unserved profile route is: acknowledged, never run.+"""+import asyncio+import json+from contextlib import asynccontextmanager+from pathlib import Path++import pytest++from gateway.config import GatewayConfig, Platform, PlatformConfig+from gateway.run import GatewayRunner+from gateway.session_results import execution_result+from tests.waiting import async_wait_until++websockets = pytest.importorskip('websockets')++SOURCE = {'platform': 'telegram', 'chat_type': 'dm'}+++@asynccontextmanager+async def _relay(runner, platforms=('telegram',)):+ """A real WebSocketRelayTransport + RelayAdapter wired as ``runner``'s primary relay, against a+ fake connector that acks every outbound. Yields ``(send, outgoing, adapter)``."""+ from gateway.relay.adapter import RelayAdapter+ from gateway.relay.descriptor import CapabilityDescriptor+ from gateway.relay.ws_transport import WebSocketRelayTransport++ outgoing, ready = [], asyncio.Queue()+ descriptor = CapabilityDescriptor(1, platforms[0], 'Fixture', 4096, False, True, False, 'plain', 'chars')++ async def connector(ws):+ await ws.recv() # hello+ await ws.send(json.dumps({'type': 'descriptor', 'descriptor': json.loads(descriptor.to_json())}) + '\n')+ await ready.put(ws)+ async for line in ws:+ frame = json.loads(line)+ outgoing.append(frame)+ if frame['type'] == 'outbound':+ await ws.send(json.dumps({'type': 'outbound_result', 'requestId': frame['requestId'],+ 'result': {'success': True, 'message_id': 'sent'}}) + '\n')+ elif frame['type'] == 'going_idle':+ await ws.send(json.dumps({'type': 'going_idle_ack'}) + '\n')++ async with websockets.serve(connector, '127.0.0.1', 0) as server:+ transport = WebSocketRelayTransport(+ f'ws://127.0.0.1:{server.sockets[0].getsockname()[1]}/relay', platforms[0], 'fixture-bot',+ gateway_id='fixture-gateway', upgrade_secret='fixture-secret', reconnect=False)+ adapter = RelayAdapter(PlatformConfig(enabled=True), descriptor, transport)+ adapter.gateway_runner = runner+ runner.adapters[Platform.RELAY] = adapter+ runner._wire_adapter_handlers(adapter)+ try:+ assert await adapter.connect()+ ws = await asyncio.wait_for(ready.get(), 5)++ async def send(message_id, chat_id, profile=None, text=None):+ source = {**SOURCE, 'chat_id': chat_id, 'user_id': 'u-' + chat_id}+ if profile is not None:+ source['profile'] = profile+ event = {'text': text or f'hello {message_id}', 'message_id': message_id, 'source': source}+ await ws.send(json.dumps({'type': 'inbound', 'event': event, 'bufferId': 'buf-' + message_id}) + '\n')++ yield send, outgoing, adapter+ finally:+ await adapter.disconnect()+++def _stub_turns(runner, monkeypatch):+ """Record each executed turn's (text, runtime home, session key) instead of calling a model."""+ from hermes_constants import get_hermes_home+ turns = []++ async def turn(event, *_args, **_kwargs):+ turns.append((event.text, Path(get_hermes_home()).resolve(),+ runner._session_key_for_source(event.source)))+ execution_result.get()['result'] = {'final_response': 'reply ' + event.text, 'messages': []}+ return 'reply ' + event.text++ monkeypatch.setattr(runner, '_handle_message_with_agent', turn)+ return turns+++def _acked(outgoing):+ return {f.get('bufferId') for f in outgoing if f.get('type') == 'inbound_ack'}+++def _replies(outgoing, chat_id):+ return [f['action'] for f in outgoing if f.get('type') == 'outbound'+ and f['action'].get('op') in ('send', 'edit') and f['action'].get('chat_id') == chat_id]+++def _requests(authority):+ from hermes_state_runtime import list_session_admissions+ rows = []+ for sid in list(authority.sessions):+ rows += list_session_admissions(authority.db, session_id=sid, pending_only=False)+ return rows+++async def test_single_profile_gateway_serves_a_connector_stamped_profile(tmp_path, monkeypatch):+ from gateway.session_authority import initialize_session_authority+ from hermes_constants import get_hermes_home+ launch = Path(get_hermes_home()).resolve()+ # A profile of the stamped name exists on disk: the single-profile gateway must still not use it.+ (launch / 'profiles' / 'work').mkdir(parents=True, exist_ok=True)+ runner = GatewayRunner(GatewayConfig(sessions_dir=tmp_path / 'sessions'))+ turns = _stub_turns(runner, monkeypatch)+ authority = await initialize_session_authority(runner, profile_id='default', instance_id='relay-profile-single')+ try:+ async with _relay(runner) as (send, outgoing, _adapter):+ await send('plain', 'chat-plain')+ await send('stamped', 'chat-stamped', profile='work')+ await async_wait_until(lambda: len(turns) == 2 and _replies(outgoing, 'chat-stamped')+ and _replies(outgoing, 'chat-plain'), timeout=20,+ what='both relay frames answered')+ assert {'buf-plain', 'buf-stamped'} <= _acked(outgoing)+ by_text = {text: (home, key) for text, home, key in turns}+ # The stamp is not a route on a single-profile gateway: the one profile, historical namespace.+ assert by_text['hello stamped'] == (launch, 'agent:main:telegram:dm:chat-stamped')+ assert by_text['hello plain'] == (launch, 'agent:main:telegram:dm:chat-plain')+ rows = {r['request_id']: r for r in _requests(authority)}+ assert sorted(rows) == ['plain', 'stamped']+ # No profile reaches durable state: the admission and the session origin (which later+ # completion/notification scopes resolve a home from) both name the launch profile only.+ assert 'profile' not in rows['stamped']['payload']['native_text_v1']['source']+ entry = runner.session_store.lookup_by_session_key('agent:main:telegram:dm:chat-stamped')+ assert entry is not None and not entry.origin.profile+ assert Path(runner._resolve_profile_home_for_source(entry.origin)).resolve() == launch+ # The connector still gets its profile echoed on egress (persona attribution).+ assert _replies(outgoing, 'chat-stamped')[-1]['metadata'].get('profile') == 'work'+ # A replay of the stamped frame is the same admission, not a second turn.+ await send('stamped', 'chat-stamped', profile='work')+ await async_wait_until(lambda: len(_acked(outgoing)) >= 2 and sum(+ 1 for f in outgoing if f.get('type') == 'inbound_ack' and f['bufferId'] == 'buf-stamped') == 2,+ timeout=10, what='the replay acknowledged')+ assert len(turns) == 2+ finally:+ authority.db.close()+++def _reserve_homes(tmp_path, monkeypatch, names):+ root = tmp_path / '.hermes'+ root.mkdir(mode=0o700)+ monkeypatch.setattr(Path, 'home', lambda: tmp_path)+ monkeypatch.setenv('HERMES_HOME', str(root))+ import hermes_state+ monkeypatch.setattr(hermes_state, 'DEFAULT_DB_PATH', hermes_state._IMPORT_DEFAULT_DB_PATH)+ homes = [('default', root)]+ for name in names:+ home = root / 'profiles' / name+ home.mkdir(parents=True, mode=0o700)+ (home / 'config.yaml').write_text('{}')+ homes.append((name, home))+ return root, homes+++async def test_multiplexed_gateway_admits_a_connector_stamped_profile_into_its_own_ledger(tmp_path, monkeypatch):+ from gateway.run_runtime import initialize_gateway_runtime+ from gateway.runtime_ownership import process_ownership+ from gateway.session_envelope import restore_native+ from hermes_state_runtime import RuntimeStoreError++ root, homes = _reserve_homes(tmp_path, monkeypatch, names=('work',))+ work = homes[1][1]+ process_ownership.reserve([home for _, home in homes])+ config = GatewayConfig(sessions_dir=tmp_path / 'sessions')+ config.multiplex_profiles = True+ config._runtime_profile_homes = tuple(homes)+ runner = GatewayRunner(config)+ turns = _stub_turns(runner, monkeypatch)+ try:+ await initialize_gateway_runtime(runner)+ default_authority = runner.session_authorities.for_home(root)+ work_authority = runner.session_authorities.for_home(work)+ async with _relay(runner) as (send, outgoing, _adapter):+ await send('plain', 'chat-plain')+ await send('to-default', 'chat-default', profile='default')+ await send('to-work', 'chat-work', profile='work')+ await send('to-ghost', 'chat-ghost', profile='ghost')+ await async_wait_until(lambda: len(turns) == 3 and all(+ _replies(outgoing, chat) for chat in ('chat-plain', 'chat-default', 'chat-work'))+ and 'buf-to-ghost' in _acked(outgoing), timeout=30, what='served frames answered, unserved acked')+ by_text = {text: (home, key) for text, home, key in turns}+ assert by_text['hello plain'] == (root.resolve(), 'agent:main:telegram:dm:chat-plain')+ assert by_text['hello to-default'] == (root.resolve(), 'agent:main:telegram:dm:chat-default')+ # The stamped profile's turn runs under its own home, in its own namespace and ledger.+ assert by_text['hello to-work'] == (work.resolve(), 'agent:work:telegram:dm:chat-work')+ assert sorted(r['request_id'] for r in _requests(default_authority)) == ['plain', 'to-default']+ work_rows = _requests(work_authority)+ assert [r['request_id'] for r in work_rows] == ['to-work']+ # An unserved stamped profile is refused like an unserved profile route: acked, never run.+ assert not _replies(outgoing, 'chat-ghost')+ assert 'hello to-ghost' not in by_text+ # The stamp is sealed into the admission: rewriting it in storage does not reroute a replay.+ forged = json.loads(json.dumps(work_rows[0]['payload']))+ forged['native_text_v1']['source']['profile'] = 'default'+ with pytest.raises(RuntimeStoreError):+ restore_native(forged, runner)+ restore_native(work_rows[0]['payload'], runner) # the genuine row still restores+ finally:+ for authority in list(getattr(runner, 'session_authorities', None) or []):+ authority.db.close()+ for _, home in homes:+ process_ownership.release(home)diff --git a/tests/gateway/relay/test_relay_native_refusal_ack.py b/tests/gateway/relay/test_relay_native_refusal_ack.pyindex aa5489e888a..cabe55ca5a1 100644--- a/tests/gateway/relay/test_relay_native_refusal_ack.py+++ b/tests/gateway/relay/test_relay_native_refusal_ack.py@@ -12,7 +12,7 @@ from tests.waiting import async_wait_until@pytest.mark.asyncio-async def test_refused_relay_frame_is_acked_and_the_reader_keeps_serving(tmp_path, monkeypatch):+async def test_refused_relay_frame_is_acked_and_the_reader_keeps_serving(tmp_path, monkeypatch, caplog):websockets = pytest.importorskip('websockets')from gateway.relay.adapter import RelayAdapterfrom gateway.relay.descriptor import CapabilityDescriptor@@ -55,15 +55,17 @@ async def test_refused_relay_frame_is_acked_and_the_reader_keeps_serving(tmp_patassert await adapter.connect()ws = await asyncio.wait_for(ready.get(), 5)source = {'platform': 'telegram', 'chat_id': 'relay-chat', 'chat_type': 'dm', 'user_id': 'relay-user'}- # A route this single-profile gateway does not serve: refused (profile_mismatch) on every replay.- refused = {'text': 'for another profile', 'message_id': 'r1', 'source': {**source, 'profile': 'foreign'}}+ # A platform this socket never advertised: refused (permission_denied) on every replay.+ refused = {'text': 'for another platform', 'message_id': 'r1', 'source': {**source, 'platform': 'discord'}}ok = {'text': 'hello', 'message_id': 'r2', 'source': source}for event, buffer_id in ((refused, 'buf-refused'), (ok, 'buf-ok')):await ws.send(json.dumps({'type': 'inbound', 'event': event, 'bufferId': buffer_id}) + '\n')acked = lambda: {f.get('bufferId') for f in outgoing if f.get('type') == 'inbound_ack'}await async_wait_until(lambda: {'buf-refused', 'buf-ok'} <= acked() and turns,timeout=20, what='both frames acknowledged and the valid one run')+ await async_wait_until(lambda: 'hello' in turns, timeout=10, what='the valid frame run')assert turns == ['hello']+ assert 'relay inbound refused (permission_denied)' in caplog.textassert len(sockets) == 1, 'the refusal tore the relay socket down and re-dialed'finally:await adapter.disconnect()