Files
hermes-agent/tests/gateway/test_clarify_active_session_bypass.py
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

142 lines
5.2 KiB
Python

"""Regression tests for clarify replies while a gateway session is busy."""
import asyncio
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from gateway.config import Platform, PlatformConfig
from gateway.platforms.base import (
BasePlatformAdapter,
SendResult,
)
from gateway.platforms.event import MessageEvent, MessageType
from gateway.session import SessionSource, build_session_key
class _ClarifyBypassAdapter(BasePlatformAdapter):
def __init__(self):
super().__init__(PlatformConfig(enabled=True, token="test"), Platform.TELEGRAM)
async def connect(self):
return True
async def disconnect(self):
pass
async def send(self, chat_id, content, reply_to=None, metadata=None):
return SendResult(success=True, message_id="text")
async def get_chat_info(self, chat_id):
return {"id": chat_id, "type": "private"}
def _event(text="custom answer"):
return MessageEvent(
text=text,
message_type=MessageType.TEXT,
source=SessionSource(
platform=Platform.TELEGRAM,
chat_id="12345",
chat_type="private",
user_id="user1",
),
message_id="msg1",
)
def _clear_clarify_state():
from tools import clarify_gateway as cm
with cm._lock:
cm._entries.clear()
cm._session_index.clear()
cm._notify_cbs.clear()
@pytest.mark.asyncio
async def test_active_session_routes_typed_choice_clarify_reply_to_runner_not_busy_queue():
"""Typed text must resolve a pending choice clarify even while the agent is busy.
Telegram button clarifies keep the adapter session active while the agent
thread blocks on ``wait_for_response``. If the adapter only bypasses for
entries already marked ``awaiting_text``, typed replies to the visible
multi-choice prompt are handled as busy follow-ups and the clarify wait is
never resolved.
"""
_clear_clarify_state()
from tools import clarify_gateway as cm
adapter = _ClarifyBypassAdapter()
adapter._message_handler = AsyncMock(return_value="")
adapter._busy_session_handler = AsyncMock(return_value=True)
event = _event("None of those are valid options")
session_key = build_session_key(
event.source,
group_sessions_per_user=adapter.config.extra.get("group_sessions_per_user", True),
thread_sessions_per_user=adapter.config.extra.get("thread_sessions_per_user", False),
)
adapter._active_sessions[session_key] = asyncio.Event()
cm.register("clarify-1", session_key, "Pick one", ["A", "B"])
await adapter.handle_message(event)
adapter._message_handler.assert_awaited_once_with(event)
adapter._busy_session_handler.assert_not_awaited()
assert adapter._pending_messages == {}
@pytest.mark.asyncio
async def test_active_session_bypass_uses_profile_namespaced_key_under_multiplex():
"""Regression for issue #82975: under a named-profile multiplex, the
adapter's clarify bypass lookup must use the SAME profile-namespaced
session key that the runner registers pending clarifies under
(SessionStore._generate_session_key() includes
profile=self._resolve_profile_for_key(source)), not the legacy
unnamespaced key. Otherwise the lookup misses, and a user's answer to
a pending clarify is routed to the busy-session queue instead of
resolving it -- the turn then hangs until the clarify's 3600s timeout."""
_clear_clarify_state()
from tools import clarify_gateway as cm
adapter = _ClarifyBypassAdapter()
adapter._message_handler = AsyncMock(return_value="")
adapter._busy_session_handler = AsyncMock(return_value=True)
event = _event("None of those are valid options")
# A session_store configured for profile multiplexing, matching what
# the runner's SessionStore._generate_session_key() actually produces.
session_store = MagicMock()
session_store._resolve_profile_for_key.return_value = "ops"
adapter._session_store = session_store
profile_namespaced_key = build_session_key(
event.source,
group_sessions_per_user=adapter.config.extra.get("group_sessions_per_user", True),
thread_sessions_per_user=adapter.config.extra.get("thread_sessions_per_user", False),
profile="ops",
)
# Sanity: the profile-namespaced key really is different from the
# legacy unnamespaced one -- otherwise this test wouldn't distinguish
# the fixed behavior from the bug.
legacy_key = build_session_key(
event.source,
group_sessions_per_user=adapter.config.extra.get("group_sessions_per_user", True),
thread_sessions_per_user=adapter.config.extra.get("thread_sessions_per_user", False),
)
assert profile_namespaced_key != legacy_key
adapter._active_sessions[profile_namespaced_key] = asyncio.Event()
# The runner registers the pending clarify under its own
# profile-namespaced key, exactly as it would in a real multiplexed
# deployment.
cm.register("clarify-1", profile_namespaced_key, "Pick one", ["A", "B"])
await adapter.handle_message(event)
adapter._message_handler.assert_awaited_once_with(event)
adapter._busy_session_handler.assert_not_awaited()
assert adapter._pending_messages == {}