Skip to content

Commit 41bbc27

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 bb3a988 commit 41bbc27

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
@@ -3014,11 +3014,17 @@ export class ClineProvider
30143014
// the handler with the replacement's settings and re-point the sticky name,
30153015
// or the running conversation keeps sending the deleted profile's
30163016
// configuration even though the view's buffer was re-pointed (the posted
3017-
// state prefers the task's own apiConfiguration).
3018-
if (replacementSettings) {
3019-
instance.updateTaskApiHandlerIfNeeded(replacementSettings, { forceRebuild: true })
3017+
// state prefers the task's own apiConfiguration). Only re-point when the
3018+
// task actually runs under the deleted profile: a view pinned to it can
3019+
// hold a task that follows a different sticky profile, and forcing the
3020+
// replacement would silently switch that conversation and rewrite its
3021+
// persisted profile name without the user asking.
3022+
if (instance.getCurrentTask()?.taskApiConfigName === deletedProfileName) {
3023+
if (replacementSettings) {
3024+
instance.updateTaskApiHandlerIfNeeded(replacementSettings, { forceRebuild: true })
3025+
}
3026+
await instance.persistStickyProviderProfileToCurrentTask(replacementName)
30203027
}
3021-
await instance.persistStickyProviderProfileToCurrentTask(replacementName)
30223028
await instance.postStateToWebview()
30233029
}),
30243030
)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,7 @@ describe("ClineProvider - Parallel Mode Support", () => {
13851385
// the task's handler and re-point its sticky profile name too.
13861386
const siblingTask = {
13871387
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "doomed-key" },
1388+
taskApiConfigName: "doomed-profile",
13881389
updateApiConfiguration: vi.fn(),
13891390
setTaskApiConfigName: vi.fn(),
13901391
}

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

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

3472+
it("does not re-point a pinned sibling's task that follows another profile when its profile is deleted", async () => {
3473+
// Defensive: the doubles in this suite can leave console writes pending
3474+
// at worker teardown.
3475+
vi.spyOn(console, "log").mockImplementation(() => {})
3476+
vi.spyOn(console, "warn").mockImplementation(() => {})
3477+
vi.spyOn(console, "error").mockImplementation(() => {})
3478+
const provider = new ClineProvider(
3479+
mockContext,
3480+
mockOutputChannel,
3481+
"sidebar",
3482+
new ContextProxy(mockContext),
3483+
new WebviewFocusTracker(),
3484+
)
3485+
const sibling = new ClineProvider(
3486+
mockContext,
3487+
mockOutputChannel,
3488+
"editor",
3489+
new ContextProxy(mockContext),
3490+
new WebviewFocusTracker(),
3491+
)
3492+
try {
3493+
const oldProfile: ProviderSettingsEntry = {
3494+
name: "old-profile",
3495+
id: "old-id",
3496+
apiProvider: providerIdentifiers.openrouter,
3497+
}
3498+
const keeperProfile: ProviderSettingsEntry = {
3499+
name: "keeper-profile",
3500+
id: "keeper-id",
3501+
apiProvider: providerIdentifiers.anthropic,
3502+
}
3503+
await provider.contextProxy.setValue("listApiConfigMeta", [oldProfile, keeperProfile])
3504+
await provider.contextProxy.setValue("currentApiConfigName", "keeper-profile")
3505+
// The sibling pins the profile being deleted, but its active task
3506+
// follows a different sticky profile: the deletion must re-point the
3507+
// sibling's buffer to the surviving profile without switching that
3508+
// conversation's handler or rewriting its persisted profile name.
3509+
sibling["viewLocalState"].currentApiConfigName = "old-profile"
3510+
sibling["viewLocalState"].apiConfiguration = {
3511+
apiProvider: providerIdentifiers.openrouter,
3512+
openRouterApiKey: "deleted-profile-secret",
3513+
}
3514+
vi.spyOn(sibling, "postStateToWebview").mockResolvedValue(undefined)
3515+
const siblingTask = {
3516+
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "other-key" },
3517+
taskApiConfigName: "other-profile",
3518+
taskId: "sibling-task",
3519+
setTaskApiConfigName: vi.fn(),
3520+
updateApiConfiguration: vi.fn(),
3521+
}
3522+
vi.spyOn(sibling, "getCurrentTask").mockReturnValue(
3523+
siblingTask as unknown as ReturnType<typeof sibling.getCurrentTask>,
3524+
)
3525+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
3526+
vi.spyOn(provider.providerSettingsManager, "getProfile").mockResolvedValue({
3527+
name: "keeper-profile",
3528+
id: "keeper-id",
3529+
apiProvider: providerIdentifiers.anthropic,
3530+
})
3531+
vi.spyOn(provider.providerSettingsManager, "deleteConfig").mockResolvedValue(undefined)
3532+
3533+
await provider.deleteProviderProfile(oldProfile)
3534+
3535+
// The sibling's view buffer is still re-pointed to the surviving
3536+
// profile...
3537+
expect(sibling["viewLocalState"].currentApiConfigName).toBe("keeper-profile")
3538+
expect(sibling["viewLocalState"].apiConfiguration).toEqual({
3539+
id: "keeper-id",
3540+
apiProvider: providerIdentifiers.anthropic,
3541+
})
3542+
// ...but the task follows another profile: neither the handler rebuild
3543+
// nor the sticky-name rewrite may touch it.
3544+
expect(siblingTask.updateApiConfiguration).not.toHaveBeenCalled()
3545+
expect(siblingTask.setTaskApiConfigName).not.toHaveBeenCalled()
3546+
// Let queued view-local persistence settle while the console spies are
3547+
// still installed, so teardown has no pending writes.
3548+
await new Promise((resolve) => setTimeout(resolve, 50))
3549+
} finally {
3550+
await sibling.dispose()
3551+
await provider.dispose()
3552+
}
3553+
})
3554+
34723555
it("leaves the view buffer untouched when the deleted profile is neither globally active nor view-pinned", async () => {
34733556
const provider = new ClineProvider(
34743557
mockContext,

0 commit comments

Comments
 (0)