From 0284174df9267774bcd804a423961b6df2569348 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 30 Apr 2026 20:43:18 +0000 Subject: [PATCH] fix: prevent empty Matrix messages when progress callback sends empty content Agent-Logs-Url: https://github.com/halldorjanetzko/nanobot/sessions/df528c59-8214-41a0-9b79-9d1d41857107 Co-authored-by: halldorjanetzko <158819146+halldorjanetzko@users.noreply.github.com> --- nanobot/channels/matrix.py | 2 +- tests/channels/test_matrix_channel.py | 38 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/nanobot/channels/matrix.py b/nanobot/channels/matrix.py index 2e0b4de6d..3a0fba8f1 100644 --- a/nanobot/channels/matrix.py +++ b/nanobot/channels/matrix.py @@ -524,7 +524,7 @@ class MatrixChannel(BaseChannel): failures.append(fail) if failures: text = f"{text.rstrip()}\n{chr(10).join(failures)}" if text.strip() else "\n".join(failures) - if text or not candidates: + if text.strip(): content = _build_matrix_text_content(text) if relates_to: content["m.relates_to"] = relates_to diff --git a/tests/channels/test_matrix_channel.py b/tests/channels/test_matrix_channel.py index bf7a09e23..8fbe2c119 100644 --- a/tests/channels/test_matrix_channel.py +++ b/tests/channels/test_matrix_channel.py @@ -1246,6 +1246,44 @@ async def test_send_progress_keeps_typing_keepalive_running() -> None: await channel.stop() +@pytest.mark.asyncio +async def test_send_empty_content_does_not_call_room_send() -> None: + """Progress messages with empty content must not produce an empty body: '' event.""" + channel = MatrixChannel(_make_config(), MessageBus()) + client = _FakeAsyncClient("", "", "", None) + channel.client = client + + await channel.send( + OutboundMessage( + channel="matrix", + chat_id="!room:matrix.org", + content="", + metadata={"_progress": True}, + ) + ) + + assert client.room_send_calls == [] + + +@pytest.mark.asyncio +async def test_send_whitespace_only_content_does_not_call_room_send() -> None: + """Progress messages with whitespace-only content must not produce an empty message.""" + channel = MatrixChannel(_make_config(), MessageBus()) + client = _FakeAsyncClient("", "", "", None) + channel.client = client + + await channel.send( + OutboundMessage( + channel="matrix", + chat_id="!room:matrix.org", + content=" \n\n ", + metadata={"_progress": True}, + ) + ) + + assert client.room_send_calls == [] + + @pytest.mark.asyncio async def test_send_clears_typing_when_send_fails() -> None: channel = MatrixChannel(_make_config(), MessageBus())