Skip to content

Commit 1b2b008

Browse files
committed
fix(core): keep a provider's own secrets when a partial profile write omits them
What: - ContextProxy.setProviderSettings derives secret-key ownership from providerSettingsSchemaDiscriminated and treats a cached secret as stale only when the provider named by the write does not own it. - Adds three ContextProxy specs: the partial-write shape, the empty-write semantics, and per-key (not all-or-nothing) application of the rule. Why: The 257eb9c stale-secret purge classified a secret as stale whenever the incoming slice omitted it. Partial writes legitimately omit secrets: api.getConfiguration() strips them, so the e2e profile test's setConfiguration(priorConfiguration) then setActiveProfile("default") sequence rewrote the default profile from a secret-less snapshot and deleted openRouterApiKey from SecretStorage. Every task rehydrated afterwards ran without an API key and hung until its 30 s timeout, which is the deterministic 9/3 mocked-e2e failure. Impact: Fixes the deterministic e2e-mock failure. Preserves the secret-purge security intent: a genuine cross-provider switch still deletes the old provider's differently-named secret, because that key is owned by a different provider. Empty writes and the codebase-index carve-out are unchanged.
1 parent 3f0e581 commit 1b2b008

2 files changed

Lines changed: 126 additions & 2 deletions

File tree

‎src/core/config/ContextProxy.ts‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
type GlobalState,
1414
type RooCodeSettings,
1515
providerSettingsSchema,
16+
providerSettingsSchemaDiscriminated,
1617
globalSettingsSchema,
1718
codebaseIndexProviderSchema,
1819
isSecretStateKey,
@@ -39,6 +40,51 @@ export const isPassThroughStateKey = (key: string) => PASS_THROUGH_STATE_KEYS.in
3940
// delete the user's index credentials on every profile switch.
4041
const FLAT_ONLY_PROVIDER_SECRET_KEYS = new Set<string>(Object.keys(codebaseIndexProviderSchema.shape))
4142

