@@ -71,6 +71,17 @@ export const PROVIDER_INTERRUPT_TIMEOUT_MS = 15 * 1000;
7171const busyTurnPrefix = ( providerThreadId : ProviderThreadId ) => `${ providerThreadId } #` ;
7272const busyTurnKey = ( providerThreadId : ProviderThreadId , runOrdinal : number ) =>
7373 `${ busyTurnPrefix ( providerThreadId ) } ${ runOrdinal } ` ;
74+ interface OwnBusyTurns {
75+ readonly providerThreadIds : ReadonlyArray < ProviderThreadId > ;
76+ readonly throughRunOrdinal : number ;
77+ }
78+ const isOwnBusyTurn = ( turnKey : string , own : OwnBusyTurns ) =>
79+ own . providerThreadIds . some ( ( id ) => {
80+ const prefix = busyTurnPrefix ( id ) ;
81+ return (
82+ turnKey . startsWith ( prefix ) && Number ( turnKey . slice ( prefix . length ) ) <= own . throughRunOrdinal
83+ ) ;
84+ } ) ;
7485const UNLOAD_THREAD_TIMEOUT_MS = 10 * 1000 ;
7586// Providers can announce a retry and never resume. Longer than any adapter's own stall handling.
7687const PROVIDER_RETRY_STALL_TIMEOUT = Duration . minutes ( 15 ) ;
@@ -243,13 +254,16 @@ export interface ProviderSessionManagerV2Shape {
243254 readonly revokeMcpCredential ?: boolean ;
244255 } ) => Effect . Effect < void , ProviderSessionManagerV2Error > ;
245256 /**
246- * Releases the session unless, at the moment of removal, a turn of a provider thread outside
247- * `ownProviderThreadIds` is busy on it . Checked atomically with turn starts. Succeeds with
248- * whether it was released.
257+ * Releases the session unless, at the moment of removal, a turn is busy on it other than one
258+ * of `ownProviderThreadIds` from a run up to `throughRunOrdinal` . Checked atomically with turn
259+ * starts. Succeeds with whether it was released.
249260 */
250261 readonly releaseUnlessBusy : ( input : {
251262 readonly providerSessionId : ProviderSessionId ;
252263 readonly ownProviderThreadIds : ReadonlyArray < ProviderThreadId > ;
264+ readonly throughRunOrdinal : number ;
265+ /** Keeps the session when it succeeds with true, checked atomically with the removal. */
266+ readonly keepIf ?: Effect . Effect < boolean > ;
253267 readonly reason : ProviderSessionReleaseReason ;
254268 readonly detail ?: string ;
255269 } ) => Effect . Effect < boolean , ProviderSessionManagerV2Error > ;
@@ -1125,48 +1139,56 @@ export const layerWithOptions = (
11251139 function * ( input : {
11261140 readonly providerSessionId : ProviderSessionId ;
11271141 readonly onlyIfIdleGeneration ?: number ;
1128- readonly keepIfBusyOtherThan ?: ReadonlyArray < ProviderThreadId > ;
1142+ readonly keepIfBusyOtherThan ?: OwnBusyTurns ;
11291143 readonly keepIf ?: Effect . Effect < boolean > ;
11301144 } ) : Effect . fn . Return < readonly [ Option . Option < LiveSessionEntry > , DateTime . Utc ] > {
11311145 const key = sessionKey ( input . providerSessionId ) ;
11321146 const candidate = ( yield * Ref . get ( sessions ) ) . get ( key ) ;
11331147 if ( candidate === undefined ) {
11341148 return [ Option . none < LiveSessionEntry > ( ) , yield * DateTime . now ] as const ;
11351149 }
1136- const remove = Effect . zip (
1137- Ref . modify ( sessions , ( current ) => {
1138- const existing = current . get ( key ) ;
1139- if ( existing !== candidate ) {
1140- return [ existing === undefined ? "gone" : "changed" , current ] as const ;
1141- }
1142- if (
1143- input . onlyIfIdleGeneration !== undefined &&
1144- ( existing . busyTurns . size > 0 ||
1145- existing . idleGeneration !== input . onlyIfIdleGeneration )
1146- ) {
1147- return [ "kept" , current ] as const ;
1148- }
1149- const own = input . keepIfBusyOtherThan ;
1150- if (
1151- own !== undefined &&
1152- [ ...existing . busyTurns ] . some (
1153- ( turnKey ) => ! own . some ( ( id ) => turnKey . startsWith ( busyTurnPrefix ( id ) ) ) ,
1154- )
1155- ) {
1156- return [ "kept" , current ] as const ;
1157- }
1158- const updated = new Map ( current ) ;
1159- updated . delete ( key ) ;
1160- return [ "removed" , updated ] as const ;
1161- } ) ,
1162- DateTime . now ,
1150+ const remove = ( keepRequested : boolean ) =>
1151+ Effect . zip (
1152+ Ref . modify ( sessions , ( current ) => {
1153+ const existing = current . get ( key ) ;
1154+ if ( existing !== candidate ) {
1155+ return [ existing === undefined ? "gone" : "changed" , current ] as const ;
1156+ }
1157+ if (
1158+ input . onlyIfIdleGeneration !== undefined &&
1159+ ( existing . busyTurns . size > 0 ||
1160+ existing . idleGeneration !== input . onlyIfIdleGeneration )
1161+ ) {
1162+ return [ "kept" , current ] as const ;
1163+ }
1164+ const own = input . keepIfBusyOtherThan ;
1165+ if (
1166+ keepRequested ||
1167+ ( own !== undefined &&
1168+ [ ...existing . busyTurns ] . some ( ( turnKey ) => ! isOwnBusyTurn ( turnKey , own ) ) )
1169+ ) {
1170+ if ( own === undefined ) return [ "kept" , current ] as const ;
1171+ // The caller settled its own turns; one whose end never arrives must not keep
1172+ // the session from going idle once the turn it was kept for ends.
1173+ const kept = new Map ( current ) ;
1174+ kept . set ( key , {
1175+ ...existing ,
1176+ busyTurns : new Set (
1177+ [ ...existing . busyTurns ] . filter ( ( turnKey ) => ! isOwnBusyTurn ( turnKey , own ) ) ,
1178+ ) ,
1179+ } ) ;
1180+ return [ "kept" , kept ] as const ;
1181+ }
1182+ const updated = new Map ( current ) ;
1183+ updated . delete ( key ) ;
1184+ return [ "removed" , updated ] as const ;
1185+ } ) ,
1186+ DateTime . now ,
1187+ ) ;
1188+ const [ outcome , releasedAt ] = yield * ( input . keepIf ?? Effect . succeed ( false ) ) . pipe (
1189+ Effect . flatMap ( remove ) ,
1190+ candidate . requestEventPermit . withPermits ( 1 ) ,
11631191 ) ;
1164- const [ outcome , releasedAt ] = yield * Effect . gen ( function * ( ) {
1165- if ( input . keepIf !== undefined && ( yield * input . keepIf ) ) {
1166- return [ "kept" as const , yield * DateTime . now ] as const ;
1167- }
1168- return yield * remove ;
1169- } ) . pipe ( candidate . requestEventPermit . withPermits ( 1 ) ) ;
11701192 if ( outcome === "changed" ) return yield * removeLiveEntry ( input ) ;
11711193 return [
11721194 outcome === "removed" ? Option . some ( candidate ) : Option . none < LiveSessionEntry > ( ) ,
@@ -1218,7 +1240,7 @@ export const layerWithOptions = (
12181240 readonly detachedThreadId ?: ThreadId ;
12191241 readonly cancelIdleFiber ?: boolean ;
12201242 readonly onlyIfIdleGeneration ?: number ;
1221- readonly keepIfBusyOtherThan ?: ReadonlyArray < ProviderThreadId > ;
1243+ readonly keepIfBusyOtherThan ?: OwnBusyTurns ;
12221244 readonly keepIf ?: Effect . Effect < boolean > ;
12231245 readonly gracefulSubscribers ?: boolean ;
12241246 readonly eventStreamCause ?: Cause . Cause < ProviderAdapter . ProviderAdapterV2Error > ;
@@ -3076,10 +3098,19 @@ export const layerWithOptions = (
30763098 providerSessionId : input . providerSessionId ,
30773099 reason : input . reason ,
30783100 ...( input . detail === undefined ? { } : { detail : input . detail } ) ,
3079- keepIfBusyOtherThan : input . ownProviderThreadIds ,
3101+ keepIfBusyOtherThan : {
3102+ providerThreadIds : input . ownProviderThreadIds ,
3103+ throughRunOrdinal : input . throughRunOrdinal ,
3104+ } ,
3105+ ...( input . keepIf === undefined ? { } : { keepIf : input . keepIf } ) ,
30803106 } ) ;
3081- // Identity, not presence: open can install a replacement under the same id meanwhile.
3082- return before !== undefined && ( yield * Ref . get ( sessions ) ) . get ( key ) !== before ;
3107+ // Scope, not presence: open can install a replacement under the same id meanwhile, and
3108+ // a kept entry is rewritten with the same scope.
3109+ const released =
3110+ before !== undefined && ( yield * Ref . get ( sessions ) ) . get ( key ) ?. scope !== before . scope ;
3111+ // The stopped turn's markBusy cancelled the idle timer and its markIdle may never come.
3112+ if ( ! released ) yield * scheduleIdleReleaseInternal ( input . providerSessionId ) ;
3113+ return released ;
30833114 } ) ,
30843115 otherWorkingThreads : Effect . fn ( "ProviderSessionManager.otherWorkingThreads" ) (
30853116 function * ( input ) {
0 commit comments