Skip to content

Commit 8b77195

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 7188324 commit 8b77195

3 files changed

Lines changed: 121 additions & 14 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,7 +2428,13 @@ export class ClineProvider
24282428
// serving the settings captured before the save under the same name.
24292429
if (this.pinnedProfileName === name) {
24302430
await this._saveViewLocalStateFromMutation({ apiConfiguration: providerSettings })
2431-
this.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
2431+
// Only rebuild when the task actually runs under the saved profile: a
2432+
// view pinned to this profile can hold a task that follows a different
2433+
// sticky profile, and the forced rebuild would silently switch that
2434+
// conversation to the saved profile's settings.
2435+
if (this.getCurrentTask()?.taskApiConfigName === name) {
2436+
this.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
2437+
}
24322438
}
24332439
await this.refreshViewLocalStateForUpdatedProfile(name, providerSettings)
24342440
}
@@ -2678,9 +2684,13 @@ export class ClineProvider
26782684
// The active task of a pinned view holds its own API handler built from
26792685
// the profile's settings: rebuild it with the saved settings or the
26802686
// running conversation keeps sending the old token even though the
2681-
// view's buffer has caught up. Direct private access across sibling
2682-
// instances of the same class is compile-time safe.
2683-
instance.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
2687+
// view's buffer has caught up. Only rebuild when the task actually runs
2688+
// under the saved profile: a task that follows a different sticky
2689+
// profile must keep its own settings. Direct private access across
2690+
// sibling instances of the same class is compile-time safe.
2691+
if (instance.getCurrentTask()?.taskApiConfigName === name) {
2692+
instance.updateTaskApiHandlerIfNeeded(providerSettings, { forceRebuild: true })
2693+
}
26842694
await instance.postStateToWebview()
26852695
}),
26862696
)

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

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

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

18771877
await provider.dispose()
@@ -7496,6 +7496,7 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
74967496
// minimal double without the full provider/services graph.
74977497
const siblingTask = {
74987498
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "old-key" },
7499+
taskApiConfigName: "saved-profile",
74997500
updateApiConfiguration: vi.fn(),
75007501
}
75017502
vi.spyOn(sibling, "getCurrentTask").mockReturnValue(
@@ -7539,5 +7540,101 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
75397540
await sibling.dispose()
75407541
}
75417542
})
7543+
7544+
it("does not rebuild a pinned sibling's task that runs under another profile", async () => {
7545+
// Defensive: the doubles in this suite can leave console writes pending
7546+
// at worker teardown.
7547+
vi.spyOn(console, "log").mockImplementation(() => {})
7548+
vi.spyOn(console, "warn").mockImplementation(() => {})
7549+
vi.spyOn(console, "error").mockImplementation(() => {})
7550+
const sibling = new ClineProvider(
7551+
mockContext,
7552+
mockOutputChannel,
7553+
"editor",
7554+
new ContextProxy(mockContext),
7555+
new WebviewFocusTracker(),
7556+
)
7557+
try {
7558+
// The sibling pins the saved profile, but its active task follows a
7559+
// different sticky profile: the save must refresh the view overlay
7560+
// without switching that conversation to the saved profile's settings.
7561+
sibling["viewLocalState"].currentApiConfigName = "saved-profile"
7562+
sibling["viewLocalState"].apiConfiguration = {
7563+
apiProvider: providerIdentifiers.anthropic,
7564+
apiKey: "old-key",
7565+
}
7566+
vi.spyOn(sibling, "postStateToWebview").mockResolvedValue(undefined)
7567+
7568+
const siblingTask = {
7569+
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "other-key" },
7570+
taskApiConfigName: "other-profile",
7571+
updateApiConfiguration: vi.fn(),
7572+
}
7573+
vi.spyOn(sibling, "getCurrentTask").mockReturnValue(
7574+
siblingTask as unknown as ReturnType<typeof sibling.getCurrentTask>,
7575+
)
7576+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
7577+
// @ts-ignore - Replace providerSettingsManager with a test double.
7578+
provider.providerSettingsManager = {
7579+
saveConfig: vi.fn().mockResolvedValue("saved-id"),
7580+
listConfig: vi.fn().mockResolvedValue([]),
7581+
setModeConfig: vi.fn(),
7582+
}
7583+
7584+
await provider.upsertProviderProfile(
7585+
"saved-profile",
7586+
{ apiProvider: providerIdentifiers.anthropic, apiKey: "new-key" },
7587+
false,
7588+
)
7589+
7590+
// The pinned view's overlay still catches up with the saved settings...
7591+
expect(sibling["viewLocalState"].apiConfiguration).toEqual({
7592+
apiProvider: providerIdentifiers.anthropic,
7593+
apiKey: "new-key",
7594+
})
7595+
// ...but the task runs under another profile: its handler must not be
7596+
// switched to the saved profile's settings.
7597+
expect(siblingTask.updateApiConfiguration).not.toHaveBeenCalled()
7598+
// Let queued view-local persistence settle while the console spies are
7599+
// still installed, so teardown has no pending writes.
7600+
await new Promise((resolve) => setTimeout(resolve, 50))
7601+
} finally {
7602+
await sibling.dispose()
7603+
}
7604+
})
7605+
7606+
it("does not rebuild the acting view's task when it runs under another profile", async () => {
7607+
// The acting view pins the saved profile while its own task follows a
7608+
// different sticky profile: the non-activating save refreshes the view
7609+
// overlay but must leave the task's handler alone.
7610+
const task = {
7611+
apiConfiguration: { apiProvider: providerIdentifiers.anthropic, apiKey: "other-key" },
7612+
taskApiConfigName: "other-profile",
7613+
updateApiConfiguration: vi.fn(),
7614+
}
7615+
vi.spyOn(provider, "getCurrentTask").mockReturnValue(
7616+
task as unknown as ReturnType<typeof provider.getCurrentTask>,
7617+
)
7618+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
7619+
provider["viewLocalState"].currentApiConfigName = "saved-profile"
7620+
// @ts-ignore - Replace providerSettingsManager with a test double.
7621+
provider.providerSettingsManager = {
7622+
saveConfig: vi.fn().mockResolvedValue("saved-id"),
7623+
listConfig: vi.fn().mockResolvedValue([]),
7624+
setModeConfig: vi.fn(),
7625+
}
7626+
7627+
await provider.upsertProviderProfile(
7628+
"saved-profile",
7629+
{ apiProvider: providerIdentifiers.anthropic, apiKey: "new-key" },
7630+
false,
7631+
)
7632+
7633+
expect(provider["viewLocalState"].apiConfiguration).toEqual({
7634+
apiProvider: providerIdentifiers.anthropic,
7635+
apiKey: "new-key",
7636+
})
7637+
expect(task.updateApiConfiguration).not.toHaveBeenCalled()
7638+
})
75427639
})
75437640
})

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

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

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

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

861861
await provider["setViewStateId"]("stable-test-view")
862862

0 commit comments

Comments
 (0)