Skip to content

Commit 73c0b90

Browse files
committed
fix(webview): refresh pinned views on non-activating saves; treat the view overlay as a complete profile
Port of the review fixes landed on the get-state unit: 1. A save that does not activate (upsertProviderProfile with activate=false, and the zoo-gateway callback's non-active profile path) rewrote the profile's settings on disk but left every view pinned to it serving the pre-save snapshot from its buffer. The non-activating branch now refreshes pinned sibling views (and this view when it holds the pin), and the callback routes non-active profiles through upsertProviderProfile instead of a bare saveConfig so the refresh cannot be forgotten at that call site. 2. getState() spread the pinned overlay over the shared provider settings, so any key the pinned profile never defined leaked in from whatever profile the shared store points at (another profile's model id, region, tuning). The overlay is a complete profile snapshot: it is now used in place, with only apiProvider falling back to the shared state when the profile omits one. Proofs: ClineProvider.spec 294/294 (2 new tests: pinned-sibling refresh on non-activating save; no shared-key leak through the overlay; callback test updated to assert the upsert path); negative controls: dropping the else-branch refresh reddens exactly the refresh test, reverting the merge semantics reddens exactly the leak test, restores byte-exact (hash match); sticky-mode 33/33, webviewMessageHandler 89/89; eslint --max-warnings=0 clean; tsc clean except the two known local telemetry-dist noise lines; prettier clean on touched files.
1 parent 7e649e9 commit 73c0b90

2 files changed

Lines changed: 146 additions & 10 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,6 +2490,15 @@ export class ClineProvider
24902490
// applies only mode, currentApiConfigName, apiConfiguration and provider-settings keys, so a
24912491
// listApiConfigMeta-only payload (deleted call or empty object) is a behavior-preserving no-op.
24922492
this._updateViewLocalStateFromMutation({ listApiConfigMeta })
2493+
2494+
// A save that does not activate still changes the profile's settings, and
2495+
// every view pinned to it buffers a full copy of them. Refresh those views
2496+
// (and this one when it is the pinned view) or their getState() keeps
2497+
// serving the settings captured before the save under the same name.
2498+
if (this.pinnedProfileName === name) {
2499+
await this._saveViewLocalStateFromMutation({ apiConfiguration: providerSettings })
2500+
}
2501+
await this.refreshViewLocalStateForUpdatedProfile(name, providerSettings)
24932502
}
24942503

24952504
await this.postStateToWebview()
@@ -3007,8 +3016,10 @@ export class ClineProvider
30073016
// picks up the new token immediately for the current task.
30083017
await this.upsertProviderProfile(entry.name, updated, true)
30093018
} else {
3010-
// Non-active profiles just need the token saved to disk.
3011-
await this.providerSettingsManager.saveConfig(entry.name, updated)
3019+
// Non-active profiles need the token saved to disk and the views pinned
3020+
// to them refreshed: a direct saveConfig leaves a pinned view serving the
3021+
// old token until it reloads.
3022+
await this.upsertProviderProfile(entry.name, updated, false)
30123023
}
30133024
}
30143025
}
@@ -3821,11 +3832,21 @@ export class ClineProvider
38213832
const taskSyncEnabled: boolean = false
38223833

