Skip to content

Commit 50aa367

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; the two re-key storage tests seed viewStates through the durable write path so their assertions can fail.
1 parent 6efdad3 commit 50aa367

3 files changed

Lines changed: 120 additions & 14 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,7 +2418,13 @@ export class ClineProvider
24182418
// serving the settings captured before the save under the same name.
24192419
if (this.pinnedProfileName === name) {
24202420
await this._saveViewLocalStateFromMutation({ apiConfiguration: providerSettings })
2421-
this.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
2421+
// Only rebuild when the task actually runs under the saved profile: a
2422+
// view pinned to this profile can hold a task that follows a different
2423+
// sticky profile, and the forced rebuild would silently switch that
2424+
// conversation to the saved profile's settings.
2425+
if (this.getCurrentTask()?.taskApiConfigName === name) {
2426+
this.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
2427+
}
24222428
}
24232429
await this.refreshViewLocalStateForUpdatedProfile(name, providerSettings)
24242430
}
@@ -2677,9 +2683,13 @@ export class ClineProvider
26772683
// The active task of a pinned view holds its own API handler built from
26782684
// the profile's settings: rebuild it with the saved settings or the
26792685
// running conversation keeps sending the old token even though the
2680-
// view's buffer has caught up. Direct private access across sibling
2681-
// instances of the same class is compile-time safe.
2682-
instance.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
2686+
// view's buffer has caught up. Only rebuild when the task actually runs
2687+
// under the saved profile: a task that follows a different sticky
2688+
// profile must keep its own settings. Direct private access across
2689+
// sibling instances of the same class is compile-time safe.
2690+
if (instance.getCurrentTask()?.taskApiConfigName === name) {
2691+
instance.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
2692+
}
26832693
await instance.postStateToWebview()
26842694
}),
26852695
)

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

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

18711871
expect(Object.keys(pruned)).toHaveLength(50)
1872-
expect(pruned["view-54"]).toBeDefined()
1873-
expect(pruned["view-5"]).toBeDefined()
1872+
expect(pruned["view-54"]).toEqual({ mode: "mode-54", updatedAt: 54 })
1873+
expect(pruned["view-5"]).toEqual({ mode: "mode-5", updatedAt: 5 })
18741874
expect(pruned["view-4"]).toBeUndefined()
18751875

18761876
await provider.dispose()
@@ -8292,6 +8292,7 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
82928292
// minimal double without the full provider/services graph.
82938293
const siblingTask = {
82948294
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "old-key" },
8295+
taskApiConfigName: "saved-profile",
82958296
updateApiConfiguration: vi.fn(),
82968297
}
82978298
vi.spyOn(sibling, "getCurrentTask").mockReturnValue(
@@ -8329,6 +8330,101 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
83298330
}
83308331
})
83318332

