Skip to content

Commit 9afc9d8

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 28e969c commit 9afc9d8

2 files changed

Lines changed: 93 additions & 3 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3474,10 +3474,17 @@ export class ClineProvider
34743474
// conversation keeps sending the deleted profile's configuration even though the view's
34753475
// buffer was re-pointed (the posted state prefers the task's own apiConfiguration).
34763476
for (const snapshot of snapshots) {
3477-
if (replacementSettings) {
3478-
snapshot.instance.updateTaskApiHandlerIfNeeded(replacementSettings, { forceRebuild: true })
3477+
// Only re-point when the task actually runs under the deleted profile:
3478+
// a view pinned to it can hold a task that follows a different sticky
3479+
// profile, and forcing the replacement would silently switch that
3480+
// conversation and rewrite its persisted profile name without the user
3481+
// asking.
3482+
if (snapshot.instance.getCurrentTask()?.taskApiConfigName === deletedProfileName) {
3483+
if (replacementSettings) {
3484+
snapshot.instance.updateTaskApiHandlerIfNeeded(replacementSettings, { forceRebuild: true })
3485+
}
3486+
await snapshot.instance.persistStickyProviderProfileToCurrentTask(replacementName)
34793487
}
3480-
await snapshot.instance.persistStickyProviderProfileToCurrentTask(replacementName)
34813488
await snapshot.instance.postStateToWebview()
34823489
}
34833490
}

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

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

4690+
it("does not re-point a pinned sibling's task that follows another profile when its profile is deleted", async () => {
4691+
// Defensive: the doubles in this suite can leave console writes pending
4692+
// at worker teardown.
4693+
vi.spyOn(console, "log").mockImplementation(() => {})
4694+
vi.spyOn(console, "warn").mockImplementation(() => {})
4695+
vi.spyOn(console, "error").mockImplementation(() => {})
4696+
const provider = new ClineProvider(
4697+
mockContext,
4698+
mockOutputChannel,
4699+
"sidebar",
4700+
new ContextProxy(mockContext),
4701+
new WebviewFocusTracker(),
4702+
)
4703+
const sibling = new ClineProvider(
4704+
mockContext,
4705+
mockOutputChannel,
4706+
"editor",
4707+
new ContextProxy(mockContext),
4708+
new WebviewFocusTracker(),
4709+
)
4710+
try {
4711+
const oldProfile: ProviderSettingsEntry = {
4712+
name: "old-profile",
4713+
id: "old-id",
4714+
apiProvider: providerIdentifiers.openrouter,
4715+
}
4716+
const keeperProfile: ProviderSettingsEntry = {
4717+
name: "keeper-profile",
4718+
id: "keeper-id",
4719+
apiProvider: providerIdentifiers.anthropic,
4720+
}
4721+
await provider.contextProxy.setValue("listApiConfigMeta", [oldProfile, keeperProfile])
4722+
await provider.contextProxy.setValue("currentApiConfigName", "keeper-profile")
4723+
// The sibling pins the profile being deleted, but its active task
4724+
// follows a different sticky profile: the deletion must re-point the
4725+
// sibling's buffer to the surviving profile without switching that
4726+
// conversation's handler or rewriting its persisted profile name.
4727+
sibling["viewLocalState"].currentApiConfigName = "old-profile"
4728+
sibling["viewLocalState"].apiConfiguration = {
4729+
apiProvider: providerIdentifiers.openrouter,
4730+
openRouterApiKey: "deleted-profile-secret",
4731+
}
4732+
vi.spyOn(sibling, "postStateToWebview").mockResolvedValue(undefined)
4733+
const siblingTask = {
4734+
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "other-key" },
4735+
taskApiConfigName: "other-profile",
4736+
taskId: "sibling-task",
4737+
setTaskApiConfigName: vi.fn(),
4738+
updateApiConfiguration: vi.fn(),
4739+
}
4740+
vi.spyOn(sibling, "getCurrentTask").mockReturnValue(
4741+
siblingTask as unknown as ReturnType<typeof sibling.getCurrentTask>,
4742+
)
4743+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
4744+
vi.spyOn(provider.providerSettingsManager, "getProfile").mockResolvedValue({
4745+
name: "keeper-profile",
4746+
id: "keeper-id",
4747+
apiProvider: providerIdentifiers.anthropic,
4748+
})
4749+
vi.spyOn(provider.providerSettingsManager, "deleteConfig").mockResolvedValue(undefined)
4750+
4751+
await provider.deleteProviderProfile(oldProfile)
4752+
4753+
// The sibling's view buffer is still re-pointed to the surviving
4754+
// profile...
4755+
expect(sibling["viewLocalState"].currentApiConfigName).toBe("keeper-profile")
4756+
expect(sibling["viewLocalState"].apiConfiguration).toEqual({
4757+
id: "keeper-id",
4758+
apiProvider: providerIdentifiers.anthropic,
4759+
})
4760+
// ...but the task follows another profile: neither the handler rebuild
4761+
// nor the sticky-name rewrite may touch it.
4762+
expect(siblingTask.updateApiConfiguration).not.toHaveBeenCalled()
4763+
expect(siblingTask.setTaskApiConfigName).not.toHaveBeenCalled()
4764+
// Let queued view-local persistence settle while the console spies are
4765+
// still installed, so teardown has no pending writes.
4766+
await new Promise((resolve) => setTimeout(resolve, 50))
4767+
} finally {
4768+
await sibling.dispose()
4769+
await provider.dispose()
4770+
}
4771+
})
4772+
46904773
it("leaves the view buffer untouched when the deleted profile is neither globally active nor view-pinned", async () => {
46914774
const provider = new ClineProvider(
46924775
mockContext,

0 commit comments

Comments
 (0)