fix: plugin inject_message enters the session FIFO under the session authority

7b2ff7a7d4e's injector dispatched internal=True events through
adapter.handle_message; _handle_message skips admit_message for internal events,
so under an authority a plugin turn ran on the in-process lane outside the
durable FIFO, with no receipt, concurrently with an admitted turn on the same
session. Route both targets through authority.admit_automation (the heartbeat /
completion producer seam) with a per-call plugin identity; a refused admission
reports not-routed instead of falling back. The origin= path admits into an
existing session only: producer admission borrows a committed human turn's
connector provenance, so a never-seen chat is refused, not run unadmitted.
Standalone (no authority) keeps the adapter path.

(cherry picked from commit db7e812d3cbaaae667e6a53909dd8326bea92334)
This commit is contained in:
Teknium
2026-10-06 16:15:57 -07:00
parent f879c0c627
commit 05305c4efb
2 changed files with 77 additions and 0 deletions

View File

@@ -143,6 +143,11 @@ class GatewayPluginInjectionMixin:
return False
async with self._plugin_injection_scope(source):
admitted = await self._admit_plugin_injection(
adapter, source, session_key=session_key, session_id=entry.session_id,
content=content, plugin_id=plugin_id)
if admitted is not None:
return admitted
await adapter.handle_message(MessageEvent(
text=content, message_type=MessageType.TEXT, source=source, internal=True,
allow_gateway_control=False,
@@ -231,6 +236,19 @@ class GatewayPluginInjectionMixin:
return False
session_key = self._session_key_for_source(source)
async with self._plugin_injection_scope(source):
from gateway.session_authorities import active_authority
if active_authority(self) is not None:
# The FIFO admits a producer turn only into a session that already holds a
# committed human turn (its connector provenance is borrowed, never minted), so
# an origin with no session yet is refused rather than run outside the FIFO.
entry = await self.async_session_store.lookup_by_session_key(session_key)
if entry is None:
logger.warning("Plugin message injection refused: plugin=%s %s has no session under "
"the session authority yet", plugin_id, target)
return False
return await self._admit_plugin_injection(
adapter, source, session_key=session_key, session_id=entry.session_id,
content=content, plugin_id=plugin_id)
await adapter.handle_message(MessageEvent(
text=content, message_type=MessageType.TEXT, source=source, internal=True,
allow_gateway_control=False,
@@ -242,3 +260,34 @@ class GatewayPluginInjectionMixin:
))
logger.info("Plugin message injection dispatched: plugin=%s %s session=%s", plugin_id, target, session_key)
return True
async def _admit_plugin_injection(
self, adapter, source: SessionSource, *, session_key: str, session_id: str, content: str,
plugin_id: str,
) -> Optional[bool]:
"""Under a session authority, commit the plugin turn into the session's durable FIFO as a
trusted producer admission (the heartbeat / completion seam) instead of running it on the
in-process lane beside an admitted turn. ``None`` means no authority serves this scope and
the caller keeps the standalone adapter path; a refused admission is ``False``, logged."""
from gateway.session_authorities import active_authority
authority = active_authority(self)
if authority is None:
return None
import json
import uuid
from hermes_state_runtime import RuntimeStoreError
identity = json.dumps(["plugin", plugin_id, session_key, uuid.uuid4().hex], separators=(",", ":"))
event = MessageEvent(
text=content, message_type=MessageType.TEXT, source=source, internal=True,
allow_gateway_control=False, message_id=identity,
metadata={"gateway_session_key": session_key, "gateway_session_id": session_id},
)
try:
await authority.admit_automation(adapter, event, identity)
except RuntimeStoreError as exc:
logger.warning("Plugin message injection refused by the session authority: plugin=%s session=%s (%s)",
plugin_id, session_key, exc.reason)
return False
logger.info("Plugin message injection admitted: plugin=%s session=%s session_id=%s",
plugin_id, session_key, session_id)
return True

View File

@@ -455,3 +455,31 @@ def test_install_and_clear_gateway_injector_preserves_newer_owner():
assert manager.has_gateway_message_injector is True
assert manager.inject_gateway_message(value="kept") is True
newer_injector.assert_called_once_with(value="kept")
@pytest.mark.asyncio
async def test_under_a_session_authority_injection_enters_the_durable_fifo(monkeypatch):
"""With an authority serving the scope, a plugin turn is committed as a trusted producer
admission into the session's FIFO; it never runs on the in-process adapter lane, and a
refused admission is reported as not routed instead of falling back to that lane."""
import gateway.session_authorities as authorities
from hermes_state_runtime import RuntimeStoreError
adapter = SimpleNamespace(handle_message=AsyncMock())
entry = _entry()
runner = _runner(entry, adapter)
authority = SimpleNamespace(admit_automation=AsyncMock())
monkeypatch.setattr(authorities, "active_authority", lambda _runner: authority)
assert await runner._dispatch_plugin_message_injection(
session_key=entry.session_key, content="check the deployment", plugin_id="notify-plugin") is True
adapter.handle_message.assert_not_awaited()
admitted_adapter, event, identity = authority.admit_automation.await_args.args
assert admitted_adapter is adapter and event.internal is True and event.message_id == identity
assert event.metadata == {"gateway_session_key": entry.session_key, "gateway_session_id": entry.session_id}
assert event.text == "check the deployment" and "notify-plugin" in identity
authority.admit_automation.side_effect = RuntimeStoreError("not_found")
assert await runner._dispatch_plugin_message_injection(
session_key=entry.session_key, content="again", plugin_id="notify-plugin") is False
adapter.handle_message.assert_not_awaited()