Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): R1-M1 reject stale sidebar index loads

Josuéca77b97022c2
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
index efa563dc6..b1d052fa3 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
@@ -1122,6 +1122,81 @@ describe('useAgentChatController — characterization (D.0)', () => {
expect(result.current.chatList.map(chat => chat.id)).toEqual(['agent-y-chat'])
})
+ it('does not let an older same-agent index replace a newer A-B-A load', async () => {
+ type Index = Awaited<ReturnType<typeof clerum.chat.getIndex>>
+ const firstAgentX = deferred<Index>()
+ const agentY = deferred<Index>()
+ const secondAgentX = deferred<Index>()
+ let agentXCalls = 0
+ clerum.chat.getIndex.mockImplementation((agentRef: string) => {
+ if (agentRef === 'agent-y') return agentY.promise
+ agentXCalls += 1
+ return agentXCalls === 1 ? firstAgentX.promise : secondAgentX.promise
+ })
+ const { result, rerender } = renderController({
+ agentNames: ['agent-x', 'agent-y'],
+ loadMenuData: false,
+ })
+ await waitFor(() => expect(agentXCalls).toBe(1))
+
+ rerender({
+ selectedAgent: 'agent-y',
+ agentNames: ['agent-x', 'agent-y'],
+ loadMenuData: false,
+ })
+ await waitFor(() => expect(clerum.chat.getIndex).toHaveBeenCalledWith('agent-y'))
+ rerender({
+ selectedAgent: 'agent-x',
+ agentNames: ['agent-x', 'agent-y'],
+ loadMenuData: false,
+ })
+ await waitFor(() => expect(agentXCalls).toBe(2))
+
+ await act(async () => {
+ secondAgentX.resolve({
+ version: 2,
+ lastActiveChatId: null,
+ onboardingDismissed: false,
+ chats: [
+ {
+ id: 'fresh-agent-x-chat',
+ title: 'Fresh',
+ createdAt: '2026-05-04T00:00:00Z',
+ updatedAt: '2026-05-04T00:00:00Z',
+ messageCount: 1,
+ },
+ ],
+ })
+ })
+ await waitFor(() => expect(result.current.chatList[0]?.id).toBe('fresh-agent-x-chat'))
+
+ await act(async () => {
+ firstAgentX.resolve({
+ version: 2,
+ lastActiveChatId: null,
+ onboardingDismissed: false,
+ chats: [
+ {
+ id: 'stale-agent-x-chat',
+ title: 'Stale',
+ createdAt: '2026-05-01T00:00:00Z',
+ updatedAt: '2026-05-01T00:00:00Z',
+ messageCount: 1,
+ },
+ ],
+ })
+ agentY.resolve({
+ version: 2,
+ lastActiveChatId: null,
+ onboardingDismissed: false,
+ chats: [],
+ })
+ await Promise.resolve()
+ })
+
+ expect(result.current.chatList.map(chat => chat.id)).toEqual(['fresh-agent-x-chat'])
+ })
+
it('renders local sessions without waiting for remote listSessions', async () => {
clerum.chat.getIndex.mockResolvedValue({
version: 1,
diff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.ts
index 9ebbc16c0..b5aa2ee6c 100644
--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts
+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts
@@ -167,6 +167,7 @@ export function useChatListController({
const loadChatListOnce = useCallback(
async (agentRef: string): Promise<{ index: ChatIndex; merged: SidebarChatEntry[] }> => {
+ const requestGeneration = ++requestGenerationRef.current
chatListNextCursorByAgentRef.current[agentRef] = null
if (selectedAgentRef.current === agentRef) {
setChatListHasMoreRemoteSessions(false)
@@ -174,10 +175,14 @@ export function useChatListController({
}
const index = await chatStore.getIndex(agentRef)
const merged = [...index.chats].sort(byUpdatedDesc)
- if (selectedAgentRef.current !== agentRef) return { index, merged }
+ if (
+ selectedAgentRef.current !== agentRef ||
+ requestGenerationRef.current !== requestGeneration
+ ) {
+ return { index, merged }
+ }
setChatList(merged)
- const requestGeneration = requestGenerationRef.current
const suppressionMarkerAtRequest = suppressAutoSelectionByAgentRef.current.get(agentRef)
scheduleAfterFirstPaint(async () => {
const serverResult = await chatStore