@@ -275,6 +275,133 @@ describe("applyOrchestrationV2ProjectionEvent", () => {
275275 expect ( next ?. visibleTurnItems . map ( ( row ) => row . position ) ) . toEqual ( [ 0 , 1 ] ) ;
276276 } ) ;
277277
278+ describe ( "partial timeline" , ( ) => {
279+ const queuedRunId = RunId . make ( "run-queued" ) ;
280+ const steerRunId = RunId . make ( "run-steer" ) ;
281+ const queuedRun = { ...run , id : queuedRunId , ordinal : 2 , status : "queued" } as const ;
282+ const steerRun = { ...run , id : steerRunId , ordinal : 3 , status : "completed" } as const ;
283+ const runItem = ( id : string , itemRunId : RunId , ordinal : number ) => ( {
284+ ...commandItem ( id , id , ordinal ) ,
285+ runId : itemRunId ,
286+ } ) ;
287+ const windowOf = (
288+ runs : ReadonlyArray < OrchestrationV2Run > ,
289+ items : ReadonlyArray < OrchestrationV2TurnItem > ,
290+ ) : OrchestrationV2ThreadProjection => ( {
291+ ...emptyProjection ,
292+ runs,
293+ turnItems : items ,
294+ visibleTurnItems : items . map ( ( item , position ) => ( {
295+ position,
296+ visibility : "local" as const ,
297+ sourceThreadId : threadId ,
298+ sourceItemId : item . id ,
299+ item,
300+ } ) ) ,
301+ } ) ;
302+ const itemEvent = ( payload : OrchestrationV2TurnItem ) =>
303+ ( {
304+ id : `event-${ payload . id } ` ,
305+ type : "turn-item.updated" ,
306+ threadId,
307+ occurredAt : now ,
308+ payload,
309+ } ) as OrchestrationV2DomainEvent ;
310+ const runEvent = ( payload : OrchestrationV2Run ) =>
311+ ( {
312+ id : `event-${ payload . id } -${ payload . status } ` ,
313+ type : "run.updated" ,
314+ threadId,
315+ occurredAt : now ,
316+ payload,
317+ } ) as OrchestrationV2DomainEvent ;
318+
319+ it ( "shows a queued run that starts after a newer steer raised the watermark" , ( ) => {
320+ // Run 2 was queued first, run 3 was steered in and ran first. Its items
321+ // sit in run 3's band and already raised the watermark past run 2's band.
322+ const steerItems = [
323+ runItem ( "item-steer-user" , steerRunId , 3_000_001 ) ,
324+ runItem ( "item-steer-reply" , steerRunId , 3_000_002 ) ,
325+ ] ;
326+ let projection : OrchestrationV2ThreadProjection | null = windowOf (
327+ [ run , queuedRun , steerRun ] ,
328+ steerItems ,
329+ ) ;
330+ let latestLocalTurnOrdinal = 3_000_002 ;
331+ // Same order the orchestrator commits a queued start: the user message
332+ // lands while the run is still queued, then the run starts and replies.
333+ const queuedUser = runItem ( "item-queued-user" , queuedRunId , 2_000_001 ) ;
334+ const queuedReply = runItem ( "item-queued-reply" , queuedRunId , 2_000_002 ) ;
335+ for ( const event of [
336+ itemEvent ( queuedUser ) ,
337+ runEvent ( { ...queuedRun , status : "running" } ) ,
338+ itemEvent ( queuedReply ) ,
339+ runEvent ( { ...queuedRun , status : "completed" } ) ,
340+ ] ) {
341+ projection = applyOrchestrationV2ProjectionEvent ( projection , event , {
342+ partialTimeline : true ,
343+ latestLocalTurnOrdinal,
344+ } ) ;
345+ if ( event . type === "turn-item.updated" ) {
346+ latestLocalTurnOrdinal = Math . max ( latestLocalTurnOrdinal , event . payload . ordinal ) ;
347+ }
348+ }
349+
350+ expect ( projection ?. visibleTurnItems . map ( ( row ) => row . item . id ) ) . toEqual ( [
351+ queuedUser . id ,
352+ queuedReply . id ,
353+ ...steerItems . map ( ( item ) => item . id ) ,
354+ ] ) ;
355+ expect ( projection ?. turnItems . map ( ( item ) => item . id ) ) . toContain ( queuedReply . id ) ;
356+ } ) ;
357+
358+ it ( "keeps a running run's item when the window starts after its earlier items" , ( ) => {
359+ // The bounded window begins inside run 3; run 2 started and kept going,
360+ // but none of its rows made it into the window.
361+ const runningRun = { ...queuedRun , status : "running" } as const ;
362+ const steerItems = [
363+ runItem ( "item-steer-user" , steerRunId , 3_000_001 ) ,
364+ runItem ( "item-steer-reply" , steerRunId , 3_000_002 ) ,
365+ ] ;
366+ const projection = windowOf ( [ run , runningRun , steerRun ] , steerItems ) ;
367+ const reply = runItem ( "item-running-reply" , queuedRunId , 2_000_003 ) ;
368+
369+ const next = applyOrchestrationV2ProjectionEvent ( projection , itemEvent ( reply ) , {
370+ partialTimeline : true ,
371+ latestLocalTurnOrdinal : 3_000_002 ,
372+ } ) ;
373+
374+ expect ( next ?. visibleTurnItems . map ( ( row ) => row . item . id ) ) . toEqual ( [
375+ reply . id ,
376+ ...steerItems . map ( ( item ) => item . id ) ,
377+ ] ) ;
378+ } ) ;
379+
380+ it . each ( [
381+ { name : "keeps an item of a run already in the window" , itemRunId : steerRunId , kept : true } ,
382+ { name : "drops an item of a finished run outside the window" , itemRunId : runId , kept : false } ,
383+ ] ) ( "$name that arrives below the watermark" , ( { itemRunId, kept } ) => {
384+ const first = runItem ( "item-first" , steerRunId , 3_000_001 ) ;
385+ const later = runItem ( "item-later" , steerRunId , 3_000_003 ) ;
386+ const late = runItem ( "item-late" , itemRunId , 3_000_002 ) ;
387+ const projection = windowOf ( [ run , steerRun ] , [ first , later ] ) ;
388+
389+ const next = applyOrchestrationV2ProjectionEvent ( projection , itemEvent ( late ) , {
390+ partialTimeline : true ,
391+ latestLocalTurnOrdinal : later . ordinal ,
392+ } ) ;
393+ if ( kept ) {
394+ expect ( next ?. visibleTurnItems . map ( ( row ) => row . item . id ) ) . toEqual ( [
395+ first . id ,
396+ late . id ,
397+ later . id ,
398+ ] ) ;
399+ } else {
400+ expect ( next ) . toBe ( projection ) ;
401+ }
402+ } ) ;
403+ } ) ;
404+
278405 it ( "removes only hidden local items while preserving inherited rows" , ( ) => {
279406 const inherited = commandItem ( "item-inherited" ) ;
280407 const local = commandItem ( "item-local" ) ;
0 commit comments