Skip to content

Commit 28e969c

Browse files
committed
fix(webview): scope profile-save task rebuilds to the task's own profile
Port of the sibling-unit fix: a non-activating profile save force-rebuilt the API handler of every view pinned to the saved profile, including views whose active task follows a different sticky profile. Both rebuild sites (the acting view's pinned branch and the pinned-sibling refresh) are now guarded by a taskApiConfigName match; the pinned view's overlay still refreshes. Regression tests cover both sites. Test hygiene: prune assertions compare full kept entries; webview state-payload test renamed.
1 parent 54b3890 commit 28e969c

3 files changed

Lines changed: 121 additions & 7 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2775,7 +2775,13 @@ export class ClineProvider
27752775
// serving the settings captured before the save under the same name.
27762776
if (this.pinnedProfileName === name) {
27772777
await this._saveViewLocalStateFromMutation({ apiConfiguration: providerSettings })
2778-
this.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
2778+
// Only rebuild when the task actually runs under the saved profile: a
2779+
// view pinned to this profile can hold a task that follows a different
2780+
// sticky profile, and the forced rebuild would silently switch that
2781+
// conversation to the saved profile's settings.
2782+
if (this.getCurrentTask()?.taskApiConfigName === name) {
2783+
this.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
2784+
}
27792785
}
27802786
await this.refreshViewLocalStateForUpdatedProfile(name, providerSettings)
27812787
}
@@ -3321,9 +3327,13 @@ export class ClineProvider
33213327
// The active task of a pinned view holds its own API handler built from
33223328
// the profile's settings: rebuild it with the saved settings or the
33233329
// running conversation keeps sending the old token even though the
3324-
// view's buffer has caught up. Direct private access across sibling
3325-
// instances of the same class is compile-time safe.
3326-
snapshot.instance.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
3330+
// view's buffer has caught up. Only rebuild when the task actually runs
3331+
// under the saved profile: a task that follows a different sticky
3332+
// profile must keep its own settings. Direct private access across
3333+
// sibling instances of the same class is compile-time safe.
3334+
if (snapshot.instance.getCurrentTask()?.taskApiConfigName === name) {
3335+
snapshot.instance.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
3336+
}
33273337
await snapshot.instance.postStateToWebview()
33283338
}),
33293339
)

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

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,8 +1977,8 @@ describe("ClineProvider", () => {
19771977
const pruned = provider["prunePersistedViewStates"](states)
19781978

19791979
expect(Object.keys(pruned)).toHaveLength(50)
1980-
expect(pruned["view-54"]).toBeDefined()
1981-
expect(pruned["view-5"]).toBeDefined()
1980+
expect(pruned["view-54"]).toEqual({ mode: "mode-54", updatedAt: 54 })
1981+
expect(pruned["view-5"]).toEqual({ mode: "mode-5", updatedAt: 5 })
19821982
expect(pruned["view-4"]).toBeUndefined()
19831983

19841984
await provider.dispose()
@@ -10810,6 +10810,7 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
1081010810
// minimal double without the full provider/services graph.
1081110811
const siblingTask = {
1081210812
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "old-key" },
10813+
taskApiConfigName: "saved-profile",
1081310814
updateApiConfiguration: vi.fn(),
1081410815
}
1081510816
vi.spyOn(sibling, "getCurrentTask").mockReturnValue(
@@ -10854,6 +10855,109 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
1085410855
}
1085510856
})
1085610857

