Skip to content

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

TypeScript

fix(desktop-ui): r7-l4 restore latest chat after failed create

Josué091e393a8f4c
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/selectionLoadingOwnership.r7.characterization.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/selectionLoadingOwnership.r7.characterization.test.tsx
index b3e2afd0c..4a316817c 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/selectionLoadingOwnership.r7.characterization.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/selectionLoadingOwnership.r7.characterization.test.tsx
@@ -352,4 +352,121 @@ describe('Round 7 selection loading ownership', () => {
controller.unmount()
}
})
+
+ it.each(['latest', 'implicit'] as const)(
+ 'restores the newest existing chat after a failed create during an unconsumed %s load',
+ async mode => {
+ await clerum.chat.create('agent-x', 'chat-older')
+ await new Promise(resolve => setTimeout(resolve, 5))
+ await clerum.chat.create('agent-x', 'chat-newest')
+ await clerum.chat.upsertMessages('agent-x', 'chat-newest', [
+ { id: 'newest-message', role: 'user', content: 'newest history', timestamp: 3 },
+ ])
+ clerum.chat.getIndex.mockImplementation(agentRef => clerum.readIndex(agentRef))
+ const controller = renderController({ navItem: 'agents', loadMenuData: false })
+ const createError = new Error('local chat creation failed')
+ let observedError: unknown
+ let createPromise!: Promise<void>
+ const heldIndex = deferred<Awaited<ReturnType<typeof clerum.chat.getIndex>>>()
+ let routeIndexReads = 0
+
+ try {
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ clerum.chat.getIndex.mockImplementation(agentRef => {
+ routeIndexReads += 1
+ if (routeIndexReads === 1) return heldIndex.promise
+ return clerum.readIndex(agentRef)
+ })
+ if (mode === 'latest') {
+ act(() =>
+ controller.result.current.setPendingChatSelection('agent-x', null, {
+ selectLatest: true,
+ })
+ )
+ }
+ await act(async () => {
+ controller.rerender({ navItem: 'chat' })
+ })
+ await waitFor(() => expect(routeIndexReads).toBe(1))
+
+ clerum.chat.create.mockRejectedValueOnce(createError)
+ act(() => {
+ createPromise = controller.result.current.handleCreateChat().catch(error => {
+ observedError = error
+ })
+ })
+ await act(async () => {
+ await createPromise
+ })
+ expect(observedError).toBe(createError)
+
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBe('chat-newest')
+ expect(controller.result.current.chatMessages).toEqual(
+ expect.arrayContaining([expect.objectContaining({ id: 'newest-message' })])
+ )
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ })
+
+ await act(async () => {
+ heldIndex.resolve(await clerum.readIndex('agent-x'))
+ })
+ expect(controller.result.current.activeChatId).toBe('chat-newest')
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ } finally {
+ heldIndex.resolve(await clerum.readIndex('agent-x'))
+ controller.unmount()
+ }
+ }
+ )
+
+ it('keeps the chat blank after a failed create when no latest chat exists', async () => {
+ clerum.chat.getIndex.mockImplementation(agentRef => clerum.readIndex(agentRef))
+ const controller = renderController({ navItem: 'agents', loadMenuData: false })
+ const createError = new Error('local chat creation failed')
+ const heldIndex = deferred<Awaited<ReturnType<typeof clerum.chat.getIndex>>>()
+ let routeIndexReads = 0
+ let observedError: unknown
+ let createPromise!: Promise<void>
+
+ try {
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ clerum.chat.getIndex.mockImplementation(agentRef => {
+ routeIndexReads += 1
+ if (routeIndexReads === 1) return heldIndex.promise
+ return clerum.readIndex(agentRef)
+ })
+ await act(async () => {
+ controller.rerender({ navItem: 'chat' })
+ })
+ await waitFor(() => expect(routeIndexReads).toBe(1))
+
+ clerum.chat.create.mockRejectedValueOnce(createError)
+ act(() => {
+ createPromise = controller.result.current.handleCreateChat().catch(error => {
+ observedError = error
+ })
+ })
+ await act(async () => {
+ await createPromise
+ })
+ expect(observedError).toBe(createError)
+
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBeNull()
+ expect(controller.result.current.chatMessages).toEqual([])
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ expect(controller.result.current.chatListLoading).toBe(false)
+ })
+
+ await act(async () => {
+ heldIndex.resolve(await clerum.readIndex('agent-x'))
+ })
+ expect(controller.result.current.activeChatId).toBeNull()
+ expect(controller.result.current.chatMessages).toEqual([])
+ } finally {
+ heldIndex.resolve(await clerum.readIndex('agent-x'))
+ controller.unmount()
+ }
+ })
})
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index 80d9008e2..679499030 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -505,6 +505,7 @@ export function useAgentChatController({
latestChatSessionsLoading,
hideAgent,
loadChatList,
+ getChatListRequestGeneration,
loadMoreChatSessions,
setChatListLoading,
clearList,
@@ -1193,12 +1194,63 @@ export function useAgentChatController({
handleDeleteChat,
handleDeleteChatForAgent,
} = chatListCtl
+ const restoreLatestChatSelection = useCallback(
+ async (agentRef: string) => {
+ const visible = activeChatVisibilityRef.current
+ if (
+ selectedAgent !== agentRef ||
+ navItem !== DESKTOP_ROUTES.chat ||
+ visible.selectedAgent !== agentRef ||
+ visible.activeChatId !== null ||
+ isHostAccessBlocked(agentRef)
+ )
+ return
+
+ const selectionIntentRevision = beginSelectionIntent(true)
+ setChatListLoading(true)
+ const listLoad = loadChatList(agentRef, selectionIntentRevision)
+ const listRequestGeneration = getChatListRequestGeneration()
+ const result = await listLoad
+ if (getChatListRequestGeneration() !== listRequestGeneration) return
+ if (
+ selectionIntentRevisionRef.current !== selectionIntentRevision ||
+ selectedAgent !== agentRef ||
+ navItem !== DESKTOP_ROUTES.chat ||
+ isHostAccessBlocked(agentRef)
+ ) {
+ setChatListLoading(false)
+ return
+ }
+
+ setChatListLoading(false)
+ const latest = result?.merged[0]
+ if (!latest) {
+ settleChatMessagesLoading(selectionIntentRevision)
+ return
+ }
+
+ autoSelectedChatIdRef.current = latest.id
+ await switchToChatForIntent(agentRef, latest.id, selectionIntentRevision)
+ },
+ [
+ beginSelectionIntent,
+ getChatListRequestGeneration,
+ isHostAccessBlocked,
+ loadChatList,
+ navItem,
+ selectedAgent,
+ setChatListLoading,
+ settleChatMessagesLoading,
+ switchToChatForIntent,
+ ]
+ )
useEffect(() => {
chatListHostRef.current = {
switchToChat,
beginSelectionIntent,
getSelectionIntentRevision: () => selectionIntentRevisionRef.current,
clearPendingSelection,
+ restoreLatestChatSelection,
scrollChatToBottom,
dispatchSession,
clearComposerDraft,
diff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.ts
index 3971e21fe..cbaa31c0f 100644
--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts
+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts
@@ -104,6 +104,7 @@ export interface ChatListControllerHost {
beginSelectionIntent: () => number
getSelectionIntentRevision: () => number
clearPendingSelection: (agentRef: string, preserveSpecificChatId?: string) => void
+ restoreLatestChatSelection: (agentRef: string) => Promise<void>
scrollChatToBottom: () => void
dispatchSession: (chatKey: string, event: SessionFsmEvent) => void
clearComposerDraft: (chatId: string) => void
@@ -1062,6 +1063,13 @@ export function useChatListController({
if (restoreChatId) {
const restore = host.current?.switchToChat(agentRef, restoreChatId)
void restore?.catch(() => undefined)
+ } else if (
+ pendingSelection?.mode !== 'none' &&
+ activeChatIdAtCreate === null &&
+ !chatMessagesLoadingAtCreate
+ ) {
+ const restoreLatest = host.current?.restoreLatestChatSelection(agentRef)
+ void restoreLatest?.catch(() => undefined)
}
}
throw error
@@ -1762,6 +1770,7 @@ export function useChatListController({
// eslint-disable-next-line react-hooks/exhaustive-deps
[latestChatSessions, isHostAccessBlocked, hostAuthorityRevision]
)
+ const getChatListRequestGeneration = useCallback(() => requestGenerationRef.current, [])
return {
// State (public contract, re-exported unchanged by the parent).
@@ -1782,6 +1791,7 @@ export function useChatListController({
handleDeleteChatForAgent,
// Loader + list-loading control (parent agent-selection effect).
loadChatList,
+ getChatListRequestGeneration,
loadMoreChatSessions,
setChatListLoading,
clearList,