Skip to content

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

TypeScript

fix(desktop-ui): r12-l2 close switch admission at settlement

Josuée24997a7343c
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/switchExecutionLifecycle.r12.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/switchExecutionLifecycle.r12.test.tsx
new file mode 100644
index 000000000..8d7716929
--- /dev/null
+++ b/desktop-app/ui/src/hooks/domain/__tests__/switchExecutionLifecycle.r12.test.tsx
@@ -0,0 +1,90 @@
+// @vitest-environment jsdom
+import { afterEach, expect, it, vi } from 'vitest'
+import { act, cleanup, waitFor } from '@testing-library/react'
+import { DESKTOP_ROUTES } from '@constants/navigation'
+import { deferred } from './__fixtures__/catalogFixtures'
+import { renderController } from './__fixtures__/controllerHarness'
+import { installMockClerum, uninstallMockClerum } from './__fixtures__/mockClerum'
+
+afterEach(() => {
+ cleanup()
+ vi.restoreAllMocks()
+ uninstallMockClerum()
+})
+
+it('retries an independently launched effect after the imperative execution settled', async () => {
+ const clerum = installMockClerum()
+ for (const id of ['chat-1', 'chat-2']) {
+ await clerum.chat.create('agent-x', id)
+ await clerum.chat.upsertMessages('agent-x', id, [
+ { id: `${id}-history`, role: 'user', content: `${id} actual history`, timestamp: 1 },
+ ])
+ }
+ const controller = renderController({ loadMenuData: false })
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ await act(async () => controller.result.current.switchToChat('agent-x', 'chat-1'))
+ act(() => controller.rerender({ navItem: DESKTOP_ROUTES.chat }))
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ // Re-open after the route's implicit selection settles, exactly as the user
+ // starts launch-from-chat with a committed conversation.
+ await act(async () => controller.result.current.switchToChat('agent-x', 'chat-1'))
+ clerum.chat.setLastActive.mockClear()
+ const error = new Error('first opening failed')
+ clerum.chat.setLastActive.mockRejectedValueOnce(error)
+ let request!: Promise<void>
+ const report = controller.spies.pushToast
+ await act(async () => {
+ controller.result.current.setPendingChatSelection('agent-x', 'chat-2', { deferSwitch: true })
+ request = controller.result.current.switchToChat('agent-x', 'chat-2', {
+ onCurrentError: cause => report(`Opening failed: ${String(cause)}`, 'error'),
+ })
+ const observed = request.catch(cause => cause)
+ controller.rerender({ navItem: DESKTOP_ROUTES.apps })
+ expect(await observed).toBe(error)
+ })
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBe('chat-2')
+ expect(controller.result.current.chatMessages).toEqual([
+ expect.objectContaining({ id: 'chat-2-history', content: 'chat-2 actual history' }),
+ ])
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ })
+ expect(clerum.chat.setLastActive.mock.calls.map(([, id]) => id)).toEqual(['chat-2', 'chat-2'])
+ expect(report).toHaveBeenCalledTimes(1)
+})
+
+it('keeps a captured in-flight duplicate even when it settles before catalog completion', async () => {
+ const clerum = installMockClerum()
+ await clerum.chat.create('agent-x', 'chat-2')
+ await clerum.chat.upsertMessages('agent-x', 'chat-2', [
+ { id: 'history-2', role: 'user', content: 'second transcript', timestamp: 1 },
+ ])
+ const controller = renderController({ loadMenuData: false })
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ const catalog = deferred<Awaited<ReturnType<typeof clerum.readIndex>>>()
+ const opening = deferred<void>()
+ clerum.chat.getIndex.mockReturnValueOnce(catalog.promise)
+ clerum.chat.setLastActive.mockImplementationOnce(() => opening.promise)
+ let request!: Promise<void>
+ act(() => {
+ controller.result.current.setPendingChatSelection('agent-x', 'chat-2', { deferSwitch: true })
+ request = controller.result.current.switchToChat('agent-x', 'chat-2', {
+ onCurrentError: cause => controller.spies.pushToast(String(cause), 'error'),
+ })
+ controller.rerender({ navItem: DESKTOP_ROUTES.chat })
+ })
+ const observed = request.catch(cause => cause)
+ await waitFor(() => expect(clerum.chat.getIndex).toHaveBeenCalledTimes(2))
+ const error = new Error('one logical failure')
+ await act(async () => {
+ opening.reject(error)
+ expect(await observed).toBe(error)
+ catalog.resolve(await clerum.readIndex('agent-x'))
+ })
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ expect(clerum.chat.setLastActive).toHaveBeenCalledTimes(1)
+ expect(controller.spies.pushToast).toHaveBeenCalledTimes(1)
+ expect(controller.result.current.activeChatId).toBe('chat-2')
+ expect(controller.result.current.chatMessages).toEqual([])
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+})
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index e4f7d9968..7cfbece6e 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -450,8 +450,8 @@ export function useAgentChatController({
const [activeChatId, setActiveChatId] = useState<string | null>(null)
const selectionIntentRevisionRef = useRef(0)
- // A matching pending effect awaits this exact execution, even if it settled
- // before the catalog read. The promise retains the outcome, never an error owner.
+ // Admission closes at settlement. An effect that captured this execution
+ // while in flight may still await its result after its catalog read.
const pendingChatSwitchRequestRef = useRef<PendingChatSwitchRequest | null>(null)
const activeChatSwitchRequestRef = useRef<symbol | null>(null)
const activeChatSwitchKeyRef = useRef<string | null>(null)
@@ -1217,17 +1217,24 @@ export function useAgentChatController({
const promise = runSwitchForIntent(agentRef, chatId, selectionIntentRevision, error => {
options.onCurrentError?.(error)
})
- pendingChatSwitchRequestRef.current =
+ const request: PendingChatSwitchRequest | null =
readPendingSelection(agentRef)?.mode === 'specific'
? {
agentRef,
chatId,
revision: selectionIntentRevision,
scope: currentAuthScopeRef.current,
+ settled: false,
promise,
}
: null
- await promise
+ pendingChatSwitchRequestRef.current = request
+ try {
+ await promise
+ } finally {
+ // Mark this execution, not whatever successor now occupies the ref.
+ if (request) request.settled = true
+ }
},
[
beginSelectionIntent,
@@ -1463,6 +1470,7 @@ export function useAgentChatController({
const pendingSwitch = pendingChatSwitchRequestRef.current
const sharesPendingSwitch = Boolean(
requestedSelection?.mode === 'specific' &&
+ !pendingSwitch?.settled &&
pendingSwitch?.revision === selectionIntentRevision &&
pendingSwitch.scope === currentAuthScopeRef.current &&
pendingSwitch.agentRef === selectedAgent &&
diff --git a/desktop-app/ui/src/lib/chatSwitchRequest.types.ts b/desktop-app/ui/src/lib/chatSwitchRequest.types.ts
index 4df9c9ff3..9577d8feb 100644
--- a/desktop-app/ui/src/lib/chatSwitchRequest.types.ts
+++ b/desktop-app/ui/src/lib/chatSwitchRequest.types.ts
@@ -3,5 +3,6 @@ export interface PendingChatSwitchRequest {
chatId: string
revision: number
scope: string
+ settled: boolean
promise: Promise<void>
}