43+
// Which provider owns each secret key, read from the same discriminated union that
44+
// `saveConfig` parses profiles with: a provider branch declares the keys its own
45+
// profile blob carries, so ownership is exactly "this key appears in this branch".
46+
// Every provider-owned secret key has exactly one owner (verified across all 37
47+
// branches); the codebase-index credentials have none, which is why they are the
48+
// FLAT_ONLY carve-out above.
49+
const PROVIDER_SECRET_KEY_OWNERS = new Map<string, Set<string>>()
50+
51+
for (const branch of providerSettingsSchemaDiscriminated.options) {
52+
const literal = branch.shape.apiProvider
53+
54+
// The union's last branch is the `apiProvider: undefined` default, which owns nothing.
55+
if (!("value" in literal) || typeof literal.value !== "string") continue
56+
57+
for (const key of Object.keys(branch.shape)) {
58+
if (!isSecretStateKey(key)) continue
59+
60+
const owners = PROVIDER_SECRET_KEY_OWNERS.get(key) ?? new Set<string>()
61+
owners.add(literal.value)
62+
PROVIDER_SECRET_KEY_OWNERS.set(key, owners)
63+
}
64+
}
65+
66+
/**
67+
* Whether the provider named by a write is the one that owns `key`.
68+
*
69+
* A partial write can legitimately omit a secret the writing provider still uses:
70+
* `api.getConfiguration()` strips secrets (`src/extension/api.ts`), so a
71+
* save-then-reactivate round trip rewrites a profile from a snapshot that has no
72+
* secret in it. Treating that omission as "this profile uses none of these" deleted
73+
* the live profile's credentials from SecretStorage and broke every task rehydrated
74+
* afterwards. Ownership answers the question the omission cannot.
75+
*
76+
* Returns `undefined` when ownership is unknown (no provider named, or a retired
77+
* provider, whose branch is not in the union), so callers keep the old
78+
* presence-based semantics instead of guessing.
79+
*/
80+
const providerOwnsSecretKey = (apiProvider: unknown, key: string): boolean | undefined => {
81+
if (typeof apiProvider !== "string" || !isProviderName(apiProvider)) {
82+
return undefined
83+
}
84+
85+
return PROVIDER_SECRET_KEY_OWNERS.get(key)?.has(apiProvider) ?? false
86+
}
87+
4288
const globalSettingsExportSchema = globalSettingsSchema.omit({
4389
taskHistory: true,
4490
listApiConfigMeta: true,
@@ -534,8 +580,23 @@ export class ContextProxy {
534580
// presence check. Without this a deleted profile's differently-named secrets
535581
// (`openRouterApiKey` when the replacement uses `apiKey`) survive the switch and
536582
// keep being merged back by `getValues`.
537-
const isStale = (key: string) =>
538-
isSecretStateKey(key) ? !!this.secretCache[key as SecretStateKey] : !!this.stateCache[key as GlobalStateKey]
583+
//
584+
// A secret the writing provider still owns is not stale, even when the write omits
585+
// it: partial writes are normal (`api.getConfiguration()` strips secrets, so a
586+
// save-then-reactivate round trip rewrites a profile from a secret-less snapshot).
587+
// Purging on omission alone deleted the live profile's credentials, and every task
588+
// rehydrated afterwards ran without an API key.
589+
const isStale = (key: string) => {
590+
if (!isSecretStateKey(key)) {
591+
return !!this.stateCache[key as GlobalStateKey]
592+
}
593+
594+
if (!this.secretCache[key as SecretStateKey]) {
595+
return false
596+
}
597+
598+
return providerOwnsSecretKey(values.apiProvider, key) !== true
599+
}
539600

540601
await this.setValues({
541602
...PROVIDER_SETTINGS_KEYS.filter((key) => !FLAT_ONLY_PROVIDER_SECRET_KEYS.has(key))

‎src/core/config/__tests__/ContextProxy.spec.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,31 @@ describe("ContextProxy", () => {
430430
expect(mockSecrets.store).toHaveBeenCalledWith("openRouterApiKey", "openrouter-secret")
431431
})
432432

433+
it("keeps a secret the writing provider still owns when a partial write omits it", async () => {
434+
// The e2e/API restore shape: `api.getConfiguration()` strips secrets, so a
435+
// save-then-reactivate round trip rewrites the active profile from a snapshot that
436+
// carries `apiProvider` plus the non-secret settings and no secret at all. Purging on
437+
// omission alone deleted `openRouterApiKey` from SecretStorage, and every task
438+
// rehydrated afterwards hung without an API key.
439+
await proxy.setProviderSettings({
440+
apiProvider: providerIdentifiers.openrouter,
441+
openRouterApiKey: "live-secret",
442+
openRouterModelId: "openai/gpt-4.1",
443+
})
444+
expect(proxy.getSecret("openRouterApiKey")).toBe("live-secret")
445+
446+
await proxy.setProviderSettings({
447+
apiProvider: providerIdentifiers.openrouter,
448+
openRouterModelId: "openai/gpt-4.1",
449+
})
450+
451+
// The provider named by the write is the one that owns `openRouterApiKey`, so the
452+
// omission is a partial write, not a declaration that the profile uses no secret.
453+
expect(proxy.getSecret("openRouterApiKey")).toBe("live-secret")
454+
expect(proxy.getValues().openRouterApiKey).toBe("live-secret")
455+
expect(mockSecrets.delete).not.toHaveBeenCalledWith("openRouterApiKey")
456+
})
457+
433458
it("keeps the global image-generation secret and codebase-index credentials", async () => {
434459
// Neither is owned by a provider profile: the image key is a global secret, and the
435460
// index credentials are written flat by the index settings handler.
@@ -446,6 +471,44 @@ describe("ContextProxy", () => {
446471
expect(mockSecrets.delete).not.toHaveBeenCalledWith("openRouterImageApiKey")
447472
expect(mockSecrets.delete).not.toHaveBeenCalledWith("codeIndexOpenAiKey")
448473
})
474+
475+
it("still purges every secret when the write names no provider", async () => {
476+
// Ownership is unknown without a provider, so the presence-based clear-out keeps its
477+
// old meaning: an empty write is a full reset, not a partial one.
478+
await proxy.setProviderSettings({
479+
apiProvider: providerIdentifiers.openrouter,
480+
openRouterApiKey: "live-secret",
481+
})
482+
await proxy.setProviderSettings({
483+
apiProvider: providerIdentifiers.anthropic,
484+
apiKey: "anthropic-secret",
485+
})
486+
487+
await proxy.setProviderSettings({})
488+
489+
expect(proxy.getSecret("openRouterApiKey")).toBeUndefined()
490+
expect(proxy.getSecret("apiKey")).toBeUndefined()
491+
expect(mockSecrets.delete).toHaveBeenCalledWith("openRouterApiKey")
492+
expect(mockSecrets.delete).toHaveBeenCalledWith("apiKey")
493+
})
494+
495+
it("applies the ownership rule per key, not to the whole secret set", async () => {
496+
// A partial write naming OpenRouter omits OpenRouter's own secret while a different
497+
// provider's secret is still cached. The owned key survives and the foreign one is
498+
// purged, so the rule is decided per key rather than as an all-or-nothing wipe.
499+
await proxy.storeSecret("apiKey", "anthropic-secret")
500+
await proxy.storeSecret("openRouterApiKey", "openrouter-secret")
501+
502+
await proxy.setProviderSettings({
503+
apiProvider: providerIdentifiers.openrouter,
504+
openRouterModelId: "openai/gpt-4.1",
505+
})
506+
507+
expect(proxy.getSecret("openRouterApiKey")).toBe("openrouter-secret")
508+
expect(proxy.getSecret("apiKey")).toBeUndefined()
509+
expect(mockSecrets.delete).toHaveBeenCalledWith("apiKey")
510+
expect(mockSecrets.delete).not.toHaveBeenCalledWith("openRouterApiKey")
511+
})
449512
})
450513

451514
describe("resetAllState", () => {

0 commit comments

Comments
 (0)