Skip to content

Commit 8a43f2b

Browse files
committed
fix(webview): finish the activation when deleting the active profile
The old deleteApiConfiguration path ended with activateProviderProfile, which updated the manager's own current-profile record and the current mode's profile mapping, rebuilt the current task's API handler, refreshed the current task's sticky profile, and emitted ProviderProfileChanged. deleteProviderProfile rewrote the durable stores and re-pinned views but dropped those live-state effects, so a running task kept serving the deleted profile's configuration until the next activation. Complete the activation-side effects after the deletion lands, gated on the deleted profile having been the active one (globally or for this view). They run after the try/catch, the same placement activateProviderProfileUnlocked gives its rebuild: the deletion has committed, so a failure there must not roll it back. Tests: rebuilds the live task state and manager selection when the active profile is deleted; does not rebuild when the deleted profile was not active. Negative control: removing the block reddens exactly the first test.
1 parent 1eb2a87 commit 8a43f2b

2 files changed

Lines changed: 212 additions & 3 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,6 +2834,11 @@ export class ClineProvider
28342834
// settings commit can be rolled back store by store instead of only re-saving the
28352835
// settings: a later failed write would otherwise leave the persisted list, the shared
28362836
// selection, or this view's own pin pointing at a profile that no longer exists.
2837+
// Hoisted so the post-commit activation-side effects below can see whether this
2838+
// deletion displaced the profile this view or the shared selection was using.
2839+
let viewWasPinnedToDeleted = false
2840+
let deletedWasGlobal = false
2841+
let survivingSettings: ProviderSettings | undefined
28372842
const previousEntries = this.getProviderProfileEntries()
28382843
const previousGlobalSelection = globalSettings.currentApiConfigName
28392844
const previousViewPin = this.viewLocalState.currentApiConfigName
@@ -2868,7 +2873,6 @@ export class ClineProvider
28682873
// Resolve the surviving profile's settings so this view and any other
28692874
// live view still pinned to the deleted profile can be re-pinned with
28702875
// a matching configuration.
2871-
let survivingSettings: ProviderSettings | undefined
28722876
try {
28732877
const { name: _survivingName, ...settings } = await this.providerSettingsManager.getProfile({
28742878
name: profileToActivate,
@@ -2897,8 +2901,8 @@ export class ClineProvider
28972901
// deleted profile while the global selection points elsewhere must still be
28982902
// reconfigured, or getState() would keep the deleted profile's settings under
28992903
// the surviving profile's name.
2900-
const viewWasPinnedToDeleted = this.viewLocalState.currentApiConfigName === profileToDelete.name
2901-
const deletedWasGlobal = profileToDelete.name === globalSettings.currentApiConfigName
2904+
viewWasPinnedToDeleted = this.viewLocalState.currentApiConfigName === profileToDelete.name
2905+
deletedWasGlobal = profileToDelete.name === globalSettings.currentApiConfigName
29022906

29032907
if (viewWasPinnedToDeleted) {
29042908
// This view's pin now dangles: re-point it. setValue also persists the
@@ -3023,6 +3027,36 @@ export class ClineProvider
30233027
)
30243028
throw error
30253029
}
3030+
3031+
// The old deleteApiConfiguration path ended with activateProviderProfile, which
3032+
// updated the manager's own current-profile record and the current mode's profile
3033+
// mapping, rebuilt the current task's API handler, refreshed the current task's
3034+
// sticky profile, and emitted ProviderProfileChanged. The storage rewrites above
3035+
// cover the durable stores; when the deleted profile was the active one (globally
3036+
// or for this view), complete the activation-side effects here so the deletion
3037+
// leaves the same live state as activating the survivor. They run after the
3038+
// try/catch because the deletion has already landed: a failure here must not roll
3039+
// it back, the same placement activateProviderProfileUnlocked gives its rebuild.
3040+
if (deletedWasGlobal || viewWasPinnedToDeleted) {
3041+
const {
3042+
id: survivingId,
3043+
name: _activatedName,
3044+
...activatedSettings
3045+
} = await this.providerSettingsManager.activateProfile({ name: profileToActivate })
3046+
if (survivingId) {
3047+
const { mode } = await this.getState()
3048+
await this.providerSettingsManager.setModeConfig(mode, survivingId)
3049+
}
3050+
this.updateTaskApiHandlerIfNeeded(activatedSettings, { forceRebuild: true })
3051+
await this.persistStickyProviderProfileToCurrentTask(profileToActivate)
3052+
if (activatedSettings.apiProvider) {
3053+
this.emit(RooCodeEventName.ProviderProfileChanged, {
3054+
name: profileToActivate,
3055+
provider: activatedSettings.apiProvider,
3056+
})
3057+
}
3058+
}
3059+
30263060
await this.postStateToWebview()
30273061
}
30283062

‎src/core/webview/__tests__/ClineProvider.spec.ts‎

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
DEFAULT_DIFF_FUZZY_THRESHOLD,
2222
DEFAULT_WRITE_DELAY_MS,
2323
providerIdentifiers,
24+
RooCodeEventName,
2425
openAiModelInfoSaneDefaults,
2526
} from "@roo-code/types"
2627
import { TelemetryService } from "@roo-code/telemetry"
@@ -2981,6 +2982,22 @@ describe("ClineProvider", () => {
29812982
id: "keeper-id",
29822983
apiProvider: providerIdentifiers.anthropic,
29832984
}
2985+
// @ts-ignore - Replace providerSettingsManager with a test double: the deletion now
2986+
// also updates the manager's own current-profile record and the mode mapping.
2987+
provider.providerSettingsManager = {
2988+
deleteConfig: vi.fn().mockResolvedValue(undefined),
2989+
getProfile: vi.fn().mockResolvedValue({
2990+
name: "keeper-profile",
2991+
id: "keeper-id",
2992+
apiProvider: providerIdentifiers.anthropic,
2993+
}),
2994+
activateProfile: vi.fn().mockResolvedValue({
2995+
name: "keeper-profile",
2996+
id: "keeper-id",
2997+
apiProvider: providerIdentifiers.anthropic,
2998+
}),
2999+
setModeConfig: vi.fn().mockResolvedValue(undefined),
3000+
}
29843001
await provider.contextProxy.setValue("listApiConfigMeta", [oldProfile, keeperProfile])
29853002
await provider.setValue("currentApiConfigName", "old-profile")
29863003
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
@@ -3016,6 +3033,10 @@ describe("ClineProvider", () => {
30163033
provider.providerSettingsManager = {
30173034
deleteConfig: vi.fn().mockResolvedValue(undefined),
30183035
getProfile: vi.fn().mockResolvedValue({ name: "keeper-profile", ...keeperSettings }),
3036+
activateProfile: vi
3037+
.fn()
3038+
.mockResolvedValue({ name: "keeper-profile", id: "keeper-id", ...keeperSettings }),
3039+
setModeConfig: vi.fn().mockResolvedValue(undefined),
30193040
}
30203041
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
30213042
await provider.contextProxy.setValue("listApiConfigMeta", [oldProfile, keeperProfile])
@@ -3039,6 +3060,147 @@ describe("ClineProvider", () => {
30393060
await provider.dispose()
30403061
})
30413062

3063+
it("rebuilds the live task state and manager selection when the active profile is deleted", async () => {
3064+
const provider = new ClineProvider(
3065+
mockContext,
3066+
mockOutputChannel,
3067+
"sidebar",
3068+
new ContextProxy(mockContext),
3069+
new WebviewFocusTracker(),
3070+
)
3071+
const doomedProfile: ProviderSettingsEntry = {
3072+
name: "doomed-profile",
3073+
id: "doomed-id",
3074+
apiProvider: providerIdentifiers.openrouter,
3075+
}
3076+
const keeperProfile: ProviderSettingsEntry = {
3077+
name: "keeper-profile",
3078+
id: "keeper-id",
3079+
apiProvider: providerIdentifiers.anthropic,
3080+
}
3081+
const keeperSettings = { apiProvider: providerIdentifiers.anthropic, apiKey: "keeper-key" }
3082+
// @ts-ignore - Replace providerSettingsManager with a test double.
3083+
provider.providerSettingsManager = {
3084+
deleteConfig: vi.fn().mockResolvedValue(undefined),
3085+
getProfile: vi.fn().mockImplementation(({ name }: { name: string }) =>
3086+
Promise.resolve(
3087+
name === "doomed-profile"
3088+
? {
3089+
name: "doomed-profile",
3090+
id: "doomed-id",
3091+
apiProvider: providerIdentifiers.openrouter,
3092+
}
3093+
: { name: "keeper-profile", id: "keeper-id", ...keeperSettings },
3094+
),
3095+
),
3096+
activateProfile: vi
3097+
.fn()
3098+
.mockResolvedValue({ name: "keeper-profile", id: "keeper-id", ...keeperSettings }),
3099+
setModeConfig: vi.fn().mockResolvedValue(undefined),
3100+
}
3101+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
3102+
const rebuildSpy = vi
3103+
.spyOn(
3104+
provider as unknown as {
3105+
updateTaskApiHandlerIfNeeded: (
3106+
settings: ProviderSettings,
3107+
options: { forceRebuild?: boolean; skipCurrentTaskRebuild?: boolean },
3108+
) => void
3109+
},
3110+
"updateTaskApiHandlerIfNeeded",
3111+
)
3112+
.mockImplementation(() => {})
3113+
const events: Array<{ name: string; provider?: string }> = []
3114+
const listener = (config: { name: string; provider?: string }) => {
3115+
events.push(config)
3116+
}
3117+
provider.on(RooCodeEventName.ProviderProfileChanged, listener)
3118+
await provider.contextProxy.setValue("listApiConfigMeta", [doomedProfile, keeperProfile])
3119+
await provider.setValue("currentApiConfigName", "doomed-profile")
3120+
3121+
await provider.deleteProviderProfile(doomedProfile)
3122+
3123+
// The manager's own current-profile record and the current mode's profile
3124+
// mapping must follow the survivor, as they did when the old path activated it.
3125+
expect(provider.providerSettingsManager.activateProfile).toHaveBeenCalledWith({ name: "keeper-profile" })
3126+
expect(provider.providerSettingsManager.setModeConfig).toHaveBeenCalledWith(
3127+
(await provider.getState()).mode,
3128+
"keeper-id",
3129+
)
3130+
// The live task's API handler must be rebuilt from the survivor's settings.
3131+
expect(rebuildSpy).toHaveBeenCalledWith(expect.objectContaining({ apiKey: "keeper-key" }), {
3132+
forceRebuild: true,
3133+
})
3134+
// Watchers must learn that the active profile was replaced.
3135+
expect(events).toEqual([{ name: "keeper-profile", provider: providerIdentifiers.anthropic }])
3136+
provider.off(RooCodeEventName.ProviderProfileChanged, listener)
3137+
await provider.dispose()
3138+
})
3139+
3140+
it("does not rebuild the live task state when the deleted profile was not active", async () => {
3141+
const provider = new ClineProvider(
3142+
mockContext,
3143+
mockOutputChannel,
3144+
"sidebar",
3145+
new ContextProxy(mockContext),
3146+
new WebviewFocusTracker(),
3147+
)
3148+
const otherProfile: ProviderSettingsEntry = {
3149+
name: "other-profile",
3150+
id: "other-id",
3151+
apiProvider: providerIdentifiers.openrouter,
3152+
}
3153+
const keeperProfile: ProviderSettingsEntry = {
3154+
name: "keeper-profile",
3155+
id: "keeper-id",
3156+
apiProvider: providerIdentifiers.anthropic,
3157+
}
3158+
// @ts-ignore - Replace providerSettingsManager with a test double.
3159+
provider.providerSettingsManager = {
3160+
deleteConfig: vi.fn().mockResolvedValue(undefined),
3161+
getProfile: vi.fn().mockResolvedValue({
3162+
name: "other-profile",
3163+
id: "other-id",
3164+
apiProvider: providerIdentifiers.openrouter,
3165+
}),
3166+
activateProfile: vi.fn().mockResolvedValue({
3167+
name: "keeper-profile",
3168+
id: "keeper-id",
3169+
apiProvider: providerIdentifiers.anthropic,
3170+
}),
3171+
setModeConfig: vi.fn().mockResolvedValue(undefined),
3172+
}
3173+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
3174+
const rebuildSpy = vi
3175+
.spyOn(
3176+
provider as unknown as {
3177+
updateTaskApiHandlerIfNeeded: (
3178+
settings: ProviderSettings,
3179+
options: { forceRebuild?: boolean; skipCurrentTaskRebuild?: boolean },
3180+
) => void
3181+
},
3182+
"updateTaskApiHandlerIfNeeded",
3183+
)
3184+
.mockImplementation(() => {})
3185+
const events: Array<{ name: string; provider?: string }> = []
3186+
const listener = (config: { name: string; provider?: string }) => {
3187+
events.push(config)
3188+
}
3189+
provider.on(RooCodeEventName.ProviderProfileChanged, listener)
3190+
await provider.contextProxy.setValue("listApiConfigMeta", [keeperProfile, otherProfile])
3191+
await provider.setValue("currentApiConfigName", "keeper-profile")
3192+
3193+
await provider.deleteProviderProfile(otherProfile)
3194+
3195+
// The active profile was untouched: no manager re-activation, no task rebuild,
3196+
// no replacement event.
3197+
expect(provider.providerSettingsManager.activateProfile).not.toHaveBeenCalled()
3198+
expect(rebuildSpy).not.toHaveBeenCalled()
3199+
expect(events).toHaveLength(0)
3200+
provider.off(RooCodeEventName.ProviderProfileChanged, listener)
3201+
await provider.dispose()
3202+
})
3203+
30423204
it("should swallow only the typed not-found signal when pruning a stale profile entry", async () => {
30433205
const provider = new ClineProvider(
30443206
mockContext,
@@ -3147,6 +3309,12 @@ describe("ClineProvider", () => {
31473309
apiProvider: providerIdentifiers.anthropic,
31483310
}),
31493311
deleteConfig: vi.fn().mockResolvedValue(undefined),
3312+
activateProfile: vi.fn().mockResolvedValue({
3313+
name: "keeper-profile",
3314+
id: "keeper-id",
3315+
apiProvider: providerIdentifiers.anthropic,
3316+
}),
3317+
setModeConfig: vi.fn().mockResolvedValue(undefined),
31503318
}
31513319
const setProviderSettingsSpy = vi.spyOn(provider.contextProxy, "setProviderSettings")
31523320

@@ -3270,6 +3438,13 @@ describe("ClineProvider", () => {
32703438
apiProvider: providerIdentifiers.anthropic,
32713439
apiKey: "keeper-key",
32723440
}),
3441+
activateProfile: vi.fn().mockResolvedValue({
3442+
name: "keeper-profile",
3443+
id: "keeper-id",
3444+
apiProvider: providerIdentifiers.anthropic,
3445+
apiKey: "keeper-key",
3446+
}),
3447+
setModeConfig: vi.fn().mockResolvedValue(undefined),
32733448
}
32743449

32753450
await writer.deleteProviderProfile(oldProfile)

0 commit comments

Comments
 (0)