10858+
it("does not rebuild a pinned sibling's task that runs under another profile", async () => {
10859+
// Defensive: the doubles in this suite can leave console writes pending
10860+
// at worker teardown.
10861+
vi.spyOn(console, "log").mockImplementation(() => {})
10862+
vi.spyOn(console, "warn").mockImplementation(() => {})
10863+
vi.spyOn(console, "error").mockImplementation(() => {})
10864+
const sibling = new ClineProvider(
10865+
mockContext,
10866+
mockOutputChannel,
10867+
"editor",
10868+
new ContextProxy(mockContext),
10869+
new WebviewFocusTracker(),
10870+
)
10871+
try {
10872+
// The sibling pins the saved profile, but its active task follows a
10873+
// different sticky profile: the save must refresh the view overlay
10874+
// without switching that conversation to the saved profile's settings.
10875+
sibling["viewLocalState"].currentApiConfigName = "saved-profile"
10876+
sibling["viewLocalState"].apiConfiguration = {
10877+
apiProvider: providerIdentifiers.anthropic,
10878+
apiKey: "old-key",
10879+
}
10880+
vi.spyOn(sibling, "postStateToWebview").mockResolvedValue(undefined)
10881+
10882+
const siblingTask = {
10883+
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "other-key" },
10884+
taskApiConfigName: "other-profile",
10885+
updateApiConfiguration: vi.fn(),
10886+
}
10887+
vi.spyOn(sibling, "getCurrentTask").mockReturnValue(
10888+
siblingTask as unknown as ReturnType<typeof sibling.getCurrentTask>,
10889+
)
10890+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
10891+
// @ts-ignore - Replace providerSettingsManager with a test double.
10892+
provider.providerSettingsManager = {
10893+
saveConfig: vi.fn().mockResolvedValue("saved-id"),
10894+
listConfig: vi.fn().mockResolvedValue([]),
10895+
setModeConfig: vi.fn(),
10896+
getProfile: vi
10897+
.fn()
10898+
.mockResolvedValue({ name: "saved-profile", apiProvider: providerIdentifiers.anthropic }),
10899+
getModeConfigId: vi.fn().mockResolvedValue(undefined),
10900+
}
10901+
10902+
await provider.upsertProviderProfile(
10903+
"saved-profile",
10904+
{ apiProvider: providerIdentifiers.anthropic, apiKey: "new-key" },
10905+
false,
10906+
)
10907+
10908+
// The pinned view's overlay still catches up with the saved settings...
10909+
expect(sibling["viewLocalState"].apiConfiguration).toEqual({
10910+
apiProvider: providerIdentifiers.anthropic,
10911+
apiKey: "new-key",
10912+
})
10913+
// ...but the task runs under another profile: its handler must not be
10914+
// switched to the saved profile's settings.
10915+
expect(siblingTask.updateApiConfiguration).not.toHaveBeenCalled()
10916+
// Let queued view-local persistence settle while the console spies are
10917+
// still installed, so teardown has no pending writes.
10918+
await new Promise((resolve) => setTimeout(resolve, 50))
10919+
} finally {
10920+
await sibling.dispose()
10921+
}
10922+
})
10923+
10924+
it("does not rebuild the acting view's task when it runs under another profile", async () => {
10925+
// The acting view pins the saved profile while its own task follows a
10926+
// different sticky profile: the non-activating save refreshes the view
10927+
// overlay but must leave the task's handler alone.
10928+
const task = {
10929+
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "other-key" },
10930+
taskApiConfigName: "other-profile",
10931+
updateApiConfiguration: vi.fn(),
10932+
}
10933+
vi.spyOn(provider, "getCurrentTask").mockReturnValue(
10934+
task as unknown as ReturnType<typeof provider.getCurrentTask>,
10935+
)
10936+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
10937+
provider["viewLocalState"].currentApiConfigName = "saved-profile"
10938+
// @ts-ignore - Replace providerSettingsManager with a test double.
10939+
provider.providerSettingsManager = {
10940+
saveConfig: vi.fn().mockResolvedValue("saved-id"),
10941+
listConfig: vi.fn().mockResolvedValue([]),
10942+
setModeConfig: vi.fn(),
10943+
getProfile: vi
10944+
.fn()
10945+
.mockResolvedValue({ name: "saved-profile", apiProvider: providerIdentifiers.anthropic }),
10946+
getModeConfigId: vi.fn().mockResolvedValue(undefined),
10947+
}
10948+
10949+
await provider.upsertProviderProfile(
10950+
"saved-profile",
10951+
{ apiProvider: providerIdentifiers.anthropic, apiKey: "new-key" },
10952+
false,
10953+
)
10954+
10955+
expect(provider["viewLocalState"].apiConfiguration).toEqual({
10956+
apiProvider: providerIdentifiers.anthropic,
10957+
apiKey: "new-key",
10958+
})
10959+
expect(task.updateApiConfiguration).not.toHaveBeenCalled()
10960+
})
1085710961
it("does not leak shared provider keys into a pinned view's api configuration", async () => {
1085810962
vi.spyOn(console, "log").mockImplementation(() => {})
1085910963
vi.spyOn(console, "warn").mockImplementation(() => {})

‎webview-ui/src/context/__tests__/ExtensionStateContext.spec.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ describe("ExtensionStateContext", () => {
168168
}
169169
})
170170

171-
it("reseeds view-local mode and API profile from a new state payload after local edits", () => {
171+
it("applies mode and profile from a later state payload after local edits", () => {
172172
render(
173173
<ExtensionStateContextProvider>
174174
<ViewLocalStateTestComponent />

0 commit comments

Comments
 (0)