Skip to content

The agent that grows with you

fix(codex): reset the streamed-text buffer at each completed agentMessage

teknium11484f82c2685
diff --git a/agent/codex_runtime.py b/agent/codex_runtime.py
index 7f9f4dba2f5..5532b5f9e66 100644
--- a/agent/codex_runtime.py
+++ b/agent/codex_runtime.py
@@ -337,6 +337,11 @@ def make_codex_app_server_event_bridge(agent) -> Callable[[dict], None]:
if isinstance(text, str) and text.strip() and getattr(agent, "show_commentary", True):
agent_cb("_emit_interim_assistant_message", "_emit_interim_assistant_message raised",
args=({"role": "assistant", "content": text},))
+ # Each agentMessage item is its own delivered message: the completed item was just compared
+ # against ITS deltas, so drop them before the next item's deltas arrive. Otherwise the buffer
+ # holds "commentary + final", the final agentMessage no longer prefix-matches, and it is
+ # re-delivered with already_streamed=False as a second copy (#74248 boundary 2).
+ agent._current_streamed_assistant_text = ""
def _on_item(params: dict, completed: bool) -> None:
item = params.get("item")
diff --git a/tests/agent/test_run_agent_codex_responses.py b/tests/agent/test_run_agent_codex_responses.py
index 3638e5679ff..c67b406261c 100644
--- a/tests/agent/test_run_agent_codex_responses.py
+++ b/tests/agent/test_run_agent_codex_responses.py
@@ -1997,6 +1997,31 @@ def test_interim_commentary_is_not_marked_already_streamed_without_callbacks(mon
}
+def test_app_server_bridge_commentary_then_final_agent_messages_are_each_already_streamed(monkeypatch):
+ """#74248 boundary 2: codex app-server emits commentary deltas + completed, then final deltas +
+ completed. The completed final must compare against ITS OWN deltas, not "commentary + final", or
+ it is re-delivered with already_streamed=False and the gateway posts a second copy."""
+ from agent.codex_runtime import make_codex_app_server_event_bridge
+
+ agent = _build_agent(monkeypatch)
+ agent.stream_delta_callback = lambda text: None
+ deliveries = []
+ agent.interim_assistant_callback = lambda text, *, already_streamed=False: deliveries.append(
+ (text, already_streamed)
+ )
+ on_event = make_codex_app_server_event_bridge(agent)
+
+ def _agent_message(item_id, text, phase):
+ on_event({"method": "item/agentMessage/delta", "params": {"itemId": item_id, "delta": text}})
+ on_event({"method": "item/completed", "params": {
+ "item": {"id": item_id, "type": "agentMessage", "text": text, "phase": phase}}})
+
+ _agent_message("m1", "Checking the config.", "commentary")
+ _agent_message("m2", "Native compaction is active.", "final_answer")
+
+ assert deliveries == [("Checking the config.", True), ("Native compaction is active.", True)]
+ assert agent._current_streamed_assistant_text == ""
+
def test_interim_content_was_streamed_matches_prefix_not_exact(monkeypatch):