Skip to content

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

TypeScript

fix(desktop-ui): r5-l2 preserve newer chat loading state

Josué1f41b125c43a
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/selectionGuardCoverage.characterization.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/selectionGuardCoverage.characterization.test.tsx
index d0a72f740..e9260cfce 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/selectionGuardCoverage.characterization.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/selectionGuardCoverage.characterization.test.tsx
@@ -104,7 +104,7 @@ function holdSessionMessages(chatId: string) {
function holdLastActive(chatId: string) {
const original = clerum.chat.setLastActive.getMockImplementation()
- if (!original) throw new Error('Expected the ChatStore setLastActive producer')
+ if (!original) throw new Error('Expected the setLastActive test mock implementation')
const entered = deferred<void>()
const release = deferred<void>()
const returned = deferred<void>()
@@ -121,7 +121,110 @@ function holdLastActive(chatId: string) {
return { entered, release, returned }
}
+function holdLocalMessages(chatId: string) {
+ const original = clerum.chat.loadMessages.getMockImplementation()
+ if (!original) throw new Error('Expected the ChatStore loadMessages producer')
+ const entered = deferred<void>()
+ const release = deferred<void>()
+ const returned = deferred<void>()
+ let held = false
+ clerum.chat.loadMessages.mockImplementation(async (agentRef, requestedChatId, limit, offset) => {
+ if (agentRef === 'agent-x' && requestedChatId === chatId && !held) {
+ held = true
+ entered.resolve()
+ await release.promise
+ }
+ const messages = await original(agentRef, requestedChatId, limit, offset)
+ if (held && requestedChatId === chatId) returned.resolve()
+ return messages
+ })
+ return { entered, release, returned }
+}
+
describe('selection intent and continuation guards', () => {
+ it.each(['latest', 'implicit'] as const)(
+ 'keeps a requested chat loading when an older %s auto-select finishes late',
+ async mode => {
+ await clerum.chat.create('agent-x', 'chat-b')
+ await clerum.chat.upsertMessages('agent-x', 'chat-b', [
+ { id: 'chat-b-message', role: 'user', content: 'chat B history', timestamp: 1 },
+ ])
+ await clerum.chat.create('agent-x', 'chat-a')
+ await clerum.chat.upsertMessages('agent-x', 'chat-a', [
+ { id: 'chat-a-message', role: 'user', content: 'chat A history', timestamp: 2 },
+ ])
+ clerum.chat.getIndex.mockImplementation(agentRef => clerum.readIndex(agentRef))
+ const heldAutoSelect = holdLastActive('chat-a')
+ const heldRequestedHistory = holdLocalMessages('chat-b')
+ const controller = renderController({
+ navItem: mode === 'latest' ? 'agents' : 'chat',
+ loadMenuData: false,
+ })
+ let requestedSwitch: Promise<void> | undefined
+
+ try {
+ if (mode === 'latest') {
+ await waitForListIdle(controller)
+ await act(async () => {
+ controller.result.current.setPendingChatSelection('agent-x', null, {
+ selectLatest: true,
+ })
+ controller.rerender({ navItem: 'chat' })
+ })
+ }
+
+ await act(async () => {
+ await heldAutoSelect.entered.promise
+ })
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBe('chat-a')
+ expect(controller.result.current.chatMessagesLoading).toBe(true)
+ })
+
+ act(() => {
+ requestedSwitch = controller.result.current.handleSelectChat('chat-b')
+ })
+ await act(async () => {
+ await heldRequestedHistory.entered.promise
+ })
+ expect(controller.result.current.activeChatId).toBe('chat-b')
+ expect(controller.result.current.chatMessages).toEqual([])
+ expect(controller.result.current.chatMessagesLoading).toBe(true)
+
+ await act(async () => {
+ heldAutoSelect.release.resolve()
+ await heldAutoSelect.returned.promise
+ await new Promise(resolve => setTimeout(resolve, 0))
+ })
+ expect(controller.result.current.activeChatId).toBe('chat-b')
+ expect(controller.result.current.chatMessages).toEqual([])
+ expect(controller.result.current.chatMessagesLoading).toBe(true)
+
+ if (!requestedSwitch) throw new Error('Expected the requested chat switch')
+ await act(async () => {
+ heldRequestedHistory.release.resolve()
+ await heldRequestedHistory.returned.promise
+ await requestedSwitch
+ })
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBe('chat-b')
+ expect(controller.result.current.chatMessages).toEqual(
+ expect.arrayContaining([expect.objectContaining({ id: 'chat-b-message' })])
+ )
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ })
+ } finally {
+ heldAutoSelect.release.resolve()
+ heldRequestedHistory.release.resolve()
+ await act(async () => {
+ await heldAutoSelect.returned.promise
+ await requestedSwitch?.catch(() => undefined)
+ })
+ controller.unmount()
+ }
+ }
+ )
+
it('keeps a matching pending selection across a direct switch and route change', async () => {
await clerum.chat.create('agent-x', 'requested-chat')
await clerum.chat.upsertMessages('agent-x', 'requested-chat', [
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index f54244c5b..56ad408d5 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -981,7 +981,6 @@ export function useAgentChatController({
// chat, and a task in flight is rejoined via the tracker D.3 already mounts.
const switchToChatForIntent = useCallback(
async (agentRef: string, chatId: string, selectionIntentRevision: number) => {
- if (selectionIntentRevisionRef.current !== selectionIntentRevision) return
if (isHostAccessBlocked(agentRef) || isChatDeleted(agentRef, chatId)) return
const key = makeTaskKey(agentRef, chatId)
const visibleBeforeSwitch = activeChatVisibilityRef.current
@@ -1457,7 +1456,9 @@ export function useAgentChatController({
autoSelectedChatIdRef.current = latest.id
await switchToChatForIntent(selectedAgent, latest.id, selectionIntentRevision)
}
- setChatMessagesLoading(false)
+ if (selectionIntentRevisionRef.current === selectionIntentRevision) {
+ setChatMessagesLoading(false)
+ }
return
}
if (requestedSelection?.mode === 'specific') {
@@ -1479,7 +1480,9 @@ export function useAgentChatController({
await switchToChatForIntent(selectedAgent, latest.id, selectionIntentRevision)
}
}
- setChatMessagesLoading(false)
+ if (selectionIntentRevisionRef.current === selectionIntentRevision) {
+ setChatMessagesLoading(false)
+ }
})()
return () => {
diff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.ts
index 3b7c40997..efd44a4b6 100644
--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts
+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts
@@ -823,8 +823,7 @@ export function useChatListController({
selectionIntentRevisionAtRequest: number
): Promise<{ index: ChatIndex; merged: SidebarChatEntry[] } | null> => {
const requestGeneration = ++requestGenerationRef.current
- // A retry remains part of this logical load, so it must keep the selection
- // authority captured before the first attempt rather than adopt a newer intent.
+ // Reuse the caller's request-start revision on retry; never capture a newer intent here.
// One retry with a short backoff: during boot a concurrent team-switch /
// access-catalog refresh can momentarily rebind the main-process chat
// store, rejecting `getIndex` with "Not authenticated". Swallowing that
@@ -859,7 +858,7 @@ export function useChatListController({
}
}
},
- [host, loadChatListOnce]
+ [loadChatListOnce]
)
// ─── Narrow chatList mutation API (called by the parent's remaining flows) ───