Skip to content

Commit 3414aa6

Browse files
easonLiangWorldedtechDSH
andcommitted
fix(extension): hand siblings the profile record that landed
setConfiguration propagated the payload to the provider for the sibling profile refresh, but saveConfig normalizes before it stores: the profile schema drops the keys it does not know and can rewrite a legacy provider on the way in. An idle sibling rebuilds its overlay from the settings the caller reports, so it could keep selecting a provider the saved record no longer holds and fail to create a task with it. setValues now accepts the record that landed (savedProfileSettings) and refreshes pinned siblings from it, keeping the payload-derived subset as the fallback for callers that save through setValues without reporting the stored record. The compensation restore is now compare-and-swap on the record id as well as on its settings. saveConfig keeps an existing id when it overwrites, so identical settings under a different id can only come from a writer that replaced the record; the prior record is kept rather than restored over that writer, which is the conservative direction for an import that replaced the profile in place. Co-authored-by: DSH <[email protected]>
1 parent 82f44f8 commit 3414aa6

3 files changed

Lines changed: 137 additions & 10 deletions

File tree

‎src/core/webview/ClineProvider.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5521,7 +5521,10 @@ export class ClineProvider
55215521
return { ...this.contextProxy.getValues(), ...publicOverlay }
55225522
}
55235523

5524-
public async setValues(values: RooCodeSettings, options?: { savedProfileName?: string }) {
5524+
public async setValues(
5525+
values: RooCodeSettings,
5526+
options?: { savedProfileName?: string; savedProfileSettings?: ProviderSettings },
5527+
) {
55255528
const sanitizedValues = withoutInvalidDisplaySettings(values, (key) =>
55265529
this.log(`[setValues] Ignored an invalid value for ${key}`),
55275530
)
@@ -5589,11 +5592,18 @@ export class ClineProvider
55895592
// Siblings pinned to the profile this payload saves: the shared store now
55905593
// carries the saved settings; refresh their overlays so they serve the
55915594
// fresh copy instead of a pre-save snapshot.
5592-
const savedSettings = Object.fromEntries(
5593-
PROVIDER_SETTINGS_KEYS.map((key) => [key, sanitizedValues[key]]).filter(
5594-
([, value]) => value !== undefined,
5595-
),
5596-
) as ProviderSettings
5595+
// The landed record when the caller reports it: the profile schema can drop keys the
5596+
// payload carried (a legacy provider is rewritten on the way in), so an overlay built
5597+
// from the payload could select a provider the saved record no longer holds. The
5598+
// payload-derived subset stays as the fallback for callers that save through this
5599+
// method without reporting the stored record.
5600+
const savedSettings =
5601+
options.savedProfileSettings ??
5602+
(Object.fromEntries(
5603+
PROVIDER_SETTINGS_KEYS.map((key) => [key, sanitizedValues[key]]).filter(
5604+
([, value]) => value !== undefined,
5605+
),
5606+
) as ProviderSettings)
55975607
await this.refreshViewLocalStateForUpdatedProfile(options.savedProfileName, savedSettings)
55985608
}
55995609
}

‎src/extension/__tests__/api-configuration.spec.ts‎

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ describe("API - configuration", () => {
120120
}),
121121
// The payload saves this profile: siblings pinned to it are refreshed
122122
// from the payload instead of pinned to a pre-save snapshot.
123-
{ savedProfileName: "default" },
123+
{ savedProfileName: "default", savedProfileSettings: {} },
124124
)
125125
expect(setModeConfig).toHaveBeenCalledTimes(2)
126126
expect(setModeConfig).toHaveBeenCalledWith("code", "code-config")
@@ -730,7 +730,103 @@ describe("API - configuration", () => {
730730
})
731731
expect(logged()).toContain("after a failed read-back: another view wrote it (keys: openRouterModelId)")
732732
})
733+
it("restores the prior record only while it still carries the id this call wrote", async () => {
734+
const store = makeProfileStoreDouble({
735+
pinned: {
736+
id: "prior-id",
737+
settings: storedProfileSettingsFor({
738+
currentApiConfigName: "pinned",
739+
apiProvider: providerIdentifiers.openrouter,
740+
apiKey: "before",
741+
}),
742+
},
743+
})
744+
const { saveConfig, deleteConfig, hasConfig } = store
745+
// An import replaced the profile in place while this call held the name: the settings now match
746+
// what this call wrote, but the id is not the one this call holds. saveConfig keeps an existing
747+
// id when it overwrites, so a different id can only come from a writer that replaced the
748+
// record, and restoring our settings over it would leave the imported profile holding
749+
// pre-import settings.
750+
const prior = storedProfileSettingsFor({
751+
currentApiConfigName: "pinned",
752+
apiProvider: providerIdentifiers.openrouter,
753+
apiKey: "before",
754+
})
755+
const replacement = storedProfileSettingsFor({
756+
currentApiConfigName: "pinned",
757+
apiProvider: providerIdentifiers.openrouter,
758+
apiKey: "after",
759+
})
760+
let reads = 0
761+
const getProfile = vi.fn(({ name }: { name: string }) => {
762+
reads += 1
763+
if (reads === 1) {
764+
// The prior record this call snapshots before it writes.
765+
return Promise.resolve({ name, id: "prior-id", ...prior })
766+
}
767+
if (reads === 2) {
768+
return Promise.reject(new Error("secrets storage read failed"))
769+
}
770+
return Promise.resolve({ name, id: "imported-id", ...replacement })
771+
})
772+
const { api, logged, setValues } = makeReadbackHarness({
773+
saveConfig,
774+
deleteConfig,
775+
hasConfig,
776+
getProfile,
777+
})
778+
779+
await expect(
780+
api.setConfiguration({
781+
currentApiConfigName: "pinned",
782+
apiProvider: providerIdentifiers.openrouter,
783+
apiKey: "after",
784+
}),
785+
).rejects.toThrow("secrets storage read failed")
786+
787+
// One save: the initial write. Restoring the prior record would have been the second save, and
788+
// a delete would have thrown the imported profile away.
789+
expect(saveConfig.mock.calls.length).toBe(1)
790+
expect(deleteConfig.mock.calls).toEqual([])
791+
expect(setValues.mock.calls).toEqual([])
792+
expect(logged()).toContain("not this call 'prior-id'")
793+
})
733794