38233834
// Return the same structure as before.
3835+
// A view-local profile is a complete snapshot, not a patch over the shared store.
3836+
// Spreading it on top of providerSettings would let a key the profile never defined
3837+
// leak in from another profile, which ContextProxy.setProviderSettings deliberately
3838+
// prevents by clearing omitted keys. Use the view's own settings in place and take
3839+
// only apiProvider from the shared state when the profile does not carry one.
3840+
const viewApiConfiguration = mergedStateValues.apiConfiguration
3841+
const resolvedApiConfiguration = viewApiConfiguration
3842+
? {
3843+
...viewApiConfiguration,
3844+
apiProvider: viewApiConfiguration.apiProvider ?? providerSettings.apiProvider,
3845+
}
3846+
: providerSettings
3847+
38243848
return {
3825-
apiConfiguration: {
3826-
...providerSettings,
3827-
...mergedStateValues.apiConfiguration,
3828-
},
3849+
apiConfiguration: resolvedApiConfiguration,
38293850
lastShownAnnouncementId: mergedStateValues.lastShownAnnouncementId,
38303851
customInstructions: mergedStateValues.customInstructions,
38313852
apiModelId: mergedStateValues.apiModelId,

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

Lines changed: 119 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,13 @@ describe("ClineProvider", () => {
500500
let mockPostMessage: any
501501
let updateGlobalStateSpy: any
502502

503+
afterEach(() => {
504+
// Tests construct providers directly and many never dispose them; clear the
505+
// static registry so a leaked instance cannot claim stable view state ids
506+
// ahead of a later test.
507+
ClineProvider["activeInstances"] = new Set()
508+
})
509+
503510
beforeEach(() => {
504511
vi.clearAllMocks()
505512

@@ -2129,6 +2136,10 @@ describe("ClineProvider", () => {
21292136
})
21302137
// @ts-ignore - Replace providerSettingsManager with a test double for the profile lookup.
21312138
provider.providerSettingsManager = { getProfile: getProfileSpy }
2139+
// The ownership check requires the writer to release the id before the
2140+
// next view adopts it; the durable entry it wrote stays in storage.
2141+
await writer.dispose()
2142+
21322143
await provider.contextProxy.setValue(
21332144
"viewStates",
21342145
mockContext.globalState.get<RooCodeSettings["viewStates"]>("viewStates"),
@@ -2141,7 +2152,6 @@ describe("ClineProvider", () => {
21412152
})
21422153
expect(getProfileSpy).toHaveBeenCalledWith({ name: "my-profile" })
21432154
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("Loaded state for viewId"))
2144-
await writer.dispose()
21452155
await provider.dispose()
21462156
})
21472157

@@ -2269,6 +2279,10 @@ describe("ClineProvider", () => {
22692279
}
22702280
// A pre-load buffer write that was never persisted must not be merged over the load.
22712281
provider["viewLocalState"] = { mode: "architect" }
2282+
// The ownership check requires the writer to release the id before the
2283+
// next view adopts it; the durable entry it wrote stays in storage.
2284+
await writer.dispose()
2285+
22722286
await provider.contextProxy.setValue(
22732287
"viewStates",
22742288
mockContext.globalState.get<RooCodeSettings["viewStates"]>("viewStates"),
@@ -2280,7 +2294,6 @@ describe("ClineProvider", () => {
22802294
apiProvider: providerIdentifiers.openrouter,
22812295
openRouterModelId: "model-x",
22822296
})
2283-
await writer.dispose()
22842297
await provider.dispose()
22852298
})
22862299

@@ -2710,6 +2723,10 @@ describe("ClineProvider", () => {
27102723
}),
27112724
}
27122725
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
2726+
// The ownership check requires the writer to release the id before the
2727+
// next view adopts it; the durable entry it wrote stays in storage.
2728+
await writer.dispose()
2729+
27132730
await provider.contextProxy.setValue(
27142731
"viewStates",
27152732
mockContext.globalState.get<RooCodeSettings["viewStates"]>("viewStates"),
@@ -2722,7 +2739,6 @@ describe("ClineProvider", () => {
27222739
// The buffer must track the activated profile so getValues() agrees with the proxy.
27232740
expect(provider.getValues().currentApiConfigName).toBe("new-profile")
27242741
expect(provider.contextProxy.getValue("currentApiConfigName")).toBe("new-profile")
2725-
await writer.dispose()
27262742
await provider.dispose()
27272743
})
27282744

@@ -6774,6 +6790,11 @@ describe("getTelemetryProperties", () => {
67746790
const { CloudService } = await import("@roo-code/cloud")
67756791
const mockCloudService = {
67766792
isAuthenticated: vi.fn().mockReturnValue(true),
6793+
6794+
// Provider dispose detaches the settings listener; later tests construct
6795+
// and dispose providers against this surviving instance getter.
6796+
on: vi.fn(),
6797+
off: vi.fn(),
67776798
}
67786799

67796800
// Update the existing mock
@@ -6792,6 +6813,11 @@ describe("getTelemetryProperties", () => {
67926813
const { CloudService } = await import("@roo-code/cloud")
67936814
const mockCloudService = {
67946815
isAuthenticated: vi.fn().mockReturnValue(false),
6816+
6817+
// Provider dispose detaches the settings listener; later tests construct
6818+
// and dispose providers against this surviving instance getter.
6819+
on: vi.fn(),
6820+
off: vi.fn(),
67956821
}
67966822

67976823
// Update the existing mock
@@ -6833,6 +6859,11 @@ describe("getTelemetryProperties", () => {
68336859
isAuthenticated: vi.fn().mockImplementation(() => {
68346860
throw new Error("Authentication check error")
68356861
}),
6862+
6863+
// Provider dispose detaches the settings listener; later tests construct
6864+
// and dispose providers against this surviving instance getter.
6865+
on: vi.fn(),
6866+
off: vi.fn(),
68366867
}
68376868

68386869
// Update the existing mock
@@ -8337,12 +8368,15 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
83378368
}),
83388369
true,
83398370
)
8340-
expect(saveConfig).toHaveBeenCalledWith(
8371+
// The non-active profile now goes through upsertProviderProfile so the views
8372+
// pinned to it are refreshed, not just the file on disk.
8373+
expect(upsertSpy).toHaveBeenCalledWith(
83418374
"Backup Zoo",
83428375
expect.objectContaining({
83438376
zooSessionToken: "new-token",
83448377
zooGatewayBaseUrl: "https://www.zoocode.dev/api/gateway/v1",
83458378
}),
8379+
false,
83468380
)
83478381
})
83488382

@@ -8439,4 +8473,85 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
84398473
})
84408474
})
84418475
})
8476+
describe("profile save without activation and overlay completeness", () => {
8477+
it("refreshes views pinned to a profile saved without activation", async () => {
8478+
// Defensive: the doubles in this suite can leave console writes pending
8479+
// at worker teardown.
8480+
vi.spyOn(console, "log").mockImplementation(() => {})
8481+
vi.spyOn(console, "warn").mockImplementation(() => {})
8482+
vi.spyOn(console, "error").mockImplementation(() => {})
8483+
const sibling = new ClineProvider(
8484+
mockContext,
8485+
mockOutputChannel,
8486+
"editor",
8487+
new ContextProxy(mockContext),
8488+
new WebviewFocusTracker(),
8489+
)
8490+
try {
8491+
// The sibling loaded the profile earlier: its buffer pins the name and
8492+
// carries the settings snapshot taken at that load.
8493+
sibling["viewLocalState"].currentApiConfigName = "saved-profile"
8494+
sibling["viewLocalState"].apiConfiguration = {
8495+
apiProvider: providerIdentifiers.anthropic,
8496+
apiKey: "old-key",
8497+
}
8498+
vi.spyOn(sibling, "postStateToWebview").mockResolvedValue(undefined)
8499+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
8500+
// @ts-ignore - Replace providerSettingsManager with a test double.
8501+
provider.providerSettingsManager = {
8502+
saveConfig: vi.fn().mockResolvedValue("saved-id"),
8503+
listConfig: vi.fn().mockResolvedValue([]),
8504+
setModeConfig: vi.fn(),
8505+
}
8506+
8507+
await provider.upsertProviderProfile(
8508+
"saved-profile",
8509+
{ apiProvider: providerIdentifiers.anthropic, apiKey: "new-key" },
8510+
false,
8511+
)
8512+
8513+
// A save that does not activate still rewrites the profile; the pinned
8514+
// sibling must not keep serving the pre-save snapshot.
8515+
expect(sibling["viewLocalState"].apiConfiguration).toEqual({
8516+
apiProvider: providerIdentifiers.anthropic,
8517+
apiKey: "new-key",
8518+
})
8519+
// Let queued view-local persistence settle while the console spies are
8520+
// still installed, so teardown has no pending writes.
8521+
await new Promise((resolve) => setTimeout(resolve, 50))
8522+
} finally {
8523+
await sibling.dispose()
8524+
}
8525+
})
8526+
8527+
it("does not leak shared provider keys into a pinned view's api configuration", async () => {
8528+
vi.spyOn(console, "log").mockImplementation(() => {})
8529+
vi.spyOn(console, "warn").mockImplementation(() => {})
8530+
// The shared store sits on a profile with keys the pinned profile never
8531+
// sets; spreading the overlay over the shared keys would report a mixed
8532+
// configuration (e.g. another profile's model id and region).
8533+
await provider.contextProxy.setValue("apiProvider", providerIdentifiers.bedrock)
8534+
await provider.contextProxy.setValue("apiModelId", "anthropic.claude-v1")
8535+
await provider.contextProxy.setValue("awsRegion", "us-east-1")
8536+
provider["viewLocalState"].currentApiConfigName = "pinned-profile"
8537+
provider["viewLocalState"].apiConfiguration = {
8538+
apiProvider: providerIdentifiers.anthropic,
8539+
apiKey: "k",
8540+
}
8541+
8542+
// The cloud doubles in this suite make getState() log its defensive catch
8543+
// branches; silence them so teardown has no pending console writes.
8544+
vi.spyOn(console, "error").mockImplementation(() => {})
8545+
8546+
const state = await provider.getState()
8547+
8548+
expect(state.apiConfiguration.apiProvider).toBe(providerIdentifiers.anthropic)
8549+
expect(state.apiConfiguration.apiKey).toBe("k")
8550+
expect(state.apiConfiguration.apiModelId).toBeUndefined()
8551+
expect(state.apiConfiguration.awsRegion).toBeUndefined()
8552+
8553+
// Let any deferred logging settle while the spies are installed.
8554+
await new Promise((resolve) => setTimeout(resolve, 50))
8555+
})
8556+
})
84428557
})

0 commit comments

Comments
 (0)