Skip to content

The agent that grows with you

fix(desktop): drop stale custom endpoint saves

Sun Haoyuan0eb0a6d78bca
diff --git a/apps/desktop/src/app/settings/custom-endpoints-settings.test.tsx b/apps/desktop/src/app/settings/custom-endpoints-settings.test.tsx
new file mode 100644
index 00000000000..a24aa436044
--- /dev/null
+++ b/apps/desktop/src/app/settings/custom-endpoints-settings.test.tsx
@@ -0,0 +1,87 @@
+// @vitest-environment jsdom
+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import type { CustomEndpointsResponse } from '@/types/hermes'
+
+const getCustomEndpoints = vi.fn()
+const saveCustomEndpoint = vi.fn()
+const notify = vi.fn()
+const notifyError = vi.fn()
+const triggerHaptic = vi.fn()
+
+vi.mock('@/hermes', () => ({
+ activateCustomEndpoint: vi.fn(),
+ deleteCustomEndpoint: vi.fn(),
+ getCustomEndpoints: (...args: unknown[]) => getCustomEndpoints(...args),
+ saveCustomEndpoint: (...args: unknown[]) => saveCustomEndpoint(...args),
+ validateCustomEndpoint: vi.fn()
+}))
+vi.mock('@/lib/haptics', () => ({ triggerHaptic: (...args: unknown[]) => triggerHaptic(...args) }))
+vi.mock('@/store/notifications', () => ({
+ notify: (...args: unknown[]) => notify(...args),
+ notifyError: (...args: unknown[]) => notifyError(...args)
+}))
+
+const emptyResponse: CustomEndpointsResponse = {
+ current: { base_url: '', model: '', provider: '' },
+ endpoints: []
+}
+
+const savedResponse: CustomEndpointsResponse = {
+ current: { base_url: 'http://profile-a.test/v1', model: 'model-a', provider: 'profile-a-endpoint' },
+ endpoints: [
+ {
+ base_url: 'http://profile-a.test/v1',
+ discover_models: true,
+ has_api_key: false,
+ id: 'profile-a-endpoint',
+ is_current: true,
+ model: 'model-a',
+ models: ['model-a'],
+ name: 'Profile A'
+ }
+ ],
+ id: 'profile-a-endpoint',
+ ok: true
+}
+
+afterEach(() => {
+ cleanup()
+ vi.clearAllMocks()
+})
+
+describe('CustomEndpointsSettings', () => {
+ it('drops a pending save completion after its profile-scoped view unmounts', async () => {
+ let resolveSave!: (value: CustomEndpointsResponse) => void
+ saveCustomEndpoint.mockReturnValue(new Promise(resolve => (resolveSave = resolve)))
+ getCustomEndpoints.mockResolvedValue(emptyResponse)
+ const onConfigSaved = vi.fn()
+ const onMainModelChanged = vi.fn()
+ const { CustomEndpointsSettings } = await import('./custom-endpoints-settings')
+
+ const view = render(
+ <CustomEndpointsSettings onConfigSaved={onConfigSaved} onMainModelChanged={onMainModelChanged} />
+ )
+
+ await screen.findByText('No custom endpoints')
+ fireEvent.change(screen.getByPlaceholderText('Axet Proxy'), { target: { value: 'Profile A' } })
+ fireEvent.change(screen.getByPlaceholderText('http://127.0.0.1:8081/v1'), {
+ target: { value: 'http://profile-a.test/v1' }
+ })
+ fireEvent.change(screen.getByPlaceholderText('gpt-5.4'), { target: { value: 'model-a' } })
+ fireEvent.click(screen.getByRole('button', { name: 'Save' }))
+ expect(saveCustomEndpoint).toHaveBeenCalledTimes(1)
+
+ // SettingsView keys ProvidersSettings by the selected profile, so a profile
+ // switch unmounts this instance while its already-routed write is pending.
+ view.unmount()
+ await act(async () => resolveSave(savedResponse))
+
+ expect(onMainModelChanged).not.toHaveBeenCalled()
+ expect(onConfigSaved).not.toHaveBeenCalled()
+ expect(triggerHaptic).not.toHaveBeenCalled()
+ expect(notify).not.toHaveBeenCalled()
+ expect(notifyError).not.toHaveBeenCalled()
+ })
+})
diff --git a/apps/desktop/src/app/settings/custom-endpoints-settings.tsx b/apps/desktop/src/app/settings/custom-endpoints-settings.tsx
index 4c48e57953d..0ce4123e4b4 100644
--- a/apps/desktop/src/app/settings/custom-endpoints-settings.tsx
+++ b/apps/desktop/src/app/settings/custom-endpoints-settings.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useState } from 'react'
+import { useEffect, useRef, useState } from 'react'
import { Button } from '@/components/ui/button'
import { Checkbox } from '@/components/ui/checkbox'
@@ -79,6 +79,7 @@ function toPayload(form: EndpointForm, models?: string[]): CustomEndpointUpdate
export function CustomEndpointsSettings({ onConfigSaved, onMainModelChanged }: CustomEndpointsSettingsProps) {
const { t } = useI18n()
+ const mounted = useRef(false)
const [loading, setLoading] = useState(true)
const [saving, setSaving] = useState(false)
const [testing, setTesting] = useState(false)
@@ -93,8 +94,10 @@ export function CustomEndpointsSettings({ onConfigSaved, onMainModelChanged }: C
setEndpoints(data.endpoints)
}
+ // eslint-disable-next-line no-restricted-syntax -- lifecycle guard drops stale async completions; it does not mirror an atom
useEffect(() => {
let cancelled = false
+ mounted.current = true
async function load() {
try {
@@ -124,6 +127,7 @@ export function CustomEndpointsSettings({ onConfigSaved, onMainModelChanged }: C
return () => {
cancelled = true
+ mounted.current = false
}
}, [])
@@ -131,6 +135,11 @@ export function CustomEndpointsSettings({ onConfigSaved, onMainModelChanged }: C
try {
setSaving(true)
const response = await saveCustomEndpoint(toPayload(form, discoveredModels))
+
+ if (!mounted.current) {
+ return
+ }
+
setEndpoints(response.endpoints)
const saved = response.endpoints.find(endpoint => endpoint.id === response.id)
@@ -147,9 +156,13 @@ export function CustomEndpointsSettings({ onConfigSaved, onMainModelChanged }: C
onConfigSaved?.()
notify({ kind: 'success', message: 'Custom endpoint saved.' })
} catch (err) {
- notifyError(err, 'Save failed')
+ if (mounted.current) {
+ notifyError(err, 'Save failed')
+ }
} finally {
- setSaving(false)
+ if (mounted.current) {
+ setSaving(false)
+ }
}
}