Files
hermes-agent/tests/gateway/relay/test_relay_per_platform_caps.py
T
kshitijk4poor ab2f4602de refactor: MessageEvent to gateway/platforms/event.py; ElicitationHandler takes a call_context thunk
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).
2026-09-07 22:47:33 +05:30

158 lines
5.8 KiB
Python

"""Per-platform capability descriptors on the relay (multi-platform Phase 1.5).
The bug class: one relay adapter fronts N platforms on one WS, but the
capability surface (``MAX_MESSAGE_LENGTH`` / ``message_len_fn``) was a SCALAR
from whichever descriptor resolved the handshake — so a Discord chat on a
gateway whose primary identity was Telegram inherited Telegram's 4,096-char
cap and over-sent into Discord's 2,000-char API 400 (observed live: 2,543 and
2,641-char sends rejected).
Covers:
- the transport accumulating one descriptor per platform (first = session
default, later frames must NOT overwrite it),
- the map resetting on a re-dial,
- RelayAdapter.max_message_length_for_chat / message_len_fn_for_chat
resolving from the chat's inbound platform,
- fallback to the scalar descriptor for unknown chats / transports without
the map,
- the stream consumer's _raw_message_limit honoring the per-chat cap.
"""
from __future__ import annotations
import asyncio
import json
from typing import Any, Dict, List, Optional
import pytest
from gateway.config import Platform, PlatformConfig
from gateway.platforms.event import MessageEvent, MessageType
from gateway.relay.adapter import RelayAdapter
from gateway.relay.descriptor import CONTRACT_VERSION, CapabilityDescriptor
from gateway.session import SessionSource
from tests.gateway.relay.stub_connector import StubConnector
def _descriptor(platform: str, max_len: int, len_unit: str = "chars") -> CapabilityDescriptor:
return CapabilityDescriptor(
contract_version=CONTRACT_VERSION,
platform=platform,
label=platform.title(),
max_message_length=max_len,
supports_draft_streaming=False,
supports_edit=True,
supports_threads=False,
markdown_dialect="plain",
len_unit=len_unit,
)
DISCORD = _descriptor("discord", 2000)
TELEGRAM = _descriptor("telegram", 4096, len_unit="utf16")
class MultiDescriptorStub(StubConnector):
"""StubConnector extended with the per-platform descriptor map."""
def __init__(self, primary: CapabilityDescriptor, *others: CapabilityDescriptor) -> None:
super().__init__(primary)
self._by_platform = {d.platform: d for d in (primary, *others)}
def descriptor_for_platform(self, platform: str) -> Optional[CapabilityDescriptor]:
return self._by_platform.get(platform)
async def _push(stub: StubConnector, platform: Platform, chat_id: str) -> None:
await stub.push_inbound(
MessageEvent(
text="hi",
message_type=MessageType.TEXT,
source=SessionSource(
platform=platform, chat_id=chat_id, chat_type="dm", user_id="u-1"
),
)
)
# ───────────────────── transport descriptor accumulation ─────────────────────
def _make_transport():
from gateway.relay.ws_transport import WebSocketRelayTransport
return WebSocketRelayTransport(
"wss://connector.example/relay",
"telegram",
"bot-9",
identities=[("telegram", "bot-9"), ("discord", "app-1")],
)
@pytest.mark.asyncio
async def test_transport_descriptor_map_resets_on_redial(monkeypatch):
"""A re-dial starts a fresh handshake generation: stale per-platform
descriptors must not survive into the new connection."""
t = _make_transport()
loop = asyncio.get_running_loop()
t._descriptor_ready = loop.create_future()
await t._handle_frame(json.dumps({"type": "descriptor", "descriptor": DISCORD.__dict__}))
assert t.descriptor_for_platform("discord") is not None
# Simulate _dial_and_start's reset preamble without a real socket.
class _FakeWs:
async def close(self): # pragma: no cover - not called
pass
sent: List[str] = []
async def _fake_connect(url, **kwargs):
return _FakeWs()
async def _fake_send(payload):
sent.append(payload)
import gateway.relay.ws_transport as wst
monkeypatch.setattr(wst, "websockets", type("M", (), {"connect": staticmethod(_fake_connect)}))
monkeypatch.setattr(t, "_send", _fake_send)
monkeypatch.setattr(
t, "_read_loop", lambda: asyncio.sleep(0)
) # substitute a no-op coroutine factory
await t._dial_and_start()
assert t.descriptor_for_platform("discord") is None
assert t._descriptor is None
# ───────────────────── adapter per-chat capability surface ─────────────────────
@pytest.mark.asyncio
async def test_adapter_resolves_per_chat_limits_from_inbound_platform():
"""The live bug shape: primary identity Telegram (4096), a Discord chat on
the same adapter must get Discord's 2000 — not Telegram's scalar."""
stub = MultiDescriptorStub(TELEGRAM, DISCORD)
stub._identities = [("telegram", "bot-9"), ("discord", "app-1")]
adapter = RelayAdapter(PlatformConfig(), TELEGRAM, transport=stub)
await adapter.connect()
await _push(stub, Platform.DISCORD, "dc-1")
await _push(stub, Platform.TELEGRAM, "tg-1")
# Scalar surface still the primary's (back-compat).
assert adapter.MAX_MESSAGE_LENGTH == 4096
# Per-chat: each chat resolves its own platform's cap.
assert adapter.max_message_length_for_chat("dc-1") == 2000
assert adapter.max_message_length_for_chat("tg-1") == 4096
# Length unit follows the chat too: Telegram utf16, Discord codepoints.
surrogate = "\U0001f600" # 2 UTF-16 units, 1 codepoint
assert adapter.message_len_fn_for_chat("tg-1")(surrogate) == 2
assert adapter.message_len_fn_for_chat("dc-1")(surrogate) == 1
# ───────────────────── stream consumer integration ─────────────────────