@@ -12,6 +12,7 @@ import * as DateTime from "effect/DateTime";
1212import * as Deferred from "effect/Deferred" ;
1313import * as Effect from "effect/Effect" ;
1414import * as Equal from "effect/Equal" ;
15+ import * as Fiber from "effect/Fiber" ;
1516import * as FileSystem from "effect/FileSystem" ;
1617import * as Layer from "effect/Layer" ;
1718import * as Option from "effect/Option" ;
@@ -405,7 +406,7 @@ export const layer = Layer.effect(
405406 cachedProviders . map ( ( provider ) => classifyCompatibility ( provider , initialManifest ) ) ,
406407 ) ;
407408 const workspaceRefreshesRef = yield * Ref . make <
408- ReadonlyMap < ProviderInstance , ReadonlyMap < string , symbol > >
409+ ReadonlyMap < ProviderInstance , ReadonlyMap < string , Deferred . Deferred < void > > >
409410 > ( new Map ( ) ) ;
410411 const maintenanceActionStatesRef = yield * Ref . make <
411412 ReadonlyMap < ProviderInstanceId , { readonly update ?: ServerProviderUpdateState | undefined } >
@@ -922,21 +923,13 @@ export const layer = Layer.effect(
922923 const scannedAt = DateTime . formatIso ( now ) ;
923924 const instance = yield * instanceRegistry . getInstance ( input . instanceId ) ;
924925 if ( ! instance ?. snapshotForCwd ) return providers ;
925- const token = Symbol ( ) ;
926- const claimed = yield * Ref . modify ( workspaceRefreshesRef , ( refreshes ) => {
927- const current = refreshes . get ( instance ) ;
928- if ( ! input . fresh && current ?. has ( input . cwd ) ) return [ false , refreshes ] as const ;
929- const next = new Map ( refreshes ) ;
930- next . set ( instance , new Map ( current ) . set ( input . cwd , token ) ) ;
931- return [ true , next ] as const ;
932- } ) ;
933- if ( ! claimed ) return yield * Ref . get ( providersRef ) ;
926+ const token = yield * Deferred . make < void > ( ) ;
934927 const refreshMachineSnapshot = input . fresh
935928 ? ( instance . invalidateCaches ?? Effect . void ) . pipe (
936929 Effect . andThen ( refreshInstance ( input . instanceId ) ) ,
937930 )
938931 : Effect . void ;
939- return yield * refreshMachineSnapshot . pipe (
932+ const runScan = refreshMachineSnapshot . pipe (
940933 Effect . andThen ( instance . snapshotForCwd ( input . cwd ) ) ,
941934 // Scans fail on error; returned inventory remains valid even when provider health reports an error.
942935 Effect . flatMap ( ( scopedSnapshot ) =>
@@ -974,9 +967,33 @@ export const layer = Layer.effect(
974967 if ( nextCwds . size ) next . set ( instance , nextCwds ) ;
975968 else next . delete ( instance ) ;
976969 return next ;
977- } ) ,
970+ } ) . pipe ( Effect . andThen ( Deferred . succeed ( token , undefined ) ) ) ,
978971 ) ,
979972 ) ;
973+ // The claim and the fork happen together, so the token is always completed. The scan is
974+ // detached so a disconnecting owner cannot cancel the result its waiters depend on.
975+ const claim = yield * Effect . uninterruptible (
976+ Effect . gen ( function * ( ) {
977+ const inFlight = yield * Ref . modify ( workspaceRefreshesRef , ( refreshes ) => {
978+ const current = refreshes . get ( instance ) ;
979+ const running = current ?. get ( input . cwd ) ;
980+ if ( ! input . fresh && running ) return [ running , refreshes ] as const ;
981+ const next = new Map ( refreshes ) ;
982+ next . set ( instance , new Map ( current ) . set ( input . cwd , token ) ) ;
983+ return [ undefined , next ] as const ;
984+ } ) ;
985+ if ( inFlight ) return { inFlight } as const ;
986+ return {
987+ // Starting now attaches the scan's `ensuring` before anything can close the scope.
988+ scan : yield * Effect . forkIn ( runScan , serviceScope , { startImmediately : true } ) ,
989+ } as const ;
990+ } ) ,
991+ ) ;
992+ // Wait for the running scan: on-demand clients get its result only in this reply.
993+ if ( "inFlight" in claim ) {
994+ return yield * Deferred . await ( claim . inFlight ) . pipe ( Effect . andThen ( Ref . get ( providersRef ) ) ) ;
995+ }
996+ return yield * Fiber . join ( claim . scan ) ;
980997 } ) ;
981998
982999 return {
0 commit comments