@@ -477,6 +477,204 @@ it.layer(layerProjectionStoreTest)("CheckpointCaptureServiceV2", (it) => {
477477 } ) ,
478478 ) ;
479479
480+ // A stopped run is captured after it ends; by then the next root run, dispatched or queued, may
481+ // have re-created the thread's one root scope with its own node and time.
482+ it . effect . each ( [ 1 , 2 ] ) (
483+ "attributes the baselines a late capture materializes to the stopped run %i" ,
484+ ( ordinal ) =>
485+ Effect . gen ( function * ( ) {
486+ const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
487+ const requestedAt = yield * DateTime . now ;
488+ const stoppedAt = DateTime . add ( requestedAt , { seconds : 1 } ) ;
489+ const rescopedAt = DateTime . add ( requestedAt , { seconds : 2 } ) ;
490+ const lateThreadId = ThreadId . make ( `thread:checkpoint-capture-late-${ ordinal } ` ) ;
491+ const lateRunId = RunId . make ( `run:checkpoint-capture-late-${ ordinal } ` ) ;
492+ const lateRootNodeId = NodeId . make ( `node:checkpoint-capture-late-${ ordinal } -root` ) ;
493+ const lateScopeId = CheckpointScopeId . make ( `scope:checkpoint-capture-late-${ ordinal } ` ) ;
494+ const lateProviderThreadId = ProviderThreadId . make (
495+ `provider-thread:checkpoint-capture-late-${ ordinal } ` ,
496+ ) ;
497+ yield * projectionStore . apply ( {
498+ id : EventId . make ( `event:checkpoint-capture-late-${ ordinal } :thread` ) ,
499+ type : "thread.created" ,
500+ threadId : lateThreadId ,
501+ occurredAt : requestedAt ,
502+ payload : {
503+ createdBy : "user" ,
504+ creationSource : "web" ,
505+ id : lateThreadId ,
506+ projectId,
507+ title : "Checkpoint capture after the next run re-created the scope" ,
508+ providerInstanceId,
509+ modelSelection,
510+ runtimeMode : "full-access" ,
511+ interactionMode : "default" ,
512+ branch : null ,
513+ worktreePath : null ,
514+ activeProviderThreadId : null ,
515+ lineage : {
516+ parentThreadId : null ,
517+ relationshipToParent : null ,
518+ rootThreadId : lateThreadId ,
519+ } ,
520+ forkedFrom : null ,
521+ createdAt : requestedAt ,
522+ updatedAt : requestedAt ,
523+ archivedAt : null ,
524+ settledOverride : null ,
525+ settledAt : null ,
526+ lastVisitedAt : null ,
527+ deletedAt : null ,
528+ } ,
529+ } ) ;
530+ yield * projectionStore . apply ( {
531+ id : EventId . make ( `event:checkpoint-capture-late-${ ordinal } :run` ) ,
532+ type : "run.updated" ,
533+ threadId : lateThreadId ,
534+ runId : lateRunId ,
535+ nodeId : lateRootNodeId ,
536+ providerInstanceId,
537+ occurredAt : stoppedAt ,
538+ payload : {
539+ id : lateRunId ,
540+ threadId : lateThreadId ,
541+ ordinal,
542+ providerInstanceId,
543+ modelSelection,
544+ providerThreadId : lateProviderThreadId ,
545+ userMessageId : MessageId . make ( `message:checkpoint-capture-late-${ ordinal } ` ) ,
546+ rootNodeId : lateRootNodeId ,
547+ activeAttemptId : null ,
548+ status : "interrupted" ,
549+ requestedAt,
550+ startedAt : requestedAt ,
551+ completedAt : stoppedAt ,
552+ checkpointId : null ,
553+ contextHandoffId : null ,
554+ } ,
555+ } ) ;
556+ yield * projectionStore . apply ( {
557+ id : EventId . make ( `event:checkpoint-capture-late-${ ordinal } :node` ) ,
558+ type : "node.updated" ,
559+ threadId : lateThreadId ,
560+ runId : lateRunId ,
561+ nodeId : lateRootNodeId ,
562+ providerInstanceId,
563+ occurredAt : stoppedAt ,
564+ payload : {
565+ id : lateRootNodeId ,
566+ threadId : lateThreadId ,
567+ runId : lateRunId ,
568+ parentNodeId : null ,
569+ rootNodeId : lateRootNodeId ,
570+ kind : "root_turn" ,
571+ status : "interrupted" ,
572+ countsForRun : true ,
573+ providerThreadId : lateProviderThreadId ,
574+ providerTurnId : null ,
575+ nativeItemRef : null ,
576+ runtimeRequestId : null ,
577+ checkpointScopeId : lateScopeId ,
578+ startedAt : requestedAt ,
579+ completedAt : stoppedAt ,
580+ } ,
581+ } ) ;
582+ yield * projectionStore . apply ( {
583+ id : EventId . make ( `event:checkpoint-capture-late-${ ordinal } :provider-thread` ) ,
584+ type : "provider-thread.updated" ,
585+ threadId : lateThreadId ,
586+ nodeId : lateRootNodeId ,
587+ driver,
588+ providerInstanceId,
589+ occurredAt : requestedAt ,
590+ payload : {
591+ id : lateProviderThreadId ,
592+ driver,
593+ providerInstanceId,
594+ providerSessionId : null ,
595+ appThreadId : lateThreadId ,
596+ ownerNodeId : lateRootNodeId ,
597+ nativeThreadRef : null ,
598+ nativeConversationHeadRef : null ,
599+ status : "idle" ,
600+ firstRunOrdinal : 1 ,
601+ lastRunOrdinal : ordinal ,
602+ handoffIds : [ ] ,
603+ forkedFrom : null ,
604+ createdAt : requestedAt ,
605+ updatedAt : requestedAt ,
606+ } ,
607+ } ) ;
608+ const nextRunId = RunId . make ( `run:checkpoint-capture-late-${ ordinal } -next` ) ;
609+ const nextRootNodeId = NodeId . make ( `node:checkpoint-capture-late-${ ordinal } -next-root` ) ;
610+ yield * projectionStore . apply ( {
611+ id : EventId . make ( `event:checkpoint-capture-late-${ ordinal } :scope-of-next-run` ) ,
612+ type : "checkpoint-scope.created" ,
613+ threadId : lateThreadId ,
614+ runId : nextRunId ,
615+ nodeId : nextRootNodeId ,
616+ occurredAt : rescopedAt ,
617+ payload : {
618+ id : lateScopeId ,
619+ threadId : lateThreadId ,
620+ runId : nextRunId ,
621+ nodeId : nextRootNodeId ,
622+ parentScopeId : null ,
623+ providerThreadId : lateProviderThreadId ,
624+ kind : "root_run" ,
625+ ordinalWithinParent : 0 ,
626+ advancesAppRunCount : true ,
627+ cwd : "/repo" ,
628+ createdAt : rescopedAt ,
629+ } ,
630+ } ) ;
631+
632+ const committed = yield * Ref . make < ReadonlyArray < OrchestrationV2DomainEvent > > ( [ ] ) ;
633+ const layerCapture = CheckpointCaptureService . layer . pipe (
634+ Layer . provide (
635+ Layer . mergeAll (
636+ IdAllocator . layer ,
637+ CheckpointService . layer . pipe (
638+ Layer . provide (
639+ Layer . mergeAll (
640+ IdAllocator . layer ,
641+ NodeCrypto . layer ,
642+ Layer . mock ( CheckpointStore . CheckpointStore ) ( {
643+ isGitRepository : ( ) => Effect . succeed ( true ) ,
644+ hasCheckpointRef : ( ) => Effect . succeed ( true ) ,
645+ captureCheckpoint : ( ) => Effect . void ,
646+ diffCheckpoints : ( ) => Effect . succeed ( "" ) ,
647+ listAuthoredPaths : ( ) => Effect . succeed ( null ) ,
648+ } ) ,
649+ ) ,
650+ ) ,
651+ ) ,
652+ Layer . mock ( EventSink . EventSinkV2 ) ( {
653+ commitCommand : ( input ) =>
654+ Ref . set ( committed , input . events ) . pipe ( Effect . as ( { committed : true } as never ) ) ,
655+ } ) ,
656+ ) ,
657+ ) ,
658+ ) ;
659+ yield * CheckpointCaptureService . CheckpointCaptureServiceV2 . use ( ( service ) =>
660+ service . execute ( { threadId : lateThreadId , runId : lateRunId , scopeId : lateScopeId } ) ,
661+ ) . pipe ( Effect . provide ( layerCapture ) ) ;
662+
663+ const baselines = ( yield * Ref . get ( committed ) ) . flatMap ( ( event ) =>
664+ event . type === "checkpoint.captured" && event . payload . runId === null ? [ event ] : [ ] ,
665+ ) ;
666+ assert . deepEqual (
667+ baselines . map ( ( event ) => event . payload . ordinalWithinScope ) ,
668+ ordinal === 1 ? [ 0 ] : [ 0 , 1 ] ,
669+ ) ;
670+ for ( const baseline of baselines ) {
671+ assert . equal ( baseline . nodeId , lateRootNodeId ) ;
672+ assert . equal ( baseline . payload . nodeId , lateRootNodeId ) ;
673+ assert . deepEqual ( baseline . payload . capturedAt , requestedAt ) ;
674+ }
675+ } ) ,
676+ ) ;
677+
480678 it . effect ( "records the checkpoint of a cancelled run and keeps it cancelled" , ( ) =>
481679 Effect . gen ( function * ( ) {
482680 const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
0 commit comments