Skip to content

The agent that grows with you

fix(desktop): share the profile-switch freshness latch across font settings and MCP tab

teknium166f9f3692aba
diff --git a/apps/desktop/src/app/hooks/use-profile-switch-latch.test.ts b/apps/desktop/src/app/hooks/use-profile-switch-latch.test.ts
new file mode 100644
index 00000000000..3ef8f5bae88
--- /dev/null
+++ b/apps/desktop/src/app/hooks/use-profile-switch-latch.test.ts
@@ -0,0 +1,34 @@
+// @vitest-environment jsdom
+import { act, renderHook } from '@testing-library/react'
+import { describe, expect, it } from 'vitest'
+
+import { useProfileSwitchLatch } from './use-profile-switch-latch'
+
+describe('useProfileSwitchLatch', () => {
+ it('stays pending until the query stamp moves past the armed one', () => {
+ const { rerender, result } = renderHook(useProfileSwitchLatch, { initialProps: { dataUpdatedAt: 1 } })
+
+ expect(result.current.pending).toBe(false)
+
+ act(() => result.current.arm())
+ // Same object reference, same stamp: still the outgoing profile's record.
+ rerender({ dataUpdatedAt: 1 })
+ expect(result.current.pending).toBe(true)
+
+ rerender({ dataUpdatedAt: 2 })
+ expect(result.current.pending).toBe(false)
+ })
+
+ it('releases on a fresh error stamp when one is supplied', () => {
+ const { rerender, result } = renderHook(useProfileSwitchLatch, {
+ initialProps: { dataUpdatedAt: 1, errorUpdatedAt: 0 }
+ })
+
+ act(() => result.current.arm())
+ rerender({ dataUpdatedAt: 1, errorUpdatedAt: 0 })
+ expect(result.current.pending).toBe(true)
+
+ rerender({ dataUpdatedAt: 1, errorUpdatedAt: 5 })
+ expect(result.current.pending).toBe(false)
+ })
+})
diff --git a/apps/desktop/src/app/hooks/use-profile-switch-latch.ts b/apps/desktop/src/app/hooks/use-profile-switch-latch.ts
new file mode 100644
index 00000000000..cfa4d2bb45d
--- /dev/null
+++ b/apps/desktop/src/app/hooks/use-profile-switch-latch.ts
@@ -0,0 +1,27 @@
+import { useState } from 'react'
+
+interface QueryStamps {
+ dataUpdatedAt: number
+ errorUpdatedAt?: number
+}
+
+/**
+ * Freshness latch for a React Query record across a gateway-profile switch.
+ * `arm()` (call it from `useOnProfileSwitch`) remembers the stamps the query
+ * carries for the OUTGOING profile; `pending` stays true until the query
+ * resettles for the new profile — a fresh success bumps `dataUpdatedAt`, a
+ * fresh failure bumps `errorUpdatedAt` (when supplied). The timestamps are
+ * the freshness proof because React Query structurally shares results, so a
+ * profile with identical settings reuses the very same object reference.
+ */
+export function useProfileSwitchLatch({ dataUpdatedAt, errorUpdatedAt }: QueryStamps): {
+ arm: () => void
+ pending: boolean
+} {
+ const [stale, setStale] = useState<null | QueryStamps>(null)
+
+ return {
+ arm: () => setStale({ dataUpdatedAt, errorUpdatedAt }),
+ pending: stale !== null && dataUpdatedAt === stale.dataUpdatedAt && errorUpdatedAt === stale.errorUpdatedAt
+ }
+}
diff --git a/apps/desktop/src/app/settings/chat-font-setting.tsx b/apps/desktop/src/app/settings/chat-font-setting.tsx
index ad54c455f9f..c401b8ff888 100644
--- a/apps/desktop/src/app/settings/chat-font-setting.tsx
+++ b/apps/desktop/src/app/settings/chat-font-setting.tsx
@@ -10,6 +10,7 @@ import type { HermesConfigRecord } from '@/types/hermes'
import { setHermesConfigCache, useHermesConfigRecord } from '../hooks/use-config-record'
import { useOnProfileSwitch } from '../hooks/use-on-profile-switch'
+import { useProfileSwitchLatch } from '../hooks/use-profile-switch-latch'
import { getNested, setNested } from './helpers'
import { ListRow } from './primitives'
@@ -31,10 +32,10 @@ export function ChatFontSetting() {
const copy = t.settings.appearance
const { data: loadedConfig, dataUpdatedAt } = useHermesConfigRecord()
const [draft, setDraft] = useState<string | null>(null)
- // dataUpdatedAt of the record the previous profile was looking at; the seed
- // effect refuses to reseed while the query still carries that stamp. A
- // structurally-shared refetch keeps the object reference but bumps the stamp.
- const [staleStamp, setStaleStamp] = useState<null | number>(null)
+ // The seed effect refuses to reseed while the query still carries the
+ // previous profile's stamp. A structurally-shared refetch keeps the object
+ // reference but bumps the stamp.
+ const { arm: armProfileLatch, pending: profilePending } = useProfileSwitchLatch({ dataUpdatedAt })
const [saveVersion, setSaveVersion] = useState(0)
const saveVersionRef = useRef(0)
@@ -43,19 +44,19 @@ export function ChatFontSetting() {
}
useEffect(() => {
- if (!loadedConfig || draft !== null || dataUpdatedAt === staleStamp) {
+ if (!loadedConfig || draft !== null || profilePending) {
return
}
const value = fontFamilyFromConfig(loadedConfig)
setDraft(value)
setChatFontFamilyFromConfig(value)
- }, [dataUpdatedAt, draft, loadedConfig, staleStamp])
+ }, [draft, loadedConfig, profilePending])
useOnProfileSwitch(() => {
saveVersionRef.current += 1
setDraft(null)
- setStaleStamp(dataUpdatedAt)
+ armProfileLatch()
setSaveVersion(0)
setChatFontFamilyFromConfig('')
})
diff --git a/apps/desktop/src/app/settings/terminal-font-setting.tsx b/apps/desktop/src/app/settings/terminal-font-setting.tsx
index cd092b960af..d4d3df73d00 100644
--- a/apps/desktop/src/app/settings/terminal-font-setting.tsx
+++ b/apps/desktop/src/app/settings/terminal-font-setting.tsx
@@ -15,6 +15,7 @@ import type { HermesConfigRecord } from '@/types/hermes'
import { setHermesConfigCache, useHermesConfigRecord } from '../hooks/use-config-record'
import { useOnProfileSwitch } from '../hooks/use-on-profile-switch'
+import { useProfileSwitchLatch } from '../hooks/use-profile-switch-latch'
import { getNested, setNested } from './helpers'
import { ListRow } from './primitives'
@@ -34,9 +35,9 @@ export function TerminalFontSetting() {
// the timestamp is the freshness proof because React Query can reuse the
// same config object when the next profile has identical settings.
const [draft, setDraft] = useState<string | null>(null)
- // dataUpdatedAt of the record the previous profile was looking at; the seed
- // effect refuses to reseed while the query still carries that stamp.
- const [staleStamp, setStaleStamp] = useState<null | number>(null)
+ // The seed effect refuses to reseed while the query still carries the
+ // previous profile's stamp.
+ const { arm: armProfileLatch, pending: profilePending } = useProfileSwitchLatch({ dataUpdatedAt })
const [saveVersion, setSaveVersion] = useState(0)
const saveVersionRef = useRef(0)
@@ -47,19 +48,19 @@ export function TerminalFontSetting() {
}
useEffect(() => {
- if (!loadedConfig || draft !== null || dataUpdatedAt === staleStamp) {
+ if (!loadedConfig || draft !== null || profilePending) {
return
}
const value = fontFamilyFromConfig(loadedConfig)
setDraft(value)
setTerminalFontFamilyFromConfig(value)
- }, [dataUpdatedAt, draft, loadedConfig, staleStamp])
+ }, [draft, loadedConfig, profilePending])
useOnProfileSwitch(() => {
saveVersionRef.current += 1
setDraft(null)
- setStaleStamp(dataUpdatedAt)
+ armProfileLatch()
setSaveVersion(0)
// Do not show the previous profile's font while the new profile loads.
setTerminalFontFamilyFromConfig('')
diff --git a/apps/desktop/src/app/skills/mcp-tab.tsx b/apps/desktop/src/app/skills/mcp-tab.tsx
index 830165b7ee2..1dfa9460ab7 100644
--- a/apps/desktop/src/app/skills/mcp-tab.tsx
+++ b/apps/desktop/src/app/skills/mcp-tab.tsx
@@ -47,6 +47,7 @@ import { $activeSessionId } from '@/store/session'
import { hermesConfigCacheWriter, useHermesConfigRecord } from '../hooks/use-config-record'
import { useOnProfileSwitch } from '../hooks/use-on-profile-switch'
+import { useProfileSwitchLatch } from '../hooks/use-profile-switch-latch'
import { DetailPane, ICON_BUTTON, MASTER_DETAIL_WIDE_COLS } from '../master-detail'
import { PanelAddButton, PanelEmpty } from '../overlays/panel'
import { prettyName } from '../settings/helpers'
@@ -378,9 +379,10 @@ export function McpTab({ gateway, profile }: { gateway: HermesGateway | null; pr
// True from a profile switch until the config query resettles for the new
// profile. Until then `config` (and thus `servers`) still holds profile A's
// data, so any persist would write A's server list into B — block mutations.
- const [profilePending, setProfilePending] = useState(false)
- const staleConfigStamp = useRef<null | number>(null)
- const staleErrorStamp = useRef<null | number>(null)
+ const { arm: armProfileLatch, pending: profilePending } = useProfileSwitchLatch({
+ dataUpdatedAt: configUpdatedAt,
+ errorUpdatedAt: configErroredAt
+ })
const [saving, setSaving] = useState(false)
const [probes, setProbes] = useState<Record<string, Probe>>({})
@@ -552,28 +554,11 @@ export function McpTab({ gateway, profile }: { gateway: HermesGateway | null; pr
setDocVersion(version => version + 1)
// Mark stale until the config query replaces profile A's data — guards
// sidebar mutations from persisting A's server list into B mid-refetch.
- staleConfigStamp.current = configUpdatedAt
- staleErrorStamp.current = configErroredAt
- setProfilePending(true)
+ // The latch releases on a fresh success OR a fresh failure, so a failed
+ // refetch surfaces the retry UI instead of leaving mutations no-op forever.
+ armProfileLatch()
})
- // Clear once the config query settles for the new profile: dataUpdatedAt bumps
- // on a fresh success, errorUpdatedAt on a fresh failure. Releasing on error too
- // means a failed refetch surfaces the retry UI instead of leaving mutations
- // silently no-op forever.
- // eslint-disable-next-line no-restricted-syntax -- legitimate non-atom ref write (see eslint rule comment)
- useEffect(() => {
- if (
- profilePending &&
- staleConfigStamp.current !== null &&
- (configUpdatedAt !== staleConfigStamp.current || configErroredAt !== staleErrorStamp.current)
- ) {
- setProfilePending(false)
- staleConfigStamp.current = null
- staleErrorStamp.current = null
- }
- }, [profilePending, configUpdatedAt, configErroredAt])
-
useDeepLinkHighlight({
block: 'nearest',
elementId: serverName => `mcp-server-${serverName}`,