@@ -166,6 +166,20 @@ export class ThreadLaunchBaseRefError extends Schema.TaggedError<ThreadLaunchBas
166166 }
167167}
168168
169+ export class ThreadLaunchBranchConflictError extends Schema . TaggedError < ThreadLaunchBranchConflictError > ( ) (
170+ "ThreadLaunchBranchConflictError" ,
171+ {
172+ projectId : ProjectId ,
173+ projectTitle : Schema . String ,
174+ branch : Schema . String ,
175+ conflictingBranch : Schema . String ,
176+ } ,
177+ ) {
178+ override get message ( ) : string {
179+ return `Branch "${ this . branch } " cannot be created in project "${ this . projectTitle } ": branch "${ this . conflictingBranch } " exists.` ;
180+ }
181+ }
182+
169183export class ThreadLaunchNotRepositoryError extends Schema . TaggedError < ThreadLaunchNotRepositoryError > ( ) (
170184 "ThreadLaunchNotRepositoryError" ,
171185 {
@@ -188,10 +202,16 @@ export class ThreadLaunchService extends Context.Service<
188202 ThreadLaunchResult ,
189203 ThreadLaunchError | ThreadManagement . ThreadManagementProviderInstanceDisabledError
190204 > ;
191- /** Check before creation for early errors; launch otherwise accepts and reports base failures inside the thread. */
205+ /** Check a new worktree's base ref and branch name before creation for early errors; launch otherwise accepts and reports these failures inside the thread. */
192206 readonly checkWorktreeBase : (
193- input : Pick < ThreadLaunchInput , "projectId" | "workspaceStrategy" > ,
194- ) => Effect . Effect < void , ThreadLaunchBaseRefError | ThreadLaunchNotRepositoryError > ;
207+ input : Pick < ThreadLaunchInput , "projectId" | "workspaceStrategy" > & {
208+ /** Skips the check when this launch was already accepted, so its retry replays. */
209+ readonly commandId ?: CommandId ;
210+ } ,
211+ ) => Effect . Effect <
212+ void ,
213+ ThreadLaunchBaseRefError | ThreadLaunchBranchConflictError | ThreadLaunchNotRepositoryError
214+ > ;
195215 readonly retryPreparation : (
196216 input : ThreadLaunchPreparationInput ,
197217 ) => Effect . Effect <
@@ -279,6 +299,12 @@ const make = Effect.gen(function* () {
279299 // launch() turns a Scratch worktree request into a root launch.
280300 if ( strategy . type !== "worktree" || ( yield * managedFolders . isScratchProject ( input . projectId ) ) )
281301 return ;
302+ if ( input . commandId !== undefined ) {
303+ const receipt = yield * receipts
304+ . getByCommandId ( input . commandId )
305+ . pipe ( Effect . orElseSucceed ( ( ) => Option . none ( ) ) ) ;
306+ if ( Option . isSome ( receipt ) && receipt . value . status === "accepted" ) return ;
307+ }
282308 const project = Option . getOrUndefined (
283309 yield * projects . getById ( input . projectId ) . pipe ( Effect . orElseSucceed ( ( ) => Option . none ( ) ) ) ,
284310 ) ;
@@ -293,29 +319,44 @@ const make = Effect.gen(function* () {
293319 projectTitle : project . title ,
294320 workspaceRoot : cwd ,
295321 } ) ;
296- // `:/text` is a commit search, which cannot be asked for a commit type.
297- if ( strategy . baseRef . startsWith ( ":" ) ) return ;
298- const { baseRef } = strategy ;
299- const unresolved = yield * Effect . gen ( function * ( ) {
300- // `git worktree add` reads `-` as the previous checkout.
301- if ( yield * git . hasCommit ( { cwd, refName : baseRef === "-" ? "@{-1}" : baseRef } ) )
302- return undefined ;
303- if (
304- strategy . startFromOrigin === true &&
305- ( yield * git . resolveWorktreeBaseRemote ( { cwd, refName : baseRef } ) ) !== null
306- )
307- return undefined ;
308- // git worktree add accepts remote-tracking branches; let Git decide when any matching ref exists.
309- if ( yield * git . hasRefNamed ( { cwd, refName : baseRef } ) ) return undefined ;
310- return ( yield * git . hasCommit ( { cwd, refName : "HEAD" } ) ) ? project : undefined ;
311- } ) . pipe ( Effect . orElseSucceed ( ( ) => undefined ) ) ;
312- if ( unresolved !== undefined )
322+ const { baseRef, branch } = strategy ;
323+ const baseResolves =
324+ // `:/text` is a commit search, which cannot be asked for a commit type.
325+ baseRef . startsWith ( ":" ) ||
326+ ( yield * Effect . gen ( function * ( ) {
327+ // `git worktree add` reads `-` as the previous checkout.
328+ if ( yield * git . hasCommit ( { cwd, refName : baseRef === "-" ? "@{-1}" : baseRef } ) )
329+ return true ;
330+ if (
331+ strategy . startFromOrigin === true &&
332+ ( yield * git . resolveWorktreeBaseRemote ( { cwd, refName : baseRef } ) ) !== null
333+ )
334+ return true ;
335+ // git worktree add accepts remote-tracking branches; let Git decide when any matching ref exists.
336+ if ( yield * git . hasRefNamed ( { cwd, refName : baseRef } ) ) return true ;
337+ return ! ( yield * git . hasCommit ( { cwd, refName : "HEAD" } ) ) ;
338+ } ) . pipe ( Effect . orElseSucceed ( ( ) => true ) ) ) ;
339+ if ( ! baseResolves )
313340 return yield * new ThreadLaunchBaseRefError ( {
314- projectId : unresolved . id ,
315- projectTitle : unresolved . title ,
316- workspaceRoot : unresolved . workspaceRoot ,
341+ projectId : project . id ,
342+ projectTitle : project . title ,
343+ workspaceRoot : project . workspaceRoot ,
317344 baseRef,
318345 } ) ;
346+ // Provisioning flattens temporary names that collide, so only explicit names are refused.
347+ if ( branch === undefined || isTemporaryWorktreeBranch ( branch ) ) return ;
348+ const conflictingBranch = ( yield * git
349+ . listLocalBranchNames ( cwd )
350+ . pipe ( Effect . orElseSucceed ( ( ) : ReadonlyArray < string > => [ ] ) ) ) . find (
351+ ( existing ) => existing . startsWith ( `${ branch } /` ) || branch . startsWith ( `${ existing } /` ) ,
352+ ) ;
353+ if ( conflictingBranch !== undefined )
354+ return yield * new ThreadLaunchBranchConflictError ( {
355+ projectId : project . id ,
356+ projectTitle : project . title ,
357+ branch,
358+ conflictingBranch,
359+ } ) ;
319360 } ) ;
320361
321362 const prepareInBackground = Effect . fn ( "ThreadLaunchService.prepareInBackground" ) ( function * (
0 commit comments