@@ -9,6 +9,10 @@ import {
99 ProviderDriverKind ,
1010 ProviderInstanceId ,
1111 ProviderSessionId ,
12+ ProviderThreadId ,
13+ ProviderTurnId ,
14+ RunAttemptId ,
15+ RunId ,
1216 RuntimeRequestId ,
1317 ThreadId ,
1418 TurnItemId ,
@@ -329,3 +333,204 @@ it.effect("implements a proposed plan that the command projection leaves out", (
329333 assert . equal ( ( yield * projections . getPlan ( threadId , planId ) ) ?. status , "completed" ) ;
330334 } ) . pipe ( Effect . provide ( testLayer ) ) ,
331335) ;
336+
337+ // Stop's settle follow-up runs after the provider interrupt returns, possibly
338+ // long after the Stop (retries) or again (an effect replayed after a crash).
339+ // A later run's background work is not that Stop's to end.
340+ it . effect ( "settles only the stopped run's background work, once" , ( ) =>
341+ Effect . gen ( function * ( ) {
342+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
343+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
344+ const threadId = ThreadId . make ( "thread:settle-binding" ) ;
345+ const providerThreadId = ProviderThreadId . make ( "provider-thread:settle-binding" ) ;
346+ const now = yield * DateTime . now ;
347+ yield * orchestrator . dispatch ( {
348+ type : "thread.create" ,
349+ commandId : CommandId . make ( "create-settle-binding" ) ,
350+ threadId,
351+ projectId : ProjectId . make ( "project:settle-binding" ) ,
352+ title : "Settle binding" ,
353+ modelSelection,
354+ runtimeMode : "full-access" ,
355+ interactionMode : "default" ,
356+ branch : null ,
357+ worktreePath : null ,
358+ createdBy : "user" ,
359+ creationSource : "web" ,
360+ } ) ;
361+ yield * projections . apply ( {
362+ id : EventId . make ( "settle-binding:provider-thread" ) ,
363+ type : "provider-thread.updated" ,
364+ threadId,
365+ occurredAt : now ,
366+ payload : {
367+ id : providerThreadId ,
368+ driver : adapter . driver ,
369+ providerInstanceId : instanceId ,
370+ providerSessionId : null ,
371+ appThreadId : threadId ,
372+ ownerNodeId : null ,
373+ nativeThreadRef : null ,
374+ nativeConversationHeadRef : null ,
375+ status : "idle" ,
376+ firstRunOrdinal : 1 ,
377+ lastRunOrdinal : 2 ,
378+ handoffIds : [ ] ,
379+ forkedFrom : null ,
380+ createdAt : now ,
381+ updatedAt : now ,
382+ } ,
383+ } ) ;
384+ const commandItem = ( ordinal : number ) => TurnItemId . make ( `turn-item:settle-binding:${ ordinal } ` ) ;
385+ for ( const ordinal of [ 1 , 2 ] ) {
386+ const runId = RunId . make ( `run:settle-binding:${ ordinal } ` ) ;
387+ const attemptId = RunAttemptId . make ( `attempt:settle-binding:${ ordinal } ` ) ;
388+ const nodeId = NodeId . make ( `node:settle-binding:${ ordinal } ` ) ;
389+ const providerTurnId = ProviderTurnId . make ( `provider-turn:settle-binding:${ ordinal } ` ) ;
390+ yield * projections . apply ( {
391+ id : EventId . make ( `settle-binding:run:${ ordinal } ` ) ,
392+ type : "run.created" ,
393+ threadId,
394+ occurredAt : now ,
395+ payload : {
396+ id : runId ,
397+ threadId,
398+ ordinal,
399+ providerInstanceId : instanceId ,
400+ modelSelection,
401+ providerThreadId,
402+ userMessageId : MessageId . make ( `message:settle-binding:${ ordinal } ` ) ,
403+ rootNodeId : nodeId ,
404+ activeAttemptId : attemptId ,
405+ status : "completed" ,
406+ requestedAt : now ,
407+ startedAt : now ,
408+ completedAt : now ,
409+ checkpointId : null ,
410+ contextHandoffId : null ,
411+ } ,
412+ } ) ;
413+ yield * projections . apply ( {
414+ id : EventId . make ( `settle-binding:attempt:${ ordinal } ` ) ,
415+ type : "run-attempt.created" ,
416+ threadId,
417+ occurredAt : now ,
418+ payload : {
419+ id : attemptId ,
420+ runId,
421+ attemptOrdinal : 1 ,
422+ rootNodeId : nodeId ,
423+ providerInstanceId : instanceId ,
424+ providerThreadId,
425+ providerTurnId,
426+ reason : "initial" ,
427+ status : "completed" ,
428+ startedAt : now ,
429+ completedAt : now ,
430+ } ,
431+ } ) ;
432+ yield * projections . apply ( {
433+ id : EventId . make ( `settle-binding:turn:${ ordinal } ` ) ,
434+ type : "provider-turn.updated" ,
435+ threadId,
436+ occurredAt : now ,
437+ payload : {
438+ id : providerTurnId ,
439+ providerThreadId,
440+ nodeId,
441+ runAttemptId : attemptId ,
442+ nativeTurnRef : null ,
443+ ordinal,
444+ status : "completed" ,
445+ startedAt : now ,
446+ completedAt : now ,
447+ } ,
448+ } ) ;
449+ yield * projections . apply ( {
450+ id : EventId . make ( `settle-binding:item:${ ordinal } ` ) ,
451+ type : "turn-item.updated" ,
452+ threadId,
453+ runId,
454+ occurredAt : now ,
455+ payload : {
456+ id : commandItem ( ordinal ) ,
457+ threadId,
458+ runId,
459+ nodeId,
460+ providerThreadId,
461+ providerTurnId,
462+ nativeItemRef : null ,
463+ parentItemId : null ,
464+ ordinal : ordinal * 10 ,
465+ status : "running" ,
466+ title : `Background command ${ ordinal } ` ,
467+ startedAt : now ,
468+ completedAt : null ,
469+ updatedAt : now ,
470+ type : "command_execution" ,
471+ input : `sleep ${ ordinal } ` ,
472+ } ,
473+ } ) ;
474+ }
475+ const itemStatuses = Effect . map ( projections . getThreadProjection ( threadId ) , ( projection ) =>
476+ projection . turnItems
477+ . flatMap ( ( item ) => ( item . type === "command_execution" ? [ `${ item . id } :${ item . status } ` ] : [ ] ) )
478+ . toSorted ( ) ,
479+ ) ;
480+ // The settle that followed a Stop of run 1's turn, dispatched only after
481+ // run 2 had settled with work of its own.
482+ const settle = {
483+ type : "thread.background-work.settle" ,
484+ commandId : CommandId . make ( "stop-run-1:background-work-settled" ) ,
485+ threadId,
486+ providerThreadId,
487+ providerTurnId : ProviderTurnId . make ( "provider-turn:settle-binding:1" ) ,
488+ } as const ;
489+ yield * orchestrator . dispatch ( settle ) ;
490+ assert . deepEqual ( yield * itemStatuses , [
491+ `${ commandItem ( 1 ) } :interrupted` ,
492+ `${ commandItem ( 2 ) } :running` ,
493+ ] ) ;
494+
495+ // A settle that found nothing to end replays as a no-op, even after work
496+ // it would match appears: its receipt is recorded with no events.
497+ const emptySettle = {
498+ ...settle ,
499+ commandId : CommandId . make ( "stop-run-1-again:background-work-settled" ) ,
500+ } ;
501+ const first = yield * orchestrator . dispatch ( emptySettle ) ;
502+ assert . lengthOf ( first . storedEvents , 0 ) ;
503+ yield * projections . apply ( {
504+ id : EventId . make ( "settle-binding:item:late" ) ,
505+ type : "turn-item.updated" ,
506+ threadId,
507+ runId : RunId . make ( "run:settle-binding:1" ) ,
508+ occurredAt : now ,
509+ payload : {
510+ id : commandItem ( 3 ) ,
511+ threadId,
512+ runId : RunId . make ( "run:settle-binding:1" ) ,
513+ nodeId : NodeId . make ( "node:settle-binding:1" ) ,
514+ providerThreadId,
515+ providerTurnId : ProviderTurnId . make ( "provider-turn:settle-binding:1" ) ,
516+ nativeItemRef : null ,
517+ parentItemId : null ,
518+ ordinal : 30 ,
519+ status : "running" ,
520+ title : "Late background command" ,
521+ startedAt : now ,
522+ completedAt : null ,
523+ updatedAt : now ,
524+ type : "command_execution" ,
525+ input : "sleep 3" ,
526+ } ,
527+ } ) ;
528+ const replayed = yield * orchestrator . dispatch ( emptySettle ) ;
529+ assert . lengthOf ( replayed . storedEvents , 0 ) ;
530+ assert . deepEqual ( yield * itemStatuses , [
531+ `${ commandItem ( 1 ) } :interrupted` ,
532+ `${ commandItem ( 2 ) } :running` ,
533+ `${ commandItem ( 3 ) } :running` ,
534+ ] ) ;
535+ } ) . pipe ( Effect . provide ( testLayer ) ) ,
536+ ) ;
0 commit comments