Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-ui): keep the newer catalog request when an invalidated one rejects
Alfredo Lopezb29077dae018
diff --git a/desktop-app/ui/src/hooks/__tests__/useChatStore.test.tsx b/desktop-app/ui/src/hooks/__tests__/useChatStore.test.tsxindex 9eaf73cec..ae1c80ac9 100644--- a/desktop-app/ui/src/hooks/__tests__/useChatStore.test.tsx+++ b/desktop-app/ui/src/hooks/__tests__/useChatStore.test.tsx@@ -81,6 +81,43 @@ describe('useChatStore remote request cache', () => {expect(callsFor('host-b')).toBe(1)})+ // A request issued before the invalidation that rejects afterwards must not+ // evict the newer request cached under the same key.+ it('keeps the newer cached request when an invalidated one rejects late', async () => {+ let rejectStale: (error: Error) => void = () => {+ throw new Error('the stale request was never issued')+ }+ const listSessions = vi+ .fn()+ .mockImplementationOnce(+ () =>+ new Promise((_resolve, reject) => {+ rejectStale = reject+ })+ )+ .mockImplementation(async () => ({ items: [] }))+ Object.defineProperty(window, 'clerum', {+ configurable: true,+ value: { rpc: { listSessions } },+ })+ const { result } = renderHook(() => useChatStore())+ result.current.setRemoteCacheScope('authenticated:user-race:team-race')+ const query = { agent: 'host', limit: 50 }++ const stale = result.current.listSessions('host', query)+ result.current.invalidateSessionCatalog('host')+ const fresh = result.current.listSessions('host', query)+ await fresh+ expect(listSessions).toHaveBeenCalledTimes(2)++ rejectStale(new Error('host access revoked'))+ await expect(stale).rejects.toThrow('host access revoked')++ // Witness: the fresh request is still the cached one for this key.+ await expect(result.current.listSessions('host', query)).resolves.toEqual({ items: [] })+ expect(listSessions).toHaveBeenCalledTimes(2)+ })+// #654 M12 — the host-model catalog has exactly one cache, in// `hostModelSelectionStore`, which also owns revision ordering. A second TTL// layer here could only serve a response read BEFORE a write, which is thediff --git a/desktop-app/ui/src/hooks/useChatStore.ts b/desktop-app/ui/src/hooks/useChatStore.tsindex 91302ecbe..f8e7c82b0 100644--- a/desktop-app/ui/src/hooks/useChatStore.ts+++ b/desktop-app/ui/src/hooks/useChatStore.ts@@ -164,16 +164,18 @@ export function useChatStore() {const cached = sessionCatalogRequests.get(key)if (!options.force && cached && cached.expiresAt > Date.now()) return cached.promise- const promise = source(hostRef, undefined, query).catch(error => {- sessionCatalogRequests.delete(key)- throw error- })- sessionCatalogRequests.set(key, {+ const entry: CachedRequest<SessionsListResult> = {expiresAt: Date.now() + SESSION_CATALOG_TTL_MS,hostRef,- promise,- })- return promise+ promise: source(hostRef, undefined, query).catch(error => {+ // An invalidation may have replaced this entry with a newer request+ // under the same key; only this request's own entry is evicted.+ if (sessionCatalogRequests.get(key) === entry) sessionCatalogRequests.delete(key)+ throw error+ }),+ }+ sessionCatalogRequests.set(key, entry)+ return entry.promise},[])