The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): skip duplicate-send diagnostic for interim-only stream consumers (#105341)
webtecnicab065e861b355
diff --git a/gateway/run_turn.py b/gateway/run_turn.pyindex 5f9808ce07b..ae69e6b99de 100644--- a/gateway/run_turn.py+++ b/gateway/run_turn.py@@ -3843,9 +3843,11 @@ class GatewayTurnMixin:ok=("Edited streamed message %s for session %s to include plugin-transformed content.", _sc.message_id, _sk),fail_result=None, fail_exc="Failed to edit streamed message for session %s: %s",)- elif _sc is not None:- # DUPLICATE-RISK DIAGNOSTIC: a stream consumer existed but suppression did NOT fire; log the- # decision inputs ("signal never set" vs "ack-pending race").+ elif _sc is not None and getattr(_sc, "stream_deltas_enabled", True):+ # DUPLICATE-RISK DIAGNOSTIC: a stream consumer existed but suppression did NOT fire; log+ # the decision inputs ("signal never set" vs "ack-pending race"). Skipped for consumers+ # never fed the final's deltas (interim-only wiring, #105341) — they cannot have raced+ # the normal final send, so the warning would be a guaranteed false positive.logger.warning("Normal final-send NOT suppressed despite active stream consumer for session %s: ""streamed=%s previewed=%s content_delivered=%s transformed=%s final_len=%d — "diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.pyindex 5713ad772f5..00729d72fb4 100644--- a/gateway/run_turn_runner.py+++ b/gateway/run_turn_runner.py@@ -925,6 +925,10 @@ class TurnRunner:initial_reply_to_id=ctx.event_message_id, run_still_current=ctx._run_still_current,)ctx.stream_consumer_holder[0] = stream_consumer+ # #105341: a consumer created only for interim commentary (text streaming off)+ # is never fed the final reply's deltas — mark it so the duplicate-risk+ # diagnostic in ``_run_agent_mark_streamed_delivery`` stays silent.+ stream_consumer.stream_deltas_enabled = want_stream_deltasexcept Exception as err:logger.debug("Could not set up stream consumer: %s", err)# Deltas tee to the stream consumer (when text streaming is on) and to streaming TTS.diff --git a/gateway/stream_consumer.py b/gateway/stream_consumer.pyindex dbd1d5e2cec..22a44e8ff0d 100644--- a/gateway/stream_consumer.py+++ b/gateway/stream_consumer.py@@ -131,6 +131,12 @@ class GatewayStreamConsumer(StreamTransportMixin, StreamFallbackMixin, StreamThiself._turn_id = str(uuid.uuid4()) # keys send_stream_frame() per concurrent consumer# Returns False after /new or /stop; run() then abandons the stream.self._run_still_current = run_still_current or (lambda: True)+ # Whether this consumer is fed the final reply's stream deltas. A consumer built only to+ # relay interim commentary (text streaming off, ``display.interim_assistant_messages`` on)+ # never receives the final's deltas, so the duplicate-risk diagnostic in+ # ``_run_agent_mark_streamed_delivery`` must not fire for it (#105341). Default True: every+ # other construction site (incl. the proxy path) creates consumers only when streaming is on.+ self.stream_deltas_enabled = True# Only platforms needing an explicit finalize call (DingTalk AI Cards) force a# redundant final edit; ``is True`` keeps MagicMock adapters out.self._adapter_requires_finalize = getattr(adapter, "REQUIRES_EDIT_FINALIZE", False) is Truediff --git a/tests/gateway/test_interim_only_consumer_warning.py b/tests/gateway/test_interim_only_consumer_warning.pynew file mode 100644index 00000000000..63713feaab0--- /dev/null+++ b/tests/gateway/test_interim_only_consumer_warning.py@@ -0,0 +1,74 @@+"""Regression coverage for #105341 — interim-only stream consumers must not+fire the duplicate-send diagnostic.++With ``streaming.enabled: false`` and ``display.interim_assistant_messages:+true`` the gateway still builds a ``GatewayStreamConsumer`` (interim+commentary is relayed through it), but the consumer is never fed the final+reply's stream deltas. ``_run_agent_mark_streamed_delivery`` could not tell+\"consumer built only for interim messages\" from \"consumer that streamed but+lost its delivery confirmation\", so it logged a guaranteed-false-positive+``possible duplicate send`` warning once per turn.+"""++import asyncio+from types import SimpleNamespace++import pytest++from gateway.run_turn import GatewayTurnMixin+++class _InterimOnlyHarness(GatewayTurnMixin):+ """Mixin with the delivery-confirmation helper stubbed to False so the+ turn always reaches the duplicate-risk diagnostic branch."""++ def _run_agent_stream_confirmed_final_delivery(self, _sc, _final, *, previewed=False):+ return False+++def _run_mark_streamed_delivery(consumer, caplog):+ harness = _InterimOnlyHarness()+ turn_ctx = SimpleNamespace(+ stream_consumer_holder=[consumer],+ source=SimpleNamespace(platform="telegram"),+ session_key="sess-105341",+ )+ with caplog.at_level("WARNING", logger="gateway.run_turn"):+ asyncio.run(+ harness._run_agent_mark_streamed_delivery(+ {"final_response": "olá"}, turn_ctx+ )+ )+ return caplog+++def _consumer(stream_deltas_enabled):+ return SimpleNamespace(+ final_content_delivered=False,+ delivered_final_matches=None,+ message_id=None,+ stream_deltas_enabled=stream_deltas_enabled,+ )+++def test_stream_capable_consumer_still_warns(caplog):+ """Control: a consumer that CAN receive final deltas keeps the diagnostic+ (the wecom ack-timeout case it was written for)."""+ caplog = _run_mark_streamed_delivery(_consumer(True), caplog)+ assert any("possible duplicate send" in r.message for r in caplog.records)+++def test_interim_only_consumer_skips_duplicate_warning(caplog):+ """#105341: consumer created only for interim commentary (streaming off,+ interim messages on) is never fed the final's deltas — no false positive."""+ caplog = _run_mark_streamed_delivery(_consumer(False), caplog)+ assert not any("possible duplicate send" in r.message for r in caplog.records)+++def test_consumer_flag_defaults_true_on_real_consumer():+ """Every construction site that can race the final send keeps the warning:+ the flag defaults to True on the real consumer."""+ from gateway.stream_consumer import GatewayStreamConsumer++ consumer = GatewayStreamConsumer(adapter=SimpleNamespace(), chat_id="c")+ assert consumer.stream_deltas_enabled is True