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-m1 restore in-flight chat after create failure

Josué2f592b434bb5
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/createChatFailureSelection.characterization.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/createChatFailureSelection.characterization.test.tsx
index 99a617232..02bb2b3d0 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/createChatFailureSelection.characterization.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/createChatFailureSelection.characterization.test.tsx
@@ -75,4 +75,148 @@ describe('New chat creation failure during a specific selection', () => {
})
controller.unmount()
})
+
+ it('restores a direct chat switch interrupted by a failed New chat', async () => {
+ const targetChatId = 'chat-b'
+ await clerum.chat.create('agent-x', targetChatId)
+ await clerum.chat.upsertMessages('agent-x', targetChatId, [
+ {
+ id: 'chat-b-message',
+ role: 'user',
+ content: 'chat B history',
+ timestamp: Date.now(),
+ },
+ ])
+ const originalSetLastActive = clerum.chat.setLastActive.getMockImplementation()
+ if (!originalSetLastActive) throw new Error('Expected the setLastActive test mock')
+ const enteredSwitch = deferred<void>()
+ const releaseSwitch = deferred<void>()
+ let held = false
+ clerum.chat.setLastActive.mockImplementation(async (agentRef, chatId) => {
+ if (agentRef === 'agent-x' && chatId === targetChatId && !held) {
+ held = true
+ enteredSwitch.resolve()
+ await releaseSwitch.promise
+ }
+ return originalSetLastActive(agentRef, chatId)
+ })
+
+ const controller = renderController()
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ let switchPromise!: Promise<void>
+ act(() => {
+ switchPromise = controller.result.current.handleSelectChat(targetChatId)
+ })
+ await enteredSwitch.promise
+ expect(controller.result.current.activeChatId).toBe(targetChatId)
+ expect(controller.result.current.chatMessagesLoading).toBe(true)
+
+ const createError = new Error('local chat creation failed')
+ clerum.chat.create.mockRejectedValueOnce(createError)
+ let observedError: unknown
+ await act(async () => {
+ await controller.result.current.handleCreateChat().catch(error => {
+ observedError = error
+ })
+ })
+ expect(observedError).toBe(createError)
+
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBe(targetChatId)
+ expect(controller.result.current.chatMessages).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({ id: 'chat-b-message', content: 'chat B history' }),
+ ])
+ )
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ })
+
+ await act(async () => {
+ releaseSwitch.resolve()
+ await switchPromise
+ })
+ expect(controller.result.current.activeChatId).toBe(targetChatId)
+ expect(controller.result.current.chatMessages).toEqual(
+ expect.arrayContaining([expect.objectContaining({ id: 'chat-b-message' })])
+ )
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ controller.unmount()
+ })
+
+ it('restores a consumed specific selection interrupted by a failed New chat', async () => {
+ const targetChatId = 'notification-target'
+ await clerum.chat.create('agent-x', targetChatId)
+ await clerum.chat.upsertMessages('agent-x', targetChatId, [
+ {
+ id: 'target-message',
+ role: 'user',
+ content: 'requested conversation history',
+ timestamp: Date.now(),
+ },
+ ])
+ const targetIndex = await clerum.readIndex('agent-x')
+ const controller = renderController({ navItem: 'agents' })
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+
+ const indexCallCount = clerum.chat.getIndex.mock.calls.length
+ const heldIndex = deferred<Awaited<ReturnType<typeof clerum.chat.getIndex>>>()
+ clerum.chat.getIndex.mockReturnValue(heldIndex.promise)
+ const originalSetLastActive = clerum.chat.setLastActive.getMockImplementation()
+ if (!originalSetLastActive) throw new Error('Expected the setLastActive test mock')
+ const enteredSwitch = deferred<void>()
+ const releaseSwitch = deferred<void>()
+ let held = false
+ clerum.chat.setLastActive.mockImplementation(async (agentRef, chatId) => {
+ if (agentRef === 'agent-x' && chatId === targetChatId && !held) {
+ held = true
+ enteredSwitch.resolve()
+ await releaseSwitch.promise
+ }
+ return originalSetLastActive(agentRef, chatId)
+ })
+
+ await act(async () => {
+ controller.result.current.setPendingChatSelection('agent-x', targetChatId)
+ controller.rerender({ navItem: 'chat' })
+ })
+ await waitFor(() =>
+ expect(clerum.chat.getIndex.mock.calls.length).toBeGreaterThan(indexCallCount)
+ )
+ await act(async () => {
+ heldIndex.resolve(targetIndex)
+ })
+ await enteredSwitch.promise
+ expect(controller.result.current.activeChatId).toBe(targetChatId)
+ expect(controller.result.current.chatMessagesLoading).toBe(true)
+
+ const createError = new Error('local chat creation failed')
+ clerum.chat.create.mockRejectedValueOnce(createError)
+ let observedError: unknown
+ await act(async () => {
+ await controller.result.current.handleCreateChat().catch(error => {
+ observedError = error
+ })
+ })
+ expect(observedError).toBe(createError)
+
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBe(targetChatId)
+ expect(controller.result.current.chatMessages).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ id: 'target-message',
+ content: 'requested conversation history',
+ }),
+ ])
+ )
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ })
+
+ await act(async () => {
+ releaseSwitch.resolve()
+ })
+ expect(controller.result.current.activeChatId).toBe(targetChatId)
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ controller.unmount()
+ })
})
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index 3ed0f5bf4..f54244c5b 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -1183,6 +1183,8 @@ export function useAgentChatController({
dispatchSession,
clearComposerDraft,
getActiveChatId: () => activeChatId,
+ getChatMessagesLoading: () => chatMessagesLoading,
+ clearChatMessagesLoading: () => setChatMessagesLoading(false),
getAutoSelectedChatId: () => autoSelectedChatIdRef.current,
markAutoSelectedChat: chatId => {
autoSelectedChatIdRef.current = chatId
diff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.ts
index 45cf19b63..3b7c40997 100644
--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts
+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts
@@ -108,6 +108,8 @@ export interface ChatListControllerHost {
dispatchSession: (chatKey: string, event: SessionFsmEvent) => void
clearComposerDraft: (chatId: string) => void
getActiveChatId: () => string | null
+ getChatMessagesLoading: () => boolean
+ clearChatMessagesLoading: () => void
getAutoSelectedChatId: () => string | null
markAutoSelectedChat: (chatId: string | null) => void
shouldAutoSelectLatest: () => boolean
@@ -1030,28 +1032,40 @@ export function useChatListController({
const agentRef = selectedAgentRef.current
if (!agentRef) return
const pendingSelection = readPendingSelection(agentRef)
+ const activeChatIdAtCreate = host.current?.getActiveChatId() ?? null
+ const chatMessagesLoadingAtCreate = host.current?.getChatMessagesLoading() ?? false
const selectionIntentRevision = host.current?.beginSelectionIntent()
host.current?.clearPendingSelection(agentRef)
const requestGeneration = requestGenerationRef.current
+ const authorityScopeGeneration = authorityScopeGenerationRef.current
+ const authorityScopeAtCreate = currentAuthorityScopeRef.current
const chatId = crypto.randomUUID()
let meta: ChatMetadata
try {
meta = await chatStore.createChat(agentRef, chatId)
} catch (error) {
- // The blank New chat intent invalidated an older specific load. If creation
- // failed without a newer navigation or scope taking ownership, restore the
- // requested conversation through the controller's authorized switch path.
+ // The failed New chat invalidated whichever selection was in flight. Restore
+ // its visible conversation only while this create still owns the same scope
+ // and selection revision; a newer navigation must remain authoritative.
if (
- pendingSelection?.mode === 'specific' &&
selectedAgentRef.current === agentRef &&
requestGenerationRef.current === requestGeneration &&
+ authorityScopeGenerationRef.current === authorityScopeGeneration &&
+ currentAuthorityScopeRef.current === authorityScopeAtCreate &&
selectionIntentRevision !== undefined &&
host.current?.getSelectionIntentRevision() === selectionIntentRevision
) {
- try {
- await host.current?.switchToChat(agentRef, pendingSelection.chatId)
- } catch {
- // Preserve the original create error for the caller.
+ const restoreChatId =
+ pendingSelection?.mode === 'specific'
+ ? pendingSelection.chatId
+ : chatMessagesLoadingAtCreate
+ ? activeChatIdAtCreate
+ : null
+ if (restoreChatId) {
+ const restore = host.current?.switchToChat(agentRef, restoreChatId)
+ void restore?.catch(() => undefined)
+ } else if (chatMessagesLoadingAtCreate) {
+ host.current?.clearChatMessagesLoading()
}
}
throw error