@@ -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
0 commit comments