@@ -15,6 +15,7 @@ import { GitWorkflowService } from "../../git/GitWorkflowService.ts";
1515import * as ProjectSetupScriptRunner from "../../project/ProjectSetupScriptRunner.ts" ;
1616import { VcsStatusBroadcaster } from "../../vcs/VcsStatusBroadcaster.ts" ;
1717import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts" ;
18+ import { ThreadDeletionReactor } from "../Services/ThreadDeletionReactor.ts" ;
1819import { ThreadBootstrapService , type ThreadBootstrapShape } from "../Services/ThreadBootstrap.ts" ;
1920
2021const isOrchestrationDispatchCommandError = Schema . is ( OrchestrationDispatchCommandError ) ;
@@ -63,6 +64,7 @@ const makeThreadBootstrap = Effect.gen(function* () {
6364 const gitWorkflow = yield * GitWorkflowService ;
6465 const projectSetupScriptRunner = yield * ProjectSetupScriptRunner . ProjectSetupScriptRunner ;
6566 const vcsStatusBroadcaster = yield * VcsStatusBroadcaster ;
67+ const threadDeletionReactor = yield * ThreadDeletionReactor ;
6668 const crypto = yield * Crypto . Crypto ;
6769
6870 const toDispatchCommandError = ( cause : unknown , fallbackMessage : string ) =>
@@ -289,7 +291,7 @@ const makeThreadBootstrap = Effect.gen(function* () {
289291
290292 const bootstrapProgram = Effect . gen ( function * ( ) {
291293 if ( bootstrap ?. createThread ) {
292- yield * orchestrationEngine . dispatch (
294+ const created = yield * orchestrationEngine . dispatch (
293295 {
294296 type : "thread.create" ,
295297 commandId : yield * serverCommandId ( "bootstrap-thread-create" ) ,
@@ -306,14 +308,20 @@ const makeThreadBootstrap = Effect.gen(function* () {
306308 } ,
307309 options ,
308310 ) ;
311+ // The successful create is a fence in the engine command queue:
312+ // every delete for the prior incarnation committed before it.
313+ // Drain through that event before setup or turn start can own
314+ // terminals and provider sessions under the reused thread id.
315+ yield * threadDeletionReactor . drainThrough ( created . sequence ) ;
309316 createdThread = true ;
310317 }
311318
312319 if ( bootstrap ?. prepareWorktree ) {
313320 let worktreeBaseRef = bootstrap . prepareWorktree . baseBranch ;
314- // "Start from origin" is a stored default; repos without an
315- // origin remote fall back to the local base branch instead of
316- // failing the whole bootstrap on `git fetch origin`.
321+ // "Start from origin" is a stored default; repos without the
322+ // requested remote branch, or without an origin remote at all,
323+ // fall back to the local base branch instead of failing the whole
324+ // bootstrap on `git fetch origin`.
317325 const startFromOrigin =
318326 bootstrap . prepareWorktree . startFromOrigin === true &&
319327 ( yield * gitWorkflow . remoteExists ( {
@@ -325,12 +333,19 @@ const makeThreadBootstrap = Effect.gen(function* () {
325333 cwd : bootstrap . prepareWorktree . projectCwd ,
326334 remoteName : "origin" ,
327335 } ) ;
328- const resolvedRemoteBase = yield * gitWorkflow . resolveRemoteTrackingCommit ( {
336+ const remoteBaseExists = yield * gitWorkflow . remoteBranchExists ( {
329337 cwd : bootstrap . prepareWorktree . projectCwd ,
330338 refName : bootstrap . prepareWorktree . baseBranch ,
331- fallbackRemoteName : "origin" ,
339+ remoteName : "origin" ,
332340 } ) ;
333- worktreeBaseRef = resolvedRemoteBase . commitSha ;
341+ if ( remoteBaseExists ) {
342+ const resolvedRemoteBase = yield * gitWorkflow . resolveRemoteTrackingCommit ( {
343+ cwd : bootstrap . prepareWorktree . projectCwd ,
344+ refName : bootstrap . prepareWorktree . baseBranch ,
345+ fallbackRemoteName : "origin" ,
346+ } ) ;
347+ worktreeBaseRef = resolvedRemoteBase . commitSha ;
348+ }
334349 }
335350 const worktree = yield * gitWorkflow . createWorktree ( {
336351 cwd : bootstrap . prepareWorktree . projectCwd ,
0 commit comments