@@ -848,7 +848,10 @@ describe("ClineProvider - Sticky Mode", () => {
848848 id : "architect-profile-id" ,
849849 apiProvider : providerIdentifiers . openrouter ,
850850 } )
851- provider [ "activateProviderProfileUnlocked" ] = vi . fn ( ) . mockImplementation ( async ( ) => {
851+ provider [ "activateProviderProfileUnlocked" ] = vi . fn ( ) . mockImplementation ( async ( _args , options ) => {
852+ // The real activation flips the committed-switch guard when it writes
853+ // the activated profile; mirror that here.
854+ options ?. onSharedWriteStart ?.( )
852855 await provider . contextProxy . setValue ( "currentApiConfigName" , "architect-profile" )
853856 throw new Error ( "activation step failed" )
854857 } )
@@ -1073,6 +1076,180 @@ describe("ClineProvider - Sticky Mode", () => {
10731076 expect ( setModeConfig ) . toHaveBeenCalledWith ( "architect" , "x-id" )
10741077 } )
10751078
1079+ it ( "reconciles the switch when the mode-linked profile lookup fails" , async ( ) => {
1080+ // Same typed double as the activation-failure rollback test above.
1081+ const mockTask = Object . assign (
1082+ { } as Task ,
1083+ {
1084+ taskId : "test-task-id" ,
1085+ taskMode : "code" ,
1086+ _taskMode : "code" ,
1087+ emit : vi . fn ( ) ,
1088+ saveClineMessages : vi . fn ( ) ,
1089+ clineMessages : [ ] ,
1090+ apiConversationHistory : [ ] ,
1091+ updateApiConfiguration : vi . fn ( ) ,
1092+ } as Partial < Task > ,
1093+ )
1094+ const historyItem : HistoryItem = {
1095+ id : "test-task-id" ,
1096+ ts : Date . now ( ) ,
1097+ task : "Test task" ,
1098+ mode : "code" ,
1099+ number : 1 ,
1100+ tokensIn : 0 ,
1101+ tokensOut : 0 ,
1102+ cacheWrites : 0 ,
1103+ cacheReads : 0 ,
1104+ totalCost : 0 ,
1105+ }
1106+ vi . spyOn ( provider . taskHistoryStore , "get" ) . mockReturnValue ( historyItem )
1107+ const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
1108+ await provider . saveViewState ( "mode" , "code" )
1109+ await provider . contextProxy . setValue ( "mode" , "code" )
1110+
1111+ // The mode's saved profile was deleted: the lookup inside the activation
1112+ // throws before any shared write lands. The committed-switch guard must still
1113+ // be off, so the reconcile restores every mode write.
1114+ vi . spyOn ( provider . providerSettingsManager , "getModeConfigId" ) . mockResolvedValue ( "architect-profile-id" )
1115+ vi . spyOn ( provider . providerSettingsManager , "listConfig" ) . mockResolvedValue ( [
1116+ { id : "architect-profile-id" , name : "architect-profile" , apiProvider : providerIdentifiers . openrouter } ,
1117+ ] )
1118+ vi . spyOn ( provider . providerSettingsManager , "getProfile" ) . mockResolvedValue ( {
1119+ name : "architect-profile" ,
1120+ id : "architect-profile-id" ,
1121+ apiProvider : providerIdentifiers . openrouter ,
1122+ } )
1123+ // The activation's own profile lookup throws before any shared write lands
1124+ // (e.g. the profile was deleted): the committed-switch guard must still be
1125+ // off, so the reconcile restores every mode write.
1126+ provider [ "activateProviderProfileUnlocked" ] = vi . fn ( ) . mockRejectedValue ( new Error ( "profile not found" ) )
1127+ await provider . addClineToStack ( mockTask )
1128+
1129+ await expect ( provider [ "handleModeSwitchUnlocked" ] ( "architect" , mockTask ) ) . rejects . toThrow (
1130+ "profile not found" ,
1131+ )
1132+
1133+ expect ( provider . getValue ( "mode" ) ) . toBe ( "code" )
1134+ expect ( provider [ "viewLocalState" ] . mode ) . toBe ( "code" )
1135+ expect ( mockTask [ "_taskMode" ] ) . toBe ( "code" )
1136+ expect ( updateTaskHistorySpy ) . toHaveBeenCalledWith ( expect . objectContaining ( { mode : "code" } ) )
1137+ } )
1138+
1139+ it ( "reports ModeChanged for a switch the catch keeps after activation wrote" , async ( ) => {
1140+ // Same typed double as the activation-failure rollback test above.
1141+ const mockTask = Object . assign (
1142+ { } as Task ,
1143+ {
1144+ taskId : "test-task-id" ,
1145+ taskMode : "code" ,
1146+ _taskMode : "code" ,
1147+ emit : vi . fn ( ) ,
1148+ saveClineMessages : vi . fn ( ) ,
1149+ clineMessages : [ ] ,
1150+ apiConversationHistory : [ ] ,
1151+ updateApiConfiguration : vi . fn ( ) ,
1152+ } as Partial < Task > ,
1153+ )
1154+ const historyItem : HistoryItem = {
1155+ id : "test-task-id" ,
1156+ ts : Date . now ( ) ,
1157+ task : "Test task" ,
1158+ mode : "code" ,
1159+ number : 1 ,
1160+ tokensIn : 0 ,
1161+ tokensOut : 0 ,
1162+ cacheWrites : 0 ,
1163+ cacheReads : 0 ,
1164+ totalCost : 0 ,
1165+ }
1166+ vi . spyOn ( provider . taskHistoryStore , "get" ) . mockReturnValue ( historyItem )
1167+ const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
1168+ await provider . saveViewState ( "mode" , "code" )
1169+ await provider . contextProxy . setValue ( "mode" , "code" )
1170+
1171+ // The activation writes the activated profile and only then rejects: the catch
1172+ // keeps the switch, and listeners plus the webview must still learn the
1173+ // committed mode before the error surfaces.
1174+ vi . spyOn ( provider . providerSettingsManager , "getModeConfigId" ) . mockResolvedValue ( "architect-profile-id" )
1175+ vi . spyOn ( provider . providerSettingsManager , "listConfig" ) . mockResolvedValue ( [
1176+ { id : "architect-profile-id" , name : "architect-profile" , apiProvider : providerIdentifiers . openrouter } ,
1177+ ] )
1178+ vi . spyOn ( provider . providerSettingsManager , "getProfile" ) . mockResolvedValue ( {
1179+ name : "architect-profile" ,
1180+ id : "architect-profile-id" ,
1181+ apiProvider : providerIdentifiers . openrouter ,
1182+ } )
1183+ const emitSpy = vi . spyOn ( provider , "emit" )
1184+ const postSpy = vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
1185+ provider [ "activateProviderProfileUnlocked" ] = vi . fn ( ) . mockImplementation ( async ( _args , options ) => {
1186+ options ?. onSharedWriteStart ?.( )
1187+ await provider . contextProxy . setValue ( "currentApiConfigName" , "architect-profile" )
1188+ throw new Error ( "activation step failed" )
1189+ } )
1190+ await provider . addClineToStack ( mockTask )
1191+
1192+ await expect ( provider [ "handleModeSwitchUnlocked" ] ( "architect" , mockTask ) ) . rejects . toThrow (
1193+ "activation step failed" ,
1194+ )
1195+
1196+ expect ( emitSpy ) . toHaveBeenCalledWith ( "modeChanged" , "architect" )
1197+ expect ( postSpy ) . toHaveBeenCalled ( )
1198+ expect ( provider . getValue ( "mode" ) ) . toBe ( "architect" )
1199+ } )
1200+
1201+ it ( "reports the committed mode when the signal aborts after the mode writes" , async ( ) => {
1202+ // Same typed double as the activation-failure rollback test above.
1203+ const mockTask = Object . assign (
1204+ { } as Task ,
1205+ {
1206+ taskId : "test-task-id" ,
1207+ taskMode : "code" ,
1208+ _taskMode : "code" ,
1209+ emit : vi . fn ( ) ,
1210+ saveClineMessages : vi . fn ( ) ,
1211+ clineMessages : [ ] ,
1212+ apiConversationHistory : [ ] ,
1213+ updateApiConfiguration : vi . fn ( ) ,
1214+ } as Partial < Task > ,
1215+ )
1216+ const historyItem : HistoryItem = {
1217+ id : "test-task-id" ,
1218+ ts : Date . now ( ) ,
1219+ task : "Test task" ,
1220+ mode : "code" ,
1221+ number : 1 ,
1222+ tokensIn : 0 ,
1223+ tokensOut : 0 ,
1224+ cacheWrites : 0 ,
1225+ cacheReads : 0 ,
1226+ totalCost : 0 ,
1227+ }
1228+ vi . spyOn ( provider . taskHistoryStore , "get" ) . mockReturnValue ( historyItem )
1229+ const updateTaskHistorySpy = vi . spyOn ( provider , "updateTaskHistory" ) . mockResolvedValue ( [ ] )
1230+ await provider . saveViewState ( "mode" , "code" )
1231+ await provider . contextProxy . setValue ( "mode" , "code" )
1232+
1233+ // The pin and shared mode writes land, then the mutation signal aborts during
1234+ // the activation reads. The activation is skipped, but the committed mode
1235+ // must still reach listeners and the webview through the emit/post tail.
1236+ const emitSpy = vi . spyOn ( provider , "emit" )
1237+ const postSpy = vi . spyOn ( provider , "postStateToWebview" ) . mockResolvedValue ( undefined )
1238+ const controller = new AbortController ( )
1239+ vi . spyOn ( provider . providerSettingsManager , "getModeConfigId" ) . mockImplementation ( async ( ) => {
1240+ controller . abort ( )
1241+ return undefined
1242+ } )
1243+ vi . spyOn ( provider . providerSettingsManager , "listConfig" ) . mockResolvedValue ( [ ] )
1244+ await provider . addClineToStack ( mockTask )
1245+
1246+ await provider [ "handleModeSwitchUnlocked" ] ( "architect" , mockTask , controller . signal )
1247+
1248+ expect ( provider . getValue ( "mode" ) ) . toBe ( "architect" )
1249+ expect ( emitSpy ) . toHaveBeenCalledWith ( "modeChanged" , "architect" )
1250+ expect ( postSpy ) . toHaveBeenCalled ( )
1251+ } )
1252+
10761253 it ( "keeps the cancellation result when the abort rollback write itself fails" , async ( ) => {
10771254 const mockTask = Object . assign (
10781255 { } as Task ,
0 commit comments