@@ -31,6 +31,7 @@ import * as Scope from "effect/Scope";
3131import * as Stream from "effect/Stream" ;
3232
3333import * as SourceControlRepositoryService from "../sourceControl/SourceControlRepositoryService.ts" ;
34+ import type { ProjectConflictError } from "./ProjectService.ts" ;
3435
3536export class ProjectCloneTracker extends Context . Service <
3637 ProjectCloneTracker ,
@@ -53,12 +54,13 @@ export class ProjectCloneTracker extends Context.Service<
5354> ( ) ( "t3/project/ProjectCloneTracker" ) { }
5455
5556export interface ProjectCloneHooks {
57+ /** Fails with `ProjectConflictError` when another project already owns the destination. */
5658 readonly createProject : ( input : {
5759 readonly projectId : ProjectId ;
5860 readonly title : string ;
5961 readonly workspaceRoot : string ;
6062 readonly createdAt : string ;
61- } ) => Effect . Effect < void , OrchestrationDispatchCommandError > ;
63+ } ) => Effect . Effect < void , OrchestrationDispatchCommandError | ProjectConflictError > ;
6264 readonly onCloned : ( input : {
6365 readonly projectId : ProjectId ;
6466 readonly workspaceRoot : string ;
@@ -266,6 +268,16 @@ export const make = Effect.gen(function* () {
266268 } ) ;
267269 } ) ;
268270
271+ const moveClaim = ( from : ProjectId , to : ProjectId ) =>
272+ Ref . modify ( clones , ( current ) => {
273+ const claim = current . get ( from ) ;
274+ if ( ! claim || current . has ( to ) ) return [ false , current ] as const ;
275+ const next = new Map ( current ) ;
276+ next . delete ( from ) ;
277+ next . set ( to , { ...claim , snapshot : { ...claim . snapshot , projectId : to } } ) ;
278+ return [ true , next ] as const ;
279+ } ) ;
280+
269281 const start : ProjectCloneTracker [ "Service" ] [ "start" ] = Effect . fn ( "ProjectCloneTracker.start" ) (
270282 function * ( input , hooks ) {
271283 const prepared = yield * repositories . prepareClone ( input ) ;
@@ -306,30 +318,44 @@ export const make = Effect.gen(function* () {
306318 } ) ;
307319 return [ true , next ] as const ;
308320 } ) ;
309- if ( ! claimed ) {
310- return yield * new SourceControlRepositoryError ( {
311- operation : "cloneRepository" ,
312- provider : input . provider ?? SourceControlProviderKind . make ( "unknown" ) ,
313- detail : "A clone into this destination is already in progress." ,
314- } ) ;
315- }
321+ const alreadyInProgress = new SourceControlRepositoryError ( {
322+ operation : "cloneRepository" ,
323+ provider : input . provider ?? SourceControlProviderKind . make ( "unknown" ) ,
324+ detail : "A clone into this destination is already in progress." ,
325+ } ) ;
326+ if ( ! claimed ) return yield * alreadyInProgress ;
316327 // Finish or release the claim despite disconnects; a claim without a fiber cannot be cancelled or retried.
317- yield * Effect . uninterruptible (
328+ const projectId = yield * Effect . uninterruptible (
318329 Effect . gen ( function * ( ) {
319330 yield * clearRetention ( input . projectId ) ;
320331 // Register before creation so racing thread.create and project.delete see the clone.
321- yield * hooks . createProject ( {
322- projectId : input . projectId ,
323- title : input . title ,
324- workspaceRoot : prepared . destinationPath ,
325- createdAt : input . createdAt ,
326- } ) ;
332+ const projectId = yield * hooks
333+ . createProject ( {
334+ projectId : input . projectId ,
335+ title : input . title ,
336+ workspaceRoot : prepared . destinationPath ,
337+ createdAt : input . createdAt ,
338+ } )
339+ . pipe (
340+ Effect . as ( input . projectId ) ,
341+ // A failed clone's project still owns its empty folder after a restart
342+ // forgets the clone; clone for that project instead.
343+ Effect . catchTags ( {
344+ ProjectConflictError : ( conflict ) =>
345+ moveClaim ( input . projectId , conflict . conflictingProjectId ) . pipe (
346+ Effect . flatMap ( ( moved ) =>
347+ moved ? Effect . succeed ( conflict . conflictingProjectId ) : alreadyInProgress ,
348+ ) ,
349+ ) ,
350+ } ) ,
351+ ) ;
327352 yield * publish ;
328- yield * launch ( input . projectId ) ;
353+ yield * launch ( projectId ) ;
354+ return projectId ;
329355 } ) . pipe ( Effect . tapError ( ( ) => remove ( input . projectId ) ) ) ,
330356 ) ;
331357 return {
332- projectId : input . projectId ,
358+ projectId,
333359 cwd : prepared . destinationPath ,
334360 remoteUrl : prepared . remoteUrl ,
335361 repository : prepared . repository ,
0 commit comments