Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions plugins/platforms/matrix/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current main no longer lets this exception abort the full gateway boot: PlatformRegistry.create_adapter() catches factory exceptions at gateway/platform_registry.py:318-328, and GatewayRunner.start() continues when it receives no adapter. Please describe this as failed Matrix adapter creation rather than a gateway-wide crash.

# boot — matching how the sibling adapters (Feishu/WeCom/Discord/Telegram)
# read this same HERMES_<PLATFORM>_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] = {}
Expand Down
7 changes: 4 additions & 3 deletions plugins/platforms/simplex/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
MessageType,
SendResult,
)
from utils import env_float

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a malformed HERMES_SIMPLEX_TEXT_BATCH_DELAY regression case in tests/gateway/test_simplex_plugin.py alongside the existing adapter-init tests, so this changed fallback path is covered as well as Matrix.

self._pending_text_batches: Dict[str, MessageEvent] = {}
self._pending_text_batch_tasks: Dict[str, asyncio.Task] = {}

Expand Down
34 changes: 34 additions & 0 deletions tests/gateway/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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