Skip to content

Commit df73920

Browse files
committed
fix(webview): refresh pinned views on non-activating saves
Port of the review fix landed on the get-state unit. 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. The overlay-completeness half of the finding does not apply to this unit: its getState() does not merge the view overlay. Proofs: ClineProvider.spec 258/258 (new test: pinned-sibling refresh on non-activating save; callback test updated to assert the upsert path); negative control: dropping the else-branch refresh reddens exactly the refresh test, restores byte-exact (hash match); sticky-mode 32/32, webviewMessageHandler 82/82; eslint --max-warnings=0 clean; tsc clean except the two known local telemetry-dist noise lines; prettier clean on touched files.
1 parent 29f4078 commit df73920

2 files changed

Lines changed: 106 additions & 6 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,6 +2421,15 @@ export class ClineProvider
24212421
await this.persistStickyProviderProfileToCurrentTask(name)
24222422
} else {
24232423
await this.updateGlobalState("listApiConfigMeta", await this.providerSettingsManager.listConfig())
2424+
2425+
// A save that does not activate still changes the profile's settings, and
2426+
// every view pinned to it buffers a full copy of them. Refresh those views
2427+
// (and this one when it is the pinned view) or their getState() keeps
2428+
// serving the settings captured before the save under the same name.
2429+
if (this.pinnedProfileName === name) {
2430+
await this._saveViewLocalStateFromMutation({ apiConfiguration: providerSettings })
2431+
}
2432+
await this.refreshViewLocalStateForUpdatedProfile(name, providerSettings)
24242433
}
24252434

24262435
await this.postStateToWebview()
@@ -2841,8 +2850,10 @@ export class ClineProvider
28412850
// picks up the new token immediately for the current task.
28422851
await this.upsertProviderProfile(entry.name, updated, true)
28432852
} else {
2844-
// Non-active profiles just need the token saved to disk.
2845-
await this.providerSettingsManager.saveConfig(entry.name, updated)
2853+
// Non-active profiles need the token saved to disk and the views pinned
2854+
// to them refreshed: a direct saveConfig leaves a pinned view serving the
2855+
// old token until it reloads.
2856+
await this.upsertProviderProfile(entry.name, updated, false)
28462857
}
28472858
}
28482859
}

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

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

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

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

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

