@@ -42,7 +42,10 @@ import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderS
4242import * as McpSessionRegistry from "../mcp/McpSessionRegistry.ts" ;
4343import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
4444import * as ServerSettings from "../serverSettings.ts" ;
45- import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts" ;
45+ import {
46+ CodexProviderCapabilitiesV2 ,
47+ normalizeCodexThreadLoadModelSelection ,
48+ } from "./Adapters/CodexAdapterV2.ts" ;
4649import * as EventSink from "./EventSink.ts" ;
4750import * as EventStore from "./EventStore.ts" ;
4851import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator" ;
@@ -427,6 +430,7 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
427430 state : Ref . Ref < TestProviderRuntimeState > ,
428431 options : {
429432 readonly failEventStream ?: boolean ;
433+ readonly normalizeThreadLoadModelSelection ?: ( selection : ModelSelection ) => ModelSelection ;
430434 readonly capabilities ?: OrchestrationV2ProviderCapabilities ;
431435 readonly extensionStatuses ?: OrchestrationV2ProviderSession [ "extensionStatuses" ] ;
432436 readonly mcpConfigs ?: Ref . Ref <
@@ -513,6 +517,9 @@ const makeProviderAdapter = Effect.fnUntraced(function* (
513517 instanceId : ProviderInstanceId . make ( "codex" ) ,
514518 driver : CODEX_DRIVER ,
515519 providerSessionId : input . providerSessionId ,
520+ ...( options . normalizeThreadLoadModelSelection === undefined
521+ ? { }
522+ : { normalizeThreadLoadModelSelection : options . normalizeThreadLoadModelSelection } ) ,
516523 get providerSession ( ) {
517524 return session ;
518525 } ,
@@ -581,6 +588,7 @@ function layerTest(input: {
581588 readonly idleTimeoutMs : number ;
582589 readonly maxIdlePinMs ?: number ;
583590 readonly failEventStream ?: boolean ;
591+ readonly normalizeThreadLoadModelSelection ?: ( selection : ModelSelection ) => ModelSelection ;
584592 readonly capabilities ?: OrchestrationV2ProviderCapabilities ;
585593 readonly extensionStatuses ?: OrchestrationV2ProviderSession [ "extensionStatuses" ] ;
586594 readonly mcpConfigs ?: Ref . Ref <
@@ -623,6 +631,9 @@ function layerTest(input: {
623631 const layerRegistry = Layer . unwrap (
624632 makeProviderAdapter ( input . state , {
625633 failEventStream : input . failEventStream ?? false ,
634+ ...( input . normalizeThreadLoadModelSelection === undefined
635+ ? { }
636+ : { normalizeThreadLoadModelSelection : input . normalizeThreadLoadModelSelection } ) ,
626637 ...( input . capabilities === undefined ? { } : { capabilities : input . capabilities } ) ,
627638 ...( input . extensionStatuses === undefined
628639 ? { }
@@ -4054,9 +4065,15 @@ it.effect(
40544065 } ) ,
40554066) ;
40564067
4057- it . effect (
4058- "ProviderSessionManagerV2 opens one shared runtime, broadcasts events, and detaches threads independently" ,
4059- ( ) =>
4068+ it . effect . each ( [
4069+ { name : "default load key" , normalizeThreadLoadModelSelection : undefined } ,
4070+ {
4071+ name : "Codex load key" ,
4072+ normalizeThreadLoadModelSelection : normalizeCodexThreadLoadModelSelection ,
4073+ } ,
4074+ ] ) (
4075+ "ProviderSessionManagerV2 opens one shared runtime, broadcasts events, and detaches threads independently ($name)" ,
4076+ ( { normalizeThreadLoadModelSelection } ) =>
40604077 Effect . gen ( function * ( ) {
40614078 const state = yield * Ref . make ( emptyState ) ;
40624079 const settings = yield * ServerSettings . ServerSettingsService . pipe (
@@ -4153,25 +4170,91 @@ it.effect(
41534170 const resumeSecondThread = secondRuntime . resumeThread ( {
41544171 providerThread : secondProviderThread ,
41554172 threadId : secondThreadId ,
4156- modelSelection,
4173+ modelSelection : {
4174+ ...modelSelection ,
4175+ options : [ { id : "reasoningEffort" , value : "medium" } ] ,
4176+ } ,
41574177 runtimePolicy,
41584178 } ) ;
41594179 yield * resumeSecondThread ;
41604180 yield * resumeSecondThread ;
41614181 assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , 1 ) ;
4182+ let resumeCount = 1 ;
4183+ const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
4184+ const appThread = ( yield * projectionStore . getThreadProjection ( secondThreadId ) ) . thread ;
4185+ const turnEvents = ( yield * Ref . get ( state ) ) . eventQueues . get ( String ( providerSessionId ) ) ;
4186+ assert . isDefined ( turnEvents ) ;
4187+ for ( const [ index , effort ] of [ "medium" , "high" , "medium" ] . entries ( ) ) {
4188+ const selection = {
4189+ ...modelSelection ,
4190+ options : [ { id : "reasoningEffort" , value : effort } ] ,
4191+ } satisfies ModelSelection ;
4192+ yield * secondRuntime . resumeThread ( {
4193+ providerThread : secondProviderThread ,
4194+ threadId : secondThreadId ,
4195+ modelSelection : selection ,
4196+ runtimePolicy,
4197+ } ) ;
4198+ const ordinal = index + 1 ;
4199+ const runId = idAllocator . derive . run ( { threadId : secondThreadId , ordinal } ) ;
4200+ yield * secondRuntime . startTurn ( {
4201+ appThread,
4202+ threadId : secondThreadId ,
4203+ runId,
4204+ runOrdinal : ordinal ,
4205+ providerTurnOrdinal : ordinal ,
4206+ attemptId : idAllocator . derive . runAttempt ( { runId, attemptOrdinal : 1 } ) ,
4207+ rootNodeId : idAllocator . derive . rootNode ( { runId } ) ,
4208+ providerThread : secondProviderThread ,
4209+ message : {
4210+ createdBy : "user" ,
4211+ creationSource : "web" ,
4212+ messageId : yield * idAllocator . allocate . message ( {
4213+ threadId : secondThreadId ,
4214+ ordinal,
4215+ } ) ,
4216+ text : "turn" ,
4217+ attachments : [ ] ,
4218+ } ,
4219+ modelSelection : selection ,
4220+ runtimePolicy,
4221+ } ) ;
4222+ const subscription = yield * secondRuntime . subscribeEvents ! ;
4223+ const terminal = yield * subscription . events . pipe (
4224+ Stream . filter ( ( event ) => event . type === "turn.terminal" ) ,
4225+ Stream . runHead ,
4226+ Effect . forkScoped ,
4227+ ) ;
4228+ yield * Queue . offer ( turnEvents ! , {
4229+ type : "turn.terminal" ,
4230+ driver : CODEX_DRIVER ,
4231+ providerThreadId : secondProviderThread . id ,
4232+ providerTurnId : idAllocator . derive . providerTurn ( {
4233+ driver : CODEX_DRIVER ,
4234+ nativeTurnId : `native-effort-turn-${ ordinal } ` ,
4235+ } ) ,
4236+ runOrdinal : ordinal ,
4237+ status : "completed" ,
4238+ failure : null ,
4239+ threadDisposition : "reusable" ,
4240+ } ) ;
4241+ yield * Fiber . join ( terminal ) ;
4242+ if ( normalizeThreadLoadModelSelection === undefined && index > 0 ) resumeCount ++ ;
4243+ assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , resumeCount , `${ effort } effort reload` ) ;
4244+ }
41624245 yield * secondRuntime . resumeThread ( {
41634246 providerThread : secondProviderThread ,
41644247 threadId : secondThreadId ,
41654248 modelSelection : { ...modelSelection , model : "gpt-5.4-mini" } ,
41664249 runtimePolicy,
41674250 } ) ;
4168- assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , 2 ) ;
4251+ assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , ++ resumeCount ) ;
41694252 yield * resumeSecondThread ;
4170- assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , 3 ) ;
4253+ assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , ++ resumeCount ) ;
41714254 // regression: #26722 a Linked environments change reloads only the idle thread's tools.
41724255 yield * settings . updateSettings ( { linkedEnvironmentsEnabled : true } ) ;
41734256 yield * resumeSecondThread ;
4174- assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , 4 ) ;
4257+ assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , ++ resumeCount ) ;
41754258 assert . deepEqual ( ( yield * Ref . get ( state ) ) . unloadedNativeThreadIds , [ "native-thread" ] ) ;
41764259 const subscribe = firstRuntime . subscribeEvents ;
41774260 assert . isDefined ( subscribe ) ;
@@ -4204,10 +4287,43 @@ it.effect(
42044287 runtimePolicy,
42054288 } ) ;
42064289 yield * resumeSecondThread ;
4207- assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , 5 ) ;
4290+ assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , ++ resumeCount ) ;
42084291
42094292 assert . deepEqual ( ( yield * Ref . get ( state ) ) . unloadedNativeThreadIds , [ "native-thread" ] ) ;
42104293
4294+ const reloadCases = [
4295+ {
4296+ name : "other model options" ,
4297+ providerThread : secondProviderThread ,
4298+ selection : {
4299+ ...modelSelection ,
4300+ options : [ { id : "serviceTier" , value : "priority" } ] ,
4301+ } ,
4302+ policy : runtimePolicy ,
4303+ } ,
4304+ {
4305+ name : "runtime policy" ,
4306+ providerThread : secondProviderThread ,
4307+ selection : modelSelection ,
4308+ policy : { ...runtimePolicy , interactionMode : "plan" } ,
4309+ } ,
4310+ ] satisfies ReadonlyArray < {
4311+ name : string ;
4312+ providerThread : OrchestrationV2ProviderThread ;
4313+ selection : ModelSelection ;
4314+ policy : ProviderAdapter . ProviderAdapterV2RuntimePolicy ;
4315+ } > ;
4316+ for ( const { name, providerThread, selection, policy } of reloadCases ) {
4317+ const before = ( yield * Ref . get ( state ) ) . resumeCount ;
4318+ yield * secondRuntime . resumeThread ( {
4319+ providerThread,
4320+ threadId : secondThreadId ,
4321+ modelSelection : selection ,
4322+ runtimePolicy : policy ,
4323+ } ) ;
4324+ assert . equal ( ( yield * Ref . get ( state ) ) . resumeCount , before + 1 , `${ name } did not reload` ) ;
4325+ }
4326+
42114327 yield * manager . detach ( { providerSessionId, threadId : firstThreadId } ) ;
42124328 assert . isTrue ( Option . isSome ( yield * manager . get ( providerSessionId ) ) ) ;
42134329 assert . equal ( ( yield * Ref . get ( state ) ) . closeCount , 0 ) ;
@@ -4228,6 +4344,9 @@ it.effect(
42284344 layerTest ( {
42294345 state,
42304346 idleTimeoutMs : 1000 ,
4347+ ...( normalizeThreadLoadModelSelection === undefined
4348+ ? { }
4349+ : { normalizeThreadLoadModelSelection } ) ,
42314350 serverSettingsLayer : Layer . succeed ( ServerSettings . ServerSettingsService , settings ) ,
42324351 } ) ,
42334352 ) ,
0 commit comments