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 pending chat after create failure

Josué278339b744e8
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
new file mode 100644
index 000000000..99a617232
--- /dev/null
+++ b/desktop-app/ui/src/hooks/domain/__tests__/createChatFailureSelection.characterization.test.tsx
@@ -0,0 +1,78 @@
+// @vitest-environment jsdom
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { act, waitFor } from '@testing-library/react'
+import { deferred } from './__fixtures__/catalogFixtures'
+import { renderController } from './__fixtures__/controllerHarness'
+import { type MockClerum, installMockClerum, uninstallMockClerum } from './__fixtures__/mockClerum'
+
+let clerum: MockClerum
+;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
+
+beforeEach(() => {
+ clerum = installMockClerum()
+})
+
+afterEach(() => {
+ vi.restoreAllMocks()
+ uninstallMockClerum()
+})
+
+describe('New chat creation failure during a specific selection', () => {
+ it('restores the requested conversation and preserves the create error', 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)
+ 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)
+ )
+ expect(controller.result.current.activeChatId).toBe(targetChatId)
+ expect(controller.result.current.chatMessagesLoading).toBe(true)
+ expect(controller.result.current.chatMessages).toEqual([])
+
+ 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 act(async () => {
+ heldIndex.resolve(targetIndex)
+ })
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBe(targetChatId)
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ expect(controller.result.current.chatMessages).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ id: 'target-message',
+ content: 'requested conversation history',
+ }),
+ ])
+ )
+ })
+ controller.unmount()
+ })
+})
diff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.ts
index fe012430a..baadb8b65 100644
--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts
+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts
@@ -1026,11 +1026,33 @@ export function useChatListController({
const handleCreateChat = useCallback(async () => {
const agentRef = selectedAgentRef.current
if (!agentRef) return
+ const pendingSelection = readPendingSelection(agentRef)
const selectionIntentRevision = host.current?.beginSelectionIntent()
host.current?.clearPendingSelection(agentRef)
const requestGeneration = requestGenerationRef.current
const chatId = crypto.randomUUID()
- const meta = await chatStore.createChat(agentRef, chatId)
+ 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.
+ if (
+ pendingSelection?.mode === 'specific' &&
+ selectedAgentRef.current === agentRef &&
+ requestGenerationRef.current === requestGeneration &&
+ selectionIntentRevision !== undefined &&
+ host.current?.getSelectionIntentRevision() === selectionIntentRevision
+ ) {
+ try {
+ await host.current?.switchToChat(agentRef, pendingSelection.chatId)
+ } catch {
+ // Preserve the original create error for the caller.
+ }
+ }
+ throw error
+ }
chatStore.clearCachedRemoteData()
if (selectedAgentRef.current !== agentRef || requestGenerationRef.current !== requestGeneration)
return
@@ -1043,7 +1065,7 @@ export function useChatListController({
host.current?.markAutoSelectedChat(null)
await host.current?.switchToChat(agentRef, chatId)
host.current?.scrollChatToBottom()
- }, [chatStore, appendNewEntry, host])
+ }, [chatStore, appendNewEntry, host, readPendingSelection])
/**
* Optimistic LOCAL-only title update (spec 15 §2.5 / B19). Persists to the