795+
it("reports the landed record, not the payload, for the sibling profile refresh", async () => {
796+
const store = makeProfileStoreDouble()
797+
const { saveConfig, getProfile, deleteConfig, hasConfig } = store
798+
const { api, setValues } = makeReadbackHarness({
799+
saveConfig,
800+
getProfile,
801+
deleteConfig,
802+
hasConfig,
803+
})
804+
// The record stores only the keys the profile schema knows, while a settings round trip also
805+
// carries the profile pin and host-owned keys. Sibling overlays are rebuilt from the settings
806+
// the caller reports, so those must be the record that landed: a provider the schema rewrote
807+
// on the way in would otherwise stay selected in an idle sibling that cannot create a task
808+
// with it. The recorded arguments come from a double-shaped mock, so reading them needs a
809+
// cast.
810+
type ProfileWriteOptions = {
811+
savedProfileName?: string
812+
savedProfileSettings?: Record<string, unknown>
813+
}
814+
const optionsOf = (call: unknown[]): ProfileWriteOptions | undefined =>
815+
call[1] as ProfileWriteOptions | undefined
816+
await api.setConfiguration({
817+
currentApiConfigName: "pinned",
818+
apiProvider: providerIdentifiers.openrouter,
819+
apiKey: "after",
820+
openRouterModelId: "gpt-4o",
821+
modeApiConfigs: { act: "other-profile" },
822+
})
823+
const call = setValues.mock.calls.find((args: unknown[]) => optionsOf(args)?.savedProfileName === "pinned")
824+
expect(call).toBeDefined()
825+
const reported = optionsOf(call ?? [])?.savedProfileSettings
826+
expect(reported).toEqual(store.entries.get("pinned")?.settings)
827+
expect(reported).not.toHaveProperty("currentApiConfigName")
828+
expect(reported).not.toHaveProperty("modeApiConfigs")
829+
})
734830
it("keeps the created record when the re-read matches the settings under a different id", async () => {
735831
const store = makeProfileStoreDouble()
736832
const { saveConfig, deleteConfig, hasConfig } = store
@@ -1744,7 +1840,12 @@ describe("API - configuration", () => {
17441840

17451841
// The settings write goes through the provider, which also carries the profile name the
17461842
// sibling refresh has to agree with.
1747-
expect(setValues).toHaveBeenCalledWith({ [key]: value }, { savedProfileName: "default" })
1843+
// The options object also carries the record that landed, which is what sibling overlays are
1844+
// rebuilt from. These payloads hold no profile keys, so the landed record is empty.
1845+
expect(setValues).toHaveBeenCalledWith(
1846+
{ [key]: value },
1847+
{ savedProfileName: "default", savedProfileSettings: {} },
1848+
)
17481849
expect(log).not.toHaveBeenCalled()
17491850
})
17501851

@@ -1775,7 +1876,10 @@ describe("API - configuration", () => {
17751876
// A hand-written value that the typed signature does not allow.
17761877
await api.setConfiguration({ [key]: value, soundEnabled: true } as unknown as RooCodeSettings)
17771878

1778-
expect(setValues).toHaveBeenCalledWith({ soundEnabled: true }, { savedProfileName: "default" })
1879+
expect(setValues).toHaveBeenCalledWith(
1880+
{ soundEnabled: true },
1881+
{ savedProfileName: "default", savedProfileSettings: {} },
1882+
)
17791883
expect(log).toHaveBeenCalledWith(`[setConfiguration] Ignored an invalid value for ${key}`)
17801884
})
17811885
})

‎src/extension/api.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,16 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
807807
`read-back: this call cannot normalize its payload to judge what the record holds, so the ` +
808808
`record is kept.`,
809809
)
810+
} else if (currentRecord.id !== id) {
811+
// Identical settings under a different id is a replacement, not our own write:
812+
// saveConfig keeps an existing id when it overwrites, so a new id can only come from
813+
// a writer that replaced the record. Keeping it is the conservative direction, and
814+
// it is what protects a profile an import replaced in place.
815+
this.log(
816+
`[setConfiguration] skipped restoring the prior profile record '${savedProfileName}' after a failed ` +
817+
`read-back: it now carries id '${currentRecord.id}', not this call '${id}', so another writer ` +
818+
`replaced it.`,
819+
)
810820
} else if (movedKeys.length > 0) {
811821
this.log(
812822
`[setConfiguration] skipped restoring the prior profile record '${savedProfileName}' after a failed ` +
@@ -857,7 +867,10 @@ export class API extends EventEmitter<RooCodeEvents> implements RooCodeAPI {
857867
await this.sidebarProvider.setValues(
858868
// Only the keys this call owns travel forward; see the filter comment above.
859869
forwardValues,
860-
{ savedProfileName },
870+
// The settings that landed, not the payload: saveConfig normalizes before it stores,
871+
// so a sibling overlay built from the payload could select a provider the saved
872+
// record no longer carries.
873+
{ savedProfileName, savedProfileSettings: savedSettings },
861874
)
862875
if (values.modeApiConfigs) {
863876
await Promise.all(

0 commit comments

Comments
 (0)