8333+
it("does not rebuild a pinned sibling's task that runs under another profile", async () => {
8334+
// Defensive: the doubles in this suite can leave console writes pending
8335+
// at worker teardown.
8336+
vi.spyOn(console, "log").mockImplementation(() => {})
8337+
vi.spyOn(console, "warn").mockImplementation(() => {})
8338+
vi.spyOn(console, "error").mockImplementation(() => {})
8339+
const sibling = new ClineProvider(
8340+
mockContext,
8341+
mockOutputChannel,
8342+
"editor",
8343+
new ContextProxy(mockContext),
8344+
new WebviewFocusTracker(),
8345+
)
8346+
try {
8347+
// The sibling pins the saved profile, but its active task follows a
8348+
// different sticky profile: the save must refresh the view overlay
8349+
// without switching that conversation to the saved profile's settings.
8350+
sibling["viewLocalState"].currentApiConfigName = "saved-profile"
8351+
sibling["viewLocalState"].apiConfiguration = {
8352+
apiProvider: providerIdentifiers.anthropic,
8353+
apiKey: "old-key",
8354+
}
8355+
vi.spyOn(sibling, "postStateToWebview").mockResolvedValue(undefined)
8356+
8357+
const siblingTask = {
8358+
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "other-key" },
8359+
taskApiConfigName: "other-profile",
8360+
updateApiConfiguration: vi.fn(),
8361+
}
8362+
vi.spyOn(sibling, "getCurrentTask").mockReturnValue(
8363+
siblingTask as unknown as ReturnType<typeof sibling.getCurrentTask>,
8364+
)
8365+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
8366+
// @ts-ignore - Replace providerSettingsManager with a test double.
8367+
provider.providerSettingsManager = {
8368+
saveConfig: vi.fn().mockResolvedValue("saved-id"),
8369+
listConfig: vi.fn().mockResolvedValue([]),
8370+
setModeConfig: vi.fn(),
8371+
}
8372+
8373+
await provider.upsertProviderProfile(
8374+
"saved-profile",
8375+
{ apiProvider: providerIdentifiers.anthropic, apiKey: "new-key" },
8376+
false,
8377+
)
8378+
8379+
// The pinned view's overlay still catches up with the saved settings...
8380+
expect(sibling["viewLocalState"].apiConfiguration).toEqual({
8381+
apiProvider: providerIdentifiers.anthropic,
8382+
apiKey: "new-key",
8383+
})
8384+
// ...but the task runs under another profile: its handler must not be
8385+
// switched to the saved profile's settings.
8386+
expect(siblingTask.updateApiConfiguration).not.toHaveBeenCalled()
8387+
// Let queued view-local persistence settle while the console spies are
8388+
// still installed, so teardown has no pending writes.
8389+
await new Promise((resolve) => setTimeout(resolve, 50))
8390+
} finally {
8391+
await sibling.dispose()
8392+
}
8393+
})
8394+
8395+
it("does not rebuild the acting view's task when it runs under another profile", async () => {
8396+
// The acting view pins the saved profile while its own task follows a
8397+
// different sticky profile: the non-activating save refreshes the view
8398+
// overlay but must leave the task's handler alone.
8399+
const task = {
8400+
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "other-key" },
8401+
taskApiConfigName: "other-profile",
8402+
updateApiConfiguration: vi.fn(),
8403+
}
8404+
vi.spyOn(provider, "getCurrentTask").mockReturnValue(
8405+
task as unknown as ReturnType<typeof provider.getCurrentTask>,
8406+
)
8407+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
8408+
provider["viewLocalState"].currentApiConfigName = "saved-profile"
8409+
// @ts-ignore - Replace providerSettingsManager with a test double.
8410+
provider.providerSettingsManager = {
8411+
saveConfig: vi.fn().mockResolvedValue("saved-id"),
8412+
listConfig: vi.fn().mockResolvedValue([]),
8413+
setModeConfig: vi.fn(),
8414+
}
8415+
8416+
await provider.upsertProviderProfile(
8417+
"saved-profile",
8418+
{ apiProvider: providerIdentifiers.anthropic, apiKey: "new-key" },
8419+
false,
8420+
)
8421+
8422+
expect(provider["viewLocalState"].apiConfiguration).toEqual({
8423+
apiProvider: providerIdentifiers.anthropic,
8424+
apiKey: "new-key",
8425+
})
8426+
expect(task.updateApiConfiguration).not.toHaveBeenCalled()
8427+
})
83328428
it("does not leak shared provider keys into a pinned view's api configuration", async () => {
83338429
vi.spyOn(console, "log").mockImplementation(() => {})
83348430
vi.spyOn(console, "warn").mockImplementation(() => {})

‎src/core/webview/__tests__/ClineProvider.sticky-mode.spec.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,10 +1579,10 @@ describe("ClineProvider - Sticky Mode", () => {
15791579
it("does not resolve a view id that names an Object.prototype key through the chain", async () => {
15801580
await provider.resolveWebviewView(mockWebviewView)
15811581

1582-
mockContext.globalState.get = vi.fn().mockImplementation((key: string) => {
1583-
if (key === "viewStates") return { "stable-test-view": { mode: "architect" } }
1584-
return undefined
1585-
})
1582+
// Seed through the durable write path so later writes land in the same
1583+
// backing map the assertions read: a frozen get() literal would make every
1584+
// post-write assertion read the seed no matter what the provider wrote.
1585+
await provider.contextProxy.setValue("viewStates", { "stable-test-view": { mode: "architect" } })
15861586

15871587
const states = provider["getPersistedViewStates"]({ fresh: true })
15881588
expect(Object.getPrototypeOf(states)).toBeNull()
@@ -1673,10 +1673,10 @@ describe("ClineProvider - Sticky Mode", () => {
16731673
// Temporary ids restart from 0 on every host start, so the same name can
16741674
// belong to a different session. An entry this instance never wrote must not
16751675
// be adopted as this view's state.
1676-
mockContext.globalState.get = vi.fn().mockImplementation((key: string) => {
1677-
if (key === "viewStates") return { [provider.viewId]: { mode: "architect" } }
1678-
return undefined
1679-
})
1676+
// Seed through the durable write path so the assertions below read the
1677+
// same backing map a re-key adoption would write to: a frozen get()
1678+
// literal made the negative assertion unable to fail.
1679+
await provider.contextProxy.setValue("viewStates", { [provider.viewId]: { mode: "architect" } })
16801680

16811681
await provider["setViewStateId"]("stable-test-view")
16821682

0 commit comments

Comments
 (0)