@@ -660,6 +660,7 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
660660 const pendingToolCalls : PendingToolCall [ ] = [ ] ;
661661 let toolCallSeq = 0 ;
662662 const flushToolCalls = function * ( ) : Generator < AdapterEvent > {
663+ if ( pendingToolCalls . length > 0 ) sawOutput = true ;
663664 for ( const call of pendingToolCalls ) {
664665 if ( ! call . id ) call . id = `call_${ ++ toolCallSeq } ` ;
665666 yield { type : "tool_call_start" , id : call . id , name : call . name } ;
@@ -674,6 +675,7 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
674675 // explicit `[DONE]` sentinel OR a chunk carrying a non-null `finish_reason` (some
675676 // OpenAI-compatible providers omit `[DONE]` but do send finish_reason).
676677 let finishReason : string | undefined ;
678+ let sawOutput = false ;
677679
678680 // Single per-line handler shared by the streaming loop and the EOF residual-frame flush, so
679681 // a final frame is parsed identically wherever it lands (no duplicated, drift-prone parsing).
@@ -740,9 +742,11 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
740742 const delta = choices [ 0 ] . delta ;
741743 if ( delta ) {
742744 if ( typeof delta . reasoning_content === "string" && delta . reasoning_content . length > 0 ) {
745+ sawOutput = true ;
743746 yield { type : "reasoning_raw_delta" , text : delta . reasoning_content } ;
744747 }
745748 if ( typeof delta . content === "string" && delta . content . length > 0 ) {
749+ sawOutput = true ;
746750 yield { type : "text_delta" , text : delta . content } ;
747751 }
748752
@@ -800,13 +804,23 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
800804 if ( buffer . length > 0 ) {
801805 if ( ( yield * handleDataLine ( buffer ) ) === "terminate" ) return ;
802806 }
807+ const hadPendingToolCallsAtEof = pendingToolCalls . length > 0 ;
803808 yield * flushToolCalls ( ) ;
804809 // Reader EOF. A graceful close shows at least one terminal signal: `[DONE]` (returns above),
805810 // a non-null finish_reason (sawFinish), or a trailing usage chunk (providers emit usage only
806811 // at end-of-generation). If NONE of those were seen, the stream was cut mid-flight — fail
807812 // closed so the bridge emits a classified response.failed rather than a silent truncation.
808813 const sawFinish = finishReason !== undefined ;
809- if ( ! sawFinish && pendingUsage === undefined ) {
814+ if ( ! sawFinish && pendingUsage === undefined && hadPendingToolCallsAtEof ) {
815+ debugProviderDiagnostic ( "openai-chat" , "stream-truncated" , {
816+ finishReason : finishReason ?? null ,
817+ hadUsage : false ,
818+ pendingToolCalls : true ,
819+ } ) ;
820+ yield { type : "error" , message : "upstream stream ended without a terminal signal ([DONE] or finish_reason) - possible truncation" } ;
821+ return ;
822+ }
823+ if ( ! sawFinish && pendingUsage === undefined && ! sawOutput ) {
810824 debugProviderDiagnostic ( "openai-chat" , "stream-truncated" , {
811825 finishReason : finishReason ?? null ,
812826 hadUsage : false ,
0 commit comments