Skip to content

The agent that grows with you

fix(tui): mark a final already delivered as an interim as previewed (#125951)

Brooklyn Nicholson1bb66fc892de
diff --git a/tests/tui_gateway/test_turn_payload_interim_final.py b/tests/tui_gateway/test_turn_payload_interim_final.py
new file mode 100644
index 00000000000..5375fa18d61
--- /dev/null
+++ b/tests/tui_gateway/test_turn_payload_interim_final.py
@@ -0,0 +1,50 @@
+"""message.complete marks a final already delivered as an interim as previewed (#125951).
+
+The Codex app-server bridge sends every completed agentMessage, the final one included,
+through the interim path and never sets ``response_previewed``. Without the flag the
+TUI and Desktop keep the sealed interim AND render the final: the reply shows twice.
+"""
+
+import contextlib
+from types import SimpleNamespace
+
+import tui_gateway.server as srv
+from agent.stream_delivery import StreamDeliveryMixin
+
+
+class _Agent(StreamDeliveryMixin):
+ _session_title_hint = "Scratch"
+
+
+def _turn(agent, text):
+ return SimpleNamespace(
+ result={"final_response": text}, agent=agent, terminal_callback=None,
+ receipt_committed=True, receipt_attempted=False, marker_key="", error_retained=False,
+ error_detail="", prompt_text="ping",
+ )
+
+
+def _payload(monkeypatch, agent, text):
+ monkeypatch.setattr(srv, "_get_usage", lambda _agent: {})
+ monkeypatch.setattr(srv, "render_message", lambda _text, _cols: None)
+ monkeypatch.setattr(srv, "_clear_inflight_turn", lambda _session: None)
+ session = {"pending_title": None, "session_key": "k", "history_lock": contextlib.nullcontext(), "agent": agent}
+ payload, _, _ = srv._complete_turn_payload(session, _turn(agent, text), None, 80)
+ return payload
+
+
+def test_final_delivered_as_interim_is_previewed(monkeypatch):
+ agent = _Agent()
+ agent._record_delivered_interim_text("Here is the answer.\n")
+
+ payload = _payload(monkeypatch, agent, "Here is the answer.")
+
+ assert payload["response_previewed"] is True
+ assert srv._event_frame("message.complete", "sid", payload)["params"]["payload"]["response_previewed"] is True
+
+
+def test_final_distinct_from_interim_commentary_is_not_previewed(monkeypatch):
+ agent = _Agent()
+ agent._record_delivered_interim_text("Looking at the config first.")
+
+ assert "response_previewed" not in _payload(monkeypatch, agent, "Here is the answer.")
diff --git a/tui_gateway/prompt_turn.py b/tui_gateway/prompt_turn.py
index 708fa4a2e20..24e2edda15e 100644
--- a/tui_gateway/prompt_turn.py
+++ b/tui_gateway/prompt_turn.py
@@ -879,7 +879,10 @@ def _complete_turn_payload(session: dict, st: _TurnRun, status_note: str | None,
payload["reasoning"] = last_reasoning
if status_note:
payload["warning"] = status_note
- if result.get("response_previewed"):
+ # A runtime that delivers its final message as an interim (the Codex app-server bridge routes every
+ # completed agentMessage there) never sets response_previewed; the client would render it twice (#125951).
+ was_delivered = getattr(agent, "_interim_text_was_delivered", None)
+ if result.get("response_previewed") or (callable(was_delivered) and was_delivered(raw) is True):
payload["response_previewed"] = True
# transform_llm_output may rewrite the final after streaming: the renderer must treat
# this payload as the authoritative replacement even without a prefix relationship.