@@ -323,7 +323,7 @@ describe("validateAnthropicTurns", () => {
323323 ] ) ;
324324 } ) ;
325325
326- it ( "should not merge consecutive assistant messages" , ( ) => {
326+ it ( "merges consecutive assistant messages" , ( ) => {
327327 const msgs = asMessages ( [
328328 { role : "user" , content : [ { type : "text" , text : "Question" } ] } ,
329329 {
@@ -338,7 +338,60 @@ describe("validateAnthropicTurns", () => {
338338
339339 const result = validateAnthropicTurns ( msgs ) ;
340340
341- expect ( result ) . toEqual ( msgs ) ;
341+ expect ( result ) . toEqual ( [
342+ { role : "user" , content : [ { type : "text" , text : "Question" } ] } ,
343+ {
344+ role : "assistant" ,
345+ content : [
346+ { type : "text" , text : "Answer 1" } ,
347+ { type : "text" , text : "Answer 2" } ,
348+ ] ,
349+ } ,
350+ ] ) ;
351+ } ) ;
352+
353+ it ( "merges an injected assistant turn before validating signed tool-result pairing" , ( ) => {
354+ const msgs = asMessages ( [
355+ { role : "user" , content : [ { type : "text" , text : "Use the gateway" } ] } ,
356+ {
357+ role : "assistant" ,
358+ content : makeSignedThinkingGatewayToolCall ( "tool-1" ) ,
359+ stopReason : "toolUse" ,
360+ } ,
361+ {
362+ role : "assistant" ,
363+ content : [ { type : "text" , text : "Subagent completion delivered." } ] ,
364+ stopReason : "stop" ,
365+ } ,
366+ {
367+ role : "toolResult" ,
368+ toolUseId : "tool-1" ,
369+ toolName : "gateway" ,
370+ content : [ { type : "text" , text : "done" } ] ,
371+ isError : false ,
372+ } ,
373+ ] ) ;
374+
375+ const result = validateAnthropicTurns ( msgs ) ;
376+
377+ expect ( result ) . toEqual ( [
378+ { role : "user" , content : [ { type : "text" , text : "Use the gateway" } ] } ,
379+ {
380+ role : "assistant" ,
381+ content : [
382+ ...makeSignedThinkingGatewayToolCall ( "tool-1" ) ,
383+ { type : "text" , text : "Subagent completion delivered." } ,
384+ ] ,
385+ stopReason : "stop" ,
386+ } ,
387+ {
388+ role : "toolResult" ,
389+ toolUseId : "tool-1" ,
390+ toolName : "gateway" ,
391+ content : [ { type : "text" , text : "done" } ] ,
392+ isError : false ,
393+ } ,
394+ ] ) ;
342395 } ) ;
343396
344397 it ( "should handle mixed scenario with steering messages" , ( ) => {
0 commit comments