@@ -1083,6 +1083,13 @@ def _consume_codex_event_stream(
10831083 collected_output_items : List [Any ] = []
10841084 collected_text_deltas : List [str ] = []
10851085 has_tool_calls = False
1086+ # Function calls announced via output_item.added but not yet confirmed by
1087+ # output_item.done, keyed by item id. Some OpenAI-compatible backends omit
1088+ # per-item done events on a successful completion (upstream evidence:
1089+ # anomalyco/opencode#37159); these are settled from accumulated stream
1090+ # state at the terminal event so the tool call executes instead of being
1091+ # silently dropped.
1092+ pending_function_calls : Dict [str , Dict [str , Any ]] = {}
10861093 first_delta_fired = False
10871094 active_message_phase : str | None = None
10881095 commentary_text_deltas : List [str ] = []
@@ -1136,6 +1143,12 @@ def _consume_codex_event_stream(
11361143 active_message_phase = None
11371144 if "function_call" in str (item_type ):
11381145 has_tool_calls = True
1146+ item_id = str (_item_field (item , "id" , "" ))
1147+ if item_id :
1148+ pending_function_calls [item_id ] = {
1149+ "item" : item ,
1150+ "arguments" : "" ,
1151+ }
11391152 continue
11401153
11411154 if "output_text.delta" in event_type or event_type == "response.output_text.delta" :
@@ -1174,7 +1187,24 @@ def _consume_codex_event_stream(
11741187
11751188 if "function_call" in event_type :
11761189 has_tool_calls = True
1177- # fall through — function_call items still get added on output_item.done
1190+ # Accumulate streamed argument deltas for calls announced via
1191+ # output_item.added, so a stream that completes without per-item
1192+ # done events can still be settled from accumulated state.
1193+ if "delta" in event_type :
1194+ delta_args = _event_field (event , "delta" , "" )
1195+ pending = pending_function_calls .get (str (_event_field (event , "item_id" , "" )))
1196+ if pending is not None and delta_args :
1197+ pending ["arguments" ] += delta_args
1198+ continue
1199+ if event_type .endswith ("function_call_arguments.done" ):
1200+ done_args = _event_field (event , "arguments" , "" )
1201+ pending = pending_function_calls .get (str (_event_field (event , "item_id" , "" )))
1202+ if pending is not None and done_args :
1203+ # Per-item arguments.done is authoritative for the
1204+ # accumulated string when the item itself never lands.
1205+ pending ["arguments" ] = done_args
1206+ continue
1207+ # other function_call frames fall through — function_call items still get added on output_item.done
11781208
11791209 if "reasoning" in event_type and "delta" in event_type :
11801210 reasoning_text = _event_field (event , "delta" , "" )
@@ -1189,6 +1219,9 @@ def _consume_codex_event_stream(
11891219 done_item = _event_field (event , "item" )
11901220 if done_item is not None :
11911221 collected_output_items .append (done_item )
1222+ # Confirmed by the authoritative per-item done event; remove
1223+ # from pending so it is not settled twice.
1224+ pending_function_calls .pop (str (_item_field (done_item , "id" , "" )), None )
11921225 done_phase = _item_field (done_item , "phase" , None )
11931226 done_phase = done_phase .strip ().lower () if isinstance (done_phase , str ) else None
11941227 if done_phase == "commentary" and on_commentary_message is not None :
@@ -1261,6 +1294,23 @@ def _consume_codex_event_stream(
12611294 else :
12621295 output = []
12631296
1297+ # Settle function calls that were announced via output_item.added and
1298+ # streamed argument deltas but never confirmed by output_item.done: some
1299+ # OpenAI-compatible backends omit per-item done events on a successful
1300+ # completion (anomalyco/opencode#37159). Done items stay authoritative;
1301+ # this only fills the gap so the call executes instead of vanishing.
1302+ if pending_function_calls and terminal_status == "completed" :
1303+ for pending in pending_function_calls .values ():
1304+ item = pending ["item" ]
1305+ output .append (SimpleNamespace (
1306+ type = "function_call" ,
1307+ id = _item_field (item , "id" , None ),
1308+ call_id = _item_field (item , "call_id" , None ),
1309+ name = _item_field (item , "name" , None ),
1310+ arguments = pending ["arguments" ],
1311+ status = "completed" ,
1312+ ))
1313+
12641314 # If the stream ended without any terminal event AND produced no usable
12651315 # content (no items, no text deltas), surface that as a RuntimeError so
12661316 # callers can distinguish "stream truncated mid-flight / provider rejected
0 commit comments