Skip to content

The agent that grows with you

fix(gateway): retain transformed api stream output

KoNit-K6882b64ff561
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index c14faa85289..8056f41f2ab 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -3989,8 +3989,9 @@ class GatewayTurnMixin:
except Exception as _edit_err:
logger.warning(fail_exc, _sk, _edit_err)
return
- if fail_result is not None and not getattr(_res, "success", True):
- logger.warning(fail_result, _sk, getattr(_res, "error", None))
+ if not getattr(_res, "success", True):
+ if fail_result is not None:
+ logger.warning(fail_result, _sk, getattr(_res, "error", None))
return
response["already_sent"] = True
logger.info(*ok)
diff --git a/tests/gateway/test_run_progress_topics.py b/tests/gateway/test_run_progress_topics.py
index 51566dd2a40..46e6e7cd465 100644
--- a/tests/gateway/test_run_progress_topics.py
+++ b/tests/gateway/test_run_progress_topics.py
@@ -147,6 +147,23 @@ class MetadataEditProgressCaptureAdapter(ProgressCaptureAdapter):
return SendResult(success=True, message_id=message_id)
+class UnsupportedEditProgressCaptureAdapter(MetadataEditProgressCaptureAdapter):
+ """Model the API server, where an HTTP/SSE response owns final delivery."""
+
+ async def edit_message(
+ self, chat_id, message_id, content, *, finalize: bool = False, metadata=None
+ ) -> SendResult:
+ self.edits.append(
+ {
+ "chat_id": chat_id,
+ "message_id": message_id,
+ "content": content,
+ "metadata": metadata,
+ }
+ )
+ return SendResult(success=False, error="Not supported")
+
+
class RetryableOverflowEditProgressAdapter(SmallLimitProgressAdapter):
"""Fail the first split edit transiently, then keep editing."""
@@ -1420,6 +1437,34 @@ async def test_transformed_response_edits_streamed_message_in_place(monkeypatch,
)
+async def test_transformed_api_stream_keeps_final_for_single_sse_delivery(monkeypatch, tmp_path):
+ """An API-server edit is unsupported, so the SSE owner must retain the final response.
+
+ Marking this response as already sent suppresses that one authoritative final delivery and
+ drops plugin-transformed content for the desktop client.
+ """
+ adapter, result = await _run_with_agent(
+ monkeypatch,
+ tmp_path,
+ TransformedStreamAgent,
+ session_id="sess-transformed-api-stream",
+ config_data={
+ "display": {"tool_progress": "off", "interim_assistant_messages": False},
+ "streaming": {"enabled": True, "edit_interval": 0.01, "buffer_threshold": 1},
+ },
+ platform=Platform.API_SERVER,
+ chat_id="desktop-session",
+ chat_type="dm",
+ thread_id=None,
+ adapter_cls=UnsupportedEditProgressCaptureAdapter,
+ )
+
+ assert result["final_response"].endswith("[plugin appended this]")
+ assert result.get("already_sent") is not True
+ assert len(adapter.edits) == 1
+
+
@pytest.mark.asyncio
async def test_run_agent_queued_message_does_not_treat_commentary_as_final(monkeypatch, tmp_path):
QueuedCommentaryAgent.calls = 0