@@ -62,6 +62,194 @@ const layerTest = Layer.mergeAll(
6262 ) ,
6363) ;
6464
65+ it . effect . each (
66+ ( [ "provider.switch" , "thread.model-selection.set" ] as const ) . flatMap ( ( type ) =>
67+ [
68+ { active : true , selectionChange : "instance" } ,
69+ { active : false , selectionChange : "instance" } ,
70+ { active : true , selectionChange : "next-turn" } ,
71+ { active : true , selectionChange : "restart" } ,
72+ ] . map ( ( scenario ) => ( { type, ...scenario } ) ) ,
73+ ) ,
74+ ) ( "$type $selectionChange ($active)" , ( { type, active, selectionChange } ) =>
75+ Effect . gen ( function * ( ) {
76+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
77+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
78+ const outbox = yield * EffectOutbox . EffectOutboxV2 ;
79+ const threadId = ThreadId . make ( "thread:instance-switch" ) ;
80+ const now = yield * DateTime . now ;
81+ const sessionId = ProviderSessionId . make ( "session:instance-switch" ) ;
82+ const providerThreadId = ProviderThreadId . make ( "provider-thread:instance-switch" ) ;
83+ const runId = RunId . make ( "run:instance-switch" ) ;
84+ const nodeId = NodeId . make ( "node:instance-switch" ) ;
85+ const attemptId = RunAttemptId . make ( "attempt:instance-switch" ) ;
86+ yield * orchestrator . dispatch ( {
87+ type : "thread.create" ,
88+ commandId : CommandId . make ( "create-instance-switch" ) ,
89+ threadId,
90+ projectId : ProjectId . make ( "project:instance-switch" ) ,
91+ title : "Instance switch" ,
92+ modelSelection,
93+ runtimeMode : "full-access" ,
94+ interactionMode : "default" ,
95+ branch : null ,
96+ worktreePath : null ,
97+ createdBy : "user" ,
98+ creationSource : "web" ,
99+ } ) ;
100+ yield * projections . apply ( {
101+ id : EventId . make ( "instance-switch:session" ) ,
102+ type : "provider-session.attached" ,
103+ threadId,
104+ occurredAt : now ,
105+ payload : {
106+ id : sessionId ,
107+ driver : adapter . driver ,
108+ providerInstanceId : instanceId ,
109+ status : "ready" ,
110+ cwd : "/repo" ,
111+ model : modelSelection . model ,
112+ capabilities : CodexProviderCapabilitiesV2 ,
113+ createdAt : now ,
114+ updatedAt : now ,
115+ lastError : null ,
116+ } ,
117+ } ) ;
118+ yield * projections . apply ( {
119+ id : EventId . make ( "instance-switch:provider-thread" ) ,
120+ type : "provider-thread.updated" ,
121+ threadId,
122+ occurredAt : now ,
123+ payload : {
124+ id : providerThreadId ,
125+ driver : adapter . driver ,
126+ providerInstanceId : instanceId ,
127+ providerSessionId : sessionId ,
128+ appThreadId : threadId ,
129+ ownerNodeId : null ,
130+ nativeThreadRef : {
131+ driver : adapter . driver ,
132+ nativeId : "native:instance-switch" ,
133+ strength : "strong" ,
134+ } ,
135+ nativeConversationHeadRef : null ,
136+ status : "idle" ,
137+ firstRunOrdinal : 1 ,
138+ lastRunOrdinal : 1 ,
139+ handoffIds : [ ] ,
140+ forkedFrom : null ,
141+ createdAt : now ,
142+ updatedAt : now ,
143+ } ,
144+ } ) ;
145+ if ( active ) {
146+ yield * projections . apply ( {
147+ id : EventId . make ( "instance-switch:run" ) ,
148+ type : "run.created" ,
149+ threadId,
150+ occurredAt : now ,
151+ payload : {
152+ id : runId ,
153+ threadId,
154+ ordinal : 1 ,
155+ providerInstanceId : instanceId ,
156+ modelSelection,
157+ providerThreadId,
158+ userMessageId : MessageId . make ( "message:instance-switch" ) ,
159+ rootNodeId : nodeId ,
160+ activeAttemptId : attemptId ,
161+ status : "running" ,
162+ requestedAt : now ,
163+ startedAt : now ,
164+ completedAt : null ,
165+ checkpointId : null ,
166+ contextHandoffId : null ,
167+ } ,
168+ } ) ;
169+ yield * projections . apply ( {
170+ id : EventId . make ( "instance-switch:turn" ) ,
171+ type : "provider-turn.updated" ,
172+ threadId,
173+ runId,
174+ occurredAt : now ,
175+ payload : {
176+ id : ProviderTurnId . make ( "provider-turn:instance-switch" ) ,
177+ providerThreadId,
178+ nodeId,
179+ runAttemptId : attemptId ,
180+ nativeTurnRef : { driver : adapter . driver , nativeId : "native:turn" , strength : "strong" } ,
181+ ordinal : 1 ,
182+ status : "running" ,
183+ startedAt : now ,
184+ completedAt : null ,
185+ } ,
186+ } ) ;
187+ }
188+ const before = yield * projections . getThreadProjection ( threadId ) ;
189+ const sequence = yield * orchestrator . getThreadEventSequence ( threadId ) ;
190+ const commandId = CommandId . make ( "switch-instance" ) ;
191+ const target = {
192+ instanceId :
193+ selectionChange === "instance" ? ProviderInstanceId . make ( "codex-other" ) : instanceId ,
194+ model : selectionChange === "instance" ? modelSelection . model : "gpt-6" ,
195+ } ;
196+ const outcome = yield * orchestrator
197+ . dispatch ( {
198+ type,
199+ commandId,
200+ threadId,
201+ modelSelection : target ,
202+ } )
203+ . pipe ( Effect . result ) ;
204+ const after = yield * projections . getThreadProjection ( threadId ) ;
205+ if ( active && selectionChange !== "next-turn" ) {
206+ assert . deepEqual (
207+ after ,
208+ before ,
209+ "a refused switch must preserve the turn, session and selection" ,
210+ ) ;
211+ assert . deepEqual (
212+ yield * outbox . listByCommandId ( commandId ) ,
213+ [ ] ,
214+ "a refused switch must not release the provider session" ,
215+ ) ;
216+ assert . equal ( yield * orchestrator . getThreadEventSequence ( threadId ) , sequence ) ;
217+ assert . ok ( Result . isFailure ( outcome ) ) ;
218+ assert . instanceOf ( outcome . failure , Orchestrator . OrchestratorProviderSelectionBusyError ) ;
219+ } else {
220+ assert . ok ( Result . isSuccess ( outcome ) ) ;
221+ assert . deepEqual ( after . thread . modelSelection , target ) ;
222+ assert . equal ( after . thread . providerInstanceId , target . instanceId ) ;
223+ assert . deepEqual ( after . runs , before . runs ) ;
224+ assert . deepEqual ( after . providerTurns , before . providerTurns ) ;
225+ }
226+ } ) . pipe (
227+ Effect . provide (
228+ Layer . mergeAll (
229+ layerDatabase ,
230+ ProjectionStore . layer . pipe ( Layer . provide ( layerDatabase ) ) ,
231+ EffectOutbox . layer . pipe ( Layer . provide ( layerDatabase ) ) ,
232+ ProviderReplayHarness . layerWithRegistry (
233+ { name : "instance-switch" } ,
234+ ProviderAdapterRegistry . layerFromAdapters ( [
235+ {
236+ ...adapter ,
237+ planSelectionTransition : ( ) =>
238+ Effect . succeed (
239+ selectionChange === "restart"
240+ ? { type : "restart_session" as const }
241+ : { type : "apply_on_next_turn" as const } ,
242+ ) ,
243+ } ,
244+ { ...adapter , instanceId : ProviderInstanceId . make ( "codex-other" ) } ,
245+ ] ) ,
246+ { databaseLayer : layerDatabase , runEffectWorker : false } ,
247+ ) ,
248+ ) ,
249+ ) ,
250+ ) ,
251+ ) ;
252+
65253it . effect (
66254 "records approval feedback and targets only its runtime request without dispatching a parent turn" ,
67255 ( ) =>
0 commit comments