Skip to content

The agent that grows with you

fix(gateway): name the refused clause on native admission

unsupportedpastels2de0378ad4ca
diff --git a/gateway/session_envelope.py b/gateway/session_envelope.py
index 22a761e5a0c..6a9ecf2c9bb 100644
--- a/gateway/session_envelope.py
+++ b/gateway/session_envelope.py
@@ -9,12 +9,15 @@ from copy import copy, deepcopy
from dataclasses import replace
from datetime import datetime
import json
+import logging
from gateway.platforms.event import MessageEvent, MessageType
from gateway.session import SessionSource
from gateway.session_ingress_media import capture_native_media, restore_native_media
from hermes_state_runtime import RuntimeStoreError
+logger = logging.getLogger(__name__)
+
_EVENT_FIELDS = (
'user_id', 'user_name', 'message_id', 'platform_update_id',
'reply_to_message_id', 'reply_to_text', 'reply_to_author_id',
@@ -63,40 +66,71 @@ def _adapter_event(event, **changes):
return replace(event, **changes) if changes else event
+def _refuse(event, reason, clause):
+ """Name the refused clause in gateway.log and on the error; the wire reason stays stable."""
+ source = event.source
+ logger.warning('Native admission refused: %s (%s) platform=%s chat=%s message=%s', reason, clause,
+ getattr(getattr(source, 'platform', None), 'value', None), getattr(source, 'chat_id', None),
+ event.message_id)
+ raise RuntimeStoreError(reason, field=clause)
+
+
+def _media_clause(event):
+ urls, types, inlined = event.media_urls, event.media_types, event.media_text_inlined
+ if not isinstance(urls, list) or not all(isinstance(item, str) for item in urls):
+ return 'media_urls'
+ if not isinstance(types, list) or not all(isinstance(item, str) for item in types):
+ return 'media_types'
+ if not isinstance(inlined, list) or not all(item is None or type(item) is bool for item in inlined):
+ return 'media_text_inlined'
+ if len(types) not in (0, len(urls)):
+ return 'media_types_length'
+ if len(inlined) not in (0, len(urls)):
+ return 'media_text_inlined_length'
+ return None
+
+
def _validate_native(runner, event, provenance=None, fresh_roles=False):
source = event.source
- if (source is None or not isinstance(event.text, str)
- or not isinstance(event.message_type, MessageType)
- # The ingress router's own classifier: ``/tmp/x is broken`` is text, not a command.
- or event.message_type == MessageType.COMMAND or event.get_command()
- or event.internal or _native_metadata(event) is None or event.prompt_response
- or (source.role_authorized and not fresh_roles)
- or (source.delivered_via_upstream_relay and not (provenance and 'relay' in provenance))
- or source.profile_route_rejected
- or (getattr(source, '_authorization_profile_home', None) is not None and provenance is None)
- or (getattr(runner.config, 'multiplex_profiles', False) and provenance is None)):
- raise RuntimeStoreError('invalid_params')
+ # First failing clause wins, in the order (and with the short-circuits) of the original check.
+ clauses = (
+ ('source', lambda: source is None),
+ ('text', lambda: not isinstance(event.text, str)),
+ ('message_type', lambda: not isinstance(event.message_type, MessageType)),
+ # The ingress router's own classifier: ``/tmp/x is broken`` is text, not a command.
+ ('command', lambda: event.message_type == MessageType.COMMAND or event.get_command()),
+ ('internal', lambda: event.internal),
+ ('metadata', lambda: _native_metadata(event) is None),
+ ('prompt_response', lambda: event.prompt_response),
+ ('role_authorized_without_fresh_roles', lambda: source.role_authorized and not fresh_roles),
+ ('upstream_relay_without_provenance',
+ lambda: source.delivered_via_upstream_relay and not (provenance and 'relay' in provenance)),
+ ('profile_route_rejected', lambda: source.profile_route_rejected),
+ ('authorization_home_without_provenance',
+ lambda: getattr(source, '_authorization_profile_home', None) is not None and provenance is None),
+ ('multiplex_without_provenance',
+ lambda: getattr(runner.config, 'multiplex_profiles', False) and provenance is None),
+ )
+ for clause, failed in clauses:
+ if failed():
+ _refuse(event, 'invalid_params', clause)
if provenance is not None:
from gateway.session_ingress_context import restore_provenance
restore_provenance(runner, source, provenance)
delegated = fresh_roles or bool(provenance and 'relay' in provenance)
if not runner._is_user_authorized_for_source(source, allow_adapter_delegation=delegated):
- raise RuntimeStoreError('permission_denied')
- if (any(value is not None and not isinstance(value, str)
- for value in (event.channel_prompt, event.channel_context))
- or event.reply_expected is not None and type(event.reply_expected) is not bool):
- raise RuntimeStoreError('invalid_params')
+ _refuse(event, 'permission_denied', 'user_not_authorized')
+ for name in ('channel_prompt', 'channel_context'):
+ if getattr(event, name) is not None and not isinstance(getattr(event, name), str):
+ _refuse(event, 'invalid_params', name)
+ if event.reply_expected is not None and type(event.reply_expected) is not bool:
+ _refuse(event, 'invalid_params', 'reply_expected')
skill = event.auto_skill
if not (skill is None or isinstance(skill, str)
or isinstance(skill, list) and all(isinstance(item, str) for item in skill)):
- raise RuntimeStoreError('invalid_params')
- if (not isinstance(event.media_urls, list) or not all(isinstance(item, str) for item in event.media_urls)
- or not isinstance(event.media_types, list) or not all(isinstance(item, str) for item in event.media_types)
- or not isinstance(event.media_text_inlined, list)
- or not all(item is None or type(item) is bool for item in event.media_text_inlined)
- or len(event.media_types) not in (0, len(event.media_urls))
- or len(event.media_text_inlined) not in (0, len(event.media_urls))):
- raise RuntimeStoreError('invalid_params')
+ _refuse(event, 'invalid_params', 'auto_skill')
+ if (clause := _media_clause(event)) is not None:
+ _refuse(event, 'invalid_params', clause)
encoded_source = source.to_dict()
encoded_source['is_bot'] = source.is_bot
restored_source = SessionSource.from_dict(encoded_source)
@@ -105,7 +139,7 @@ def _validate_native(runner, event, provenance=None, fresh_roles=False):
restore_provenance(runner, restored_source, provenance)
adapter = runner._adapter_for_source(source)
if adapter is None or runner._adapter_for_source(restored_source) is not adapter:
- raise RuntimeStoreError('not_found')
+ _refuse(event, 'not_found', 'adapter' if adapter is None else 'adapter_mismatch')
return encoded_source
diff --git a/gateway/session_ingress.py b/gateway/session_ingress.py
index 817284bc3c7..e7194b2f715 100644
--- a/gateway/session_ingress.py
+++ b/gateway/session_ingress.py
@@ -23,6 +23,10 @@ async def admit_message(authority, event):
try:
receipt = await authority.admit_native(event)
except RuntimeStoreError as exc:
+ if exc.reason == 'invalid_params' and getattr(exc, 'field', None):
+ # Refused before commit (_validate_native): name the clause instead of a generic turn error.
+ return (f'⚠️ Hermes refused this message ({exc.reason}: {exc.field}), so it was not saved '
+ 'or answered. Ask the operator to check gateway.log.')
if exc.reason != 'queue_full':
raise
# Nothing was committed (main's busy-queue cap): say so instead of a generic turn error.
diff --git a/tests/gateway/test_native_admission_named_refusal.py b/tests/gateway/test_native_admission_named_refusal.py
new file mode 100644
index 00000000000..15b72b91afe
--- /dev/null
+++ b/tests/gateway/test_native_admission_named_refusal.py
@@ -0,0 +1,63 @@
+"""A native admission refusal names the failing clause in the error, the log and the chat reply."""
+import logging
+
+import pytest
+
+from gateway.config import GatewayConfig, Platform, PlatformConfig
+from gateway.platforms.event import MessageEvent
+from gateway.run import GatewayRunner
+from gateway.session_authority import initialize_session_authority
+from gateway.session_envelope import prepare_native
+from gateway.session_ingress import admit_message
+from hermes_state_runtime import RuntimeStoreError, list_session_admissions
+
+
+
+async def _authority(tmp_path, monkeypatch):
+ from plugins.platforms.whatsapp.adapter import WhatsAppAdapter
+ monkeypatch.setenv('WHATSAPP_ALLOWED_USERS', '15551234567')
+ runner = GatewayRunner(GatewayConfig(sessions_dir=tmp_path / 'sessions'))
+ adapter = WhatsAppAdapter(PlatformConfig(enabled=True, extra={'session_path': str(tmp_path / 'wa')}))
+ adapter.gateway_runner, runner.adapters = runner, {Platform.WHATSAPP: adapter}
+ authority = await initialize_session_authority(runner, profile_id='default', instance_id='named')
+ runner._wire_adapter_handlers(adapter)
+ return authority, adapter.build_source(chat_id=CHAT, user_id=CHAT, chat_type='dm')
+
+
[email protected]('fields, clause', [
+ ({'media_urls': 'https://example.com/a.png'}, 'media_urls'),
+ ({'reply_expected': 'yes'}, 'reply_expected'),
+ ({'auto_skill': 7}, 'auto_skill'),
+ # Overlapping failures: the first clause in the original check's order is the one named.
+ ({'reply_expected': 'yes', 'auto_skill': 7}, 'reply_expected'),
+ ({'media_urls': 'https://example.com/a.png', 'media_types': 'image/png'}, 'media_urls'),
+])
+async def test_invalid_params_names_the_failing_clause(tmp_path, monkeypatch, caplog, fields, clause):
+ authority, source = await _authority(tmp_path, monkeypatch)
+ try:
+ with caplog.at_level(logging.WARNING, logger='gateway.session_envelope'):
+ with pytest.raises(RuntimeStoreError) as raised:
+ await prepare_native(authority.runner, MessageEvent(text='hi', source=source, **fields))
+ # A string still refuses after MSG-2's None -> [] normalisation. The wire reason is unchanged; the clause rides beside it (was a bare invalid_params).
+ assert raised.value.reason == 'invalid_params' and raised.value.field == clause
+ assert any(clause in record.getMessage() and 'invalid_params' in record.getMessage()
+ for record in caplog.records), [r.getMessage() for r in caplog.records]
+ finally:
+ authority.db.close()
+
+
+async def test_messaging_user_sees_named_refusal_not_generic_error(tmp_path, monkeypatch):
+ authority, source = await _authority(tmp_path, monkeypatch)
+ try:
+ reply = await admit_message(authority, MessageEvent(text='hi', source=source, message_id='m1',
+ media_urls='https://example.com/a.png'))
+ # Was: RuntimeStoreError escaped -> "Sorry, I encountered an error (RuntimeStoreError)".
+ assert 'invalid_params' in reply and 'media_urls' in reply, reply
+ # Refused before commit: nothing is queued for a retry or a drain to run.
+ assert not any(list_session_admissions(authority.db, session_id=live, pending_only=False)
+ for live in authority.sessions)
+ finally:
+ authority.db.close()