Skip to content

The agent that grows with you

fix(desktop): drop unsaved credential edits when the settings target changes

DavidMetcalfe6201a8236fd5
diff --git a/apps/desktop/src/app/settings/env-credentials.tsx b/apps/desktop/src/app/settings/env-credentials.tsx
index 490d08c0660..8be974dc680 100644
--- a/apps/desktop/src/app/settings/env-credentials.tsx
+++ b/apps/desktop/src/app/settings/env-credentials.tsx
@@ -68,7 +68,14 @@ export function useEnvCredentials(profile?: string): UseEnvCredentials {
useEffect(() => {
let cancelled = false
+ // Everything keyed by var name is dropped together with the reload: the
+ // cached `vars`, plus any in-flight edit or revealed value. Those maps are
+ // keyed by name alone, so leaving a draft behind after the target profile
+ // changed left its Save button live — writing the value into the profile
+ // now being targeted instead of the one it was typed for.
setVars(null)
+ setEdits({})
+ setRevealed({})
void (async () => {
try {
diff --git a/apps/desktop/src/app/settings/keys-settings.test.tsx b/apps/desktop/src/app/settings/keys-settings.test.tsx
index 699fd0e4a7f..4d3f4b0dc96 100644
--- a/apps/desktop/src/app/settings/keys-settings.test.tsx
+++ b/apps/desktop/src/app/settings/keys-settings.test.tsx
@@ -126,4 +126,55 @@ describe('KeysSettings', () => {
})
expect(screen.getByText('Crawl and extract websites.')).toBeTruthy()
})
+
+ it('drops an unsaved credential edit when the settings target switches profile', async () => {
+ // Regression: `vars` is re-fetched when the shared "Applies to" target
+ // changes, but the in-flight edit map was not reset with it. A value typed
+ // while targeting profile-b survived the switch to profile-c, where the
+ // still-live Save would persist it into the WRONG profile.
+ const { $settingsScopeOverride } = await import('@/store/settings-scope')
+
+ $settingsScopeOverride.set('profile-b')
+ getEnvVars.mockResolvedValue({
+ WIDGET_API_KEY: envVar('tool', { description: 'Widget key.', is_set: true, redacted_value: '••••••' })
+ })
+
+ try {
+ const { KeysSettings } = await import('./keys-settings')
+
+ const { container } = render(
+ <MemoryRouter initialEntries={['/settings']}>
+ <KeysSettings view="tools" />
+ </MemoryRouter>
+ )
+
+ expect(await screen.findByText('WIDGET')).toBeTruthy()
+ await waitFor(() => expect(getEnvVars).toHaveBeenCalledWith('profile-b'))
+
+ // Open the field and type a value without saving it.
+ fireEvent.focus(container.querySelector('input[readonly]') as HTMLInputElement)
+ fireEvent.change(container.querySelector('input[type="password"]') as HTMLInputElement, {
+ target: { value: 'typed-secret' }
+ })
+
+ expect(screen.getByDisplayValue('typed-secret')).toBeTruthy()
+
+ // Re-target Settings at another profile. This is where the leak
+ // manifested: the draft stayed live, so the (still-rendered) Save would
+ // dispatch it through setEnvVar against the NEW target.
+ await act(async () => {
+ $settingsScopeOverride.set('profile-c')
+ })
+ await waitFor(() => expect(getEnvVars).toHaveBeenCalledWith('profile-c'))
+
+ // The draft belonged to the previous target: it is gone, and so is the
+ // Save control that would have dispatched it — no path is left that can
+ // write the stale value into the profile now being targeted.
+ expect(screen.queryByDisplayValue('typed-secret')).toBeNull()
+ expect(screen.queryByRole('button', { name: 'Save' })).toBeNull()
+ } finally {
+ cleanup()
+ $settingsScopeOverride.set(null)
+ }
+ })
})