@@ -784,6 +784,104 @@ describe("session.message-v2.toModelMessage", () => {
784784 } ,
785785 ] )
786786 } )
787+
788+ test ( "does not produce interleaved tool-call and text/reasoning in a single assistant block when step boundaries are missing" , ( ) => {
789+ // When the finish-step handler in processor.ts throws during a retryable error,
790+ // step-finish for step 1 and step-start for step 2 are never saved. Both steps'
791+ // content merges into one DB message without boundaries. On replay,
792+ // convertToModelMessages() produces a single assistant block with interleaved
793+ // tool_use/reasoning/text, which the Anthropic API rejects with:
794+ // "tool_use ids were found without tool_result blocks immediately after"
795+ // or "Expected thinking or redacted_thinking, but found tool_use"
796+ //
797+ // Real-world DB evidence from session ses_32fb35486ffeeJAHmplKU1gB2t:
798+ // step-start → text → tool(write, error, input={}) → [96s gap] → text → tool(write, completed) → step-finish
799+ // The error tool had "Tool execution aborted" and empty input — no step boundary between the two steps.
800+ const userID = "m-user"
801+ const assistantID = "m-assistant"
802+
803+ const input : MessageV2 . WithParts [ ] = [
804+ {
805+ info : userInfo ( userID ) ,
806+ parts : [
807+ {
808+ ...basePart ( userID , "u1" ) ,
809+ type : "text" ,
810+ text : "write the file" ,
811+ } ,
812+ ] as MessageV2 . Part [ ] ,
813+ } ,
814+ {
815+ info : assistantInfo ( assistantID , userID ) ,
816+ parts : [
817+ {
818+ ...basePart ( assistantID , "s1" ) ,
819+ type : "step-start" ,
820+ } ,
821+ {
822+ ...basePart ( assistantID , "t1" ) ,
823+ type : "text" ,
824+ text : "Now let me write the implementation." ,
825+ } ,
826+ {
827+ // Step 1's tool — errored with empty input (aborted before tool-call event)
828+ ...basePart ( assistantID , "tool1" ) ,
829+ type : "tool" ,
830+ callID : "call-1" ,
831+ tool : "write" ,
832+ state : {
833+ status : "error" ,
834+ input : { } ,
835+ error : "Tool execution aborted" ,
836+ time : { start : 0 , end : 1 } ,
837+ } ,
838+ } ,
839+ // NO step-finish or step-start here — the boundary was lost
840+ {
841+ ...basePart ( assistantID , "t2" ) ,
842+ type : "text" ,
843+ text : "Now let me write the implementation." ,
844+ } ,
845+ {
846+ // Step 2's tool — completed successfully on retry
847+ ...basePart ( assistantID , "tool2" ) ,
848+ type : "tool" ,
849+ callID : "call-2" ,
850+ tool : "write" ,
851+ state : {
852+ status : "completed" ,
853+ input : { filePath : "/tmp/test.ts" , content : "export const x = 1" } ,
854+ output : "ok" ,
855+ title : "Write" ,
856+ metadata : { } ,
857+ time : { start : 2 , end : 3 } ,
858+ } ,
859+ } ,
860+ // step-finish only exists for the final step
861+ ] as MessageV2 . Part [ ] ,
862+ } ,
863+ ]
864+
865+ const result = MessageV2 . toModelMessages ( input , model )
866+
867+ // Structural invariant: in every assistant ModelMessage, no text or reasoning
868+ // part should appear AFTER a tool-call part. If it does, the Anthropic API
869+ // will reject the message because it expects tool_result immediately after tool_use.
870+ for ( const msg of result ) {
871+ if ( msg . role !== "assistant" || ! Array . isArray ( msg . content ) ) continue
872+ let sawToolCall = false
873+ for ( const part of msg . content as any [ ] ) {
874+ if ( part . type === "tool-call" ) sawToolCall = true
875+ if ( sawToolCall && ( part . type === "text" || part . type === "reasoning" ) ) {
876+ throw new Error (
877+ `Invalid interleaving: found "${ part . type } " part after "tool-call" in the same assistant message. ` +
878+ `This violates the Anthropic API requirement that tool_result must immediately follow tool_use. ` +
879+ `Content types in this message: [${ ( msg . content as any [ ] ) . map ( ( p : any ) => p . type ) . join ( ", " ) } ]` ,
880+ )
881+ }
882+ }
883+ }
884+ } )
787885} )
788886
789887describe ( "session.message-v2.fromError" , ( ) => {
0 commit comments