@@ -217,7 +217,7 @@ export class ProviderSessionManagerV2 extends Context.Service<
217217
218218interface LiveSessionEntry {
219219 readonly attachedThreadIds : ReadonlySet < ThreadId > ;
220- readonly loadedProviderThreadKeyByThread : ReadonlyMap < ThreadId , string > ;
220+ readonly loadedProviderThreadKeyByThread : ReadonlyMap < ThreadId , ProviderThreadLoadKey > ;
221221 /** Scope revocation to recorded credential ids so superseded sessions cannot revoke replacements opened before detach. */
222222 readonly mcpCredentialIdByThread : ReadonlyMap < ThreadId , string > ;
223223 readonly supportsMultipleProviderThreads : boolean ;
@@ -301,18 +301,25 @@ function providerThreadRuntimeKey(
301301 : `${ nativeThreadRef . driver } :${ nativeThreadRef . nativeId } ` ;
302302}
303303
304+ interface ProviderThreadLoadKey {
305+ readonly providerThread : string ;
306+ readonly config : string ;
307+ }
308+
304309function providerThreadLoadKey ( input : {
305310 readonly providerThread : Parameters <
306311 ProviderAdapter . ProviderAdapterV2SessionRuntime [ "resumeThread" ]
307312 > [ 0 ] [ "providerThread" ] ;
308313 readonly modelSelection ?: ModelSelection ;
309314 readonly runtimePolicy ?: ProviderAdapter . ProviderAdapterV2RuntimePolicy ;
310- } ) : string {
311- return JSON . stringify ( {
315+ } ) : ProviderThreadLoadKey {
316+ return {
312317 providerThread : providerThreadRuntimeKey ( input . providerThread ) ,
313- modelSelection : input . modelSelection ?? null ,
314- runtimePolicy : input . runtimePolicy ?? null ,
315- } ) ;
318+ config : JSON . stringify ( {
319+ modelSelection : input . modelSelection ?? null ,
320+ runtimePolicy : input . runtimePolicy ?? null ,
321+ } ) ,
322+ } ;
316323}
317324
318325export const layerWithOptions = (
@@ -1297,24 +1304,22 @@ export const layerWithOptions = (
12971304 return updated ;
12981305 } ) ;
12991306
1300- const isProviderThreadLoaded = ( input : {
1307+ const loadedProviderThreadKey = ( input : {
13011308 readonly providerSessionId : ProviderSessionId ;
13021309 readonly threadId : ThreadId ;
1303- readonly providerThreadKey : string ;
13041310 } ) =>
13051311 Ref . get ( sessions ) . pipe (
1306- Effect . map (
1307- ( current ) =>
1308- current
1309- . get ( sessionKey ( input . providerSessionId ) )
1310- ?. loadedProviderThreadKeyByThread . get ( input . threadId ) === input . providerThreadKey ,
1312+ Effect . map ( ( current ) =>
1313+ current
1314+ . get ( sessionKey ( input . providerSessionId ) )
1315+ ?. loadedProviderThreadKeyByThread . get ( input . threadId ) ,
13111316 ) ,
13121317 ) ;
13131318
13141319 const markProviderThreadLoaded = ( input : {
13151320 readonly providerSessionId : ProviderSessionId ;
13161321 readonly threadId : ThreadId ;
1317- readonly providerThreadKey : string ;
1322+ readonly providerThreadKey : ProviderThreadLoadKey ;
13181323 } ) =>
13191324 Ref . update ( sessions , ( current ) => {
13201325 const key = sessionKey ( input . providerSessionId ) ;
@@ -1708,6 +1713,62 @@ export const layerWithOptions = (
17081713 modelFamily : normalizeModelMetricLabel ( model ) ,
17091714 } ,
17101715 } ) ;
1716+ const loadThread = (
1717+ input : Parameters < ProviderAdapter . ProviderAdapterV2SessionRuntime [ "resumeThread" ] > [ 0 ] & {
1718+ readonly threadId : ThreadId ;
1719+ } ,
1720+ isLoaded : ( loaded : ProviderThreadLoadKey | undefined ) => boolean ,
1721+ ) =>
1722+ observeActivity (
1723+ providerSessionId ,
1724+ ensureThreadAttached ( {
1725+ providerSessionId,
1726+ threadId : input . threadId ,
1727+ providerInstanceId : runtime . instanceId ,
1728+ } ) ,
1729+ ) . pipe (
1730+ Effect . andThen (
1731+ holdThreadLoaded ( {
1732+ providerSessionId,
1733+ threadId : input . threadId ,
1734+ providerThread : input . providerThread ,
1735+ } ) ,
1736+ ) ,
1737+ Effect . andThen (
1738+ loadedProviderThreadKey ( { providerSessionId, threadId : input . threadId } ) ,
1739+ ) ,
1740+ Effect . flatMap ( ( loaded ) =>
1741+ isLoaded ( loaded )
1742+ ? Effect . succeed ( input . providerThread )
1743+ : runtime . resumeThread ( input ) . pipe (
1744+ Effect . tap ( ( providerThread ) =>
1745+ markProviderThreadLoaded ( {
1746+ providerSessionId,
1747+ threadId : input . threadId ,
1748+ providerThreadKey : providerThreadLoadKey ( { ...input , providerThread } ) ,
1749+ } ) ,
1750+ ) ,
1751+ ) ,
1752+ ) ,
1753+ ) ;
1754+ const mcpApps = runtime . mcpApps ;
1755+ // Any load of this provider thread serves MCP calls; a load made here records no model or
1756+ // policy, so the next turn still resumes with its own.
1757+ const withThreadLoaded = < A > (
1758+ providerThread : OrchestrationV2ProviderThread ,
1759+ call : Effect . Effect < A , ProviderAdapter . ProviderAdapterV2Error > ,
1760+ ) => {
1761+ const threadId = providerThread . appThreadId ;
1762+ if ( threadId === null ) return call ;
1763+ const runtimeKey = providerThreadRuntimeKey ( providerThread ) ;
1764+ return loadThread (
1765+ { providerThread, threadId } ,
1766+ ( loaded ) => loaded ?. providerThread === runtimeKey ,
1767+ ) . pipe (
1768+ Effect . andThen ( call ) ,
1769+ Effect . ensuring ( scheduleThreadUnload ( providerSessionId , providerThread . id ) ) ,
1770+ ) ;
1771+ } ;
17111772 return {
17121773 ...runtime ,
17131774 subscribeEvents,
@@ -1741,51 +1802,26 @@ export const layerWithOptions = (
17411802 if ( threadId === null || threadId === undefined ) {
17421803 return runtime . resumeThread ( input ) ;
17431804 }
1744- const providerThreadKey = providerThreadLoadKey ( {
1745- providerThread : input . providerThread ,
1746- ...( input . modelSelection === undefined
1747- ? { }
1748- : { modelSelection : input . modelSelection } ) ,
1749- ...( input . runtimePolicy === undefined ? { } : { runtimePolicy : input . runtimePolicy } ) ,
1750- } ) ;
1751- return observeActivity (
1752- providerSessionId ,
1753- ensureThreadAttached ( {
1754- providerSessionId,
1755- threadId,
1756- providerInstanceId : runtime . instanceId ,
1757- } ) ,
1758- ) . pipe (
1759- Effect . andThen (
1760- holdThreadLoaded ( {
1761- providerSessionId,
1762- threadId,
1763- providerThread : input . providerThread ,
1764- } ) ,
1765- ) ,
1766- Effect . andThen (
1767- isProviderThreadLoaded ( { providerSessionId, threadId, providerThreadKey } ) ,
1768- ) ,
1769- Effect . flatMap ( ( loaded ) =>
1770- loaded ? Effect . succeed ( input . providerThread ) : runtime . resumeThread ( input ) ,
1771- ) ,
1772- Effect . tap ( ( providerThread ) =>
1773- markProviderThreadLoaded ( {
1774- providerSessionId,
1775- threadId,
1776- providerThreadKey : providerThreadLoadKey ( {
1777- providerThread,
1778- ...( input . modelSelection === undefined
1779- ? { }
1780- : { modelSelection : input . modelSelection } ) ,
1781- ...( input . runtimePolicy === undefined
1782- ? { }
1783- : { runtimePolicy : input . runtimePolicy } ) ,
1784- } ) ,
1785- } ) ,
1786- ) ,
1805+ const providerThreadKey = providerThreadLoadKey ( input ) ;
1806+ return loadThread (
1807+ { ...input , threadId } ,
1808+ ( loaded ) =>
1809+ loaded ?. providerThread === providerThreadKey . providerThread &&
1810+ loaded . config === providerThreadKey . config ,
17871811 ) ;
17881812 } ,
1813+ ...( mcpApps === undefined
1814+ ? { }
1815+ : {
1816+ mcpApps : {
1817+ listTools : ( input ) =>
1818+ withThreadLoaded ( input . providerThread , mcpApps . listTools ( input ) ) ,
1819+ callTool : ( input ) =>
1820+ withThreadLoaded ( input . providerThread , mcpApps . callTool ( input ) ) ,
1821+ readResource : ( input ) =>
1822+ withThreadLoaded ( input . providerThread , mcpApps . readResource ( input ) ) ,
1823+ } ,
1824+ } ) ,
17891825 forkThread : ( input ) =>
17901826 observeActivity (
17911827 providerSessionId ,
0 commit comments