The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): retain config routes through cache refresh
KoNit-K389e89bd47bb
diff --git a/apps/desktop/src/api/config.ts b/apps/desktop/src/api/config.tsindex c04beea0cfd..5f94ef7feaa 100644--- a/apps/desktop/src/api/config.ts+++ b/apps/desktop/src/api/config.ts@@ -118,7 +118,7 @@ export async function getHermesConfigRecord(profile?: ProfileScope,{ includeDefaults = true }: { includeDefaults?: boolean } = {}): Promise<HermesConfigRecord> {- const origin = capabilityScoped(profile)+ const origin = capabilityScoped(profile ?? undefined)const record = await window.hermesDesktop.api<HermesConfigRecord>({...origin,path: includeDefaults ? '/api/config' : '/api/config?include_defaults=false'diff --git a/apps/desktop/src/app/hooks/use-config-record.test.tsx b/apps/desktop/src/app/hooks/use-config-record.test.tsxnew file mode 100644index 00000000000..7a8889e1362--- /dev/null+++ b/apps/desktop/src/app/hooks/use-config-record.test.tsx@@ -0,0 +1,53 @@+import { QueryClientProvider } from '@tanstack/react-query'+import { cleanup, renderHook, waitFor } from '@testing-library/react'+import { createElement } from 'react'+import { afterEach, expect, it, vi } from 'vitest'++import { bindConfigReadOrigin } from '@/api/config'+import { getHermesConfigRecord } from '@/hermes'+import { queryClient } from '@/lib/query-client'++import { HERMES_CONFIG_KEY, useHermesConfigRecord } from './use-config-record'++vi.mock('@/hermes', () => ({ getHermesConfigRecord: vi.fn() }))++afterEach(() => {+ cleanup()+ queryClient.clear()+ vi.clearAllMocks()+})++const wrapper = ({ children }: { children: React.ReactNode }) =>+ createElement(QueryClientProvider, { client: queryClient }, children)++it('keeps a cached record bound to the origin that served it before refetching', () => {+ const record = { display: { theme: 'dark' } }+ bindConfigReadOrigin(record, { connectionId: 'connection-a', profile: 'worker' })+ queryClient.setQueryData(HERMES_CONFIG_KEY, record)+ vi.mocked(getHermesConfigRecord).mockImplementation(() => new Promise(() => {}))++ const { result } = renderHook(() => useHermesConfigRecord(), { wrapper })++ expect(result.current.data).toBe(record)+ expect(result.current.writeScope).toEqual({ connectionId: 'connection-a', profile: 'worker' })+})++it('updates the write origin when a refetch replaces the displayed record', async () => {+ const first = { display: { theme: 'dark' } }+ const second = { display: { theme: 'light' } }+ bindConfigReadOrigin(first, { connectionId: 'connection-a', profile: 'worker' })+ bindConfigReadOrigin(second, { connectionId: 'connection-b', profile: 'worker' })+ vi.mocked(getHermesConfigRecord).mockResolvedValueOnce(first).mockResolvedValueOnce(second)++ const { result } = renderHook(() => useHermesConfigRecord(), { wrapper })++ await waitFor(() => expect(result.current.data).toBe(first))+ expect(result.current.writeScope).toEqual({ connectionId: 'connection-a', profile: 'worker' })++ await queryClient.invalidateQueries({ queryKey: HERMES_CONFIG_KEY })++ await waitFor(() => expect(result.current.data).toEqual(second))+ await waitFor(() =>+ expect(result.current.writeScope).toEqual({ connectionId: 'connection-b', profile: 'worker' })+ )+})diff --git a/apps/desktop/src/app/hooks/use-config-record.ts b/apps/desktop/src/app/hooks/use-config-record.tsindex 3d32e5e0cb1..632646d63f0 100644--- a/apps/desktop/src/app/hooks/use-config-record.ts+++ b/apps/desktop/src/app/hooks/use-config-record.ts@@ -1,10 +1,9 @@import { useQuery } from '@tanstack/react-query'-import { useRef } from 'react'import { type ProfileScope, profileScopeKey } from '@/api/client'-import { peekConfigReadOrigin } from '@/api/config'+import { bindConfigReadOrigin, peekConfigReadOrigin } from '@/api/config'import { getHermesConfigRecord } from '@/hermes'-import { queryClient, writeCache } from '@/lib/query-client'+import { queryClient } from '@/lib/query-client'import type { HermesConfigRecord } from '@/types/hermes'// One shared cache for the whole profile config record (`GET /api/config`).@@ -31,15 +30,6 @@ export const hermesConfigKey = (profile?: ProfileScope) =>// `profile` scopes both the query key and the fetch; omitting it preserves the// exact app-wide behavior (base key, `profileScoped(undefined)` fallback).export const useHermesConfigRecord = (profile?: ProfileScope) => {- const writeScopeRef = useRef<{ connectionId?: string; profile?: string } | null>(null)- const scopeKey = profileScopeKey(profile)- const scopeKeyRef = useRef(scopeKey)-- if (scopeKeyRef.current !== scopeKey) {- scopeKeyRef.current = scopeKey- writeScopeRef.current = null- }-const query = useQuery({queryKey: hermesConfigKey(profile),// null/undefined both mean "no override" → fetch with undefined so@@ -48,27 +38,39 @@ export const useHermesConfigRecord = (profile?: ProfileScope) => {queryFn: async () => {const record = await getHermesConfigRecord(profile ?? undefined)- if (!writeScopeRef.current) {- writeScopeRef.current = peekConfigReadOrigin(record) ?? {}- }-return record},- staleTime: 0+ staleTime: 0,+ // The read origin is held in a WeakMap keyed by the record itself. Keep+ // the fetched object intact so the cached record and its origin cannot+ // diverge during React Query's structural-sharing pass.+ structuralSharing: false})return {...query,- writeScope: writeScopeRef.current+ writeScope: peekConfigReadOrigin(query.data) ?? null}}// setHermesConfigCache writes the app-wide (base-key) record. Pass a profile to// write the suffixed per-profile cache instead — keeps the selector's optimistic// write-through landing on the same key its query reads.-export const setHermesConfigCache = writeCache<HermesConfigRecord>(HERMES_CONFIG_KEY)-export const hermesConfigCacheWriter = (profile?: ProfileScope) =>- writeCache<HermesConfigRecord>(hermesConfigKey(profile))+const writeHermesConfigCache = (key: ReturnType<typeof hermesConfigKey>) =>+ (next: HermesConfigRecord | undefined | ((previous: HermesConfigRecord | undefined) => HermesConfigRecord | undefined)) =>+ void queryClient.setQueryData<HermesConfigRecord>(key, previous => {+ const record = typeof next === 'function' ? next(previous) : next+ const origin = peekConfigReadOrigin(previous)++ if (record && origin) {+ bindConfigReadOrigin(record, origin)+ }++ return record+ })++export const setHermesConfigCache = writeHermesConfigCache(HERMES_CONFIG_KEY)+export const hermesConfigCacheWriter = (profile?: ProfileScope) => writeHermesConfigCache(hermesConfigKey(profile))export const invalidateHermesConfig = (profile?: ProfileScope) =>queryClient.invalidateQueries({ queryKey: hermesConfigKey(profile) })