From c850a40e4e1226b381aa9d76e71efd97807e7d8d Mon Sep 17 00:00:00 2001 From: Teknium Date: Wed, 15 Apr 2026 17:36:28 -0700 Subject: [PATCH] fix: gate Matrix adapter path on media_files presence Text-only Matrix sends should continue using the lightweight _send_matrix() HTTP helper (~100ms). Only route through the heavy MatrixAdapter (full sync + E2EE setup) when media files are present. Adds test verifying text-only messages don't take the adapter path. --- tests/tools/test_send_message_tool.py | 19 +++++++++++++++++++ tools/send_message_tool.py | 4 ++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/tests/tools/test_send_message_tool.py b/tests/tools/test_send_message_tool.py index 17c95d797..a174cf24f 100644 --- a/tests/tools/test_send_message_tool.py +++ b/tests/tools/test_send_message_tool.py @@ -622,6 +622,25 @@ class TestSendToPlatformChunking: finally: doc_path.unlink(missing_ok=True) + def test_matrix_text_only_uses_lightweight_path(self): + """Text-only Matrix sends should NOT go through the heavy adapter path.""" + helper = AsyncMock() + lightweight = AsyncMock(return_value={"success": True, "platform": "matrix", "chat_id": "!room:ex.com", "message_id": "$txt"}) + with patch("tools.send_message_tool._send_matrix_via_adapter", helper), \ + patch("tools.send_message_tool._send_matrix", lightweight): + result = asyncio.run( + _send_to_platform( + Platform.MATRIX, + SimpleNamespace(enabled=True, token="tok", extra={"homeserver": "https://matrix.example.com"}), + "!room:ex.com", + "just text, no files", + ) + ) + + assert result["success"] is True + helper.assert_not_awaited() + lightweight.assert_awaited_once() + def test_send_matrix_via_adapter_sends_document(self, tmp_path): file_path = tmp_path / "report.pdf" file_path.write_bytes(b"%PDF-1.4 test") diff --git a/tools/send_message_tool.py b/tools/send_message_tool.py index cc681adc7..8c673c170 100644 --- a/tools/send_message_tool.py +++ b/tools/send_message_tool.py @@ -404,8 +404,8 @@ async def _send_to_platform(platform, pconfig, chat_id, message, thread_id=None, last_result = result return last_result - # --- Matrix: use the native adapter helper for text + media --- - if platform == Platform.MATRIX: + # --- Matrix: use the native adapter helper when media is present --- + if platform == Platform.MATRIX and media_files: last_result = None for i, chunk in enumerate(chunks): is_last = (i == len(chunks) - 1)