Skip to content

Commit 720344c

Browse files
cxxCoolStarteknium1
authored andcommitted
fix(codex): settle pending Responses tool calls when output_item.done is omitted
Backends that omit per-item done events on a successful completion (anomalyco/opencode#37159) caused an announced function call to be silently dropped: the turn ended with output == [] and the tool never executed. Track calls announced via output_item.added, accumulate argument deltas, and settle still-pending calls from accumulated state at a successful terminal event. output_item.done stays authoritative. Mirrors anomalyco/opencode#43575.
1 parent 081cdd9 commit 720344c

2 files changed

Lines changed: 164 additions & 1 deletion

File tree

‎agent/codex_runtime.py‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,13 @@ def _consume_codex_event_stream(
10861086
collected_output_items: List[Any] = []
10871087
collected_text_deltas: List[str] = []
10881088
has_tool_calls = False
1089+
# Function calls announced via output_item.added but not yet confirmed by
1090+
# output_item.done, keyed by item id. Some OpenAI-compatible backends omit
1091+
# per-item done events on a successful completion (upstream evidence:
1092+
# anomalyco/opencode#37159); these are settled from accumulated stream
1093+
# state at the terminal event so the tool call executes instead of being
1094+
# silently dropped.
1095+
pending_function_calls: Dict[str, Dict[str, Any]] = {}
10891096
first_delta_fired = False
10901097
active_message_phase: str | None = None
10911098
commentary_text_deltas: List[str] = []
@@ -1143,6 +1150,12 @@ def _consume_codex_event_stream(
11431150
active_message_phase = None
11441151
if "function_call" in str(item_type):
11451152
has_tool_calls = True
1153+
item_id = str(_item_field(item, "id", ""))
1154+
if item_id:
1155+
pending_function_calls[item_id] = {
1156+
"item": item,
1157+
"arguments": "",
1158+
}
11461159
continue
11471160

11481161
if "output_text.delta" in event_type or event_type == "response.output_text.delta":
@@ -1181,7 +1194,24 @@ def _consume_codex_event_stream(
11811194

11821195
if "function_call" in event_type:
11831196
has_tool_calls = True
1184-
# fall through — function_call items still get added on output_item.done
1197+
# Accumulate streamed argument deltas for calls announced via
1198+
# output_item.added, so a stream that completes without per-item
1199+
# done events can still be settled from accumulated state.
1200+
if "delta" in event_type:
1201+
delta_args = _event_field(event, "delta", "")
1202+
pending = pending_function_calls.get(str(_event_field(event, "item_id", "")))
1203+
if pending is not None and delta_args:
1204+
pending["arguments"] += delta_args
1205+
continue
1206+
if event_type.endswith("function_call_arguments.done"):
1207+
done_args = _event_field(event, "arguments", "")
1208+
pending = pending_function_calls.get(str(_event_field(event, "item_id", "")))
1209+
if pending is not None and done_args:
1210+
# Per-item arguments.done is authoritative for the
1211+
# accumulated string when the item itself never lands.
1212+
pending["arguments"] = done_args
1213+
continue
1214+
# other function_call frames fall through — function_call items still get added on output_item.done
11851215

11861216
if "reasoning" in event_type and "delta" in event_type:
11871217
reasoning_text = _event_field(event, "delta", "")
@@ -1207,6 +1237,9 @@ def _consume_codex_event_stream(
12071237
done_item = _event_field(event, "item")
12081238
if done_item is not None:
12091239
collected_output_items.append(done_item)
1240+
# Confirmed by the authoritative per-item done event; remove
1241+
# from pending so it is not settled twice.
1242+
pending_function_calls.pop(str(_item_field(done_item, "id", "")), None)
12101243
done_phase = _item_field(done_item, "phase", None)
12111244
done_phase = done_phase.strip().lower() if isinstance(done_phase, str) else None
12121245
if done_phase == "commentary" and on_commentary_message is not None:
@@ -1279,6 +1312,23 @@ def _consume_codex_event_stream(
12791312
else:
12801313
output = []
12811314

1315+
# Settle function calls that were announced via output_item.added and
1316+
# streamed argument deltas but never confirmed by output_item.done: some
1317+
# OpenAI-compatible backends omit per-item done events on a successful
1318+
# completion (anomalyco/opencode#37159). Done items stay authoritative;
1319+
# this only fills the gap so the call executes instead of vanishing.
1320+
if pending_function_calls and terminal_status == "completed":
1321+
for pending in pending_function_calls.values():
1322+
item = pending["item"]
1323+
output.append(SimpleNamespace(
1324+
type="function_call",
1325+
id=_item_field(item, "id", None),
1326+
call_id=_item_field(item, "call_id", None),
1327+
name=_item_field(item, "name", None),
1328+
arguments=pending["arguments"],
1329+
status="completed",
1330+
))
1331+
12821332
# If the stream ended without any terminal event AND produced no usable
12831333
# content (no items, no text deltas), surface that as a RuntimeError so
12841334
# callers can distinguish "stream truncated mid-flight / provider rejected
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"""Regression: settle pending function calls when a Responses stream
2+
completes successfully without ``response.output_item.done``.
3+
4+
Some OpenAI-compatible backends (see anomalyco/opencode#37159, fixed in
5+
anomalyco/opencode#43575) omit per-item ``response.output_item.done``
6+
events on a successful completion. ``_consume_codex_event_stream`` assembles
7+
its final ``output`` purely from ``.done`` items, so a function call that
8+
was announced via ``response.output_item.added`` and streamed argument
9+
deltas is silently dropped: the turn ends with an empty output and the tool
10+
never executes.
11+
12+
These tests pin the desired behavior (settle the pending call from the
13+
accumulated stream state at the terminal event, mirroring the opencode fix
14+
semantics). They fail against current ``main`` — that red state is the
15+
reproduction for the linked issue.
16+
"""
17+
18+
from types import SimpleNamespace
19+
20+
from agent.codex_runtime import _consume_codex_event_stream
21+
22+
23+
def _stream_completed_without_done():
24+
"""Successful Responses stream whose only function call never receives
25+
``response.output_item.done`` (backend omits it; terminal response
26+
carries ``output=None`` so there is nothing to reconstruct from)."""
27+
return [
28+
SimpleNamespace(
29+
type="response.created",
30+
response=SimpleNamespace(id="resp_1"),
31+
),
32+
SimpleNamespace(
33+
type="response.output_item.added",
34+
output_index=0,
35+
item=SimpleNamespace(
36+
type="function_call",
37+
id="fc_1",
38+
call_id="call_1",
39+
name="get_weather",
40+
arguments="",
41+
),
42+
),
43+
SimpleNamespace(
44+
type="response.function_call_arguments.delta",
45+
item_id="fc_1",
46+
output_index=0,
47+
delta='{"city"',
48+
),
49+
SimpleNamespace(
50+
type="response.function_call_arguments.delta",
51+
item_id="fc_1",
52+
output_index=0,
53+
delta=': "SF"}',
54+
),
55+
# NOTE: no response.output_item.done for fc_1.
56+
SimpleNamespace(
57+
type="response.completed",
58+
response=SimpleNamespace(
59+
id="resp_1",
60+
status="completed",
61+
usage=SimpleNamespace(
62+
input_tokens=10, output_tokens=5, total_tokens=15
63+
),
64+
output=None,
65+
),
66+
),
67+
]
68+
69+
70+
def test_completed_without_done_settles_pending_function_call():
71+
final = _consume_codex_event_stream(_stream_completed_without_done(), model="gpt-test")
72+
73+
calls = [
74+
item
75+
for item in final.output
76+
if getattr(item, "type", "") == "function_call"
77+
]
78+
assert calls, (
79+
"function_call announced via output_item.added (+ argument deltas) "
80+
"was silently dropped on successful completion without "
81+
"output_item.done; the tool never executes"
82+
)
83+
settled = calls[0]
84+
assert getattr(settled, "name", None) == "get_weather"
85+
assert getattr(settled, "arguments", None) == '{"city": "SF"}'
86+
assert final.status == "completed"
87+
88+
89+
def test_control_stream_with_done_still_authoritative():
90+
"""Sanity: the same stream WITH output_item.done must keep working
91+
(the fix must not regress the normal path or override .done data)."""
92+
events = _stream_completed_without_done()
93+
done = SimpleNamespace(
94+
type="response.output_item.done",
95+
output_index=0,
96+
item=SimpleNamespace(
97+
type="function_call",
98+
id="fc_1",
99+
call_id="call_1",
100+
name="get_weather",
101+
arguments='{"city": "SF"}',
102+
),
103+
)
104+
events.insert(-1, done)
105+
final = _consume_codex_event_stream(events, model="gpt-test")
106+
107+
calls = [
108+
item
109+
for item in final.output
110+
if getattr(item, "type", "") == "function_call"
111+
]
112+
assert calls and calls[0].arguments == '{"city": "SF"}'
113+
assert final.status == "completed"

0 commit comments

Comments
 (0)