@@ -455,6 +455,23 @@ function wakeWorkStartedAt(
455455
456456const threadPullRequestLinksEqual = Schema . toEquivalence ( Schema . NullOr ( ThreadLinkedPullRequest ) ) ;
457457
458+ /** Unfinished runs (including queued) or open requests block moving a thread to another project. */
459+ export function threadHasUnfinishedWork ( records : {
460+ readonly runs : ReadonlyArray < { readonly status : string } > ;
461+ readonly runtimeRequests : ReadonlyArray < { readonly status : string } > ;
462+ } ) : boolean {
463+ return (
464+ records . runs . some (
465+ ( run ) =>
466+ run . status !== "completed" &&
467+ run . status !== "failed" &&
468+ run . status !== "cancelled" &&
469+ run . status !== "interrupted" &&
470+ run . status !== "rolled_back" ,
471+ ) || records . runtimeRequests . some ( ( request ) => request . status === "pending" )
472+ ) ;
473+ }
474+
458475function commandThreadId ( command : OrchestrationV2ServerCommand ) : ThreadId {
459476 switch ( command . type ) {
460477 case "thread.create" :
@@ -486,6 +503,7 @@ function commandThreadId(command: OrchestrationV2ServerCommand): ThreadId {
486503 case "thread.pull-request-watch.sync" :
487504 case "thread.pull-request.sync" :
488505 case "thread.title.regeneration.complete" :
506+ case "thread.project.adopt-folder" :
489507 case "thread.runtime-mode.set" :
490508 case "thread.interaction-mode.set" :
491509 case "thread.model-selection.set" :
@@ -3983,6 +4001,56 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
39834001 } ,
39844002 ) ;
39854003
4004+ const dispatchThreadProjectAdoptFolder = Effect . fn (
4005+ "orchestrationV2.dispatch.threadProjectAdoptFolder" ,
4006+ ) ( function * (
4007+ command : Extract <
4008+ OrchestrationV2ServerCommand ,
4009+ { readonly type : "thread.project.adopt-folder" }
4010+ > ,
4011+ events : Ref . Ref < Array < OrchestrationV2DomainEvent > > ,
4012+ ) {
4013+ const reject = ( cause : string ) =>
4014+ new OrchestratorDispatchError ( {
4015+ commandId : command . commandId ,
4016+ commandType : command . type ,
4017+ cause : `Thread ${ command . threadId } ${ cause } ` ,
4018+ } ) ;
4019+ const projection = yield * projectionStore
4020+ . getThreadRecords ( command . threadId , [ "runs" , "runtimeRequests" ] , { turnItemTypes : [ ] } )
4021+ . pipe (
4022+ Effect . mapError (
4023+ ( cause ) => new OrchestratorProjectionError ( { threadId : command . threadId , cause } ) ,
4024+ ) ,
4025+ ) ;
4026+ const thread = projection . thread ;
4027+ if ( thread . deletedAt !== null ) return yield * reject ( "is deleted." ) ;
4028+ if ( threadHasUnfinishedWork ( projection ) ) {
4029+ return yield * reject ( "still has work in progress. Finish or stop it first." ) ;
4030+ }
4031+ const current = yield * projects . get ( thread . projectId ) . pipe ( mapDispatchError ( command ) ) ;
4032+ const target = yield * projects . get ( command . projectId ) . pipe ( mapDispatchError ( command ) ) ;
4033+ if ( Option . isNone ( target ) || target . value . deletedAt !== null ) {
4034+ return yield * reject ( `cannot move to missing project ${ command . projectId } .` ) ;
4035+ }
4036+ // The thread always lands in the project root, so the move must not change its cwd.
4037+ const cwd = thread . worktreePath ?? Option . getOrUndefined ( current ) ?. workspaceRoot ;
4038+ if ( cwd !== target . value . workspaceRoot ) {
4039+ return yield * reject ( `does not work in ${ target . value . workspaceRoot } .` ) ;
4040+ }
4041+ const now = yield * DateTime . now ;
4042+ yield * emit (
4043+ events ,
4044+ command ,
4045+ ) ( {
4046+ type : "thread.metadata-updated" ,
4047+ threadId : command . threadId ,
4048+ providerInstanceId : thread . providerInstanceId ,
4049+ occurredAt : now ,
4050+ payload : { ...thread , projectId : command . projectId , worktreePath : null , updatedAt : now } ,
4051+ } ) ;
4052+ } ) ;
4053+
39864054 const refuseAboveDispatchModeLimit = Effect . fn ( "orchestrationV2.dispatch.refuseAboveModeLimit" ) (
39874055 function * (
39884056 command : OrchestrationV2ServerCommand ,
@@ -12487,6 +12555,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1248712555 case "provider-session.detach" :
1248812556 yield * dispatchProviderSessionDetach ( command , events , effects ) ;
1248912557 break ;
12558+ case "thread.project.adopt-folder" :
12559+ yield * dispatchThreadProjectAdoptFolder ( command , events ) ;
12560+ break ;
1249012561 case "message.dispatch" : {
1249112562 // Providers own native subagent conversations; messages cannot target them, and adapters ask questions on the parent.
1249212563 const thread = yield * projectionStore
0 commit comments