@@ -63,6 +63,23 @@ function responseError(status: number, type: string, message: string): OcxErrorP
6363 return classifyError ( status , type , message ) ;
6464}
6565
66+ /**
67+ * Whether assembled function-call arguments are usable JSON.
68+ * An empty buffer is valid (no-arg tools send no deltas). Non-empty must parse —
69+ * once fragments have been streamed to the client they cannot be repaired the way
70+ * non-stream adapters degrade a bad payload to `{}`.
71+ */
72+ function toolCallArgumentsUsable ( args : string ) : boolean {
73+ const trimmed = args . trim ( ) ;
74+ if ( ! trimmed ) return true ;
75+ try {
76+ JSON . parse ( trimmed ) ;
77+ return true ;
78+ } catch {
79+ return false ;
80+ }
81+ }
82+
6683function adapterFailureFromEvent ( event : Extract < AdapterEvent , { type : "error" } > ) : { httpStatus : number ; error : OcxErrorPayload } {
6784 if ( event . status === undefined && event . errorType === undefined && event . code === undefined ) {
6885 return adapterFailureFromMessage ( event . message ) ;
@@ -417,6 +434,39 @@ export function bridgeToResponsesSSE(
417434 currentToolCall = null ;
418435 } ;
419436
437+ // Terminal-error / incomplete path for an open tool call (#765 remainder).
438+ // Closing via closeCurrentToolCall() would emit function_call_arguments.done and
439+ // status:"completed" BEFORE response.failed — the client still sees an issued call.
440+ // Cancel instead: no *.done argument frames, status:"incomplete" (same pattern as an
441+ // in-flight web_search_call closing as "failed"). Args still serialize as "{}" when
442+ // empty so echoed items cannot poison the next turn with JSON.parse("").
443+ const failCurrentToolCall = ( ) => {
444+ if ( ! currentToolCall ) return ;
445+ const argsStr = currentToolCall . args || "{}" ;
446+ const item = currentToolCall . toolSearch
447+ ? {
448+ type : "tool_search_call" , id : currentToolCall . itemId ,
449+ call_id : currentToolCall . callId , execution : "client" ,
450+ arguments : parseArgsObj ( currentToolCall . args ) , status : "incomplete" ,
451+ }
452+ : currentToolCall . freeform
453+ ? {
454+ type : "custom_tool_call" , id : currentToolCall . itemId ,
455+ call_id : currentToolCall . callId , name : currentToolCall . name ,
456+ input : freeformInput ( currentToolCall . args ) , status : "incomplete" ,
457+ }
458+ : {
459+ type : "function_call" , id : currentToolCall . itemId ,
460+ call_id : currentToolCall . callId , name : currentToolCall . name ,
461+ arguments : argsStr , status : "incomplete" ,
462+ ...( currentToolCall . namespace ? { namespace : currentToolCall . namespace } : { } ) ,
463+ } ;
464+ emit ( "response.output_item.done" , { output_index : currentToolCall . outputIndex , item } ) ;
465+ finishedItems . push ( item as OutputItem ) ;
466+ outputIndex ++ ;
467+ currentToolCall = null ;
468+ } ;
469+
420470 // Finalize an open web-search cell. `status` is "completed" on a normal end, or "failed" when
421471 // the stream terminates (error/incomplete) while a search was still in flight, so Codex never
422472 // leaves a "Searching the web" spinner spinning forever.
@@ -653,6 +703,32 @@ export function bridgeToResponsesSSE(
653703 break ;
654704 }
655705 case "tool_call_end" : {
706+ // Fragments already streamed cannot be repaired. Refuse to complete a function call
707+ // whose assembled arguments do not parse — cancel the item and fail the turn so the
708+ // client never sees status:"completed" for unusable args (#765 stream remainder).
709+ if (
710+ currentToolCall
711+ && ! currentToolCall . freeform
712+ && ! currentToolCall . toolSearch
713+ && ! toolCallArgumentsUsable ( currentToolCall . args )
714+ ) {
715+ failCurrentToolCall ( ) ;
716+ const failure = responseError (
717+ 502 ,
718+ "upstream_error" ,
719+ "upstream stream produced malformed tool call arguments" ,
720+ ) ;
721+ emit ( "response.failed" , {
722+ response : {
723+ ...responseSnapshot ( "failed" , finishedItems ) ,
724+ error : failure ,
725+ last_error : failure ,
726+ } ,
727+ } ) ;
728+ reportTerminal ( "failed" ) ;
729+ terminalEvent = true ;
730+ break ;
731+ }
656732 closeCurrentToolCall ( ) ;
657733 break ;
658734 }
@@ -749,7 +825,7 @@ export function bridgeToResponsesSSE(
749825 if ( currentReasoning ) closeCurrentReasoning ( ) ;
750826 if ( currentRawReasoning ) closeCurrentRawReasoning ( ) ;
751827 flushHiddenRawReasoning ( ) ;
752- if ( currentToolCall ) closeCurrentToolCall ( ) ;
828+ if ( currentToolCall ) failCurrentToolCall ( ) ;
753829 if ( currentWebSearch ) closeCurrentWebSearch ( "failed" , [ ] ) ;
754830 flushHiddenReasoningEnvelope ( ) ;
755831 options ?. onUsage ?.( event . usage ) ;
@@ -773,7 +849,7 @@ export function bridgeToResponsesSSE(
773849 if ( currentReasoning ) closeCurrentReasoning ( ) ;
774850 if ( currentRawReasoning ) closeCurrentRawReasoning ( ) ;
775851 flushHiddenRawReasoning ( ) ;
776- if ( currentToolCall ) closeCurrentToolCall ( ) ;
852+ if ( currentToolCall ) failCurrentToolCall ( ) ;
777853 if ( currentWebSearch ) closeCurrentWebSearch ( "failed" , [ ] ) ;
778854 const failure = adapterFailureFromEvent ( event ) ;
779855 if ( event . usage ) options ?. onUsage ?.( event . usage ) ;
@@ -803,6 +879,7 @@ export function bridgeToResponsesSSE(
803879 } catch ( err ) {
804880 if ( ! terminated ) {
805881 flushHiddenRawReasoning ( ) ;
882+ if ( currentToolCall ) failCurrentToolCall ( ) ;
806883 if ( currentWebSearch ) closeCurrentWebSearch ( "failed" , [ ] ) ;
807884 emit ( "response.failed" , {
808885 response : {
@@ -832,7 +909,7 @@ export function bridgeToResponsesSSE(
832909 if ( currentReasoning ) closeCurrentReasoning ( ) ;
833910 if ( currentRawReasoning ) closeCurrentRawReasoning ( ) ;
834911 flushHiddenRawReasoning ( ) ;
835- if ( currentToolCall ) closeCurrentToolCall ( ) ;
912+ if ( currentToolCall ) failCurrentToolCall ( ) ;
836913 if ( currentWebSearch ) closeCurrentWebSearch ( "failed" , [ ] ) ;
837914 options ?. onUsage ?.( undefined ) ;
838915 emit ( "response.incomplete" , {
@@ -872,7 +949,7 @@ export function bridgeToResponsesSSE(
872949 if ( currentReasoning ) closeCurrentReasoning ( ) ;
873950 if ( currentRawReasoning ) closeCurrentRawReasoning ( ) ;
874951 flushHiddenRawReasoning ( ) ;
875- if ( currentToolCall ) closeCurrentToolCall ( ) ;
952+ if ( currentToolCall ) failCurrentToolCall ( ) ;
876953 if ( currentWebSearch ) closeCurrentWebSearch ( "failed" , [ ] ) ;
877954 emit ( "response.incomplete" , {
878955 response : {
@@ -1021,7 +1098,7 @@ export function buildResponseJSON(
10211098 } ) ;
10221099 currentRawReasoning = "" ;
10231100 } ;
1024- const flushToolCall = ( ) => {
1101+ const flushToolCall = ( status : "completed" | "incomplete" = "completed" ) => {
10251102 if ( ! currentToolCallId ) return ;
10261103 const mapped = options ?. toolNsMap ?. get ( currentToolCallName ) ;
10271104 const realName = mapped ?. name ?? currentToolCallName ;
@@ -1032,19 +1109,19 @@ export function buildResponseJSON(
10321109 output . push ( {
10331110 type : "tool_search_call" , id : `tsc_${ uuid ( ) } ` ,
10341111 call_id : currentToolCallId , execution : "client" ,
1035- arguments : parseArgsObj ( currentToolCallArgs ) , status : "completed" ,
1112+ arguments : parseArgsObj ( currentToolCallArgs ) , status,
10361113 } ) ;
10371114 } else if ( freeform ) {
10381115 output . push ( {
10391116 type : "custom_tool_call" , id : `ctc_${ uuid ( ) } ` ,
10401117 call_id : currentToolCallId , name : realName ,
1041- input : freeformInput ( currentToolCallArgs ) , status : "completed" ,
1118+ input : freeformInput ( currentToolCallArgs ) , status,
10421119 } ) ;
10431120 } else {
10441121 output . push ( {
10451122 type : "function_call" , id : `fc_${ uuid ( ) } ` ,
10461123 call_id : currentToolCallId , name : realName ,
1047- arguments : currentToolCallArgs || "{}" , status : "completed" ,
1124+ arguments : currentToolCallArgs || "{}" , status,
10481125 ...( ns ? { namespace : ns } : { } ) ,
10491126 } ) ;
10501127 }
@@ -1110,6 +1187,23 @@ export function buildResponseJSON(
11101187 currentToolCallArgs += e . arguments ;
11111188 break ;
11121189 case "tool_call_end" :
1190+ if ( ! toolCallArgumentsUsable ( currentToolCallArgs ) && currentToolCallId ) {
1191+ // Mirror the streaming path: refuse to complete unusable arguments.
1192+ const mapped = options ?. toolNsMap ?. get ( currentToolCallName ) ;
1193+ const realName = mapped ?. name ?? currentToolCallName ;
1194+ const toolSearch = options ?. toolSearchToolNames ?. has ( realName ) ?? false ;
1195+ const freeform = ! toolSearch && ( options ?. freeformToolNames ?. has ( realName ) ?? false ) ;
1196+ if ( ! freeform && ! toolSearch ) {
1197+ flushToolCall ( "incomplete" ) ;
1198+ errorEvent = {
1199+ type : "error" ,
1200+ message : "upstream stream produced malformed tool call arguments" ,
1201+ status : 502 ,
1202+ errorType : "upstream_error" ,
1203+ } ;
1204+ break ;
1205+ }
1206+ }
11131207 flushToolCall ( ) ;
11141208 break ;
11151209 case "web_search_call_begin" :
@@ -1155,7 +1249,8 @@ export function buildResponseJSON(
11551249 flushText ( cleanDone && ! errorEvent && ! incompleteEvent ? "final_answer" : undefined ) ;
11561250 flushSummaryReasoning ( ) ;
11571251 flushRawReasoning ( ) ;
1158- flushToolCall ( ) ;
1252+ // Open tool call on a failed/incomplete turn must not land as status:"completed".
1253+ if ( currentToolCallId ) flushToolCall ( errorEvent || incompleteEvent ? "incomplete" : "completed" ) ;
11591254 // A truncated turn must never be installed as replacement history: emit the
11601255 // compaction item only when the turn actually completed (#422).
11611256 if (
0 commit comments