@@ -3116,6 +3116,161 @@ describe("ClaudeAdapterLive", () => {
31163116 } ,
31173117 ) ;
31183118
3119+ it . effect (
3120+ "aligns a multi-block snapshot by streamed prefix when the stall predates later blocks" ,
3121+ ( ) => {
3122+ const harness = makeHarness ( ) ;
3123+ const firstParagraph = "Fixing the adapter now." ;
3124+ const secondParagraph = "Second paragraph the stream never reached." ;
3125+ return Effect . gen ( function * ( ) {
3126+ const adapter = yield * ClaudeAdapter ;
3127+
3128+ const runtimeEventsFiber = yield * Stream . take ( adapter . streamEvents , 12 ) . pipe (
3129+ Stream . runCollect ,
3130+ Effect . forkChild ,
3131+ ) ;
3132+
3133+ const session = yield * adapter . startSession ( {
3134+ threadId : THREAD_ID ,
3135+ provider : ProviderDriverKind . make ( "claudeAgent" ) ,
3136+ runtimeMode : "full-access" ,
3137+ } ) ;
3138+
3139+ yield * adapter . sendTurn ( {
3140+ threadId : session . threadId ,
3141+ input : "go" ,
3142+ attachments : [ ] ,
3143+ } ) ;
3144+
3145+ // An earlier message of the same turn completes normally, leaving a completed block
3146+ // at the head of the turn-wide block list.
3147+ harness . query . emit ( {
3148+ type : "stream_event" ,
3149+ session_id : "sdk-session-misaligned-snapshot" ,
3150+ uuid : "misaligned-start-1" ,
3151+ parent_tool_use_id : null ,
3152+ event : {
3153+ type : "content_block_start" ,
3154+ index : 0 ,
3155+ content_block : { type : "text" , text : "" } ,
3156+ } ,
3157+ } as unknown as SDKMessage ) ;
3158+
3159+ harness . query . emit ( {
3160+ type : "stream_event" ,
3161+ session_id : "sdk-session-misaligned-snapshot" ,
3162+ uuid : "misaligned-delta-1" ,
3163+ parent_tool_use_id : null ,
3164+ event : {
3165+ type : "content_block_delta" ,
3166+ index : 0 ,
3167+ delta : { type : "text_delta" , text : "Earlier answer." } ,
3168+ } ,
3169+ } as unknown as SDKMessage ) ;
3170+
3171+ harness . query . emit ( {
3172+ type : "stream_event" ,
3173+ session_id : "sdk-session-misaligned-snapshot" ,
3174+ uuid : "misaligned-stop-1" ,
3175+ parent_tool_use_id : null ,
3176+ event : { type : "content_block_stop" , index : 0 } ,
3177+ } as unknown as SDKMessage ) ;
3178+
3179+ // The next message stalls after one delta of its FIRST text block; its second text
3180+ // block never starts streaming. Naive tail alignment would slide the snapshot's last
3181+ // block onto the stalled one and attach the wrong paragraph.
3182+ harness . query . emit ( {
3183+ type : "stream_event" ,
3184+ session_id : "sdk-session-misaligned-snapshot" ,
3185+ uuid : "misaligned-start-2" ,
3186+ parent_tool_use_id : null ,
3187+ event : {
3188+ type : "content_block_start" ,
3189+ index : 0 ,
3190+ content_block : { type : "text" , text : "" } ,
3191+ } ,
3192+ } as unknown as SDKMessage ) ;
3193+
3194+ harness . query . emit ( {
3195+ type : "stream_event" ,
3196+ session_id : "sdk-session-misaligned-snapshot" ,
3197+ uuid : "misaligned-delta-2" ,
3198+ parent_tool_use_id : null ,
3199+ event : {
3200+ type : "content_block_delta" ,
3201+ index : 0 ,
3202+ delta : { type : "text_delta" , text : "Fixing" } ,
3203+ } ,
3204+ } as unknown as SDKMessage ) ;
3205+
3206+ harness . query . emit ( {
3207+ type : "assistant" ,
3208+ session_id : "sdk-session-misaligned-snapshot" ,
3209+ uuid : "misaligned-assistant-snapshot" ,
3210+ parent_tool_use_id : null ,
3211+ message : {
3212+ id : "assistant-message-misaligned" ,
3213+ content : [
3214+ { type : "text" , text : firstParagraph } ,
3215+ { type : "text" , text : secondParagraph } ,
3216+ ] ,
3217+ } ,
3218+ } as unknown as SDKMessage ) ;
3219+
3220+ harness . query . emit ( {
3221+ type : "result" ,
3222+ subtype : "success" ,
3223+ is_error : false ,
3224+ errors : [ ] ,
3225+ session_id : "sdk-session-misaligned-snapshot" ,
3226+ uuid : "result-misaligned" ,
3227+ } as unknown as SDKMessage ) ;
3228+
3229+ const runtimeEvents = Array . from ( yield * Fiber . join ( runtimeEventsFiber ) ) ;
3230+
3231+ const assistantDeltas = runtimeEvents . filter (
3232+ ( event ) =>
3233+ event . type === "content.delta" && event . payload . streamKind === "assistant_text" ,
3234+ ) ;
3235+ const deltaText = ( event : ( typeof assistantDeltas ) [ number ] ) =>
3236+ event . type === "content.delta" ? String ( event . payload . delta ) : "" ;
3237+
3238+ // Stalled block: streamed prefix plus the repaired suffix, exactly once.
3239+ const stalledItemId = String ( assistantDeltas [ 1 ] ?. itemId ) ;
3240+ const stalledText = assistantDeltas
3241+ . filter ( ( event ) => String ( event . itemId ) === stalledItemId )
3242+ . map ( deltaText )
3243+ . join ( "" ) ;
3244+ assert . equal ( stalledText , firstParagraph ) ;
3245+
3246+ // The paragraph that never streamed is synthesized as its own block, not glued onto
3247+ // the stalled one and not dropped.
3248+ const synthesizedDelta = assistantDeltas . find (
3249+ ( event ) => deltaText ( event ) === secondParagraph ,
3250+ ) ;
3251+ assert . ok ( synthesizedDelta ) ;
3252+ assert . notEqual ( String ( synthesizedDelta ?. itemId ) , stalledItemId ) ;
3253+
3254+ const assistantCompletions = runtimeEvents . filter (
3255+ ( event ) =>
3256+ event . type === "item.completed" && event . payload . itemType === "assistant_message" ,
3257+ ) ;
3258+ // Completion order: the streamed first message (no detail — nothing was backfilled),
3259+ // then the synthesized paragraph (closed right at the snapshot), and the stalled block
3260+ // last — it stays open for late deltas until the turn result force-completes it.
3261+ assert . deepEqual (
3262+ assistantCompletions . map ( ( event ) =>
3263+ event . type === "item.completed" ? event . payload . detail : undefined ,
3264+ ) ,
3265+ [ undefined , secondParagraph , firstParagraph ] ,
3266+ ) ;
3267+ } ) . pipe (
3268+ Effect . provideService ( Random . Random , makeDeterministicRandomService ( ) ) ,
3269+ Effect . provide ( harness . layer ) ,
3270+ ) ;
3271+ } ,
3272+ ) ;
3273+
31193274 it . effect ( "segments Claude assistant text blocks around tool calls" , ( ) => {
31203275 const harness = makeHarness ( ) ;
31213276 return Effect . gen ( function * ( ) {
0 commit comments