@@ -914,7 +914,10 @@ export class ClineProvider
914914
915915 if ( profile ?. name ) {
916916 try {
917- await this . activateProviderProfile ( { name : profile . name } )
917+ await this . activateProviderProfile (
918+ { name : profile . name } ,
919+ { persistModeConfig : false , persistTaskHistory : false } ,
920+ )
918921 } catch ( error ) {
919922 // Log the error but continue with task restoration.
920923 this . log (
@@ -1414,6 +1417,9 @@ export class ClineProvider
14141417 // Change the provider for the current task.
14151418 // TODO: We should rename `buildApiHandler` for clarity (e.g. `getProviderClient`).
14161419 this . updateTaskApiHandlerIfNeeded ( providerSettings , { forceRebuild : true } )
1420+
1421+ // Keep the current task's sticky provider profile in sync with the newly-activated profile.
1422+ await this . persistStickyProviderProfileToCurrentTask ( name )
14171423 } else {
14181424 await this . updateGlobalState ( "listApiConfigMeta" , await this . providerSettingsManager . listConfig ( ) )
14191425 }
@@ -1453,9 +1459,42 @@ export class ClineProvider
14531459 await this . postStateToWebview ( )
14541460 }
14551461
1456- async activateProviderProfile ( args : { name : string } | { id : string } ) {
1462+ private async persistStickyProviderProfileToCurrentTask ( apiConfigName : string ) : Promise < void > {
1463+ const task = this . getCurrentTask ( )
1464+ if ( ! task ) {
1465+ return
1466+ }
1467+
1468+ try {
1469+ const history = this . getGlobalState ( "taskHistory" ) ?? [ ]
1470+ const taskHistoryItem = history . find ( ( item ) => item . id === task . taskId )
1471+
1472+ if ( taskHistoryItem ) {
1473+ taskHistoryItem . apiConfigName = apiConfigName
1474+ await this . updateTaskHistory ( taskHistoryItem )
1475+ }
1476+
1477+ // Only update in-memory state after successful persistence.
1478+ task . setTaskApiConfigName ( apiConfigName )
1479+ } catch ( error ) {
1480+ // If persistence fails, log the error but don't fail the profile switch.
1481+ this . log (
1482+ `Failed to persist provider profile switch for task ${ task . taskId } : ${
1483+ error instanceof Error ? error . message : String ( error )
1484+ } `,
1485+ )
1486+ }
1487+ }
1488+
1489+ async activateProviderProfile (
1490+ args : { name : string } | { id : string } ,
1491+ options ?: { persistModeConfig ?: boolean ; persistTaskHistory ?: boolean } ,
1492+ ) {
14571493 const { name, id, ...providerSettings } = await this . providerSettingsManager . activateProfile ( args )
14581494
1495+ const persistModeConfig = options ?. persistModeConfig ?? true
1496+ const persistTaskHistory = options ?. persistTaskHistory ?? true
1497+
14591498 // See `upsertProviderProfile` for a description of what this is doing.
14601499 await Promise . all ( [
14611500 this . contextProxy . setValue ( "listApiConfigMeta" , await this . providerSettingsManager . listConfig ( ) ) ,
@@ -1465,35 +1504,17 @@ export class ClineProvider
14651504
14661505 const { mode } = await this . getState ( )
14671506
1468- if ( id ) {
1507+ if ( id && persistModeConfig ) {
14691508 await this . providerSettingsManager . setModeConfig ( mode , id )
14701509 }
1510+
14711511 // Change the provider for the current task.
14721512 this . updateTaskApiHandlerIfNeeded ( providerSettings , { forceRebuild : true } )
14731513
1474- // Update the current task's sticky provider profile if there's an active task.
1475- // This ensures the task retains its designated provider profile and switching
1476- // profiles in one workspace doesn't alter other tasks.
1477- const task = this . getCurrentTask ( )
1478- if ( task ) {
1479- try {
1480- // Update the task history with the new provider profile first.
1481- const history = this . getGlobalState ( "taskHistory" ) ?? [ ]
1482- const taskHistoryItem = history . find ( ( item ) => item . id === task . taskId )
1483-
1484- if ( taskHistoryItem ) {
1485- taskHistoryItem . apiConfigName = name
1486- await this . updateTaskHistory ( taskHistoryItem )
1487- }
1488-
1489- // Only update the task's provider profile after successful persistence.
1490- task . setTaskApiConfigName ( name )
1491- } catch ( error ) {
1492- // If persistence fails, log the error but don't fail the profile switch.
1493- this . log (
1494- `Failed to persist provider profile switch for task ${ task . taskId } : ${ error instanceof Error ? error . message : String ( error ) } ` ,
1495- )
1496- }
1514+ // Update the current task's sticky provider profile, unless this activation is
1515+ // being used purely as a non-persisting restoration (e.g., reopening a task from history).
1516+ if ( persistTaskHistory ) {
1517+ await this . persistStickyProviderProfileToCurrentTask ( name )
14971518 }
14981519
14991520 await this . postStateToWebview ( )
0 commit comments