@@ -19,7 +19,7 @@ import * as Effect from "effect/Effect";
1919import * as Exit from "effect/Exit" ;
2020import * as Fiber from "effect/Fiber" ;
2121import * as FileSystem from "effect/FileSystem" ;
22- import * as Option from "effect/Option" ;
22+
2323import * as Path from "effect/Path" ;
2424import * as PubSub from "effect/PubSub" ;
2525import * as Ref from "effect/Ref" ;
@@ -113,6 +113,11 @@ interface FxSessionContext {
113113 stopped : boolean ;
114114}
115115
116+ interface ThreadLockEntry {
117+ readonly semaphore : Semaphore . Semaphore ;
118+ readonly users : number ;
119+ }
120+
116121function settlePendingApprovalsAsCancelled (
117122 pendingApprovals : ReadonlyMap < ApprovalRequestId , PendingApproval > ,
118123) : Effect . Effect < void > {
@@ -232,7 +237,7 @@ export function makeFxAdapter(fxSettings: FxSettings, options?: FxAdapterLiveOpt
232237 const makeAcpNativeLoggers = yield * makeAcpNativeLoggerFactory ( ) ;
233238
234239 const sessions = new Map < ThreadId , FxSessionContext > ( ) ;
235- const threadLocksRef = yield * SynchronizedRef . make ( new Map < string , Semaphore . Semaphore > ( ) ) ;
240+ const threadLocksRef = yield * SynchronizedRef . make ( new Map < string , ThreadLockEntry > ( ) ) ;
236241 const runtimeEventPubSub = yield * PubSub . unbounded < ProviderRuntimeEvent > ( ) ;
237242
238243 const nowIso = Effect . map ( DateTime . now , DateTime . formatIso ) ;
@@ -263,26 +268,42 @@ export function makeFxAdapter(fxSettings: FxSettings, options?: FxAdapterLiveOpt
263268 const offerRuntimeEvent = ( event : ProviderRuntimeEvent ) =>
264269 PubSub . publish ( runtimeEventPubSub , event ) . pipe ( Effect . asVoid ) ;
265270
266- const getThreadSemaphore = ( threadId : string ) =>
271+ const acquireThreadSemaphore = ( threadId : string ) =>
267272 SynchronizedRef . modifyEffect ( threadLocksRef , ( current ) => {
268- const existing : Option . Option < Semaphore . Semaphore > = Option . fromNullishOr (
269- current . get ( threadId ) ,
273+ const existing = current . get ( threadId ) ;
274+ if ( existing ) {
275+ const next = new Map ( current ) ;
276+ next . set ( threadId , { ...existing , users : existing . users + 1 } ) ;
277+ return Effect . succeed ( [ existing . semaphore , next ] as const ) ;
278+ }
279+ return Semaphore . make ( 1 ) . pipe (
280+ Effect . map ( ( semaphore ) => {
281+ const next = new Map ( current ) ;
282+ next . set ( threadId , { semaphore, users : 1 } ) ;
283+ return [ semaphore , next ] as const ;
284+ } ) ,
270285 ) ;
271- return Option . match ( existing , {
272- onNone : ( ) =>
273- Semaphore . make ( 1 ) . pipe (
274- Effect . map ( ( semaphore ) => {
275- const next = new Map ( current ) ;
276- next . set ( threadId , semaphore ) ;
277- return [ semaphore , next ] as const ;
278- } ) ,
279- ) ,
280- onSome : ( semaphore ) => Effect . succeed ( [ semaphore , current ] as const ) ,
281- } ) ;
286+ } ) ;
287+
288+ const releaseThreadSemaphore = ( threadId : string ) =>
289+ SynchronizedRef . update ( threadLocksRef , ( current ) => {
290+ const existing = current . get ( threadId ) ;
291+ if ( ! existing ) return current ;
292+ const next = new Map ( current ) ;
293+ if ( existing . users <= 1 ) {
294+ next . delete ( threadId ) ;
295+ } else {
296+ next . set ( threadId , { ...existing , users : existing . users - 1 } ) ;
297+ }
298+ return next ;
282299 } ) ;
283300
284301 const withThreadLock = < A , E , R > ( threadId : string , effect : Effect . Effect < A , E , R > ) =>
285- Effect . flatMap ( getThreadSemaphore ( threadId ) , ( semaphore ) => semaphore . withPermit ( effect ) ) ;
302+ Effect . acquireUseRelease (
303+ acquireThreadSemaphore ( threadId ) ,
304+ ( semaphore ) => semaphore . withPermit ( effect ) ,
305+ ( ) => releaseThreadSemaphore ( threadId ) ,
306+ ) ;
286307
287308 const settlePromptInFlight = (
288309 threadId : ThreadId ,
@@ -563,7 +584,6 @@ export function makeFxAdapter(fxSettings: FxSettings, options?: FxAdapterLiveOpt
563584 const acp = yield * makeFxAcpRuntime ( {
564585 fxSettings,
565586 ...( options ?. environment ? { environment : options . environment } : { } ) ,
566- childProcessSpawner,
567587 cwd,
568588 ...( resumeSessionId ? { resumeSessionId } : { } ) ,
569589 clientInfo : { name : "t3-code" , version : "0.0.0" } ,
@@ -586,14 +606,15 @@ export function makeFxAdapter(fxSettings: FxSettings, options?: FxAdapterLiveOpt
586606 : { } ) ,
587607 ...acpNativeLoggers ,
588608 } ) . pipe (
609+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , childProcessSpawner ) ,
589610 Effect . provideService ( Crypto . Crypto , crypto ) ,
590611 Effect . provideService ( Scope . Scope , sessionScope ) ,
591612 Effect . mapError (
592613 ( cause ) =>
593614 new ProviderAdapterProcessError ( {
594615 provider : PROVIDER ,
595616 threadId : input . threadId ,
596- detail : cause . message ,
617+ detail : "Failed to start the fx ACP runtime." ,
597618 cause,
598619 } ) ,
599620 ) ,
@@ -919,7 +940,7 @@ export function makeFxAdapter(fxSettings: FxSettings, options?: FxAdapterLiveOpt
919940 new ProviderAdapterRequestError ( {
920941 provider : PROVIDER ,
921942 method : "session/prompt" ,
922- detail : cause . message ,
943+ detail : "Failed to read an fx prompt attachment." ,
923944 cause,
924945 } ) ,
925946 ) ,
0 commit comments