@@ -18,7 +18,11 @@ import {
1818 workEntryDisplayIndicatesToolFailure ,
1919 type TimelineEntry ,
2020} from "../../session-logic" ;
21- import { makeStreamingTimelineFixture } from "../../test-fixtures" ;
21+ import {
22+ makeStreamingTimelineFixture ,
23+ makeStreamingWorkTimelineFixture ,
24+ withStreamedText ,
25+ } from "../../test-fixtures" ;
2226import type { TurnDiffSummary } from "../../types" ;
2327import { describe , expect , it } from "vite-plus/test" ;
2428import { MessageId , RunId } from "@t3tools/contracts" ;
@@ -3891,6 +3895,220 @@ describe("streaming v2 row projection", () => {
38913895 } ) ) ,
38923896 } ) ;
38933897 } ) ;
3898+
3899+ it . each ( [ "reasoning" , "proposed_plan" ] as const ) (
3900+ "keeps every row not holding streaming %s text when it changes" ,
3901+ ( kind ) => {
3902+ const source = makeStreamingWorkTimelineFixture ( kind ) ;
3903+ const rowsInput = ( timelineEntries : ReadonlyArray < TimelineEntry > ) => ( {
3904+ timelineEntries,
3905+ latestRun : {
3906+ runId : source . runId ,
3907+ status : "running" as const ,
3908+ startedAt : source . time ( 5 ) ,
3909+ completedAt : null ,
3910+ } ,
3911+ runningRunId : source . runId ,
3912+ isWorking : true ,
3913+ activeTurnStartedAt : source . time ( 5 ) ,
3914+ turnDiffSummaries : [ ] ,
3915+ supportsConversationRollback : true ,
3916+ } ) ;
3917+ let timelineInput = {
3918+ visibleTurnItems : withStreamedText (
3919+ source . visibleTurnItems ,
3920+ source . streamingItemId ,
3921+ "Let" ,
3922+ source . time ( 20 ) ,
3923+ ) ,
3924+ optimisticMessages : [ ] ,
3925+ } ;
3926+ let timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState ( timelineInput ) ;
3927+ let projection = deriveMessagesTimelineRowsWithState ( rowsInput ( timeline . entries ) ) ;
3928+ for ( const [ step , text ] of [ "Let me" , "Let me check the tests" ] . entries ( ) ) {
3929+ timelineInput = {
3930+ visibleTurnItems : withStreamedText (
3931+ timelineInput . visibleTurnItems ,
3932+ source . streamingItemId ,
3933+ text ,
3934+ source . time ( 21 + step ) ,
3935+ ) ,
3936+ optimisticMessages : [ ] ,
3937+ } ;
3938+ const previous = projection ;
3939+ timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState ( timelineInput , timeline ) ;
3940+ projection = deriveMessagesTimelineRowsWithState ( rowsInput ( timeline . entries ) , previous ) ;
3941+ expect ( projection . rows ) . toEqual (
3942+ deriveMessagesTimelineRows (
3943+ rowsInput ( deriveTimelineEntriesFromVisibleTurnItems ( timelineInput ) ) ,
3944+ ) ,
3945+ ) ;
3946+ expect ( projection . rows ) . toHaveLength ( previous . rows . length ) ;
3947+ for ( const [ index , row ] of previous . rows . entries ( ) ) {
3948+ const holdsItem =
3949+ row . id === source . streamingItemId ||
3950+ ( ( row . kind === "work" || row . kind === "work-live" ) &&
3951+ row . groupedEntries . some ( ( entry ) => entry . id === source . streamingItemId ) ) ;
3952+ if ( ! holdsItem ) expect ( projection . rows [ index ] ) . toBe ( row ) ;
3953+ }
3954+ }
3955+ } ,
3956+ ) ;
3957+
3958+ it . each ( [ "reasoning" , "proposed_plan" ] as const ) (
3959+ "matches a full derivation when streaming %s text turns blank or completes" ,
3960+ ( kind ) => {
3961+ const source = makeStreamingWorkTimelineFixture ( kind ) ;
3962+ const rowsInput = ( timelineEntries : ReadonlyArray < TimelineEntry > ) => ( {
3963+ timelineEntries,
3964+ latestRun : {
3965+ runId : source . runId ,
3966+ status : "running" as const ,
3967+ startedAt : source . time ( 5 ) ,
3968+ completedAt : null ,
3969+ } ,
3970+ runningRunId : source . runId ,
3971+ isWorking : true ,
3972+ activeTurnStartedAt : source . time ( 5 ) ,
3973+ turnDiffSummaries : [ ] ,
3974+ supportsConversationRollback : true ,
3975+ } ) ;
3976+ let visibleTurnItems = source . visibleTurnItems ;
3977+ let timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState ( {
3978+ visibleTurnItems,
3979+ optimisticMessages : [ ] ,
3980+ } ) ;
3981+ let projection = deriveMessagesTimelineRowsWithState ( rowsInput ( timeline . entries ) ) ;
3982+ const steps = [
3983+ ...[ "Let" , " " , "Let me" ] . map (
3984+ ( text , step ) => ( rows : typeof visibleTurnItems ) =>
3985+ withStreamedText ( rows , source . streamingItemId , text , source . time ( 20 + step ) ) ,
3986+ ) ,
3987+ ( rows : typeof visibleTurnItems ) =>
3988+ rows . map ( ( row ) =>
3989+ row . item . id === source . streamingItemId &&
3990+ ( row . item . type === "reasoning" || row . item . type === "proposed_plan" )
3991+ ? {
3992+ ...row ,
3993+ item : {
3994+ ...row . item ,
3995+ status : "completed" as const ,
3996+ streaming : false ,
3997+ completedAt : DateTime . makeUnsafe ( source . time ( 30 ) ) ,
3998+ updatedAt : DateTime . makeUnsafe ( source . time ( 30 ) ) ,
3999+ } ,
4000+ }
4001+ : row ,
4002+ ) ,
4003+ ] ;
4004+ for ( const step of steps ) {
4005+ visibleTurnItems = step ( visibleTurnItems ) ;
4006+ const timelineInput = { visibleTurnItems, optimisticMessages : [ ] } ;
4007+ timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState ( timelineInput , timeline ) ;
4008+ expect ( timeline . entries ) . toEqual ( deriveTimelineEntriesFromVisibleTurnItems ( timelineInput ) ) ;
4009+ projection = deriveMessagesTimelineRowsWithState ( rowsInput ( timeline . entries ) , projection ) ;
4010+ expect ( projection . rows ) . toEqual (
4011+ deriveMessagesTimelineRows (
4012+ rowsInput ( deriveTimelineEntriesFromVisibleTurnItems ( timelineInput ) ) ,
4013+ ) ,
4014+ ) ;
4015+ }
4016+ } ,
4017+ ) ;
4018+
4019+ it . each ( [
4020+ "expanded live group" ,
4021+ "expanded live group followed by a hidden tool" ,
4022+ "thinking superseded by a running tool" ,
4023+ "settled turn" ,
4024+ ] as const ) ( "matches a full derivation when streaming thinking sits in a %s" , ( scenario ) => {
4025+ const source = makeStreamingWorkTimelineFixture ( "reasoning" ) ;
4026+ const running = scenario !== "settled turn" ;
4027+ const hiddenTool = scenario === "expanded live group followed by a hidden tool" ;
4028+ const rowsInput = (
4029+ timelineEntries : ReadonlyArray < TimelineEntry > ,
4030+ expandedWorkGroupIds ?: ReadonlySet < string > ,
4031+ ) => ( {
4032+ timelineEntries,
4033+ latestRun : {
4034+ runId : source . runId ,
4035+ status : running ? ( "running" as const ) : ( "completed" as const ) ,
4036+ startedAt : source . time ( 5 ) ,
4037+ completedAt : running ? null : source . time ( 40 ) ,
4038+ } ,
4039+ runningRunId : running ? source . runId : null ,
4040+ isWorking : running ,
4041+ activeTurnStartedAt : source . time ( 5 ) ,
4042+ turnDiffSummaries : [ ] ,
4043+ supportsConversationRollback : true ,
4044+ ...( expandedWorkGroupIds ? { expandedWorkGroupIds } : { } ) ,
4045+ ...( running ? { } : { expandedRunIds : new Set ( [ source . runId ] ) } ) ,
4046+ ...( hiddenTool ? { toolCallVisibility : "hideAll" as const } : { } ) ,
4047+ } ) ;
4048+ const tool = source . visibleTurnItems . find ( ( row ) => row . item . id === "live-work" ) ! ;
4049+ const laterTool = ( status : "running" | "completed" ) => ( {
4050+ ...tool ,
4051+ position : source . visibleTurnItems . length ,
4052+ sourceItemId : TurnItemId . make ( "live-later-tool" ) ,
4053+ item : {
4054+ ...tool . item ,
4055+ id : TurnItemId . make ( "live-later-tool" ) ,
4056+ ordinal : 8 ,
4057+ status,
4058+ ...( status === "running" ? { completedAt : null } : { } ) ,
4059+ } ,
4060+ } ) ;
4061+ let visibleTurnItems =
4062+ scenario === "thinking superseded by a running tool" || hiddenTool
4063+ ? [ ...source . visibleTurnItems , laterTool ( hiddenTool ? "completed" : "running" ) ]
4064+ : // Alone in a settled group, the thought's text becomes the group summary.
4065+ source . visibleTurnItems . filter ( ( row ) => running || row !== tool ) ;
4066+ visibleTurnItems = withStreamedText (
4067+ visibleTurnItems ,
4068+ source . streamingItemId ,
4069+ "Let" ,
4070+ source . time ( 20 ) ,
4071+ ) ;
4072+ let timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState ( {
4073+ visibleTurnItems,
4074+ optimisticMessages : [ ] ,
4075+ } ) ;
4076+ const liveRow = deriveMessagesTimelineRows ( rowsInput ( timeline . entries ) ) . find (
4077+ ( row ) => row . kind === "work-live" ,
4078+ ) ;
4079+ const expanded =
4080+ scenario . startsWith ( "expanded live group" ) && liveRow ?. kind === "work-live"
4081+ ? new Set ( [ liveRow . groupId ] )
4082+ : undefined ;
4083+ let projection = deriveMessagesTimelineRowsWithState ( rowsInput ( timeline . entries , expanded ) ) ;
4084+ for ( const [ step , text ] of [ "Let me" , "Let me check the tests" ] . entries ( ) ) {
4085+ visibleTurnItems = withStreamedText (
4086+ visibleTurnItems ,
4087+ source . streamingItemId ,
4088+ text ,
4089+ source . time ( 21 + step ) ,
4090+ ) ;
4091+ const timelineInput = { visibleTurnItems, optimisticMessages : [ ] } ;
4092+ const previous = projection ;
4093+ timeline = deriveTimelineEntriesFromVisibleTurnItemsWithState ( timelineInput , timeline ) ;
4094+ projection = deriveMessagesTimelineRowsWithState (
4095+ rowsInput ( timeline . entries , expanded ) ,
4096+ previous ,
4097+ ) ;
4098+ expect ( projection . rows ) . toEqual (
4099+ deriveMessagesTimelineRows (
4100+ rowsInput ( deriveTimelineEntriesFromVisibleTurnItems ( timelineInput ) , expanded ) ,
4101+ ) ,
4102+ ) ;
4103+ if ( ! running ) continue ;
4104+ for ( const [ index , row ] of previous . rows . entries ( ) ) {
4105+ const holdsItem =
4106+ ( row . kind === "work" || row . kind === "work-live" ) &&
4107+ row . groupedEntries . some ( ( entry ) => entry . id === source . streamingItemId ) ;
4108+ if ( ! holdsItem ) expect ( projection . rows [ index ] ) . toBe ( row ) ;
4109+ }
4110+ }
4111+ } ) ;
38944112} ) ;
38954113
38964114describe ( "linked timeline resources" , ( ) => {
0 commit comments