Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(channels): stop dropping matrix replies when the stream preview never sent

Lanre Shittu5bc94a718453
diff --git a/agent/src/channels/matrix.py b/agent/src/channels/matrix.py
index 5c11b0241..a5a3107c9 100644
--- a/agent/src/channels/matrix.py
+++ b/agent/src/channels/matrix.py
@@ -546,11 +546,17 @@ class MatrixChannel(BaseChannel):
if meta.get("_stream_end"):
buf = self._stream_bufs.pop(chat_id, None)
- if not buf or not buf.event_id or not buf.text:
+ if not buf or not buf.text:
return
await self._stop_typing_keepalive(chat_id, clear_typing=True)
+ # buf.event_id can still be None here: every periodic edit above
+ # is wrapped in try/except, so a transient send failure (or the
+ # client being briefly unset during a reconnect) leaves it unset
+ # while buf.text keeps accumulating. Send the accumulated reply
+ # as a brand-new message in that case instead of discarding it.
+
content = _build_matrix_text_content(
buf.text,
buf.event_id,
diff --git a/agent/tests/test_matrix_stream_end_no_preview.py b/agent/tests/test_matrix_stream_end_no_preview.py
new file mode 100644
index 000000000..62ff92057
--- /dev/null
+++ b/agent/tests/test_matrix_stream_end_no_preview.py
@@ -0,0 +1,81 @@
+"""Regression: a Matrix streaming reply must not be dropped when no preview edit ever succeeded."""
+
+from __future__ import annotations
+
+import asyncio
+
+from src.channels.matrix import MatrixChannel, _StreamBuf
+
+
+class _FakeResponse:
+ def __init__(self, event_id: str) -> None:
+ self.event_id = event_id
+
+
+def _make_channel() -> MatrixChannel:
+ return MatrixChannel({}, bus=None)
+
+
+class TestStreamEndWithoutPreview:
+ """If every periodic preview edit failed (buf.event_id stayed None), the
+ accumulated text must still reach the room as a fresh message, never be
+ silently dropped.
+ """
+
+ def test_missing_event_id_still_sends_the_accumulated_reply(self) -> None:
+ channel = _make_channel()
+ chat_id = "!room:example.org"
+ channel._stream_bufs[chat_id] = _StreamBuf(text="Hello, world!", event_id=None)
+
+ sent: list[tuple[str, dict]] = []
+
+ async def fake_send_room_content(room_id: str, content: dict) -> _FakeResponse:
+ sent.append((room_id, content))
+ return _FakeResponse("evt-new")
+
+ channel._send_room_content = fake_send_room_content
+
+ asyncio.run(channel.send_delta(chat_id, "", {"_stream_end": True}))
+
+ assert len(sent) == 1
+ room_id, content = sent[0]
+ assert room_id == chat_id
+ assert content["body"] == "Hello, world!"
+ assert "m.relates_to" not in content
+ assert chat_id not in channel._stream_bufs
+
+ def test_existing_event_id_still_sends_an_edit(self) -> None:
+ channel = _make_channel()
+ chat_id = "!room:example.org"
+ channel._stream_bufs[chat_id] = _StreamBuf(text="Hello, world!", event_id="evt-preview")
+
+ sent: list[tuple[str, dict]] = []
+
+ async def fake_send_room_content(room_id: str, content: dict) -> _FakeResponse:
+ sent.append((room_id, content))
+ return _FakeResponse("evt-preview")
+
+ channel._send_room_content = fake_send_room_content
+
+ asyncio.run(channel.send_delta(chat_id, "", {"_stream_end": True}))
+
+ assert len(sent) == 1
+ content = sent[0][1]
+ assert content["m.relates_to"]["event_id"] == "evt-preview"
+
+ def test_empty_text_with_no_preview_sends_nothing(self) -> None:
+ channel = _make_channel()
+ chat_id = "!room:example.org"
+ channel._stream_bufs[chat_id] = _StreamBuf(text="", event_id=None)
+
+ sent: list[tuple[str, dict]] = []
+
+ async def fake_send_room_content(room_id: str, content: dict) -> _FakeResponse:
+ sent.append((room_id, content))
+ return _FakeResponse("evt-new")
+
+ channel._send_room_content = fake_send_room_content
+
+ asyncio.run(channel.send_delta(chat_id, "", {"_stream_end": True}))
+
+ assert not sent