Skip to content

The agent that grows with you

fix: queued-follow-up lane marks the first response delivered so the completion path never re-sends it

teknium1ddd3c5429a82
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index b8b16459824..8cd49381521 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -3716,6 +3716,16 @@ class GatewayTurnMixin:
)
except Exception as e:
logger.warning("Failed to send first response before queued message: %s", e)
+ else:
+ # One source of truth for "this turn's final already reached the chat": the normal
+ # completion path (`_hmwa_deliver_turn_response`) consults ``already_sent`` on whatever
+ # result the queued lane hands back. Every early `return result` after this point
+ # (follow-up text refused, stale goal continuation) otherwise re-sends the text the
+ # fallback just delivered — the #81052 duplicate. Mark both result objects: the
+ # lane may return either one.
+ for _r in (response, result):
+ if isinstance(_r, dict):
+ _r["already_sent"] = True
# Release deferred bg-review notifications: pop (no double-fire in base.py's finally) and call.
_bg_cb = self._pop_post_delivery_callback(adapter, session_key, turn_ctx.run_generation)
if callable(_bg_cb):
diff --git a/tests/gateway/test_queued_followup_final_delivery.py b/tests/gateway/test_queued_followup_final_delivery.py
new file mode 100644
index 00000000000..d3bd55a9880
--- /dev/null
+++ b/tests/gateway/test_queued_followup_final_delivery.py
@@ -0,0 +1,77 @@
+"""Queued-follow-up lane: the fallback first-response send and the normal completion send share one
+"already delivered" source of truth (#81052).
+
+With streaming disabled (the shipped default) there is never a stream consumer, so the queued
+lane always sends the first turn's final itself before running the follow-up. When the lane then
+hands the FIRST turn's result back (the follow-up's text was refused), the normal completion path
+must see ``already_sent`` and not send that text a second time.
+"""
+
+import pytest
+
+from gateway.config import Platform
+from gateway.platforms.event import MessageEvent, MessageType, SessionSource
+from gateway.run import GatewayRunner
+from tests.gateway.test_run_progress_topics import ProgressCaptureAdapter, _make_runner, _run_with_agent
+
+
+class _FirstTurnAgent:
+ calls = 0
+
+ def __init__(self, **kwargs):
+ self.tools = []
+
+ def run_conversation(self, message, conversation_history=None, task_id=None, **kwargs):
+ type(self).calls += 1
+ return {"final_response": f"answer {type(self).calls}", "messages": [], "api_calls": 1}
+
+
+async def test_refused_followup_returns_first_result_marked_delivered(monkeypatch, tmp_path):
+ """Follow-up text refused after the fallback send: the returned first-turn result carries
+ ``already_sent`` so the caller's normal send is suppressed, and the text went out exactly once."""
+ _FirstTurnAgent.calls = 0
+ monkeypatch.setattr(
+ GatewayRunner, "_expand_inbound_context_references",
+ lambda self, source, session_key, message_text: _none(),
+ )
+ adapter, result = await _run_with_agent(
+ monkeypatch, tmp_path, _FirstTurnAgent, session_id="sess-refused-followup",
+ pending_text="@file:/etc/shadow please",
+ )
+ assert _FirstTurnAgent.calls == 1
+ assert [c["content"] for c in adapter.sent] == ["answer 1"]
+ assert result["final_response"] == "answer 1"
+ assert result.get("already_sent") is True
+
+
+async def test_completion_path_skips_body_the_queued_lane_delivered(monkeypatch, tmp_path):
+ """Control at the completion seam: a result the queued lane marked delivered is not re-sent,
+ while an unmarked result still is."""
+ adapter = ProgressCaptureAdapter(platform=Platform.TELEGRAM)
+ runner = _make_runner(adapter)
+ source = SessionSource(platform=adapter.platform, chat_id="-1001", chat_type="group", user_id="u1")
+ event = MessageEvent(text="hi", message_type=MessageType.TEXT, source=source, message_id="1")
+
+ class _Entry:
+ session_id = "s1"
+
+ async def _noop_media(*a, **k):
+ return None
+
+ monkeypatch.setattr(runner, "_deliver_media_from_response", _noop_media)
+ delivered = await runner._hmwa_deliver_turn_response(
+ event, source, _Entry(), "agent:main:telegram:group:-1001", None,
+ {"final_response": "answer 1", "already_sent": True}, [], "answer 1", None, False,
+ )
+ assert delivered is None
+ not_marked = await runner._hmwa_deliver_turn_response(
+ event, source, _Entry(), "agent:main:telegram:group:-1001", None,
+ {"final_response": "answer 1"}, [], "answer 1", None, False,
+ )
+ assert not_marked == "answer 1"
+
+
+async def _none():
+ return None