Skip to content

The agent that grows with you

fix(matrix): enable progressive response edits

xz-dev3b3890bb15a7
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index 8056f41f2ab..b49e52aefab 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -2682,8 +2682,7 @@ class GatewayTurnMixin:
raise RuntimeError("skip streaming for non-editable platform")
_effective_cursor = scfg.cursor if _adapter_supports_edit else ""
# Some Matrix clients render the cursor as tofu: stream text, no cursor.
- _buffer_only = source.platform == Platform.MATRIX
- if _buffer_only:
+ if source.platform == Platform.MATRIX:
_effective_cursor = ""
# Fresh-final applies to Telegram only (others edit in place cheaply).
# Fresh-final applies to Telegram only — other platforms either edit in place cheaply (Discord,
@@ -2695,7 +2694,7 @@ class GatewayTurnMixin:
)
_consumer_cfg = StreamConsumerConfig(
edit_interval=scfg.edit_interval, buffer_threshold=scfg.buffer_threshold,
- cursor=_effective_cursor, buffer_only=_buffer_only,
+ cursor=_effective_cursor,
fresh_final_after_seconds=_fresh_final_secs, transport=scfg.transport or "edit",
chat_type=getattr(source, "chat_type", "") or "",
)
diff --git a/tests/gateway/test_run_progress_topics.py b/tests/gateway/test_run_progress_topics.py
index d66cf61e12d..961b7ec3e56 100644
--- a/tests/gateway/test_run_progress_topics.py
+++ b/tests/gateway/test_run_progress_topics.py
@@ -3,6 +3,7 @@
import asyncio
import importlib
import sys
+import threading
import time
import types
from types import SimpleNamespace
@@ -147,6 +148,33 @@ class MetadataEditProgressCaptureAdapter(ProgressCaptureAdapter):
return SendResult(success=True, message_id=message_id)
+class MatrixStreamingProgressCaptureAdapter(ProgressCaptureAdapter):
+ """Records whether Matrix edits are progressive or finalizing."""
+
+ initial_send_seen = threading.Event()
+ progressive_edit_seen = threading.Event()
+
+ async def send(self, chat_id, content, reply_to=None, metadata=None) -> SendResult:
+ result = await super().send(chat_id, content, reply_to=reply_to, metadata=metadata)
+ self.initial_send_seen.set()
+ return result
+
+ async def edit_message(
+ self, chat_id, message_id, content, *, finalize: bool = False
+ ) -> SendResult:
+ self.edits.append(
+ {
+ "chat_id": chat_id,
+ "message_id": message_id,
+ "content": content,
+ "finalize": finalize,
+ }
+ )
+ if not finalize:
+ self.progressive_edit_seen.set()
+ return SendResult(success=True, message_id=message_id)
+
+
class UnsupportedEditProgressCaptureAdapter(MetadataEditProgressCaptureAdapter):
"""Chat adapter whose message edits fail (e.g. edit unsupported or rejected)."""
@@ -888,6 +916,26 @@ class FinalAsInterimAgent:
}
+class StreamingRefineAgent:
+ def __init__(self, **kwargs):
+ self.stream_delta_callback = kwargs.get("stream_delta_callback")
+ self.tools = []
+
+ def run_conversation(self, message, conversation_history=None, task_id=None, **kwargs):
+ if self.stream_delta_callback:
+ self.stream_delta_callback("Continuing to refine:")
+ MatrixStreamingProgressCaptureAdapter.initial_send_seen.wait(timeout=2.0)
+ if self.stream_delta_callback:
+ self.stream_delta_callback(" Final answer.")
+ MatrixStreamingProgressCaptureAdapter.progressive_edit_seen.wait(timeout=2.0)
+ return {
+ "final_response": "Continuing to refine: Final answer.",
+ "response_previewed": True,
+ "messages": [],
+ "api_calls": 1,
+ }
+
+
class QueuedCommentaryAgent:
calls = 0
@@ -1381,6 +1429,36 @@ async def test_non_editable_interim_final_is_recorded_for_final_send_dedup(monke
assert adapter.edits == []
+async def test_run_agent_matrix_streaming_omits_cursor(monkeypatch, tmp_path):
+ MatrixStreamingProgressCaptureAdapter.initial_send_seen.clear()
+ MatrixStreamingProgressCaptureAdapter.progressive_edit_seen.clear()
+ adapter, result = await _run_with_agent(
+ monkeypatch,
+ tmp_path,
+ StreamingRefineAgent,
+ session_id="sess-matrix-streaming",
+ config_data={
+ "display": {"tool_progress": "off", "interim_assistant_messages": False},
+ "streaming": {"enabled": True, "edit_interval": 0.01, "buffer_threshold": 1},
+ },
+ platform=Platform.MATRIX,
+ chat_id="!room:matrix.example.org",
+ chat_type="group",
+ thread_id="$thread",
+ adapter_cls=MatrixStreamingProgressCaptureAdapter,
+ )
+
+ assert result.get("already_sent") is True
+ all_text = [call["content"] for call in adapter.sent] + [call["content"] for call in adapter.edits]
+ assert all_text, "expected streamed Matrix content to be sent or edited"
+ assert any(not call["finalize"] for call in adapter.edits), (
+ "Matrix should progressively edit the active message before finalization"
+ )
+ assert all("▉" not in text for text in all_text)
+ assert any("Continuing to refine:" in text for text in all_text)
+
+
class TransformedStreamAgent:
"""Streams a response, then signals the gateway that a plugin hook
(``transform_llm_output``) modified the final text after streaming