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