Skip to content

Commit afb629e

Browse files
committed
fix(core): derive a profile's secret keys from its provider as well as its blob
What: `getProfileSecretKeys` now returns the union of the keys the blob declares and the keys the profile's own `apiProvider` owns, both drawn from `PROVIDER_SECRET_KEYS`. Why: `api.getConfiguration()` strips secret fields, so a profile saved from that round trip declares no secret while its live credential is still in SecretStorage. Deleting that profile purged zero keys and left the credential behind indefinitely. Impact: a deleted profile's own credentials are removed even when its blob is secret-less. A provider with no ownership branch keeps the presence-based answer, and the surviving-profile keep-set is derived from the same candidate set, so it can only grow. Net 0 counted executable lines. Tests: three cases pin the union, the declared-key path, and the retired-provider fallback.
1 parent 3b2f073 commit afb629e

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎src/core/config/ContextProxy.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,23 @@ export const providerOwnsSecretKey = (apiProvider: unknown, key: string): boolea
9393
}
9494

9595
/**
96-
* The secret keys a stored profile blob declares.
96+
* The secret keys a stored profile uses.
9797
*
9898
* A profile blob is the record of which credentials that profile uses: `saveConfig`'s
9999
* discriminated parse keeps only the branch's own keys, so the keys present in the blob
100100
* are exactly the ones the profile owns. Used to purge a deleted profile's credentials
101101
* explicitly instead of inferring them from a later write.
102102
*
103+
* The blob alone is not enough: `api.getConfiguration()` strips secret fields, so a profile
104+
* saved from that round trip declares none while its live credential sits in SecretStorage.
105+
* The profile's own `apiProvider` still names the keys it owns, so the candidates are the
106+
* union of both. A provider with no ownership branch keeps the presence-based answer.
107+
*
103108
* Membership is `PROVIDER_SETTINGS_KEYS ∩ SECRET_STATE_KEYS`, the same set the clear-out
104109
* loop iterates: it excludes the global image-generation key, which no profile blob owns.
105110
*/
106111
export const getProfileSecretKeys = (config: Record<string, unknown>): string[] =>
107-
Object.keys(config).filter((key) => PROVIDER_SECRET_KEYS.has(key))
112+
[...PROVIDER_SECRET_KEYS].filter((key) => key in config || providerOwnsSecretKey(config.apiProvider, key) === true)
108113

109114
const globalSettingsExportSchema = globalSettingsSchema.omit({
110115
taskHistory: true,

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { GLOBAL_STATE_KEYS, SECRET_STATE_KEYS, GLOBAL_SECRET_KEYS } from "@roo-c
77
import { clearAllMocks } from "../../../test-utils/reset"
88
import { makeExtensionContext, makeUri } from "../../../test-utils/vscode"
99

10-
import { ContextProxy } from "../ContextProxy"
10+
import { ContextProxy, getProfileSecretKeys } from "../ContextProxy"
1111
import { providerIdentifiers, retiredProviderIdentifiers } from "@roo-code/types/provider-identifiers"
1212

1313
vi.mock("vscode", () => ({
@@ -576,6 +576,46 @@ describe("ContextProxy", () => {
576576
})
577577
})
578578

579+
describe("getProfileSecretKeys", () => {
580+
it("includes the keys the profile's own provider owns even when the blob omits them", async () => {
581+
// A profile written from an `api.getConfiguration()` snapshot has its secret fields
582+
// stripped while keeping `apiProvider`. Deriving the purge candidates from the blob alone
583+
// therefore declares no secret, so deleting that profile would leave its live
584+
// SecretStorage credential behind. Ownership answers what the omission cannot.
585+
expect(
586+
getProfileSecretKeys({
587+
name: "stripped-profile",
588+
apiProvider: providerIdentifiers.openrouter,
589+
openRouterModelId: "openai/gpt-4.1",
590+
}),
591+
).toEqual(["openRouterApiKey"])
592+
})
593+
594+
it("keeps a declared key and drops nothing the provider does not own", async () => {
595+
// The union must not turn into "every key the provider owns": a key the blob declares
596+
// stays, and a non-secret setting is never a candidate.
597+
expect(
598+
getProfileSecretKeys({
599+
apiProvider: providerIdentifiers.anthropic,
600+
apiKey: "declared-key",
601+
apiModelId: "claude-sonnet-4-5",
602+
}),
603+
).toEqual(["apiKey"])
604+
})
605+
606+
it("stays blob-only for a profile whose provider has no ownership branch", async () => {
607+
// A retired provider has no branch in the discriminated union, so ownership is unknown.
608+
// Guessing would either drop a declared key or invent one; the presence-based answer is
609+
// the only safe one.
610+
expect(
611+
getProfileSecretKeys({
612+
apiProvider: retiredProviderIdentifiers.groq,
613+
apiKey: "retired-key",
614+
}),
615+
).toEqual(["apiKey"])
616+
})
617+
})
618+
579619
describe("resetAllState", () => {
580620
it("should clear all in-memory caches", async () => {
581621
// Setup initial state in caches

0 commit comments

Comments
 (0)