From 3bb2028691d3932d9c08607766d00d0d09542945 Mon Sep 17 00:00:00 2001 From: ly-wang19 Date: Sun, 21 Jun 2026 00:43:01 +0800 Subject: [PATCH] fix(matrix,simplex): tolerate malformed HERMES_*_TEXT_BATCH_* delay env vars MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit a7dd98c86 swept the unguarded float(os.getenv()) text-batch delay casts onto the safe utils.env_float helper for Feishu/WeCom/Discord/Telegram, but missed the identical sibling sites in the Matrix and SimpleX adapters. A non-numeric value (e.g. a `HERMES_MATRIX_TEXT_BATCH_DELAY_SECONDS=0.6s` unit-suffix typo) raises an uncaught ValueError in MatrixAdapter.__init__, which propagates through GatewayRunner._create_adapter and the unguarded platform-init loop in start(), aborting gateway boot when Matrix is enabled. The same __init__ already guards MATRIX_ROOM_IDENTITY_TTL_SECONDS with try/except, and the sweep's own message says only already-guarded sites were left untouched — so these are overlooked misses, not intentional fail-fast. Route both through env_float (default-on-malformed), matching the four sibling adapters. Adds Matrix init tests; the malformed cases raise without the fix. --- plugins/platforms/matrix/adapter.py | 13 +++++++---- plugins/platforms/simplex/adapter.py | 7 +++--- tests/gateway/test_matrix.py | 34 ++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 8 deletions(-) diff --git a/plugins/platforms/matrix/adapter.py b/plugins/platforms/matrix/adapter.py index 3835d8e31efa6..f32dc18725212 100644 --- a/plugins/platforms/matrix/adapter.py +++ b/plugins/platforms/matrix/adapter.py @@ -131,6 +131,7 @@ class _TrustStateStub: # type: ignore[no-redef] _ssrf_redirect_guard, ) from gateway.platforms.helpers import ThreadParticipationTracker +from utils import env_float logger = logging.getLogger(__name__) @@ -946,11 +947,13 @@ def __init__(self, config: PlatformConfig): # Text batching: merge rapid successive messages (Telegram-style). # Matrix clients split long messages around 4000 chars. - self._text_batch_delay_seconds = float( - os.getenv("HERMES_MATRIX_TEXT_BATCH_DELAY_SECONDS", "0.6") - ) - self._text_batch_split_delay_seconds = float( - os.getenv("HERMES_MATRIX_TEXT_BATCH_SPLIT_DELAY_SECONDS", "2.0") + # env_float tolerates a malformed value (e.g. a "0.6s" unit-suffix typo) + # instead of raising ValueError out of __init__ and crashing gateway + # boot — matching how the sibling adapters (Feishu/WeCom/Discord/Telegram) + # read this same HERMES__TEXT_BATCH_* family. + self._text_batch_delay_seconds = env_float("HERMES_MATRIX_TEXT_BATCH_DELAY_SECONDS", 0.6) + self._text_batch_split_delay_seconds = env_float( + "HERMES_MATRIX_TEXT_BATCH_SPLIT_DELAY_SECONDS", 2.0 ) self._pending_text_batches: Dict[str, MessageEvent] = {} self._pending_text_batch_tasks: Dict[str, asyncio.Task] = {} diff --git a/plugins/platforms/simplex/adapter.py b/plugins/platforms/simplex/adapter.py index ae4c6be34b64d..3c9f3c6713b32 100644 --- a/plugins/platforms/simplex/adapter.py +++ b/plugins/platforms/simplex/adapter.py @@ -66,6 +66,7 @@ MessageType, SendResult, ) +from utils import env_float logger = logging.getLogger(__name__) @@ -191,9 +192,9 @@ def __init__(self, config: PlatformConfig, **kwargs): # Text message batching — concatenate rapid-fire messages into one # event before dispatching, mirroring Telegram's batching. - self._text_batch_delay = float( - os.getenv("HERMES_SIMPLEX_TEXT_BATCH_DELAY", "0.8") - ) + # env_float tolerates a malformed value instead of raising out of + # __init__ (mirrors the other text-batch adapters). + self._text_batch_delay = env_float("HERMES_SIMPLEX_TEXT_BATCH_DELAY", 0.8) self._pending_text_batches: Dict[str, MessageEvent] = {} self._pending_text_batch_tasks: Dict[str, asyncio.Task] = {} diff --git a/tests/gateway/test_matrix.py b/tests/gateway/test_matrix.py index d239728b79419..46ae2c17c5b01 100644 --- a/tests/gateway/test_matrix.py +++ b/tests/gateway/test_matrix.py @@ -5309,3 +5309,37 @@ async def _ok(): assert ran["ok"] is True # the sibling handler still ran assert "event handler failed" in caplog.text # failure surfaced, not swallowed +class TestMatrixTextBatchEnvParsing: + """A malformed HERMES_MATRIX_TEXT_BATCH_* value must not crash adapter init. + + The env-cast sweep (a7dd98c86) routed these casts through utils.env_float for + Feishu/WeCom/Discord/Telegram but missed Matrix. A non-numeric value (e.g. a + ``0.6s`` unit-suffix typo) previously raised ValueError out of __init__, + which propagates through the gateway platform-init loop and aborts boot. + """ + + def _build(self): + from plugins.platforms.matrix.adapter import MatrixAdapter + + return MatrixAdapter( + PlatformConfig( + enabled=True, + token="tok", + extra={"homeserver": "https://matrix.example.org"}, + ) + ) + + def test_malformed_delay_falls_back_to_default(self, monkeypatch): + monkeypatch.setenv("HERMES_MATRIX_TEXT_BATCH_DELAY_SECONDS", "0.6s") + adapter = self._build() # must not raise ValueError + assert adapter._text_batch_delay_seconds == 0.6 + + def test_malformed_split_delay_falls_back_to_default(self, monkeypatch): + monkeypatch.setenv("HERMES_MATRIX_TEXT_BATCH_SPLIT_DELAY_SECONDS", "abc") + adapter = self._build() # must not raise ValueError + assert adapter._text_batch_split_delay_seconds == 2.0 + + def test_valid_delay_is_honored(self, monkeypatch): + monkeypatch.setenv("HERMES_MATRIX_TEXT_BATCH_DELAY_SECONDS", "1.5") + adapter = self._build() + assert adapter._text_batch_delay_seconds == 1.5