Skip to content

The agent that grows with you

fix(relay): clear the single-profile stamp on every Relay ingress path

unsupportedpastelsaeb39ea006b5
diff --git a/gateway/relay/adapter.py b/gateway/relay/adapter.py
index e44a8c1e92d..a865614ec61 100644
--- a/gateway/relay/adapter.py
+++ b/gateway/relay/adapter.py
@@ -895,7 +895,7 @@ class RelayAdapter(RelayDiscordInteractionMixin, BasePlatformAdapter):
self._seen_inbound[dedupe_key] = None
self._evict_oldest(self._seen_inbound, self._SEEN_INBOUND_MAX)
try:
- self._capture_scope(event)
+ self._capture_ingress_scope(event)
await self._remember_discord_labels(event.source)
self._stamp_slack_session_thread(event)
# A structured prompt answer resolves its waiting primitive and is CONSUMED —
diff --git a/gateway/relay/adapter_discord_interaction.py b/gateway/relay/adapter_discord_interaction.py
index 8d7fb87bd8c..3c0196eae2a 100644
--- a/gateway/relay/adapter_discord_interaction.py
+++ b/gateway/relay/adapter_discord_interaction.py
@@ -27,6 +27,16 @@ class RelayDiscordInteractionMixin:
_DISCORD_LABELS_MAX = 2048
+ def _capture_ingress_scope(self, event) -> None:
+ """``_capture_scope`` for a connector-delivered event, on every Relay ingress path. A
+ single-profile gateway serves the connector's profile route from its one profile, so once the
+ stamp is kept for egress (persona attribution) it is cleared before admission, command
+ dispatch or a passthrough turn. A bare adapter with no runner keeps it."""
+ self._capture_scope(event)
+ config = getattr(getattr(self._message_handler, '__self__', None), 'config', None)
+ if config is not None and not getattr(config, 'multiplex_profiles', False):
+ event.source.profile = None
+
async def _remember_discord_labels(self, source) -> None:
"""Keep the text lane's Discord chat labels for the interaction lane: the pinned
session-context prompt renders them, so a slash turn without them re-rendered the cached
@@ -93,7 +103,7 @@ class RelayDiscordInteractionMixin:
if platform == "discord":
event = self._discord_interaction_to_event(forward)
if event is not None:
- self._capture_scope(event)
+ self._capture_ingress_scope(event)
# A prompt-token component press is consumed (same gate as _on_inbound).
if await self._consume_prompt_response(event):
return
diff --git a/gateway/relay/inbound_admission.py b/gateway/relay/inbound_admission.py
index b1c631843df..95c71a4f154 100644
--- a/gateway/relay/inbound_admission.py
+++ b/gateway/relay/inbound_admission.py
@@ -45,10 +45,6 @@ async def _admit(adapter, runner, event):
from gateway.parked_route_refusal import ParkedProfileRefusal, refuse_parked_route
from gateway.platforms.webhook_ingress import admit_producer
from 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 = None
try:
receipt = await admit_producer(adapter, event)
except ParkedProfileRefusal as exc:
diff --git a/tests/gateway/relay/test_relay_connector_profile_admission.py b/tests/gateway/relay/test_relay_connector_profile_admission.py
index 26e60851684..bcfedd927ca 100644
--- a/tests/gateway/relay/test_relay_connector_profile_admission.py
+++ b/tests/gateway/relay/test_relay_connector_profile_admission.py
@@ -7,6 +7,7 @@ own ledger. Neither may acknowledge the frame and drop it. An unserved stamped p
the way an unserved profile route is: acknowledged, never run.
"""
import asyncio
+import base64
import json
from contextlib import asynccontextmanager
from pathlib import Path
@@ -261,3 +262,40 @@ async def test_stamped_profile_completion_finds_its_relay_after_a_restart(tmp_pa
authority.db.close()
for _, home in homes:
process_ownership.release(home)
+
+
+def _discord_modal(profile):
+ body = {'type': 5, 'id': 'modal-1', 'channel_id': 'chat-modal', 'user': {'id': 'u-modal'},
+ 'data': {'custom_id': 'feedback', 'components': [{'type': 1, 'components': [
+ {'type': 4, 'custom_id': 'answer', 'value': 'Please summarize'}]}]}}
+ return {'platform': 'discord', 'botId': 'fixture-bot', 'method': 'POST', 'path': '/interactions',
+ 'headers': [], 'bodyB64': base64.b64encode(json.dumps(body).encode()).decode(), 'profile': profile}
+
+
+async def test_single_profile_gateway_serves_stamped_commands_and_discord_interactions(tmp_path, monkeypatch):
+ from gateway.session_authority import initialize_session_authority
+ from hermes_constants import get_hermes_home
+ launch = Path(get_hermes_home()).resolve()
+ (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-stamp-paths')
+ try:
+ async with _relay(runner, platforms=('discord',)) as (_send, outgoing, adapter):
+ # A stamped slash command that becomes a turn (``_on_inbound`` skips native admission).
+ await adapter._transport._on_inbound({'event': {'text': '/queue summarize', 'message_id': 'cmd-1', 'source': {
+ 'platform': 'discord', 'chat_type': 'dm', 'chat_id': 'chat-cmd', 'user_id': 'u-cmd', 'profile': 'work'}}})
+ # A stamped Discord modal submit (``_on_passthrough`` calls handle_message directly).
+ await adapter._transport._on_passthrough_forward({'forward': _discord_modal('work')})
+ await async_wait_until(lambda: _replies(outgoing, 'chat-cmd') and _replies(outgoing, 'chat-modal'),
+ timeout=20, what='both stamped inputs answered')
+ assert len(turns) == 2, turns
+ assert {home for _, home, _ in turns} == {launch}
+ for chat in ('chat-cmd', 'chat-modal'):
+ sent = [a.get('content') or '' for a in _replies(outgoing, chat)]
+ assert sent and not any('profile_mismatch' in text for text in sent), sent
+ # The stamp is still echoed on egress.
+ assert _replies(outgoing, chat)[-1]['metadata'].get('profile') == 'work'
+ finally:
+ authority.db.close()