@@ -2254,16 +2254,13 @@ export class ClineProvider
22542254 removedModeConfigs ,
22552255 )
22562256
2257- // The write below is fenced behind the abort, so this abandoned body cannot
2258- // land a late `setValues` after a newer queued mutation has written. It still
2259- // converges: the list is the manager's current view, not the `listApiConfigMeta`
2260- // cache, which never observed a profile created while the purge held the lock
2261- // and would drop it from the webview's list while the manager keeps it.
2257+ // Fenced behind the abort, and read from the manager rather than the
2258+ // `listApiConfigMeta` cache, which never observed a profile created while the
2259+ // purge held the lock.
22622260 await this . contextProxy . setValues ( {
22632261 listApiConfigMeta : await this . providerSettingsManager . listConfig ( ) ,
22642262 modeApiConfigs : await this . providerSettingsManager . getModeApiConfigs ( ) ,
22652263 } )
2266-
22672264 return
22682265 }
22692266
@@ -2273,29 +2270,27 @@ export class ClineProvider
22732270 // cache and in SecretStorage until some unrelated future write happens to name a
22742271 // provider that does not own them, which may never happen.
22752272 //
2276- // The keep-set comes from the *surviving* profile's provider, not from its blob.
2277- // A blob-derived set is empty exactly when the live credential needs protecting:
2278- // the `api.getConfiguration()` round-trip rewrites a profile without its secret
2279- // fields while keeping `apiProvider`, so a surviving OpenRouter profile's blob
2280- // declares no `openRouterApiKey` even though that SecretStorage slot holds its
2281- // live value. When the deleted profile and the survivor share a provider and a key
2282- // name, the slot they would purge *is* the survivor's credential.
2273+ // The keep-set comes from the providers of the profiles that remain, not from the
2274+ // active profile's blob. A blob-derived set is empty exactly when the live
2275+ // credential needs protecting: the `api.getConfiguration()` round-trip rewrites a
2276+ // profile without its secret fields while keeping `apiProvider`, so a surviving
2277+ // OpenRouter profile's blob declares no `openRouterApiKey` even though that
2278+ // SecretStorage slot holds its live value. When the deleted profile and a
2279+ // remaining profile share a provider and a key name, the slot they would purge
2280+ // *is* that profile's credential.
22832281 await this . purgeDeletedProfileSecrets (
22842282 undoConfig ,
2285- await this . getSurvivingProviderSecretKeys ( globalSettings . currentApiConfigName , undoConfig ) ,
2283+ await this . getSurvivingProviderSecretKeys ( profileToDelete . name , undoConfig ) ,
22862284 )
22872285
2288- // The purge above can outlast the mutation timeout. Once the abort fired, this
2289- // abandoned body must not land a late `setValues` after a newer queued mutation
2290- // has written: a profile created during the window would be dropped from
2291- // `listApiConfigMeta` while the manager still holds it.
2286+ // The purge above can outlast the mutation timeout: once the abort fired, this
2287+ // abandoned body must not land a late `setValues` after a newer queued mutation.
22922288 if ( signal . aborted ) {
22932289 return
22942290 }
22952291
2296- // Read the list from the manager rather than filtering the `listApiConfigMeta`
2297- // cache: the cache only observes writes this provider made, so a profile created
2298- // by a concurrent mutation would be missing from the filtered result.
2292+ // Read the list from the manager rather than the `listApiConfigMeta` cache: the
2293+ // cache only observes writes this provider made.
22992294 await this . contextProxy . setValues ( {
23002295 listApiConfigMeta : ( await this . providerSettingsManager . listConfig ( ) ) . filter (
23012296 ( { name } ) => name !== profileToDelete . name ,
@@ -2359,10 +2354,12 @@ export class ClineProvider
23592354 // The keep-set is ownership-derived for the same reason as the inactive branch: a
23602355 // blob-derived set is empty when the replacement's blob was written from an
23612356 // `api.getConfiguration()` snapshot (secrets stripped), and purging then deletes the
2362- // replacement's live credential from the shared SecretStorage slot.
2357+ // replacement's live credential from the shared SecretStorage slot. It spans every
2358+ // remaining profile, so a same-provider profile that is not the replacement keeps its
2359+ // credential too.
23632360 await this . purgeDeletedProfileSecrets (
23642361 deletedProfileConfig ,
2365- await this . getSurvivingProviderSecretKeys ( profileToActivate , deletedProfileConfig ) ,
2362+ await this . getSurvivingProviderSecretKeys ( profileToDelete . name , deletedProfileConfig ) ,
23662363 )
23672364
23682365 // The activation above only rebinds the *current* mode, so every other mode that
@@ -2398,44 +2395,54 @@ export class ClineProvider
23982395 }
23992396
24002397 /**
2401- * The deleted profile's secret keys that the *surviving * profile's provider also owns.
2398+ * The deleted profile's secret keys that any *remaining * profile's provider also owns.
24022399 *
24032400 * Both delete branches purge the deleted profile's credentials explicitly, and both need the
2404- * same answer: is a key about to be deleted still in use by the profile that survives? That
2405- * cannot be answered from the surviving profile's blob. The `api.getConfiguration()` round-trip
2401+ * same answer: is a key about to be deleted still in use by a profile that survives? That
2402+ * cannot be answered from a surviving profile's blob. The `api.getConfiguration()` round-trip
24062403 * rewrites a profile without its secret fields while keeping `apiProvider`, so a surviving
24072404 * OpenRouter profile's blob declares no `openRouterApiKey` even though that SecretStorage slot
24082405 * holds its live value. Provider ownership answers it from the schema instead, which no
24092406 * round-trip can strip.
24102407 *
2411- * A same-provider succession (the deleted profile and the survivor both OpenRouter) shares one
2412- * SecretStorage slot per key name, so "the deleted profile's key" *is* the survivor's credential:
2413- * keeping it is the correct semantics, not a leak, because nothing is orphaned to purge. A
2414- * cross-provider delete keeps nothing, because the survivor owns none of the deleted keys.
2408+ * The answer spans *every* remaining profile, not just the one that ends up active. A profile
2409+ * shares one SecretStorage slot per key name with every profile of the same provider, so a
2410+ * remaining OpenRouter profile that is not active still reads the slot an inactive delete would
2411+ * otherwise purge. Keeping a key is the correct semantics, not a leak, because nothing is
2412+ * orphaned: a key no remaining provider owns is still purged, so a cross-provider delete keeps
2413+ * nothing.
24152414 *
2416- * An unreadable survivor yields an empty set, so the purge removes every key the deleted profile
2417- * declared. That is the confidentiality-safe direction.
2415+ * A remaining profile that cannot be read contributes nothing, so its keys are purged. That is
2416+ * the confidentiality-safe direction, and it is also what an unresolvable survivor set does:
2417+ * the keep-set is empty and every key the deleted profile declared is removed.
24182418 */
24192419 private async getSurvivingProviderSecretKeys (
2420- survivingProfileName : string | undefined ,
2420+ deletedName : string ,
24212421 deletedConfig : ( ProviderSettingsWithId & { name : string } ) | undefined ,
24222422 ) : Promise < Set < string > > {
2423- if ( ! survivingProfileName || ! deletedConfig ) {
2424- return new Set ( )
2425- }
2426-
2427- let survivingProvider : unknown
2423+ const deletedKeys = deletedConfig ? getProfileSecretKeys ( deletedConfig ) : [ ]
2424+ const keep = new Set < string > ( )
24282425
24292426 try {
2430- const { apiProvider } = await this . providerSettingsManager . getProfile ( { name : survivingProfileName } )
2431- survivingProvider = apiProvider
2427+ for ( const { name } of await this . providerSettingsManager . listConfig ( ) ) {
2428+ if ( name === deletedName || keep . size === deletedKeys . length ) {
2429+ continue
2430+ }
2431+
2432+ try {
2433+ const { apiProvider } = await this . providerSettingsManager . getProfile ( { name } )
2434+ for ( const key of deletedKeys ) {
2435+ if ( providerOwnsSecretKey ( apiProvider , key ) === true ) keep . add ( key )
2436+ }
2437+ } catch {
2438+ // An unreadable remaining profile contributes nothing, so its keys are purged.
2439+ }
2440+ }
24322441 } catch {
24332442 return new Set ( )
24342443 }
24352444
2436- return new Set (
2437- getProfileSecretKeys ( deletedConfig ) . filter ( ( key ) => providerOwnsSecretKey ( survivingProvider , key ) === true ) ,
2438- )
2445+ return keep
24392446 }
24402447
24412448 /**
@@ -2446,12 +2453,16 @@ export class ClineProvider
24462453 * same-provider succession is exactly the case ownership preserves. A delete is explicit
24472454 * intent, so the deleted profile's keys are removed by name here instead of being inferred.
24482455 *
2449- * `keep` names the keys the surviving profile's provider still owns: those carry a live
2450- * credential (a same-provider succession shares one SecretStorage slot per key name), so
2451- * removing them would delete the surviving profile's own key.
2456+ * `keep` names the keys a remaining profile's provider still owns: those carry a live
2457+ * credential (profiles of one provider share one SecretStorage slot per key name), so removing
2458+ * them would delete a surviving profile's own key.
24522459 *
2453- * Best-effort: the profile is already gone from the manager, so a failure here must not turn
2454- * a successful delete into an error. The keys are logged by name only, never by value.
2460+ * The delete itself stays successful: the profile is already gone from the manager, so a purge
2461+ * failure is retried once and then surfaced, never rethrown. A retry is needed because
2462+ * `storeSecret` clears the cache before it awaits `secrets.delete`: a rejected delete leaves the
2463+ * cache empty while SecretStorage still holds the value, and the ownership clear-out skips keys
2464+ * absent from the cache, so nothing else retries them in this session. The keys are logged by
2465+ * name only, never by value.
24552466 */
24562467 private async purgeDeletedProfileSecrets (
24572468 config : ( ProviderSettingsWithId & { name : string } ) | undefined ,
@@ -2468,14 +2479,22 @@ export class ClineProvider
24682479 }
24692480
24702481 try {
2471- await this . contextProxy . purgeProviderSecrets ( secretKeys )
2482+ try {
2483+ await this . contextProxy . purgeProviderSecrets ( secretKeys )
2484+ } catch {
2485+ // The cache is already cleared for these keys, so a retry re-issues the storage delete
2486+ // that failed rather than relying on a later clear-out that would skip them.
2487+ await this . contextProxy . purgeProviderSecrets ( secretKeys )
2488+ }
2489+
24722490 this . log ( `Purged ${ secretKeys . length } secret(s) of the deleted profile '${ config . name } '` )
24732491 } catch ( error ) {
2474- this . log (
2475- `Failed to purge the secrets of the deleted profile '${ config . name } ': ${
2476- error instanceof Error ? error . message : String ( error )
2477- } `,
2478- )
2492+ const message = `Failed to purge the secrets of the deleted profile '${ config . name } ': ${ error instanceof Error ? error . message : String ( error ) } `
2493+
2494+ this . log ( message )
2495+ // The delete succeeded, so this must not reject it; the user still has to learn that a
2496+ // credential may remain in SecretStorage until the next successful purge or restart.
2497+ void vscode . window . showWarningMessage ( message )
24792498 }
24802499 }
24812500
@@ -2519,13 +2538,19 @@ export class ClineProvider
25192538
25202539 if ( restored ) {
25212540 this . log ( `Restored provider profile '${ name } ' after a failed replacement activation` )
2522- return
2541+ } else {
2542+ // The restore did not finish, so both stores may still name a profile the
2543+ // manager no longer has. The reconcile also re-applies the purge's dropped mode
2544+ // bindings and mirrors the mode map, so an aborted restore cannot silently lose
2545+ // them.
2546+ await this . reconcileActiveProfileAfterFailedRestore ( removedModeConfigs , previousContextValues )
25232547 }
25242548
2525- // The restore did not finish, so both stores may still name a profile the manager
2526- // no longer has. The reconcile also re-applies the purge's dropped mode bindings
2527- // and mirrors the mode map, so an aborted restore cannot silently lose them.
2528- await this . reconcileActiveProfileAfterFailedRestore ( removedModeConfigs )
2549+ // The replacement activation posted state before this rollback landed, and the
2550+ // rollback's own writes (the restore slice or the reconcile's fallback) change what
2551+ // the webview must show. Without a post here the webview keeps naming the
2552+ // replacement while both stores name the restored or reconciled profile.
2553+ await this . postStateToWebview ( )
25292554 } ,
25302555 { runToCompletion : true , abortOnTimeout : true } ,
25312556 )
@@ -2565,12 +2590,8 @@ export class ClineProvider
25652590 // The check and the write share one manager lock: a queued save for the same name can
25662591 // land while the purge holds that lock, and re-saving the pre-delete snapshot over it
25672592 // would silently revert the newer profile's credentials.
2568- const restoredId = await this . providerSettingsManager . saveConfigIfAbsent ( name , providerSettings )
2569-
2570- if ( restoredId === undefined ) {
2571- this . log (
2572- `Skipped undoing the provider profile purge for '${ name } ': a profile with that name was saved after the timeout` ,
2573- )
2593+ if ( ( await this . providerSettingsManager . saveConfigIfAbsent ( name , providerSettings ) ) === undefined ) {
2594+ this . log ( `Skipped undoing the purge for '${ name } ': a same-name profile was saved after the timeout` )
25742595 return false
25752596 }
25762597
@@ -2606,45 +2627,48 @@ export class ClineProvider
26062627 removedModeConfigs : Record < string , string > ,
26072628 signal : AbortSignal ,
26082629 ) : Promise < boolean > {
2609- if ( ! config ) {
2610- return false
2611- }
2630+ if ( config ) {
2631+ // Keep `id`: the manager reuses it for the mode associations that point at this profile.
2632+ const { name : _name , ...providerSettings } = config
2633+ await this . providerSettingsManager . saveConfig ( name , providerSettings )
26122634
2613- // Keep `id`: the manager reuses it for the mode associations that point at this profile.
2614- const { name : _name , ...providerSettings } = config
2615- await this . providerSettingsManager . saveConfig ( name , providerSettings )
2635+ if ( signal . aborted ) return false
26162636
2617- if ( signal . aborted ) return false
2637+ await this . providerSettingsManager . activateProfile ( { name } )
26182638
2619- await this . providerSettingsManager . activateProfile ( { name } )
2639+ if ( signal . aborted ) return false
26202640
2621- if ( signal . aborted ) return false
2641+ // Restore the whole provider slice, not just the name pointers: a partial activation may
2642+ // have left the replacement's provider settings under the deleted name. `id` is manager
2643+ // metadata, not a setting, so it must not reach `ContextProxy`. `apiProvider` belongs to
2644+ // this slice, so the snapshot below deliberately does not carry it: writing it again
2645+ // would overwrite the restored provider with the pre-delete pointer. It is still passed
2646+ // as the ownership discriminator, because the profile being restored is the one whose
2647+ // own secrets must survive this write.
2648+ const { id : _id , ...settings } = providerSettings
2649+ await this . contextProxy . setProviderSettings ( settings , config . apiProvider )
26222650
2623- // Restore the whole provider slice, not just the name pointers: a partial activation may
2624- // have left the replacement's provider settings under the deleted name. `id` is manager
2625- // metadata, not a setting, so it must not reach `ContextProxy`. `apiProvider` belongs to
2626- // this slice, so the snapshot below deliberately does not carry it: writing it again
2627- // would overwrite the restored provider with the pre-delete pointer. It is still passed
2628- // as the ownership discriminator, because the profile being restored is the one whose
2629- // own secrets must survive this write.
2630- const { id : _id , ...settings } = providerSettings
2631- await this . contextProxy . setProviderSettings ( settings , config . apiProvider )
2651+ if ( signal . aborted ) return false
26322652
2633- if ( signal . aborted ) return false
2653+ // `saveConfig` above reused the profile's `id`, so the mode bindings the purge dropped
2654+ // name a profile that exists again. Re-apply them in the manager with a per-write abort
2655+ // check, then mirror the pre-delete map into ContextProxy: the snapshot below restores
2656+ // every mode, including the ones the purge never touched.
2657+ for ( const [ mode , configId ] of Object . entries ( removedModeConfigs ) ) {
2658+ if ( signal . aborted ) return false
2659+ await this . providerSettingsManager . setModeConfig ( mode as Mode , configId )
2660+ }
26342661
2635- // `saveConfig` above reused the profile's `id`, so the mode bindings the purge dropped
2636- // name a profile that exists again. Re-apply them in the manager with a per-write abort
2637- // check, then mirror the pre-delete map into ContextProxy: the snapshot below restores
2638- // every mode, including the ones the purge never touched.
2639- for ( const [ mode , configId ] of Object . entries ( removedModeConfigs ) ) {
26402662 if ( signal . aborted ) return false
2641- await this . providerSettingsManager . setModeConfig ( mode as Mode , configId )
2642- }
26432663
2644- if ( signal . aborted ) return false
2664+ await this . contextProxy . setValues ( previousContextValues )
2665+ return true
2666+ }
26452667
2646- await this . contextProxy . setValues ( previousContextValues )
2647- return true
2668+ // The inactive branch never captures a config, so there is nothing to restore: writing the
2669+ // `previousContextValues` snapshot would re-mirror a list that predates any profile created
2670+ // while the purge held the lock.
2671+ return false
26482672 }
26492673
26502674 /**
@@ -2671,10 +2695,12 @@ export class ClineProvider
26712695 ) : Promise < void > {
26722696 try {
26732697 const selectedName = this . contextProxy . getValues ( ) . currentApiConfigName
2674- const selectionIsNewer = ! ! selectedName && selectedName !== previousContextValues . currentApiConfigName
2675- const selectionSurvives = selectionIsNewer && ( await this . providerSettingsManager . hasConfig ( selectedName ) )
2698+ const keepSelection =
2699+ ! ! selectedName &&
2700+ selectedName !== previousContextValues . currentApiConfigName &&
2701+ ( await this . providerSettingsManager . hasConfig ( selectedName ) )
26762702
2677- if ( ! selectionSurvives ) {
2703+ if ( ! keepSelection ) {
26782704 const remainingProfiles = await this . providerSettingsManager . listConfig ( )
26792705 const fallback = remainingProfiles [ 0 ] ?. name
26802706
0 commit comments