@@ -3,6 +3,7 @@ import { expect, it, vi } from "vite-plus/test";
33import { it as effectIt } from "@effect/vitest" ;
44import {
55 CheckpointScopeId ,
6+ ContextTransferId ,
67 MessageId ,
78 NodeId ,
89 ProviderSessionId ,
@@ -828,6 +829,125 @@ effectIt.effect("reads current startup state after waiting for the workspace lea
828829 } ) ,
829830) ;
830831
832+ effectIt . effect . each ( [
833+ { boundary : "policy resolution" , nativeMode : "ensure" , loss : "replaced" } ,
834+ { boundary : "policy resolution" , nativeMode : "ensure" , loss : "interrupted" } ,
835+ { boundary : "session opening" , nativeMode : "ensure" , loss : "replaced" } ,
836+ { boundary : "session opening" , nativeMode : "ensure" , loss : "interrupted" } ,
837+ { boundary : "session opening" , nativeMode : "resume" , loss : "replaced" } ,
838+ { boundary : "session opening" , nativeMode : "fork" , loss : "replaced" } ,
839+ ] ) (
840+ "does not continue $nativeMode startup when $loss during $boundary" ,
841+ ( { boundary, nativeMode, loss } ) =>
842+ Effect . gen ( function * ( ) {
843+ const entered = yield * Deferred . make < void > ( ) ;
844+ const release = yield * Deferred . make < void > ( ) ;
845+ const gate = Deferred . succeed ( entered , undefined ) . pipe (
846+ Effect . andThen ( Deferred . await ( release ) ) ,
847+ ) ;
848+ const harness = makeLocalCommandHarness ( { text : "Continue" } ) ;
849+ const projection = harness . projection ( ) ;
850+ const run = projection . runs [ 0 ] ! ;
851+ const providerThread = projection . providerThreads [ 0 ] ! ;
852+ if ( nativeMode === "resume" ) {
853+ harness . setProjection ( {
854+ ...projection ,
855+ providerThreads : [
856+ {
857+ ...providerThread ,
858+ nativeThreadRef : {
859+ driver : providerThread . driver ,
860+ nativeId : "existing" ,
861+ strength : "strong" ,
862+ } ,
863+ } ,
864+ ] ,
865+ } ) ;
866+ } else if ( nativeMode === "fork" ) {
867+ const sourceThreadId = ThreadId . make ( "fork-source" ) ;
868+ harness . getThreadRecords . mockReturnValue (
869+ Effect . succeed ( {
870+ ...projection ,
871+ thread : { ...projection . thread , id : sourceThreadId } ,
872+ } ) ,
873+ ) ;
874+ harness . setProjection ( {
875+ ...projection ,
876+ contextTransfers : [
877+ {
878+ id : ContextTransferId . make ( "pending-fork" ) ,
879+ type : "fork" ,
880+ sourceThreadId,
881+ targetThreadId : projection . thread . id ,
882+ sourcePoint : { threadId : sourceThreadId , runId : run . id } ,
883+ basePoint : null ,
884+ sourceProviderInstanceId : run . providerInstanceId ,
885+ targetProviderInstanceId : run . providerInstanceId ,
886+ targetRunId : run . id ,
887+ status : "pending" ,
888+ resolution : null ,
889+ createdBy : "user" ,
890+ error : null ,
891+ createdAt : projection . updatedAt ,
892+ updatedAt : projection . updatedAt ,
893+ consumedAt : null ,
894+ } ,
895+ ] ,
896+ } ) ;
897+ }
898+ const ensureThread = vi . fn ( ( ) => Effect . succeed ( providerThread ) ) ;
899+ const resumeThread = vi . fn ( ( ) => Effect . succeed ( providerThread ) ) ;
900+ const forkThread = vi . fn ( ( ) => Effect . succeed ( providerThread ) ) ;
901+ harness . open . mockReturnValue (
902+ ( boundary === "session opening" ? gate : Effect . void ) . pipe (
903+ Effect . as ( {
904+ driver : providerThread . driver ,
905+ ensureThread,
906+ resumeThread,
907+ forkThread,
908+ } as never ) ,
909+ ) ,
910+ ) ;
911+ if ( boundary === "policy resolution" ) {
912+ harness . resolveRuntimePolicy . mockReturnValue (
913+ gate . pipe ( Effect . as ( { cwd : "/tmp/native-account-command" } as never ) ) ,
914+ ) ;
915+ }
916+
917+ const startup = yield * harness . start . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
918+ yield * Deferred . await ( entered ) ;
919+ const current = harness . projection ( ) ;
920+ const replacementId = RunAttemptId . make ( "replacement-attempt" ) ;
921+ const updated = {
922+ ...current ,
923+ runs : current . runs . map ( ( candidate ) =>
924+ loss === "replaced"
925+ ? { ...candidate , activeAttemptId : replacementId }
926+ : { ...candidate , status : "interrupted" as const , completedAt : current . updatedAt } ,
927+ ) ,
928+ attempts :
929+ loss === "replaced"
930+ ? [
931+ ...current . attempts ,
932+ { ...current . attempts [ 0 ] ! , id : replacementId , attemptOrdinal : 2 } ,
933+ ]
934+ : current . attempts ,
935+ } ;
936+ harness . setProjection ( updated ) ;
937+ yield * Deferred . succeed ( release , undefined ) ;
938+ yield * Fiber . join ( startup ) ;
939+
940+ expect ( harness . open ) . toHaveBeenCalledTimes ( boundary === "session opening" ? 1 : 0 ) ;
941+ expect ( ensureThread ) . not . toHaveBeenCalled ( ) ;
942+ expect ( resumeThread ) . not . toHaveBeenCalled ( ) ;
943+ expect ( forkThread ) . not . toHaveBeenCalled ( ) ;
944+ expect ( harness . startRootRun ) . not . toHaveBeenCalled ( ) ;
945+ expect ( harness . writeIfRunCurrent ) . not . toHaveBeenCalled ( ) ;
946+ expect ( harness . events ) . toEqual ( [ ] ) ;
947+ expect ( harness . projection ( ) ) . toEqual ( updated ) ;
948+ } ) ,
949+ ) ;
950+
831951effectIt . effect ( "starts another provider in the same checkout while a turn is pending" , ( ) =>
832952 Effect . gen ( function * ( ) {
833953 const worktreePath = "/tmp/provider-start-shared-checkout" ;
0 commit comments