Skip to content

The agent that grows with you

fix(gateway): codex app-server final replies are no longer delivered twice

teknium130f4cc707ccd
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index 7fecca5fd0b..26b2dd85a66 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -3320,13 +3320,17 @@ class GatewayTurnMixin:
if matcher(final_text) is False:
return False
return True
- if previewed:
- has_delivered_text = getattr(consumer, "has_delivered_text", None)
- if callable(has_delivered_text):
- try:
- return bool(has_delivered_text(final_text))
- except Exception:
- return False
+ # Exact-text match against what the consumer genuinely delivered (visible prefix, commentary,
+ # segments) — safe without the ``previewed`` flag. The codex app-server bridge delivers the
+ # final agentMessage through the commentary path and never sets response_previewed
+ # (#74248 / #80519); gating on the flag re-sent every such reply. Mismatching commentary
+ # still returns False, so a distinct final answer is never suppressed (#65919).
+ has_delivered_text = getattr(consumer, "has_delivered_text", None)
+ if callable(has_delivered_text):
+ try:
+ return bool(has_delivered_text(final_text))
+ except Exception:
+ return False
return False
def _run_agent_start_turn_worker(self, turn_ctx: TurnContext, run_sync: Callable[[], Any]) -> "GatewayRunner._RunAgentWorker":
diff --git a/tests/gateway/test_stream_consumer.py b/tests/gateway/test_stream_consumer.py
index 9ee121eefb2..a99c1bd2788 100644
--- a/tests/gateway/test_stream_consumer.py
+++ b/tests/gateway/test_stream_consumer.py
@@ -1396,6 +1396,32 @@ class TestStripOrphanCloseTags:
assert GatewayStreamConsumer._strip_orphan_close_tags("") == ""
+class TestConfirmedFinalDeliveryConsultsCommentaryRecord:
+ """The gateway's final-send predicate must recognise a final reply the consumer already
+ delivered through the commentary path even when the runtime never set ``response_previewed``
+ (codex app-server final agentMessage, #74248 / #80519) — and must still send a distinct final."""
+
+ @staticmethod
+ def _consumer_after_commentary(text):
+ adapter = MagicMock()
+ adapter.send = AsyncMock(return_value=SimpleNamespace(success=True, message_id="m1"))
+ c = GatewayStreamConsumer(adapter=adapter, chat_id="c1")
+ assert asyncio.run(c._send_commentary(text)) is True
+ return c
+
+ def test_same_text_delivered_as_commentary_is_confirmed_without_previewed_flag(self):
+ from gateway.run_turn import GatewayTurnMixin
+ c = self._consumer_after_commentary("Native compaction is active.")
+ assert GatewayTurnMixin._run_agent_stream_confirmed_final_delivery(
+ c, "Native compaction is active.", previewed=False) is True
+
+ def test_distinct_final_after_commentary_is_still_sent(self):
+ from gateway.run_turn import GatewayTurnMixin
+ c = self._consumer_after_commentary("Checking the compaction config first.")
+ assert GatewayTurnMixin._run_agent_stream_confirmed_final_delivery(
+ c, "Native compaction is active.", previewed=False) is False
+
+
class TestHasDeliveredTextAfterSegmentBreak:
"""has_delivered_text must find a delivered segment after a segment break,
but must not claim text from a failed delivery. (#65919 review)"""