@@ -28,6 +28,7 @@ import {
2828 ProjectId ,
2929 ProviderDriverKind ,
3030 ProviderInstanceId ,
31+ ProviderThreadId ,
3132 OrchestrationV2ThreadProjectionJson ,
3233 ScheduledTaskId ,
3334 type ServerProvider ,
@@ -338,6 +339,7 @@ function waitUntil<E, R>(predicate: () => Effect.Effect<boolean, E, R>): Effect.
338339
339340const seedForkSource = Effect . fn ( "seedForkSource" ) ( function * (
340341 checkpointStatus : "ready" | "missing" = "ready" ,
342+ providerThreadId : ProviderThreadId | null = null ,
341343) {
342344 const threads = yield * ThreadManagement . ThreadManagementService ;
343345 const sink = yield * EventSink . EventSinkV2 ;
@@ -373,7 +375,7 @@ const seedForkSource = Effect.fn("seedForkSource")(function* (
373375 ordinal : 1 ,
374376 providerInstanceId : modelSelection . instanceId ,
375377 modelSelection,
376- providerThreadId : null ,
378+ providerThreadId,
377379 userMessageId : MessageId . make ( "message:fork-source" ) ,
378380 rootNodeId : null ,
379381 activeAttemptId : null ,
@@ -407,6 +409,37 @@ const seedForkSource = Effect.fn("seedForkSource")(function* (
407409 capturedAt : now ,
408410 } ,
409411 } ,
412+ ...( providerThreadId === null
413+ ? [ ]
414+ : [
415+ {
416+ id : EventId . make ( "event:fork-source-provider-thread" ) ,
417+ type : "provider-thread.updated" as const ,
418+ threadId,
419+ occurredAt : now ,
420+ payload : {
421+ id : providerThreadId ,
422+ driver : adapter . driver ,
423+ providerInstanceId : modelSelection . instanceId ,
424+ providerSessionId : null ,
425+ appThreadId : threadId ,
426+ ownerNodeId : null ,
427+ nativeThreadRef : {
428+ driver : adapter . driver ,
429+ nativeId : "native-source" ,
430+ strength : "strong" as const ,
431+ } ,
432+ nativeConversationHeadRef : null ,
433+ status : "idle" as const ,
434+ firstRunOrdinal : 1 ,
435+ lastRunOrdinal : 1 ,
436+ handoffIds : [ ] ,
437+ forkedFrom : null ,
438+ createdAt : now ,
439+ updatedAt : now ,
440+ } ,
441+ } ,
442+ ] ) ,
410443 ] ,
411444 } ) ;
412445 return { sourceThreadId : threadId , sourcePoint : { type : "run" as const , runId } } ;
@@ -719,6 +752,22 @@ it.effect("rejects a replay after synchronous fork setup failed in a bound workt
719752 assert . include ( String ( replayError . cause ) , "did not complete" ) ;
720753 assert . equal ( harness . createWorktree . mock . calls . length , 1 ) ;
721754 assert . equal ( harness . runSetup . mock . calls . length , 1 ) ;
755+ const sendError = yield * threads
756+ . dispatch ( {
757+ type : "message.dispatch" ,
758+ commandId : CommandId . make ( "fork:send-after-failed-setup" ) ,
759+ threadId : input . threadId ,
760+ messageId : MessageId . make ( "fork:message-after-failed-setup" ) ,
761+ text : "Continue from another client" ,
762+ attachments : [ ] ,
763+ dispatchMode : { type : "start_immediately" } ,
764+ createdBy : "user" ,
765+ creationSource : "mobile" ,
766+ } )
767+ . pipe ( Effect . flip ) ;
768+ assert ( sendError . _tag === "OrchestratorDispatchError" ) ;
769+ assert . include ( String ( sendError . cause ) , "workspace is not ready" ) ;
770+ assert . isEmpty ( ( yield * threads . getThreadProjection ( input . threadId ) ) . runs ) ;
722771 } ) . pipe ( Effect . provide ( harness . layer ) ) ;
723772} ) ;
724773
@@ -986,6 +1035,94 @@ it.effect("waits for a fork's setup when the script requires completion", () =>
9861035 } ) ,
9871036) ;
9881037
1038+ it . effect . each ( [ "checkout" , "setup" ] as const ) (
1039+ "rejects independent sends to a fork during %s, then starts in its prepared worktree" ,
1040+ ( stage ) =>
1041+ Effect . gen ( function * ( ) {
1042+ const entered = yield * Deferred . make < void > ( ) ;
1043+ const proceed = yield * Deferred . make < void > ( ) ;
1044+ const harness = makeHarness (
1045+ stage === "checkout"
1046+ ? {
1047+ createWorktree : ( input ) =>
1048+ Deferred . succeed ( entered , undefined ) . pipe (
1049+ Effect . andThen ( Deferred . await ( proceed ) ) ,
1050+ Effect . as ( {
1051+ worktree : {
1052+ path : "/repo-worktrees/feature" ,
1053+ refName : input . newRefName ?? input . refName ,
1054+ } ,
1055+ } ) ,
1056+ ) ,
1057+ }
1058+ : {
1059+ runSetup : ( ) =>
1060+ Effect . succeed ( {
1061+ status : "started" as const ,
1062+ async : false ,
1063+ scriptId : "setup" ,
1064+ scriptName : "Setup" ,
1065+ scriptCommand : "vp install" ,
1066+ terminalId : "setup" ,
1067+ cwd : "/repo-worktrees/feature" ,
1068+ completion : Deferred . succeed ( entered , undefined ) . pipe (
1069+ Effect . andThen ( Deferred . await ( proceed ) ) ,
1070+ Effect . as ( { exitCode : 0 , durationMs : 1 } ) ,
1071+ ) ,
1072+ } ) ,
1073+ } ,
1074+ ) ;
1075+ yield * Effect . gen ( function * ( ) {
1076+ const launches = yield * ThreadLaunch . ThreadLaunchService ;
1077+ const threads = yield * ThreadManagement . ThreadManagementService ;
1078+ const outbox = yield * EffectOutbox . EffectOutboxV2 ;
1079+ const forkSource = yield * seedForkSource (
1080+ "ready" ,
1081+ ProviderThreadId . make ( "provider-thread:fork-source" ) ,
1082+ ) ;
1083+ const input = {
1084+ ...launchInput ( {
1085+ command : "fork:independent-send" ,
1086+ thread : "thread:fork-independent-send" ,
1087+ workspace : { type : "worktree" , baseRef : "main" } ,
1088+ } ) ,
1089+ forkSource,
1090+ } ;
1091+ const launch = yield * launches . launch ( input ) . pipe ( Effect . forkChild ) ;
1092+ yield * Deferred . await ( entered ) ;
1093+ const send = {
1094+ type : "message.dispatch" as const ,
1095+ commandId : CommandId . make ( "fork:premature-send" ) ,
1096+ threadId : input . threadId ,
1097+ messageId : MessageId . make ( "fork:premature-message" ) ,
1098+ text : "Continue from another client" ,
1099+ attachments : [ ] ,
1100+ dispatchMode : { type : "start_immediately" as const } ,
1101+ createdBy : "user" as const ,
1102+ creationSource : "mobile" as const ,
1103+ } ;
1104+ const premature = yield * threads . dispatch ( send ) . pipe ( Effect . result ) ;
1105+ const beforeReady = yield * threads . getThreadProjection ( input . threadId ) ;
1106+ const prematureEffects = yield * outbox . listByCommandId ( send . commandId ) ;
1107+ yield * Deferred . succeed ( proceed , undefined ) ;
1108+ yield * Fiber . join ( launch ) ;
1109+ assert . isTrue ( Result . isFailure ( premature ) ) ;
1110+ assert . isEmpty ( beforeReady . runs ) ;
1111+ assert . isEmpty ( beforeReady . checkpointScopes ) ;
1112+ assert . isEmpty ( prematureEffects ) ;
1113+
1114+ yield * threads . dispatch ( {
1115+ ...send ,
1116+ commandId : CommandId . make ( "fork:ready-send" ) ,
1117+ messageId : MessageId . make ( "fork:ready-message" ) ,
1118+ } ) ;
1119+ const prepared = yield * threads . getThreadProjection ( input . threadId ) ;
1120+ assert . equal ( prepared . runs [ 0 ] ?. status , "starting" ) ;
1121+ assert . equal ( prepared . checkpointScopes [ 0 ] ?. cwd , "/repo-worktrees/feature" ) ;
1122+ } ) . pipe ( Effect . provide ( harness . layer ) ) ;
1123+ } ) ,
1124+ ) ;
1125+
9891126it . effect ( "honors nonblocking setup for a fork while its async script continues" , ( ) =>
9901127 Effect . gen ( function * ( ) {
9911128 const completion = yield * Deferred . make < { exitCode : number | null ; durationMs : number } > ( ) ;
@@ -1006,7 +1143,11 @@ it.effect("honors nonblocking setup for a fork while its async script continues"
10061143 const launches = yield * ThreadLaunch . ThreadLaunchService ;
10071144 const tracker = yield * WorktreeSetupTracker . WorktreeSetupTracker ;
10081145 const receipts = yield * CommandReceiptStore . CommandReceiptStoreV2 ;
1009- const forkSource = yield * seedForkSource ( ) ;
1146+ const threads = yield * ThreadManagement . ThreadManagementService ;
1147+ const forkSource = yield * seedForkSource (
1148+ "ready" ,
1149+ ProviderThreadId . make ( "provider-thread:fork-source" ) ,
1150+ ) ;
10101151 const input = {
10111152 ...launchInput ( {
10121153 command : "fork:async" ,
@@ -1030,6 +1171,21 @@ it.effect("honors nonblocking setup for a fork while its async script continues"
10301171 assert . equal ( replay . projection . thread . worktreePath , "/repo-worktrees/feature" ) ;
10311172 assert . equal ( harness . createWorktree . mock . calls . length , 1 ) ;
10321173 assert . equal ( harness . runSetup . mock . calls . length , 1 ) ;
1174+ yield * threads . dispatch ( {
1175+ type : "message.dispatch" ,
1176+ commandId : CommandId . make ( "fork:async-send" ) ,
1177+ threadId : input . threadId ,
1178+ messageId : MessageId . make ( "fork:async-message" ) ,
1179+ text : "Continue while the async script runs" ,
1180+ attachments : [ ] ,
1181+ dispatchMode : { type : "start_immediately" } ,
1182+ createdBy : "user" ,
1183+ creationSource : "mobile" ,
1184+ } ) ;
1185+ const prepared = yield * threads . getThreadProjection ( input . threadId ) ;
1186+ assert . equal ( prepared . runs [ 0 ] ?. status , "starting" ) ;
1187+ assert . equal ( prepared . checkpointScopes [ 0 ] ?. cwd , "/repo-worktrees/feature" ) ;
1188+ assert . equal ( ( yield * tracker . get ( input . threadId ) ) ?. phase , "running" ) ;
10331189 yield * Deferred . succeed ( completion , { exitCode : 0 , durationMs : 1 } ) ;
10341190 yield * tracker . stream ( result . threadId ) . pipe (
10351191 Stream . filter ( ( snapshot ) => snapshot ?. phase === "done" ) ,
0 commit comments