@@ -801,6 +801,58 @@ describe("ProviderCommandReactor", () => {
801801 } ) ;
802802 } ) ;
803803
804+ it ( "rejects a first turn when requested provider conflicts with the thread model" , async ( ) => {
805+ const harness = await createHarness ( {
806+ threadModelSelection : { provider : "codex" , model : "gpt-5-codex" } ,
807+ } ) ;
808+ const now = new Date ( ) . toISOString ( ) ;
809+
810+ await Effect . runPromise (
811+ harness . engine . dispatch ( {
812+ type : "thread.turn.start" ,
813+ commandId : CommandId . make ( "cmd-turn-start-provider-first" ) ,
814+ threadId : ThreadId . make ( "thread-1" ) ,
815+ message : {
816+ messageId : asMessageId ( "user-message-provider-first" ) ,
817+ role : "user" ,
818+ text : "hello claude" ,
819+ attachments : [ ] ,
820+ } ,
821+ modelSelection : {
822+ provider : "claudeAgent" ,
823+ model : "claude-opus-4-6" ,
824+ } ,
825+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
826+ runtimeMode : "approval-required" ,
827+ createdAt : now ,
828+ } ) ,
829+ ) ;
830+
831+ await waitFor ( async ( ) => {
832+ const readModel = await Effect . runPromise ( harness . engine . getReadModel ( ) ) ;
833+ const thread = readModel . threads . find ( ( entry ) => entry . id === ThreadId . make ( "thread-1" ) ) ;
834+ return (
835+ thread ?. activities . some ( ( activity ) => activity . kind === "provider.turn.start.failed" ) ??
836+ false
837+ ) ;
838+ } ) ;
839+
840+ expect ( harness . startSession ) . not . toHaveBeenCalled ( ) ;
841+ expect ( harness . sendTurn ) . not . toHaveBeenCalled ( ) ;
842+
843+ const readModel = await Effect . runPromise ( harness . engine . getReadModel ( ) ) ;
844+ const thread = readModel . threads . find ( ( entry ) => entry . id === ThreadId . make ( "thread-1" ) ) ;
845+ expect ( thread ?. session ) . toBeNull ( ) ;
846+ expect (
847+ thread ?. activities . find ( ( activity ) => activity . kind === "provider.turn.start.failed" ) ,
848+ ) . toMatchObject ( {
849+ summary : "Provider turn start failed" ,
850+ payload : {
851+ detail : expect . stringContaining ( "cannot switch to 'claudeAgent'" ) ,
852+ } ,
853+ } ) ;
854+ } ) ;
855+
804856 it ( "reuses the same provider session when runtime mode is unchanged" , async ( ) => {
805857 const harness = await createHarness ( ) ;
806858 const now = new Date ( ) . toISOString ( ) ;
@@ -1113,6 +1165,78 @@ describe("ProviderCommandReactor", () => {
11131165 expect ( thread ?. session ?. runtimeMode ) . toBe ( "full-access" ) ;
11141166 } ) ;
11151167
1168+ it ( "rejects provider changes after a thread is already bound to a session provider" , async ( ) => {
1169+ const harness = await createHarness ( ) ;
1170+ const now = new Date ( ) . toISOString ( ) ;
1171+
1172+ await Effect . runPromise (
1173+ harness . engine . dispatch ( {
1174+ type : "thread.turn.start" ,
1175+ commandId : CommandId . make ( "cmd-turn-start-provider-switch-1" ) ,
1176+ threadId : ThreadId . make ( "thread-1" ) ,
1177+ message : {
1178+ messageId : asMessageId ( "user-message-provider-switch-1" ) ,
1179+ role : "user" ,
1180+ text : "first" ,
1181+ attachments : [ ] ,
1182+ } ,
1183+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
1184+ runtimeMode : "approval-required" ,
1185+ createdAt : now ,
1186+ } ) ,
1187+ ) ;
1188+
1189+ await waitFor ( ( ) => harness . startSession . mock . calls . length === 1 ) ;
1190+ await waitFor ( ( ) => harness . sendTurn . mock . calls . length === 1 ) ;
1191+
1192+ await Effect . runPromise (
1193+ harness . engine . dispatch ( {
1194+ type : "thread.turn.start" ,
1195+ commandId : CommandId . make ( "cmd-turn-start-provider-switch-2" ) ,
1196+ threadId : ThreadId . make ( "thread-1" ) ,
1197+ message : {
1198+ messageId : asMessageId ( "user-message-provider-switch-2" ) ,
1199+ role : "user" ,
1200+ text : "second" ,
1201+ attachments : [ ] ,
1202+ } ,
1203+ modelSelection : {
1204+ provider : "claudeAgent" ,
1205+ model : "claude-opus-4-6" ,
1206+ } ,
1207+ interactionMode : DEFAULT_PROVIDER_INTERACTION_MODE ,
1208+ runtimeMode : "approval-required" ,
1209+ createdAt : now ,
1210+ } ) ,
1211+ ) ;
1212+
1213+ await waitFor ( async ( ) => {
1214+ const readModel = await Effect . runPromise ( harness . engine . getReadModel ( ) ) ;
1215+ const thread = readModel . threads . find ( ( entry ) => entry . id === ThreadId . make ( "thread-1" ) ) ;
1216+ return (
1217+ thread ?. activities . some ( ( activity ) => activity . kind === "provider.turn.start.failed" ) ??
1218+ false
1219+ ) ;
1220+ } ) ;
1221+
1222+ expect ( harness . startSession . mock . calls . length ) . toBe ( 1 ) ;
1223+ expect ( harness . sendTurn . mock . calls . length ) . toBe ( 1 ) ;
1224+ expect ( harness . stopSession . mock . calls . length ) . toBe ( 0 ) ;
1225+
1226+ const readModel = await Effect . runPromise ( harness . engine . getReadModel ( ) ) ;
1227+ const thread = readModel . threads . find ( ( entry ) => entry . id === ThreadId . make ( "thread-1" ) ) ;
1228+ expect ( thread ?. session ?. threadId ) . toBe ( "thread-1" ) ;
1229+ expect ( thread ?. session ?. providerName ) . toBe ( "codex" ) ;
1230+ expect ( thread ?. session ?. runtimeMode ) . toBe ( "approval-required" ) ;
1231+ expect (
1232+ thread ?. activities . find ( ( activity ) => activity . kind === "provider.turn.start.failed" ) ,
1233+ ) . toMatchObject ( {
1234+ payload : {
1235+ detail : expect . stringContaining ( "cannot switch to 'claudeAgent'" ) ,
1236+ } ,
1237+ } ) ;
1238+ } ) ;
1239+
11161240 it ( "reacts to thread.turn.interrupt-requested by calling provider interrupt" , async ( ) => {
11171241 const harness = await createHarness ( ) ;
11181242 const now = new Date ( ) . toISOString ( ) ;
0 commit comments