@@ -113,6 +113,7 @@ type AuthScopedProvider = typeof providerIdentifiers.zooGateway | typeof provide
113113type AuthScopedGetModelsOptions = Extract < GetModelsOptions , { provider : AuthScopedProvider } >
114114
115115const AUTH_SESSION_TTL_MS = 5 * 60 * 1000
116+ const AUTH_SESSION_MAX_ENTRIES = 64
116117
117118type AuthSessionCacheEntry = {
118119 models : ModelRecord
@@ -140,6 +141,34 @@ function isAuthSessionFresh(entry: AuthSessionCacheEntry): boolean {
140141 return Date . now ( ) - entry . fetchedAt < AUTH_SESSION_TTL_MS
141142}
142143
144+ function pruneExpiredAuthSessionEntries ( ) : void {
145+ const staleCutoffMs = AUTH_SESSION_TTL_MS * 2
146+ const now = Date . now ( )
147+ for ( const [ key , entry ] of authSessionCache ) {
148+ if ( now - entry . fetchedAt >= staleCutoffMs ) {
149+ authSessionCache . delete ( key )
150+ }
151+ }
152+ }
153+
154+ function enforceAuthSessionCacheBound ( ) : void {
155+ pruneExpiredAuthSessionEntries ( )
156+ while ( authSessionCache . size > AUTH_SESSION_MAX_ENTRIES ) {
157+ let oldestKey : string | undefined
158+ let oldestFetchedAt = Infinity
159+ for ( const [ key , entry ] of authSessionCache ) {
160+ if ( entry . fetchedAt < oldestFetchedAt ) {
161+ oldestFetchedAt = entry . fetchedAt
162+ oldestKey = key
163+ }
164+ }
165+ if ( ! oldestKey ) {
166+ break
167+ }
168+ authSessionCache . delete ( oldestKey )
169+ }
170+ }
171+
143172function getAuthSessionEntry ( cacheKey : string ) : AuthSessionCacheEntry | undefined {
144173 return authSessionCache . get ( cacheKey )
145174}
@@ -149,10 +178,12 @@ function setAuthSessionEntry(cacheKey: string, models: ModelRecord, etag?: strin
149178 return
150179 }
151180 authSessionCache . set ( cacheKey , { models, etag, fetchedAt : Date . now ( ) } )
181+ enforceAuthSessionCacheBound ( )
152182}
153183
154184function touchAuthSessionEntry ( cacheKey : string , entry : AuthSessionCacheEntry ) : void {
155185 authSessionCache . set ( cacheKey , { ...entry , fetchedAt : Date . now ( ) } )
186+ enforceAuthSessionCacheBound ( )
156187}
157188
158189function deleteAuthSessionEntry ( cacheKey : string ) : void {
@@ -634,7 +665,11 @@ export const flushModels = async (options: GetModelsOptions, refresh: boolean =
634665 if ( isAuthScopedProvider ( options . provider ) ) {
635666 deleteAuthSessionEntry ( getCacheKey ( options ) )
636667 if ( refresh ) {
637- await resolveAuthScopedModels ( options , { forceRefresh : true } )
668+ try {
669+ await resolveAuthScopedModels ( options , { forceRefresh : true } )
670+ } catch ( error ) {
671+ console . error ( `[flushModels] Failed to refresh auth-scoped ${ getCacheKey ( options ) } models:` , error )
672+ }
638673 }
639674 return
640675 }
0 commit comments