Skip to content

Commit 23838ac

Browse files
committed
fix(webview): gate deleted-profile re-pin task writes on the task's own profile
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 279e9da commit 23838ac

2 files changed

Lines changed: 93 additions & 4 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,11 +2723,17 @@ export class ClineProvider
27232723
// the handler with the replacement's settings and re-point the sticky name,
27242724
// or the running conversation keeps sending the deleted profile's
27252725
// configuration even though the view's buffer was re-pointed (the posted
2726-
// state prefers the task's own apiConfiguration).
2727-
if (replacementSettings) {
2728-
instance.updateTaskApiHandlerIfNeeded(replacementSettings, { forceRebuild: true })
2726+
// state prefers the task's own apiConfiguration). Only re-point when the
2727+
// task actually runs under the deleted profile: a view pinned to it can
2728+
// hold a task that follows a different sticky profile, and forcing the
2729+
// replacement would silently switch that conversation and rewrite its
2730+
// persisted profile name without the user asking.
2731+
if (instance.getCurrentTask()?.taskApiConfigName === deletedProfileName) {
2732+
if (replacementSettings) {
2733+
instance.updateTaskApiHandlerIfNeeded(replacementSettings, { forceRebuild: true })
2734+
}
2735+
await instance.persistStickyProviderProfileToCurrentTask(replacementName)
27292736
}
2730-
await instance.persistStickyProviderProfileToCurrentTask(replacementName)
27312737
await instance.postStateToWebview()
27322738
}),
27332739
)

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

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

2867+
it("does not re-point a pinned sibling's task that follows another profile when its profile is deleted", async () => {
2868+
// Defensive: the doubles in this suite can leave console writes pending
2869+
// at worker teardown.
2870+
vi.spyOn(console, "log").mockImplementation(() => {})
2871+
vi.spyOn(console, "warn").mockImplementation(() => {})
2872+
vi.spyOn(console, "error").mockImplementation(() => {})
2873+
const provider = new ClineProvider(
2874+
mockContext,
2875+
mockOutputChannel,
2876+
"sidebar",
2877+
new ContextProxy(mockContext),
2878+
new WebviewFocusTracker(),
2879+
)
2880+
const sibling = new ClineProvider(
2881+
mockContext,
2882+
mockOutputChannel,
2883+
"editor",
2884+
new ContextProxy(mockContext),
2885+
new WebviewFocusTracker(),
2886+
)
2887+
try {
2888+
const oldProfile: ProviderSettingsEntry = {
2889+
name: "old-profile",
2890+
id: "old-id",
2891+
apiProvider: providerIdentifiers.openrouter,
2892+
}
2893+
const keeperProfile: ProviderSettingsEntry = {
2894+
name: "keeper-profile",
2895+
id: "keeper-id",
2896+
apiProvider: providerIdentifiers.anthropic,
2897+
}
2898+
await provider.contextProxy.setValue("listApiConfigMeta", [oldProfile, keeperProfile])
2899+
await provider.contextProxy.setValue("currentApiConfigName", "keeper-profile")
2900+
// The sibling pins the profile being deleted, but its active task
2901+
// follows a different sticky profile: the deletion must re-point the
2902+
// sibling's buffer to the surviving profile without switching that
2903+
// conversation's handler or rewriting its persisted profile name.
2904+
sibling["viewLocalState"].currentApiConfigName = "old-profile"
2905+
sibling["viewLocalState"].apiConfiguration = {
2906+
apiProvider: providerIdentifiers.openrouter,
2907+
openRouterApiKey: "deleted-profile-secret",
2908+
}
2909+
vi.spyOn(sibling, "postStateToWebview").mockResolvedValue(undefined)
2910+
const siblingTask = {
2911+
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "other-key" },
2912+
taskApiConfigName: "other-profile",
2913+
taskId: "sibling-task",
2914+
setTaskApiConfigName: vi.fn(),
2915+
updateApiConfiguration: vi.fn(),
2916+
}
2917+
vi.spyOn(sibling, "getCurrentTask").mockReturnValue(
2918+
siblingTask as unknown as ReturnType<typeof sibling.getCurrentTask>,
2919+
)
2920+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
2921+
vi.spyOn(provider.providerSettingsManager, "getProfile").mockResolvedValue({
2922+
name: "keeper-profile",
2923+
id: "keeper-id",
2924+
apiProvider: providerIdentifiers.anthropic,
2925+
})
2926+
vi.spyOn(provider.providerSettingsManager, "deleteConfig").mockResolvedValue(undefined)
2927+
2928+
await provider.deleteProviderProfile(oldProfile)
2929+
2930+
// The sibling's view buffer is still re-pointed to the surviving
2931+
// profile...
2932+
expect(sibling["viewLocalState"].currentApiConfigName).toBe("keeper-profile")
2933+
expect(sibling["viewLocalState"].apiConfiguration).toEqual({
2934+
id: "keeper-id",
2935+
apiProvider: providerIdentifiers.anthropic,
2936+
})
2937+
// ...but the task follows another profile: neither the handler rebuild
2938+
// nor the sticky-name rewrite may touch it.
2939+
expect(siblingTask.updateApiConfiguration).not.toHaveBeenCalled()
2940+
expect(siblingTask.setTaskApiConfigName).not.toHaveBeenCalled()
2941+
// Let queued view-local persistence settle while the console spies are
2942+
// still installed, so teardown has no pending writes.
2943+
await new Promise((resolve) => setTimeout(resolve, 50))
2944+
} finally {
2945+
await sibling.dispose()
2946+
await provider.dispose()
2947+
}
2948+
})
2949+
28672950
it("leaves the view buffer untouched when the deleted profile is neither globally active nor view-pinned", async () => {
28682951
const provider = new ClineProvider(
28692952
mockContext,

0 commit comments

Comments
 (0)