Skip to content

Commit 1849e73

Browse files
committed
fix(webview): gate deleted-profile re-pin task writes on the task's own profile
Port of the sibling-unit fix: a view pinned to a deleted profile can hold a task that follows a different sticky profile: the deletion re-pin must re-point the view buffer but must not force the replacement's settings onto that conversation's handler, nor rewrite its persisted sticky profile name. Both task-side writes are now gated on a taskApiConfigName match with the deleted profile, mirroring the profile-save rebuild guards. Regression test included.
1 parent 574dfeb commit 1849e73

3 files changed

Lines changed: 94 additions & 4 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,11 +2965,17 @@ export class ClineProvider
29652965
// the handler with the replacement's settings and re-point the sticky name,
29662966
// or the running conversation keeps sending the deleted profile's
29672967
// configuration even though the view's buffer was re-pointed (the posted
2968-
// state prefers the task's own apiConfiguration).
2969-
if (replacementSettings) {
2970-
instance.updateTaskApiHandlerIfNeeded(replacementSettings, { forceRebuild: true })
2968+
// state prefers the task's own apiConfiguration). Only re-point when the
2969+
// task actually runs under the deleted profile: a view pinned to it can
2970+
// hold a task that follows a different sticky profile, and forcing the
2971+
// replacement would silently switch that conversation and rewrite its
2972+
// persisted profile name without the user asking.
2973+
if (instance.getCurrentTask()?.taskApiConfigName === deletedProfileName) {
2974+
if (replacementSettings) {
2975+
instance.updateTaskApiHandlerIfNeeded(replacementSettings, { forceRebuild: true })
2976+
}
2977+
await instance.persistStickyProviderProfileToCurrentTask(replacementName)
29712978
}
2972-
await instance.persistStickyProviderProfileToCurrentTask(replacementName)
29732979
await instance.postStateToWebview()
29742980
}),
29752981
)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@ describe("ClineProvider - Parallel Mode Support", () => {
13221322
// the task's handler and re-point its sticky profile name too.
13231323
const siblingTask = {
13241324
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "doomed-key" },
1325+
taskApiConfigName: "doomed-profile",
13251326
updateApiConfiguration: vi.fn(),
13261327
setTaskApiConfigName: vi.fn(),
13271328
}

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3094,6 +3094,89 @@ describe("ClineProvider", () => {
30943094
await provider.dispose()
30953095
})
30963096

3097+
it("does not re-point a pinned sibling's task that follows another profile when its profile is deleted", async () => {
3098+
// Defensive: the doubles in this suite can leave console writes pending
3099+
// at worker teardown.
3100+
vi.spyOn(console, "log").mockImplementation(() => {})
3101+
vi.spyOn(console, "warn").mockImplementation(() => {})
3102+
vi.spyOn(console, "error").mockImplementation(() => {})
3103+
const provider = new ClineProvider(
3104+
mockContext,
3105+
mockOutputChannel,
3106+
"sidebar",
3107+
new ContextProxy(mockContext),
3108+
new WebviewFocusTracker(),
3109+
)
3110+
const sibling = new ClineProvider(
3111+
mockContext,
3112+
mockOutputChannel,
3113+
"editor",
3114+
new ContextProxy(mockContext),
3115+
new WebviewFocusTracker(),
3116+
)
3117+
try {
3118+
const oldProfile: ProviderSettingsEntry = {
3119+
name: "old-profile",
3120+
id: "old-id",
3121+
apiProvider: providerIdentifiers.openrouter,
3122+
}
3123+
const keeperProfile: ProviderSettingsEntry = {
3124+
name: "keeper-profile",
3125+
id: "keeper-id",
3126+
apiProvider: providerIdentifiers.anthropic,
3127+
}
3128+
await provider.contextProxy.setValue("listApiConfigMeta", [oldProfile, keeperProfile])
3129+
await provider.contextProxy.setValue("currentApiConfigName", "keeper-profile")
3130+
// The sibling pins the profile being deleted, but its active task
3131+
// follows a different sticky profile: the deletion must re-point the
3132+
// sibling's buffer to the surviving profile without switching that
3133+
// conversation's handler or rewriting its persisted profile name.
3134+
sibling["viewLocalState"].currentApiConfigName = "old-profile"
3135+
sibling["viewLocalState"].apiConfiguration = {
3136+
apiProvider: providerIdentifiers.openrouter,
3137+
openRouterApiKey: "deleted-profile-secret",
3138+
}
3139+
vi.spyOn(sibling, "postStateToWebview").mockResolvedValue(undefined)
3140+
const siblingTask = {
3141+
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "other-key" },
3142+
taskApiConfigName: "other-profile",
3143+
taskId: "sibling-task",
3144+
setTaskApiConfigName: vi.fn(),
3145+
updateApiConfiguration: vi.fn(),
3146+
}
3147+
vi.spyOn(sibling, "getCurrentTask").mockReturnValue(
3148+
siblingTask as unknown as ReturnType<typeof sibling.getCurrentTask>,
3149+
)
3150+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
3151+
vi.spyOn(provider.providerSettingsManager, "getProfile").mockResolvedValue({
3152+
name: "keeper-profile",
3153+
id: "keeper-id",
3154+
apiProvider: providerIdentifiers.anthropic,
3155+
})
3156+
vi.spyOn(provider.providerSettingsManager, "deleteConfig").mockResolvedValue(undefined)
3157+
3158+
await provider.deleteProviderProfile(oldProfile)
3159+
3160+
// The sibling's view buffer is still re-pointed to the surviving
3161+
// profile...
3162+
expect(sibling["viewLocalState"].currentApiConfigName).toBe("keeper-profile")
3163+
expect(sibling["viewLocalState"].apiConfiguration).toEqual({
3164+
id: "keeper-id",
3165+
apiProvider: providerIdentifiers.anthropic,
3166+
})
3167+
// ...but the task follows another profile: neither the handler rebuild
3168+
// nor the sticky-name rewrite may touch it.
3169+
expect(siblingTask.updateApiConfiguration).not.toHaveBeenCalled()
3170+
expect(siblingTask.setTaskApiConfigName).not.toHaveBeenCalled()
3171+
// Let queued view-local persistence settle while the console spies are
3172+
// still installed, so teardown has no pending writes.
3173+
await new Promise((resolve) => setTimeout(resolve, 50))
3174+
} finally {
3175+
await sibling.dispose()
3176+
await provider.dispose()
3177+
}
3178+
})
3179+
30973180
it("leaves the view buffer untouched when the deleted profile is neither globally active nor view-pinned", async () => {
30983181
const provider = new ClineProvider(
30993182
mockContext,

0 commit comments

Comments
 (0)