hermes-agent/tests/gateway/test_relay_capability_surface.py
teknium1 43b8ba4181 fix(telegram): preserve Bot API update queue on watcher reconnect
After a prolonged outage the in-process network-error ladder escalates to
fatal and GatewayRunner._platform_reconnect_watcher rebuilds a fresh adapter
that reconnects through the bootstrap path. That path called
start_polling(drop_pending_updates=True), discarding every update Telegram
queued during the outage — all messages sent while the bot was down were
silently lost. The in-process ladder and 409-conflict handler already passed
drop_pending_updates=False; only bootstrap did not distinguish a cold first
boot from a reconnect.

Thread an is_reconnect signal from the watcher through
_connect_adapter_with_timeout into adapter.connect(). The base
BasePlatformAdapter.connect() gains a keyword-only is_reconnect=False so every
adapter inherits a tolerant signature (no per-platform breakage when the
runner forwards the kwarg). Telegram translates is_reconnect into
drop_pending_updates=not is_reconnect on both the polling and webhook bootstrap
calls. Cold boot still drops the stale queue; a watcher reconnect preserves it.

Fixes #46621.

Co-authored-by: annguyenNous <annguyen@nousresearch.com>
Co-authored-by: kyssta-exe <kyssta-exe@users.noreply.github.com>
Co-authored-by: Kewe63 <Kewe63@users.noreply.github.com>
2026-06-25 21:29:57 -07:00

99 lines
4 KiB
Python

"""Phase 0 regression harness for the relay/connector work.
Locks the *behavioral contract* that the future ``RelayAdapter`` must reproduce:
the gateway's ``stream_consumer`` and ``BasePlatformAdapter`` read per-platform
capabilities through a small, stable surface. A relay adapter that exposes the
same surface (``MAX_MESSAGE_LENGTH`` attribute, ``message_len_fn`` property,
``supports_draft_streaming`` probe, and only the abstract methods) slots into
the existing consumer with no consumer changes.
These are deliberately *behavioral* (construct an adapter, drive the code,
assert the observable outcome) rather than source-string snapshots, per the
repo's "don't write change-detector tests" rule. They pass on ``main`` before
any ``RelayAdapter`` exists — they describe the contract, not the relay.
"""
import inspect
from gateway.config import Platform, PlatformConfig
from gateway.platforms.base import BasePlatformAdapter
class _MinAdapter(BasePlatformAdapter):
"""Smallest concrete adapter: implements exactly the abstract methods."""
async def connect(self, *, is_reconnect: bool = False): # pragma: no cover - not called
return True
async def disconnect(self): # pragma: no cover - not called
return None
async def send(self, *args, **kwargs): # pragma: no cover - not called
return None
async def get_chat_info(self, chat_id): # pragma: no cover - not called
return {}
def _make() -> BasePlatformAdapter:
return _MinAdapter(PlatformConfig(), Platform.LOCAL)
def test_abstract_methods_are_the_known_set():
"""The relay adapter must implement exactly this set of abstract methods.
Everything else on BasePlatformAdapter has a default, so ONE generic
RelayAdapter overriding the right subset is feasible without per-platform
gateway classes. If a new abstractmethod is added here, the relay design
(and the cross-repo contract) must be revisited — hence the lock.
NOTE: this is four methods, not three — ``get_chat_info`` is abstract too
(defined far below the connect/disconnect/send cluster in base.py). The
RelayAdapter must implement it (proxying a chat-info lookup to the
connector, or returning a descriptor-derived stub).
"""
abstract = {
name
for name, member in inspect.getmembers(BasePlatformAdapter)
if getattr(member, "__isabstractmethod__", False)
}
assert abstract == {"connect", "disconnect", "send", "get_chat_info"}
def test_message_len_fn_defaults_to_len():
"""message_len_fn is the per-platform length-unit hook (Telegram overrides
it for UTF-16). The default is plain ``len``; the relay adapter will
override it from its negotiated descriptor's ``len_unit``."""
inst = _make()
assert inst.message_len_fn("hello") == 5
def test_supports_draft_streaming_defaults_false():
"""Draft streaming is opt-in per platform; the consumer falls back to the
edit-based path when False. The relay adapter flips this from its
descriptor's ``supports_draft_streaming`` flag."""
inst = _make()
assert inst.supports_draft_streaming() is False
def test_stream_consumer_reads_max_message_length_by_attribute():
"""The consumer resolves the per-platform char limit by reading the
adapter's ``MAX_MESSAGE_LENGTH`` attribute (defaulting to 4096 when
absent). The relay adapter exposes this as an attribute set from its
descriptor — so a relay adapter that sets the attribute is chunked
correctly with no consumer change.
"""
from gateway import stream_consumer
class _NoLimit:
pass
class _WithLimit:
MAX_MESSAGE_LENGTH = 1234
assert getattr(_NoLimit(), "MAX_MESSAGE_LENGTH", 4096) == 4096
assert getattr(_WithLimit(), "MAX_MESSAGE_LENGTH", 4096) == 1234
# The consumer depends on BasePlatformAdapter for the message_len_fn
# isinstance guard (import-level contract the relay adapter satisfies by
# subclassing BasePlatformAdapter).
assert stream_consumer._BasePlatformAdapter is BasePlatformAdapter