@@ -391,35 +391,24 @@ export const layer: Layer.Layer<
391391 ) ;
392392
393393 /**
394- * A native subagent's thread starts on the parent's model when the provider
395- * names the real one later (a Claude agent file's model arrives with the
396- * subagent's first reply). Thread commands rewrite the whole thread row
397- * under the thread's lock, so this read and write take it too.
394+ * Thread commands rewrite the whole thread row under the thread's lock, so
395+ * this read and write take it too.
398396 */
399- const syncSubagentThreadModel = Effect . fn ( "ProviderEventIngestor.syncSubagentThreadModel" ) (
400- function * ( input : ProviderEventIngestInput , subagent : OrchestrationV2Subagent ) {
401- const { childThreadId, model } = subagent ;
402- if (
403- subagent . origin !== "provider_native" ||
404- childThreadId === null ||
405- ( model === null && subagent . modelSelection === undefined )
406- ) {
407- return [ ] ;
408- }
409- const reportedSelection = ( { modelSelection } : { modelSelection : ModelSelection } ) =>
410- subagent . modelSelection ??
411- ( model === null || model === modelSelection . model
412- ? modelSelection
413- : { instanceId : modelSelection . instanceId , model } ) ;
414- const staleThread = projections . getThread ( childThreadId ) . pipe (
397+ const syncThreadModelSelection = Effect . fn ( "ProviderEventIngestor.syncThreadModelSelection" ) (
398+ function * (
399+ input : ProviderEventIngestInput ,
400+ threadId : ThreadId ,
401+ reportedSelection : ( thread : { modelSelection : ModelSelection } ) => ModelSelection ,
402+ ) {
403+ const staleThread = projections . getThread ( threadId ) . pipe (
415404 Effect . map ( ( thread ) =>
416405 modelSelectionsEqual ( thread . modelSelection , reportedSelection ( thread ) ) ? null : thread ,
417406 ) ,
418407 Effect . catchTags ( { ProjectionStoreThreadNotFoundError : ( ) => Effect . succeed ( null ) } ) ,
419408 ) ;
420409 if ( ( yield * staleThread ) === null ) return [ ] ;
421410 return yield * threadCommands . withLock (
422- childThreadId ,
411+ threadId ,
423412 Effect . gen ( function * ( ) {
424413 const thread = yield * staleThread ;
425414 if ( thread === null ) return [ ] ;
@@ -440,6 +429,33 @@ export const layer: Layer.Layer<
440429 } ,
441430 ) ;
442431
432+ /**
433+ * A native subagent's thread starts on the parent's model when the provider
434+ * names the real one later (a Claude agent file's model arrives with the
435+ * subagent's first reply).
436+ */
437+ const syncSubagentThreadModel = Effect . fn ( "ProviderEventIngestor.syncSubagentThreadModel" ) (
438+ function * ( input : ProviderEventIngestInput , subagent : OrchestrationV2Subagent ) {
439+ const { childThreadId, model } = subagent ;
440+ if (
441+ subagent . origin !== "provider_native" ||
442+ childThreadId === null ||
443+ ( model === null && subagent . modelSelection === undefined )
444+ ) {
445+ return [ ] ;
446+ }
447+ return yield * syncThreadModelSelection (
448+ input ,
449+ childThreadId ,
450+ ( { modelSelection } ) =>
451+ subagent . modelSelection ??
452+ ( model === null || model === modelSelection . model
453+ ? modelSelection
454+ : { instanceId : modelSelection . instanceId , model } ) ,
455+ ) ;
456+ } ,
457+ ) ;
458+
443459 /**
444460 * Retire the runs that fell off its active branch with the same run,
445461 * root-node, and checkpoint updates CheckpointRollbackService writes for a
@@ -555,6 +571,9 @@ export const layer: Layer.Layer<
555571 } ) ,
556572 ] ;
557573 }
574+ case "app_thread.model_selection.updated" :
575+ // Written under the thread's lock after ingestion.
576+ return [ ] ;
558577 case "provider_session.updated" :
559578 return [
560579 yield * makeDomainEvent ( input , {
@@ -815,10 +834,22 @@ export const layer: Layer.Layer<
815834 ) ,
816835 ) ,
817836 ) ,
818- Effect . flatMap ( ( storedEvents ) =>
819- storedEvents . length === 0 || input . event . type !== "subagent.updated"
837+ Effect . flatMap ( ( storedEvents ) => {
838+ const event = input . event ;
839+ const sync =
840+ event . type === "app_thread.model_selection.updated"
841+ ? // A thread the user has since moved to another provider keeps its choice.
842+ syncThreadModelSelection ( input , event . threadId , ( { modelSelection } ) =>
843+ modelSelection . instanceId === event . modelSelection . instanceId
844+ ? event . modelSelection
845+ : modelSelection ,
846+ )
847+ : storedEvents . length > 0 && event . type === "subagent.updated"
848+ ? syncSubagentThreadModel ( input , event . subagent )
849+ : null ;
850+ return sync === null
820851 ? Effect . succeed ( storedEvents )
821- : syncSubagentThreadModel ( input , input . event . subagent ) . pipe (
852+ : sync . pipe (
822853 Effect . map ( ( synced ) => [ ...storedEvents , ...synced ] ) ,
823854 Effect . mapError (
824855 ( cause ) =>
@@ -828,8 +859,8 @@ export const layer: Layer.Layer<
828859 cause,
829860 } ) ,
830861 ) ,
831- ) ,
832- ) ,
862+ ) ;
863+ } ) ,
833864 Effect . tap ( ( storedEvents ) =>
834865 Effect . gen ( function * ( ) {
835866 if ( storedEvents . length === 0 || input . event . type !== "provider_turn.updated" )
0 commit comments