@@ -31,8 +31,15 @@ export interface ContinuationRefreshOptions {
3131const inFlightRefreshes = new Map < string , Promise < InferenceSource > > ( ) ;
3232
3333// Wrapped directors, mapped back to their proxy, so a second application is a
34- // no-op instead of stacking duplicate refreshes.
35- const wrappedDirectors = new WeakMap < object , ReactorDirector > ( ) ;
34+ // no-op instead of stacking duplicate refreshes. The entry also holds the
35+ // latest options: a re-wrap with a new source set must update what decide()
36+ // reads, not be silently dropped by the cache.
37+ interface WrappedDirectorEntry {
38+ proxy : ReactorDirector ;
39+ options : ContinuationRefreshOptions ;
40+ }
41+
42+ const wrappedDirectors = new WeakMap < object , WrappedDirectorEntry > ( ) ;
3643
3744async function stagedOAuthTokensFresh (
3845 provenance : OAuthCredentialProvenance ,
@@ -125,20 +132,28 @@ export function withContinuationOAuthRefresh<TDirector extends ReactorDirector>(
125132 options : ContinuationRefreshOptions ,
126133) : TDirector {
127134 const cached = wrappedDirectors . get ( inner ) ;
128- if ( cached !== undefined ) return cached as TDirector ;
135+ if ( cached !== undefined ) {
136+ // Second application is a no-op for the proxy, but not for the options:
137+ // the caller may re-wrap the same director with a different source set,
138+ // and decide() below reads entry.options at call time, so the latest
139+ // wrap's sources win instead of the first wrap's.
140+ cached . options = options ;
141+ return cached . proxy as TDirector ;
142+ }
143+ const entry : WrappedDirectorEntry = { proxy : inner , options } ;
129144 const decide = async (
130145 event : Parameters < TDirector [ "decide" ] > [ 0 ] ,
131146 state : Parameters < TDirector [ "decide" ] > [ 1 ] ,
132147 capabilities : Parameters < TDirector [ "decide" ] > [ 2 ] ,
133148 ) => {
134149 const result = await inner . decide ( event , state , capabilities ) ;
135150 if ( ! returnsInferAction ( result ) ) return result ;
136- const agent = options . getAgent ( ) ;
151+ const agent = entry . options . getAgent ( ) ;
137152 if ( agent === null ) return result ;
138153 const fresh = await refreshInferenceSourceBundle (
139- options . sources ,
140- options . defaultSource ,
141- options . catalog ,
154+ entry . options . sources ,
155+ entry . options . defaultSource ,
156+ entry . options . catalog ,
142157 ) ;
143158 agent . setSources ( fresh . sources , fresh . defaultSource ) ;
144159 return result ;
@@ -149,7 +164,8 @@ export function withContinuationOAuthRefresh<TDirector extends ReactorDirector>(
149164 return Reflect . get ( target , property ) ;
150165 } ,
151166 } ) ;
152- wrappedDirectors . set ( inner , proxy ) ;
153- wrappedDirectors . set ( proxy , proxy ) ;
167+ entry . proxy = proxy ;
168+ wrappedDirectors . set ( inner , entry ) ;
169+ wrappedDirectors . set ( proxy , entry ) ;
154170 return proxy ;
155171}
0 commit comments