ab2f4602de
Breaks the two import cycles that forced Protocol stand-ins in the F821 sweep, so the two sites now name the real types. gateway/platforms/event.py (new leaf): MessageType, ProcessingOutcome, MessageEvent moved out of base.py verbatim. Their only dependency is gateway.session.SessionSource; base.py imported helpers.py at module level, so helpers could not name MessageEvent. Now TextBatchAggregator is typed by the real MessageEvent. 249 importers repointed (`from gateway.platforms.base import` -> `.event`, preserving each import's layout); gateway.platforms.__init__ re-exports from .event. The three revert-scheduled PLUGIN-COMPAT pointers that named these symbols (gateway.slash_commands → MessageType, dingtalk → MessageType, photon → ProcessingOutcome) and their COMPAT_MANIFEST rows now target gateway.platforms.event. Docs updated: ADDING_A_PLATFORM.md, adding-platform-adapters.md (en + zh-Hans). tools/mcp_tool_sampling.py: ElicitationHandler no longer holds a back-reference to its MCPServerTask (mcp_tool imports sampling, so the task type cannot be named there). It only ever read owner._pending_call_context, so it takes `call_context: Callable[[], Context | None]` and MCPServerTask passes `lambda: self._pending_call_context`. The consent call is one `functools.partial`, run directly or inside the captured Context. ty on the 11 touched production files vs origin/main: 0 new diagnostics, 14 resolved. (The one `source: SessionSource = None` diagnostic moves with the class; typing it Optional exposes ~60 unguarded call sites — separate follow-up.) Tests: tests/gateway + tests/plugins + tests/tools + touched files, 18,235 passed; the 31 failures reproduce identically on origin/main (macOS /private/tmp, systemd socket, long-path fixtures, live-service tests).
121 lines
3.9 KiB
Python
121 lines
3.9 KiB
Python
"""Tests for the pre_gateway_dispatch plugin hook.
|
|
|
|
The hook allows plugins to intercept incoming messages before auth and
|
|
agent dispatch. It runs in _handle_message and acts on returned action
|
|
dicts: {"action": "skip"|"rewrite"|"allow"}.
|
|
"""
|
|
|
|
from types import SimpleNamespace
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from gateway.config import GatewayConfig, Platform, PlatformConfig
|
|
from gateway.platforms.event import MessageEvent
|
|
from gateway.session import SessionSource
|
|
|
|
|
|
def _clear_auth_env(monkeypatch) -> None:
|
|
for key in (
|
|
"TELEGRAM_ALLOWED_USERS",
|
|
"WHATSAPP_ALLOWED_USERS",
|
|
"GATEWAY_ALLOWED_USERS",
|
|
"TELEGRAM_ALLOW_ALL_USERS",
|
|
"WHATSAPP_ALLOW_ALL_USERS",
|
|
"GATEWAY_ALLOW_ALL_USERS",
|
|
):
|
|
monkeypatch.delenv(key, raising=False)
|
|
|
|
|
|
def _make_event(text: str = "hello", platform: Platform = Platform.WHATSAPP) -> MessageEvent:
|
|
return MessageEvent(
|
|
text=text,
|
|
message_id="m1",
|
|
source=SessionSource(
|
|
platform=platform,
|
|
user_id="15551234567@s.whatsapp.net",
|
|
chat_id="15551234567@s.whatsapp.net",
|
|
user_name="tester",
|
|
chat_type="dm",
|
|
),
|
|
)
|
|
|
|
|
|
def _make_runner(platform: Platform):
|
|
from gateway.run import GatewayRunner
|
|
|
|
config = GatewayConfig(
|
|
platforms={platform: PlatformConfig(enabled=True)},
|
|
)
|
|
runner = object.__new__(GatewayRunner)
|
|
runner.config = config
|
|
adapter = SimpleNamespace(send=AsyncMock())
|
|
runner.adapters = {platform: adapter}
|
|
runner.pairing_store = MagicMock()
|
|
runner.pairing_store.is_approved.return_value = False
|
|
runner.pairing_store._is_rate_limited.return_value = False
|
|
runner.session_store = MagicMock()
|
|
runner._running_agents = {}
|
|
runner._update_prompt_pending = {}
|
|
return runner, adapter
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_internal_events_bypass_hook(monkeypatch):
|
|
"""Internal events (event.internal=True) skip the plugin hook entirely."""
|
|
_clear_auth_env(monkeypatch)
|
|
monkeypatch.setenv("WHATSAPP_ALLOWED_USERS", "*")
|
|
|
|
called = {"count": 0}
|
|
|
|
def _fake_hook(name, **kwargs):
|
|
called["count"] += 1
|
|
return [{"action": "skip"}]
|
|
|
|
async def _capture(event, source, _quick_key, _run_generation):
|
|
return "ok"
|
|
|
|
monkeypatch.setattr("hermes_cli.plugins.invoke_hook", _fake_hook)
|
|
|
|
runner, _adapter = _make_runner(Platform.WHATSAPP)
|
|
runner._handle_message_with_agent = _capture # noqa: SLF001
|
|
|
|
event = _make_event("hi")
|
|
event.internal = True
|
|
|
|
# Even though the hook would say skip, internal events bypass it.
|
|
await runner._handle_message(event)
|
|
assert called["count"] == 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_hook_fires_without_session_store_attribute(monkeypatch):
|
|
"""A runner missing session_store still delivers the event to plugins.
|
|
|
|
Regression: the hook kwargs read ``self.session_store`` directly, so a
|
|
partially-initialized runner raised AttributeError inside the dispatch
|
|
try-block — the hook never fired, and every message logged
|
|
"pre_gateway_dispatch invocation failed: 'GatewayRunner' object has no
|
|
attribute 'session_store'". Plugins must receive the event (with
|
|
session_store=None) instead.
|
|
"""
|
|
_clear_auth_env(monkeypatch)
|
|
|
|
seen = {}
|
|
|
|
def _fake_hook(name, **kwargs):
|
|
if name == "pre_gateway_dispatch":
|
|
seen["session_store"] = kwargs.get("session_store", "MISSING")
|
|
return [{"action": "skip", "reason": "plugin-handled"}]
|
|
return []
|
|
|
|
monkeypatch.setattr("hermes_cli.plugins.invoke_hook", _fake_hook)
|
|
|
|
runner, adapter = _make_runner(Platform.WHATSAPP)
|
|
del runner.session_store
|
|
|
|
result = await runner._handle_message(_make_event("hi"))
|
|
assert result is None
|
|
# Hook actually fired (skip short-circuited before auth) with a None store.
|
|
assert seen == {"session_store": None}
|
|
adapter.send.assert_not_awaited()
|