The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): keep trust clauses out of the chat refusal reply
unsupportedpastels872cea386b3a
diff --git a/gateway/session_envelope.py b/gateway/session_envelope.pyindex 6a9ecf2c9bb..478bfc497a5 100644--- a/gateway/session_envelope.py+++ b/gateway/session_envelope.py@@ -90,30 +90,40 @@ def _media_clause(event):return None+def _event_clause(runner, event, provenance, fresh_roles):+ source = event.source+ if source is None:+ return 'source'+ if not isinstance(event.text, str):+ return 'text'+ if not isinstance(event.message_type, MessageType):+ return 'message_type'+ # The ingress router's own classifier: ``/tmp/x is broken`` is text, not a command.+ if event.message_type == MessageType.COMMAND or event.get_command():+ return 'command'+ if event.internal:+ return 'internal'+ if _native_metadata(event) is None:+ return 'metadata'+ if event.prompt_response:+ return 'prompt_response'+ if source.role_authorized and not fresh_roles:+ return 'role_authorized_without_fresh_roles'+ if source.delivered_via_upstream_relay and not (provenance and 'relay' in provenance):+ return 'upstream_relay_without_provenance'+ if source.profile_route_rejected:+ return 'profile_route_rejected'+ if getattr(source, '_authorization_profile_home', None) is not None and provenance is None:+ return 'authorization_home_without_provenance'+ if getattr(runner.config, 'multiplex_profiles', False) and provenance is None:+ return 'multiplex_without_provenance'+ return None++def _validate_native(runner, event, provenance=None, fresh_roles=False):source = event.source- # 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 (clause := _event_clause(runner, event, provenance, fresh_roles)) is not None:+ _refuse(event, 'invalid_params', clause)if provenance is not None:from gateway.session_ingress_context import restore_provenancerestore_provenance(runner, source, provenance)diff --git a/gateway/session_ingress.py b/gateway/session_ingress.pyindex e7194b2f715..d4d656f41e0 100644--- a/gateway/session_ingress.py+++ b/gateway/session_ingress.py@@ -15,6 +15,11 @@ admission_author = ContextVar('admission_author', default=None)executing_admission = ContextVar('executing_admission', default=False)# Set by a busy-path caller that must be released once its input is durably queued.busy_acceptance = ContextVar('busy_acceptance', default=None)+# ``_validate_native`` clauses about the message's own content, safe to name to the sender's chat.+_CONTENT_CLAUSES = frozenset({+ 'text', 'auto_skill', 'channel_prompt', 'channel_context', 'reply_expected',+ 'media_urls', 'media_types', 'media_text_inlined', 'media_types_length', 'media_text_inlined_length',+})async def admit_message(authority, event):@@ -23,9 +28,12 @@ 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 '+ if exc.reason == 'invalid_params' and exc.field:+ # Refused before commit (_validate_native): a refusal notice instead of a generic turn error.+ # The reply reaches the whole chat, so only content clauses are named; trust and provenance+ # clauses (and any new one) stay generic, and gateway.log keeps the clause (``_refuse``).+ named = f'{exc.reason}: {exc.field}' if exc.field in _CONTENT_CLAUSES else exc.reason+ return (f'⚠️ Hermes refused this message ({named}), so it was not saved ''or answered. Ask the operator to check gateway.log.')if exc.reason != 'queue_full':raisediff --git a/tests/gateway/test_native_admission_named_refusal.py b/tests/gateway/test_native_admission_named_refusal.pyindex 15b72b91afe..88490023477 100644--- a/tests/gateway/test_native_admission_named_refusal.py+++ b/tests/gateway/test_native_admission_named_refusal.py@@ -1,4 +1,5 @@-"""A native admission refusal names the failing clause in the error, the log and the chat reply."""+"""A native admission refusal names the failing clause in the error and the log, and in the chat+reply only for content clauses; trust and provenance clauses stay generic there."""import loggingimport pytest@@ -61,3 +62,36 @@ async def test_messaging_user_sees_named_refusal_not_generic_error(tmp_path, monfor live in authority.sessions)finally:authority.db.close()+++def _forge_multiplex(authority, source):+ authority.runner.config.multiplex_profiles = True+++def _forge_authorization_home(authority, source):+ source._authorization_profile_home = '/forged/home'++[email protected]('forge, clause', [+ (_forge_multiplex, 'multiplex_without_provenance'),+ (_forge_authorization_home, 'authorization_home_without_provenance'),+])+async def test_trust_clause_refusal_is_not_named_to_the_chat(tmp_path, monkeypatch, caplog, forge, clause):+ authority, source = await _authority(tmp_path, monkeypatch)+ try:+ forge(authority, source)+ 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))+ reply = await admit_message(authority, MessageEvent(text='hi', source=source, message_id='m1'))+ assert raised.value.reason == 'invalid_params' and raised.value.field == clause+ # The reply reaches everyone in a group chat: a trust/provenance check stays generic there,+ # so a sender probing forged trust fields learns nothing; the operator log keeps the clause.+ assert 'invalid_params' in reply and clause not in reply and 'provenance' not in reply, reply+ assert any(clause in record.getMessage() and 'invalid_params' in record.getMessage()+ for record in caplog.records), [r.getMessage() for r in caplog.records]+ assert not any(list_session_admissions(authority.db, session_id=live, pending_only=False)+ for live in authority.sessions)+ finally:+ authority.db.close()