The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(codex): keep answer text sent beside a tool call when its message item never completes
Tekniume4347f96b195
diff --git a/agent/codex_runtime.py b/agent/codex_runtime.pyindex 3436c38f357..1cb04390545 100644--- a/agent/codex_runtime.py+++ b/agent/codex_runtime.py@@ -1101,10 +1101,17 @@ class _CodexResponseAssembler:# Successful completion orders all output, even when no calls remain pending.# Done items stay authoritative; only successful streams settle missing done events.output: list[Any] = self._settled_output() if self.saw_response_completed else list(self.output_items)- # With only plain text deltas (no tool calls), synthesize one message item.- if not output and self.text_deltas and not self.has_tool_calls:+ # Plain text deltas with no answer message item (the backend never sent its done event): synthesize one,+ # also beside tool calls and commentary items, or the text said around a call is dropped from the turn+ # and never shown (#143893). Commentary/analysis deltas never reach text_deltas.+ if self.text_deltas and not any(+ _event_field(item, "type", "") == "message" and _message_phase(item) not in ("commentary", "analysis")+ for item in output):content = [SimpleNamespace(type="output_text", text="".join(self.text_deltas))]- output = [SimpleNamespace(type="message", role="assistant", status="completed", content=content)]+ message = SimpleNamespace(type="message", role="assistant", status="completed", content=content)+ first_call = next((i for i, item in enumerate(output)+ if "function_call" in str(_event_field(item, "type", ""))), len(output))+ output.insert(first_call, message)# No terminal frame AND no usable content = truncated / rejected stream.if not self.saw_terminal and not output:raise RuntimeError("Codex Responses stream did not emit a terminal response")diff --git a/tests/agent/test_codex_pending_tool_text.py b/tests/agent/test_codex_pending_tool_text.pynew file mode 100644index 00000000000..39a10c7c2af--- /dev/null+++ b/tests/agent/test_codex_pending_tool_text.py@@ -0,0 +1,53 @@+"""Answer text a Responses stream sends beside a tool call, without that message item's done event,+must reach the assistant turn (#143893).++The assembler only synthesized a message from buffered text deltas when the stream carried no tool+call. With a commentary item done and a call pending, the normalizer's ``output_text`` fallback is+skipped too, so the narration vanished from the turn: no bubble, an empty stored assistant row.+"""++from types import SimpleNamespace as NS++from agent.codex_responses_adapter import _normalize_codex_response+from agent.codex_runtime import _consume_codex_event_stream++NARRATION = "Let me read the file first."+++def _stream(with_commentary: bool):+ msg = NS(id="msg_1", type="message", role="assistant", status="in_progress", content=[])+ call = NS(id="fc_1", type="function_call", call_id="call_1", name="read_file", arguments="",+ status="in_progress")+ done_call = NS(id="fc_1", type="function_call", call_id="call_1", name="read_file",+ arguments='{"path": "a.txt"}', status="completed")+ events = [NS(type="response.created", response=NS(id="resp_1"))]+ if with_commentary:+ commentary = NS(id="msg_c", type="message", role="assistant", status="completed", phase="commentary",+ content=[NS(type="output_text", text="Planning.")])+ events += [NS(type="response.output_item.added", output_index=0, item=NS(**{**vars(commentary), "content": []})),+ NS(type="response.output_text.delta", item_id="msg_c", output_index=0, delta="Planning."),+ NS(type="response.output_item.done", output_index=0, item=commentary)]+ events += [+ NS(type="response.output_item.added", output_index=1, item=call),+ NS(type="response.output_item.added", output_index=2, item=msg),+ NS(type="response.output_text.delta", item_id="msg_1", output_index=2, delta=NARRATION),+ NS(type="response.function_call_arguments.delta", item_id="fc_1", output_index=1, delta='{"path": "a.txt"}'),+ NS(type="response.output_item.done", output_index=1, item=done_call),+ NS(type="response.completed", response=NS(id="resp_1", status="completed", output=None,+ usage=NS(input_tokens=1, output_tokens=1, total_tokens=2))),+ ]+ return events+++def test_narration_beside_a_commentary_item_and_a_call_reaches_the_turn():+ response = _consume_codex_event_stream(iter(_stream(with_commentary=True)), model="m")+ assistant, _finish = _normalize_codex_response(response)+ assert NARRATION in (assistant.content or "")+ assert [c.function.name for c in assistant.tool_calls] == ["read_file"]+++def test_narration_beside_a_call_is_carried_once():+ response = _consume_codex_event_stream(iter(_stream(with_commentary=False)), model="m")+ assistant, _finish = _normalize_codex_response(response)+ assert (assistant.content or "").count(NARRATION) == 1+ assert [c.function.name for c in assistant.tool_calls] == ["read_file"]