@@ -476,6 +476,111 @@ describe("openai-chat stream response hardening", () => {
476476 expect ( lines ) . toContain ( '"callIndex":1' ) ;
477477 expect ( lines ) . not . toContain ( '"tool_call_function_name_invalid"' ) ;
478478 } ) ;
479+
480+ test ( "non-string repeated name/arguments on a resolved call are padding, not terminal" , async ( ) => {
481+ // opencode.ai/zen continuation deltas: the first chunk carries id+name; later repeats
482+ // can arrive with wrong JSON types (object name) instead of the null #1731 describes.
483+ const adapter = createOpenAIChatAdapter ( provider ( ) ) ;
484+ const response = new Response ( [
485+ `data: ${ JSON . stringify ( { choices : [ { delta : { tool_calls : [ {
486+ index : 0 ,
487+ id : "chatcmpl-tool-abc" ,
488+ type : "function" ,
489+ function : { name : "terminal" , arguments : "" } ,
490+ } ] } } ] } ) } \n\n`,
491+ `data: ${ JSON . stringify ( { choices : [ { delta : { tool_calls : [ {
492+ index : 0 ,
493+ id : null ,
494+ type : null ,
495+ function : { name : { unexpected : true } , arguments : { no : "string" } } ,
496+ } ] } } ] } ) } \n\n`,
497+ `data: ${ JSON . stringify ( { choices : [ { delta : { tool_calls : [ {
498+ index : 0 ,
499+ id : null ,
500+ type : null ,
501+ function : { name : null , arguments : "{\"command\":\"ls\"}" } ,
502+ } ] } } ] } ) } \n\n`,
503+ `data: ${ JSON . stringify ( { choices : [ { delta : { } , finish_reason : "tool_calls" } ] , usage : { prompt_tokens : 7 , completion_tokens : 3 } } ) } \n\n` ,
504+ "data: [DONE]\n\n" ,
505+ ] . join ( "" ) ) ;
506+
507+ const events = await collect ( adapter . parseStream ( response ) ) ;
508+ expect ( events ) . toEqual ( [
509+ { type : "tool_call_start" , id : "chatcmpl-tool-abc" , name : "terminal" } ,
510+ { type : "tool_call_delta" , arguments : '{"command":"ls"}' } ,
511+ { type : "tool_call_end" } ,
512+ { type : "done" , usage : { inputTokens : 7 , outputTokens : 3 } } ,
513+ ] ) ;
514+ } ) ;
515+
516+ test ( "non-string name on a call without its canonical value still fails closed" , async ( ) => {
517+ const adapter = createOpenAIChatAdapter ( provider ( ) ) ;
518+ const response = new Response ( [
519+ `data: ${ JSON . stringify ( { choices : [ { delta : { tool_calls : [ {
520+ index : 0 ,
521+ id : "chatcmpl-tool-abc" ,
522+ type : "function" ,
523+ function : { name : { unexpected : true } , arguments : "" } ,
524+ } ] } } ] } ) } \n\n`,
525+ "data: [DONE]\n\n" ,
526+ ] . join ( "" ) ) ;
527+
528+ const events = await collect ( adapter . parseStream ( response ) ) ;
529+ expect ( events ) . toEqual ( [ {
530+ type : "error" ,
531+ status : 502 ,
532+ errorType : "upstream_error" ,
533+ message : "upstream response contained invalid tool calls (tool_call_function_name_invalid; callIndex=0; valueType=object)" ,
534+ } ] ) ;
535+ } ) ;
536+
537+ test ( "non-string arguments on a call that has accumulated nothing still fails closed" , async ( ) => {
538+ const adapter = createOpenAIChatAdapter ( provider ( ) ) ;
539+ const response = new Response ( [
540+ `data: ${ JSON . stringify ( { choices : [ { delta : { tool_calls : [ {
541+ index : 0 ,
542+ id : "chatcmpl-tool-abc" ,
543+ type : "function" ,
544+ function : { name : "terminal" , arguments : { command : "ls" } } ,
545+ } ] } } ] } ) } \n\n`,
546+ "data: [DONE]\n\n" ,
547+ ] . join ( "" ) ) ;
548+
549+ const events = await collect ( adapter . parseStream ( response ) ) ;
550+ expect ( events ) . toEqual ( [ {
551+ type : "error" ,
552+ status : 502 ,
553+ errorType : "upstream_error" ,
554+ message : "upstream response contained invalid tool calls (tool_call_function_arguments_invalid; callIndex=0; valueType=object)" ,
555+ } ] ) ;
556+ } ) ;
557+
558+ test ( "whitespace-only initial name followed by non-string delta fails closed" , async ( ) => {
559+ const adapter = createOpenAIChatAdapter ( provider ( ) ) ;
560+ const response = new Response ( [
561+ `data: ${ JSON . stringify ( { choices : [ { delta : { tool_calls : [ {
562+ index : 0 ,
563+ id : "chatcmpl-tool-abc" ,
564+ type : "function" ,
565+ function : { name : " " , arguments : "" } ,
566+ } ] } } ] } ) } \n\n`,
567+ `data: ${ JSON . stringify ( { choices : [ { delta : { tool_calls : [ {
568+ index : 0 ,
569+ id : null ,
570+ type : null ,
571+ function : { name : { unexpected : true } , arguments : { no : "string" } } ,
572+ } ] } } ] } ) } \n\n`,
573+ "data: [DONE]\n\n" ,
574+ ] . join ( "" ) ) ;
575+
576+ const events = await collect ( adapter . parseStream ( response ) ) ;
577+ expect ( events ) . toEqual ( [ {
578+ type : "error" ,
579+ status : 502 ,
580+ errorType : "upstream_error" ,
581+ message : "upstream response contained invalid tool calls (tool_call_function_name_invalid; callIndex=0; valueType=object)" ,
582+ } ] ) ;
583+ } ) ;
479584} ) ;
480585
481586describe ( "openai-chat credential hardening" , ( ) => {
0 commit comments