@@ -2693,6 +2706,10 @@ describe("ClineProvider", () => {
26932706
}),
26942707
}
26952708
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
2709+
// The ownership check requires the writer to release the id before the
2710+
// next view adopts it; the durable entry it wrote stays in storage.
2711+
await writer.dispose()
2712+
26962713
await provider.contextProxy.setValue(
26972714
"viewStates",
26982715
mockContext.globalState.get<RooCodeSettings["viewStates"]>("viewStates"),
@@ -2705,7 +2722,6 @@ describe("ClineProvider", () => {
27052722
// The buffer must track the activated profile so getValues() agrees with the proxy.
27062723
expect(provider.getValues().currentApiConfigName).toBe("new-profile")
27072724
expect(provider.contextProxy.getValue("currentApiConfigName")).toBe("new-profile")
2708-
await writer.dispose()
27092725
await provider.dispose()
27102726
})
27112727

@@ -5768,6 +5784,11 @@ describe("getTelemetryProperties", () => {
57685784
const { CloudService } = await import("@roo-code/cloud")
57695785
const mockCloudService = {
57705786
isAuthenticated: vi.fn().mockReturnValue(true),
5787+
5788+
// Provider dispose detaches the settings listener; later tests construct
5789+
// and dispose providers against this surviving instance getter.
5790+
on: vi.fn(),
5791+
off: vi.fn(),
57715792
}
57725793

57735794
// Update the existing mock
@@ -5786,6 +5807,11 @@ describe("getTelemetryProperties", () => {
57865807
const { CloudService } = await import("@roo-code/cloud")
57875808
const mockCloudService = {
57885809
isAuthenticated: vi.fn().mockReturnValue(false),
5810+
5811+
// Provider dispose detaches the settings listener; later tests construct
5812+
// and dispose providers against this surviving instance getter.
5813+
on: vi.fn(),
5814+
off: vi.fn(),
57895815
}
57905816

57915817
// Update the existing mock
@@ -5827,6 +5853,11 @@ describe("getTelemetryProperties", () => {
58275853
isAuthenticated: vi.fn().mockImplementation(() => {
58285854
throw new Error("Authentication check error")
58295855
}),
5856+
5857+
// Provider dispose detaches the settings listener; later tests construct
5858+
// and dispose providers against this surviving instance getter.
5859+
on: vi.fn(),
5860+
off: vi.fn(),
58305861
}
58315862

58325863
// Update the existing mock
@@ -7331,12 +7362,15 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
73317362
}),
73327363
true,
73337364
)
7334-
expect(saveConfig).toHaveBeenCalledWith(
7365+
// The non-active profile now goes through upsertProviderProfile so the views
7366+
// pinned to it are refreshed, not just the file on disk.
7367+
expect(upsertSpy).toHaveBeenCalledWith(
73357368
"Backup Zoo",
73367369
expect.objectContaining({
73377370
zooSessionToken: "new-token",
73387371
zooGatewayBaseUrl: "https://www.zoocode.dev/api/gateway/v1",
73397372
}),
7373+
false,
73407374
)
73417375
})
73427376

@@ -7433,4 +7467,59 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
74337467
})
74347468
})
74357469
})
7470+
describe("profile save without activation and overlay completeness", () => {
7471+
it("refreshes views pinned to a profile saved without activation", async () => {
7472+
// Defensive: the doubles in this suite can leave console writes pending
7473+
// at worker teardown.
7474+
vi.spyOn(console, "log").mockImplementation(() => {})
7475+
vi.spyOn(console, "warn").mockImplementation(() => {})
7476+
vi.spyOn(console, "error").mockImplementation(() => {})
7477+
const sibling = new ClineProvider(
7478+
mockContext,
7479+
mockOutputChannel,
7480+
"editor",
7481+
new ContextProxy(mockContext),
7482+
new WebviewFocusTracker(),
7483+
)
7484+
try {
7485+
// The sibling loaded the profile earlier: its buffer pins the name and
7486+
// carries the settings snapshot taken at that load.
7487+
sibling["viewLocalState"].currentApiConfigName = "saved-profile"
7488+
sibling["viewLocalState"].apiConfiguration = {
7489+
apiProvider: providerIdentifiers.anthropic,
7490+
apiKey: "old-key",
7491+
}
7492+
vi.spyOn(sibling, "postStateToWebview").mockResolvedValue(undefined)
7493+
vi.spyOn(provider, "postStateToWebview").mockResolvedValue(undefined)
7494+
// @ts-ignore - Replace providerSettingsManager with a test double.
7495+
provider.providerSettingsManager = {
7496+
saveConfig: vi.fn().mockResolvedValue("saved-id"),
7497+
listConfig: vi.fn().mockResolvedValue([]),
7498+
setModeConfig: vi.fn(),
7499+
getProfile: vi
7500+
.fn()
7501+
.mockResolvedValue({ name: "saved-profile", apiProvider: providerIdentifiers.anthropic }),
7502+
getModeConfigId: vi.fn().mockResolvedValue(undefined),
7503+
}
7504+
7505+
await provider.upsertProviderProfile(
7506+
"saved-profile",
7507+
{ apiProvider: providerIdentifiers.anthropic, apiKey: "new-key" },
7508+
false,
7509+
)
7510+
7511+
// A save that does not activate still rewrites the profile; the pinned
7512+
// sibling must not keep serving the pre-save snapshot.
7513+
expect(sibling["viewLocalState"].apiConfiguration).toEqual({
7514+
apiProvider: providerIdentifiers.anthropic,
7515+
apiKey: "new-key",
7516+
})
7517+
// Let queued view-local persistence settle while the console spies are
7518+
// still installed, so teardown has no pending writes.
7519+
await new Promise((resolve) => setTimeout(resolve, 50))
7520+
} finally {
7521+
await sibling.dispose()
7522+
}
7523+
})
7524+
})
74367525
})

0 commit comments

Comments
 (0)