The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a plugin-rewritten message keeps its callback provenance
Teknium988c5d8447c2
diff --git a/gateway/run_inbound_hooks.py b/gateway/run_inbound_hooks.pyindex a3f69aeacec..07ffb720407 100644--- a/gateway/run_inbound_hooks.py+++ b/gateway/run_inbound_hooks.py@@ -91,7 +91,10 @@ class GatewayInboundHooksMixin:if _action == "rewrite":_new_text = _result.get("text")if isinstance(_new_text, str):- event = dataclasses.replace(event, text=_new_text)+ from gateway.session_ingress_context import carry_callback+ rewritten = dataclasses.replace(event, text=_new_text)+ carry_callback(event, rewritten)+ event = rewrittenbreakif _action == "allow":breakdiff --git a/gateway/session_ingress_context.py b/gateway/session_ingress_context.pyindex 9538bc99192..27dac13b3a5 100644--- a/gateway/session_ingress_context.py+++ b/gateway/session_ingress_context.py@@ -68,6 +68,15 @@ def native_callback(runner, event, transport_home, profile=None):_callback.reset(token)+def carry_callback(event, rewritten):+ """A gateway-side rewrite of a callback's event (``pre_gateway_dispatch``) keeps the callback's+ provenance: it changes the text only, never the source the callback authenticated (#139045)."""+ for bound_var in (_callback, _relay_callback):+ bound = bound_var.get()+ if bound is not None and bound[1] is event and rewritten.source is event.source:+ bound_var.set((bound[0], rewritten, *bound[2:]))++def _binding(runner, source, profile):registries = [(None, runner.adapters), *getattr(runner, '_profile_adapters', {}).items()]from gateway.config import Platformdiff --git a/tests/gateway/test_pre_gateway_dispatch.py b/tests/gateway/test_pre_gateway_dispatch.pyindex abf824e08be..f481394e3b0 100644--- a/tests/gateway/test_pre_gateway_dispatch.py+++ b/tests/gateway/test_pre_gateway_dispatch.py@@ -153,3 +153,42 @@ async def test_async_hook_callback_is_awaited_on_the_gateway_loop(monkeypatch):result = await asyncio.wait_for(runner._handle_message(_make_event("hi")), timeout=5)assert result is Noneadapter.send.assert_not_awaited()+++async def test_rewritten_message_keeps_its_callback_provenance_and_is_admitted(monkeypatch):+ """A rewrite builds a new event. Under ``gateway.multiplex_profiles`` an event without its+ adapter callback's provenance is refused ``invalid_params`` (#139045: every message to a profile+ with a rewriting plugin failed), so the rewritten event must carry the callback's provenance and+ be admitted with the rewritten text."""+ from gateway.config import GatewayConfig, Platform, PlatformConfig+ from gateway.platforms.event import MessageEvent+ from gateway.run import GatewayRunner+ from gateway.session_authority import SessionAuthority, initialize_session_authority+ from gateway.session_ingress_context import capture_provenance, native_callback+ from hermes_constants import get_hermes_home+ from hermes_state_runtime import list_session_admissions+ from plugins.platforms.discord.adapter import DiscordAdapter++ async def rewrite(name, **_kwargs):+ return [{"action": "rewrite", "text": "rewritten"}] if name == "pre_gateway_dispatch" else []++ monkeypatch.setattr("hermes_cli.lifecycle.ainvoke_hook", rewrite)+ monkeypatch.setattr(SessionAuthority, "_schedule", lambda self, ref: None)+ monkeypatch.setenv("DISCORD_ALLOWED_USERS", "42")+ runner = GatewayRunner(GatewayConfig())+ adapter = DiscordAdapter(PlatformConfig(enabled=True, token="fixture-token", typing_indicator=False))+ runner.adapters = {Platform.DISCORD: adapter}+ authority = await initialize_session_authority(runner, profile_id="default", instance_id="owner")+ runner._wire_adapter_handlers(adapter)+ source = adapter.build_source(chat_id="42", chat_type="dm", user_id="42")+ human = MessageEvent(text="original", source=source, message_id="h")++ with native_callback(runner, human, get_hermes_home()):+ expected = capture_provenance(runner, human)+ event = await runner._hm_pre_gateway_dispatch_hook(human, source)+ receipt = await authority.admit_native(event)++ payload = list_session_admissions(authority.db, session_id=receipt.ref.session_id, pending_only=False)[-1]["payload"]+ assert expected is not None and event is not human+ assert (payload["text"], payload["native_text_v1"].get("provenance")) == ("rewritten", expected)