@@ -44,6 +44,8 @@ import {
4444 WS_METHODS ,
4545 WsRpcGroup ,
4646 EditorId ,
47+ type WorktreeSetupSnapshot ,
48+ type WorktreeSetupStageId ,
4749} from "@t3tools/contracts" ;
4850import {
4951 computeDpopAccessTokenHash ,
@@ -10759,6 +10761,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
1075910761 scriptCommand : "npm install" ,
1076010762 terminalId : "setup-setup" ,
1076110763 cwd : "/tmp/bootstrap-worktree" ,
10764+ async : true ,
1076210765 } ) ,
1076310766 ) ;
1076410767
@@ -11324,6 +11327,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
1132411327 scriptCommand : "npm install" ,
1132511328 terminalId : "setup-setup" ,
1132611329 cwd : "/tmp/bootstrap-worktree" ,
11330+ async : true ,
1132711331 } ) ,
1132811332 ) ;
1132911333 let setupActivityAppendAttempt = 0 ;
@@ -11427,6 +11431,153 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
1142711431 } ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
1142811432 ) ;
1142911433
11434+ it . effect . each ( [
11435+ { caseName : "async setup scripts let the turn start before the script exits" , async : true } ,
11436+ { caseName : "sync setup scripts hold the turn until the script exits" , async : false } ,
11437+ ] ) ( "$caseName" , ( { async } ) =>
11438+ Effect . gen ( function * ( ) {
11439+ const dispatchedCommands : Array < OrchestrationCommand > = [ ] ;
11440+ const scriptExit = yield * Deferred . make < void > ( ) ;
11441+ const runForThread = vi . fn (
11442+ (
11443+ _ : Parameters <
11444+ ProjectSetupScriptRunner . ProjectSetupScriptRunner [ "Service" ] [ "runForThread" ]
11445+ > [ 0 ] ,
11446+ ) =>
11447+ Effect . succeed ( {
11448+ status : "started" as const ,
11449+ scriptId : "setup" ,
11450+ scriptName : "Setup" ,
11451+ scriptCommand : "npm install" ,
11452+ terminalId : "setup-setup" ,
11453+ cwd : "/tmp/bootstrap-worktree" ,
11454+ async,
11455+ completion : Deferred . await ( scriptExit ) . pipe ( Effect . as ( { exitCode : 0 , durationMs : 1 } ) ) ,
11456+ } ) ,
11457+ ) ;
11458+
11459+ yield * buildAppUnderTest ( {
11460+ layers : {
11461+ vcsDriver : {
11462+ isInsideWorkTree : ( ) => Effect . succeed ( true ) ,
11463+ } ,
11464+ gitVcsDriver : {
11465+ execute : ( ) => Effect . succeed ( SUCCESSFUL_GIT_EXECUTION ) ,
11466+ createWorktree : ( ) =>
11467+ Effect . succeed ( {
11468+ worktree : {
11469+ refName : "t3code/bootstrap-refName" ,
11470+ path : "/tmp/bootstrap-worktree" ,
11471+ } ,
11472+ } ) ,
11473+ } ,
11474+ orchestrationEngine : {
11475+ dispatch : ( command ) =>
11476+ Effect . sync ( ( ) => {
11477+ dispatchedCommands . push ( command ) ;
11478+ return { sequence : dispatchedCommands . length } ;
11479+ } ) ,
11480+ readEvents : ( ) => Stream . empty ,
11481+ } ,
11482+ projectSetupScriptRunner : {
11483+ runForThread,
11484+ } ,
11485+ } ,
11486+ } ) ;
11487+
11488+ const createdAt = "2026-01-01T00:00:00.000Z" ;
11489+ const threadId = ThreadId . make ( `thread-bootstrap-${ async ? "async" : "sync" } -setup` ) ;
11490+ const wsUrl = yield * getWsServerUrl ( "/ws" ) ;
11491+ const dispatchFiber = yield * Effect . scoped (
11492+ withWsRpcClient ( wsUrl , ( client ) =>
11493+ client [ ORCHESTRATION_WS_METHODS . dispatchCommand ] ( {
11494+ type : "thread.turn.start" ,
11495+ commandId : CommandId . make ( `cmd-bootstrap-${ async ? "async" : "sync" } -setup` ) ,
11496+ threadId,
11497+ message : {
11498+ messageId : MessageId . make ( "msg-bootstrap-setup" ) ,
11499+ role : "user" ,
11500+ text : "hello" ,
11501+ attachments : [ ] ,
11502+ } ,
11503+ modelSelection : defaultModelSelection ,
11504+ runtimeMode : "full-access" ,
11505+ interactionMode : "default" ,
11506+ bootstrap : {
11507+ createThread : {
11508+ projectId : defaultProjectId ,
11509+ title : "Bootstrap Thread" ,
11510+ modelSelection : defaultModelSelection ,
11511+ runtimeMode : "full-access" ,
11512+ interactionMode : "default" ,
11513+ branch : "main" ,
11514+ worktreePath : null ,
11515+ createdAt,
11516+ } ,
11517+ prepareWorktree : {
11518+ projectCwd : "/tmp/project" ,
11519+ baseBranch : "main" ,
11520+ branch : "t3code/bootstrap-refName" ,
11521+ } ,
11522+ runSetupScript : true ,
11523+ } ,
11524+ createdAt,
11525+ } ) ,
11526+ ) ,
11527+ ) . pipe ( Effect . forkChild ) ;
11528+
11529+ const turnStarted = ( ) =>
11530+ dispatchedCommands . some ( ( command ) => command . type === "thread.turn.start" ) ;
11531+ const snapshotWhere = ( predicate : ( snapshot : WorktreeSetupSnapshot ) => boolean ) =>
11532+ Effect . scoped (
11533+ withWsRpcClient ( wsUrl , ( client ) =>
11534+ client [ WS_METHODS . subscribeWorktreeSetup ] ( { threadId } ) . pipe (
11535+ Stream . filter (
11536+ ( snapshot ) : snapshot is WorktreeSetupSnapshot =>
11537+ snapshot !== null && predicate ( snapshot ) ,
11538+ ) ,
11539+ Stream . runHead ,
11540+ Effect . map ( Option . getOrThrow ) ,
11541+ ) ,
11542+ ) ,
11543+ ) ;
11544+ const stageStatus = ( snapshot : WorktreeSetupSnapshot , id : WorktreeSetupStageId ) =>
11545+ snapshot . stages . find ( ( stage ) => stage . id === id ) ?. status ;
11546+
11547+ if ( async ) {
11548+ // The turn is dispatched while the script is still running.
11549+ const started = yield * snapshotWhere (
11550+ ( snapshot ) => stageStatus ( snapshot , "agent" ) === "done" ,
11551+ ) ;
11552+ assertTrue ( turnStarted ( ) ) ;
11553+ assert . equal ( started . phase , "running" ) ;
11554+ assert . equal ( stageStatus ( started , "setup-script" ) , "running" ) ;
11555+ yield * Fiber . join ( dispatchFiber ) ;
11556+
11557+ yield * Deferred . succeed ( scriptExit , undefined ) ;
11558+ const settled = yield * snapshotWhere ( ( snapshot ) => snapshot . phase !== "running" ) ;
11559+ assert . equal ( settled . phase , "done" ) ;
11560+ assert . equal ( stageStatus ( settled , "setup-script" ) , "done" ) ;
11561+ return ;
11562+ }
11563+
11564+ // The script is running and the turn has not been dispatched yet.
11565+ const running = yield * snapshotWhere (
11566+ ( snapshot ) => stageStatus ( snapshot , "setup-script" ) === "running" ,
11567+ ) ;
11568+ assert . equal ( stageStatus ( running , "agent" ) , "pending" ) ;
11569+ assert . isFalse ( turnStarted ( ) ) ;
11570+
11571+ yield * Deferred . succeed ( scriptExit , undefined ) ;
11572+ yield * Fiber . join ( dispatchFiber ) ;
11573+ assertTrue ( turnStarted ( ) ) ;
11574+ const settled = yield * snapshotWhere ( ( snapshot ) => snapshot . phase !== "running" ) ;
11575+ assert . equal ( settled . phase , "done" ) ;
11576+ assert . equal ( stageStatus ( settled , "setup-script" ) , "done" ) ;
11577+ assert . equal ( stageStatus ( settled , "agent" ) , "done" ) ;
11578+ } ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
11579+ ) ;
11580+
1143011581 it . effect ( "cleans up created bootstrap threads when worktree creation defects" , ( ) =>
1143111582 Effect . gen ( function * ( ) {
1143211583 const dispatchedCommands : Array < OrchestrationCommand > = [ ] ;
0 commit comments