@@ -18,12 +18,16 @@ import {
1818} from "@t3tools/contracts" ;
1919import * as Cause from "effect/Cause" ;
2020import * as DateTime from "effect/DateTime" ;
21+ import * as Deferred from "effect/Deferred" ;
2122import * as Effect from "effect/Effect" ;
2223import * as FileSystem from "effect/FileSystem" ;
24+ import * as Fiber from "effect/Fiber" ;
2325import * as Layer from "effect/Layer" ;
2426import * as Option from "effect/Option" ;
27+ import * as Path from "effect/Path" ;
2528import * as Schema from "effect/Schema" ;
2629
30+ import { withWorkspaceLease } from "../workspace/workspaceLease.ts" ;
2731import * as GitWorkflow from "../git/GitWorkflowService.ts" ;
2832import * as ProjectService from "../project/ProjectService.ts" ;
2933import * as ProviderAuthService from "../provider/Services/ProviderAuthService.ts" ;
@@ -95,6 +99,7 @@ it("does not commit running state when inherited background routing cannot be re
9599 Layer . mock ( ContextHandoffService . ContextHandoffServiceV2 ) ( { } ) ,
96100 Layer . mock ( EventSink . EventSinkV2 ) ( { writeIfRunCurrent } ) ,
97101 IdAllocator . layer ,
102+ Path . layer ,
98103 Layer . succeed ( FileSystem . FileSystem , { exists : ( ) => Effect . succeed ( false ) } as never ) ,
99104 Layer . mock ( GitWorkflow . GitWorkflowService ) ( { pruneWorktrees, createWorktree } ) ,
100105 Layer . mock ( ProjectService . ProjectService ) ( {
@@ -155,6 +160,7 @@ it("does not commit running state when inherited background routing cannot be re
155160
156161function makeLocalCommandHarness ( input : {
157162 readonly text : string ;
163+ readonly worktreePath ?: string ;
158164 readonly previousNativeSession ?: boolean ;
159165 readonly previousMessages ?: ReadonlyArray < string > ;
160166 readonly logoutFailure ?: string ;
@@ -236,8 +242,9 @@ function makeLocalCommandHarness(input: {
236242 thread : {
237243 id : threadId ,
238244 activeProviderThreadId : providerThreadId ,
239- branch : null ,
240- worktreePath : null ,
245+ projectId : ProjectId . make ( "project-native-account-command" ) ,
246+ branch : input . worktreePath === undefined ? null : "feature/restore" ,
247+ worktreePath : input . worktreePath ?? null ,
241248 } as OrchestrationV2ThreadProjection [ "thread" ] ,
242249 runs : [
243250 ...( input . previousNativeSession
@@ -483,6 +490,8 @@ function makeLocalCommandHarness(input: {
483490 return { committed, storedEvents : [ ] } ;
484491 } ) ,
485492 ) ;
493+ const exists = vi . fn ( ( ) => Effect . succeed ( true ) ) ;
494+ const createWorktree = vi . fn ( ( ) => Effect . succeed ( { } as never ) ) ;
486495 const layer = ProviderTurnStart . layer . pipe (
487496 Layer . provide (
488497 Layer . mergeAll (
@@ -491,9 +500,16 @@ function makeLocalCommandHarness(input: {
491500 } ) ,
492501 Layer . mock ( EventSink . EventSinkV2 ) ( { writeIfRunCurrent } ) ,
493502 IdAllocator . layer ,
494- FileSystem . layerNoop ( { } ) ,
495- Layer . mock ( GitWorkflow . GitWorkflowService ) ( { } ) ,
496- Layer . mock ( ProjectService . ProjectService ) ( { } ) ,
503+ Path . layer ,
504+ FileSystem . layerNoop ( { exists } ) ,
505+ Layer . mock ( GitWorkflow . GitWorkflowService ) ( {
506+ pruneWorktrees : ( ) => Effect . void ,
507+ createWorktree,
508+ } ) ,
509+ Layer . mock ( ProjectService . ProjectService ) ( {
510+ getById : ( ) =>
511+ Effect . succeed ( Option . some ( { workspaceRoot : "/tmp/native-account-project" } as never ) ) ,
512+ } ) ,
497513 Layer . mock ( ProjectionStore . ProjectionStoreV2 ) ( {
498514 getTurnStartContext : ( ) =>
499515 Effect . succeed ( {
@@ -531,6 +547,8 @@ function makeLocalCommandHarness(input: {
531547 ) ,
532548 ) ;
533549 return {
550+ exists,
551+ createWorktree,
534552 open,
535553 writeIfRunCurrent,
536554 startRootRun,
@@ -553,6 +571,86 @@ function makeLocalCommandHarness(input: {
553571 } ;
554572}
555573
574+ effectIt . effect ( "waits for cleanup and repairs its checkout before opening a provider" , ( ) =>
575+ Effect . gen ( function * ( ) {
576+ const worktreePath = "/tmp/provider-start-cleanup-first" ;
577+ const releaseCleanup = yield * Deferred . make < void > ( ) ;
578+ let checkoutExists = true ;
579+ const harness = makeLocalCommandHarness ( {
580+ text : "Continue" ,
581+ worktreePath,
582+ failReadsAfterRunning : true ,
583+ } ) ;
584+ harness . exists . mockImplementation ( ( ) => Effect . sync ( ( ) => checkoutExists ) ) ;
585+ harness . createWorktree . mockImplementation ( ( ) =>
586+ Effect . sync ( ( ) => {
587+ checkoutExists = true ;
588+ return { } as never ;
589+ } ) ,
590+ ) ;
591+ const open = harness . open . getMockImplementation ( ) ! ;
592+ harness . open . mockImplementation ( ( ) => {
593+ expect ( checkoutExists ) . toBe ( true ) ;
594+ return open ( ) ;
595+ } ) ;
596+ const cleanup = yield * withWorkspaceLease (
597+ worktreePath ,
598+ Deferred . await ( releaseCleanup ) . pipe (
599+ Effect . andThen (
600+ Effect . sync ( ( ) => {
601+ checkoutExists = false ;
602+ } ) ,
603+ ) ,
604+ ) ,
605+ ) . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
606+ const startup = yield * harness . start . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
607+ expect ( harness . open ) . not . toHaveBeenCalled ( ) ;
608+ expect ( harness . exists ) . not . toHaveBeenCalled ( ) ;
609+ yield * Deferred . succeed ( releaseCleanup , undefined ) ;
610+ yield * Fiber . join ( cleanup ) ;
611+ yield * Fiber . join ( startup ) ;
612+ expect ( harness . createWorktree ) . toHaveBeenCalledWith ( {
613+ cwd : "/tmp/native-account-project" ,
614+ refName : "feature/restore" ,
615+ path : worktreePath ,
616+ } ) ;
617+ expect ( harness . open ) . toHaveBeenCalledOnce ( ) ;
618+ expect ( harness . startRootRun ) . toHaveBeenCalledOnce ( ) ;
619+ } ) ,
620+ ) ;
621+
622+ effectIt . effect ( "keeps cleanup out until provider turn startup accepts the turn" , ( ) =>
623+ Effect . gen ( function * ( ) {
624+ const worktreePath = "/tmp/provider-start-before-cleanup" ;
625+ const startupEntered = yield * Deferred . make < void > ( ) ;
626+ const releaseStartup = yield * Deferred . make < void > ( ) ;
627+ const harness = makeLocalCommandHarness ( {
628+ text : "Continue" ,
629+ worktreePath,
630+ failReadsAfterRunning : true ,
631+ } ) ;
632+ harness . startRootRun . mockImplementation ( ( ) =>
633+ Deferred . succeed ( startupEntered , undefined ) . pipe (
634+ Effect . andThen ( Deferred . await ( releaseStartup ) ) ,
635+ ) ,
636+ ) ;
637+ const startup = yield * harness . start . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
638+ yield * Deferred . await ( startupEntered ) ;
639+ let cleanupEntered = false ;
640+ const cleanup = yield * withWorkspaceLease (
641+ worktreePath ,
642+ Effect . sync ( ( ) => {
643+ cleanupEntered = true ;
644+ } ) ,
645+ ) . pipe ( Effect . forkChild ( { startImmediately : true } ) ) ;
646+ expect ( cleanupEntered ) . toBe ( false ) ;
647+ yield * Deferred . succeed ( releaseStartup , undefined ) ;
648+ yield * Fiber . join ( startup ) ;
649+ yield * Fiber . join ( cleanup ) ;
650+ expect ( cleanupEntered ) . toBe ( true ) ;
651+ } ) ,
652+ ) ;
653+
556654effectIt . effect ( "terminalizes a starting run when its provider session cannot open" , ( ) =>
557655 Effect . gen ( function * ( ) {
558656 const harness = makeLocalCommandHarness ( {
0 commit comments