Skip to content

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

TypeScript

fix(desktop-ui): r13-l1 distinguish successful switch settlement

Josué498c9c362d1d
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
index 8d7716929..69c6d152a 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/switchExecutionLifecycle.r12.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/switchExecutionLifecycle.r12.test.tsx
@@ -12,6 +12,52 @@ afterEach(() => {
uninstallMockClerum()
})
+it('preserves a successful same-intent switch while a duplicate effect waits for its catalog', async () => {
+ const clerum = installMockClerum()
+ clerum.chat.getIndex.mockImplementation(agent => clerum.readIndex(agent))
+ clerum.chat.setLastActive.mockImplementation((agent, chat) =>
+ clerum.persistLastActive(agent, chat)
+ )
+ 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'))
+ clerum.chat.setLastActive.mockClear()
+ await act(async () => {
+ controller.result.current.setPendingChatSelection('agent-x', 'chat-2', { deferSwitch: true })
+ await controller.result.current.switchToChat('agent-x', 'chat-2', {
+ onCurrentError: cause => controller.spies.pushToast(String(cause), 'error'),
+ })
+ })
+ const expectedHistory = [
+ expect.objectContaining({ id: 'chat-2-history', content: 'chat-2 actual history' }),
+ ]
+ expect(controller.result.current.chatMessages).toEqual(expectedHistory)
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ const catalog = deferred<Awaited<ReturnType<typeof clerum.readIndex>>>()
+ clerum.chat.getIndex.mockReturnValueOnce(catalog.promise)
+ const catalogCalls = clerum.chat.getIndex.mock.calls.length
+ act(() => controller.rerender({ navItem: DESKTOP_ROUTES.apps }))
+ await waitFor(() => expect(clerum.chat.getIndex).toHaveBeenCalledTimes(catalogCalls + 1))
+ expect(controller.result.current.activeChatId).toBe('chat-2')
+ expect(controller.result.current.chatMessages).toEqual(expectedHistory)
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ expect(clerum.chat.setLastActive).toHaveBeenCalledTimes(1)
+ await act(async () => catalog.resolve(await clerum.readIndex('agent-x')))
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ expect(controller.result.current.activeChatId).toBe('chat-2')
+ expect(controller.result.current.chatMessages).toEqual(expectedHistory)
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ expect(clerum.chat.setLastActive).toHaveBeenCalledTimes(1)
+ expect((await clerum.readIndex('agent-x')).lastActiveChatId).toBe('chat-2')
+ expect(controller.spies.pushToast).not.toHaveBeenCalled()
+})
+
it('retries an independently launched effect after the imperative execution settled', async () => {
const clerum = installMockClerum()
for (const id of ['chat-1', 'chat-2']) {
@@ -88,3 +134,59 @@ it('keeps a captured in-flight duplicate even when it settles before catalog com
expect(controller.result.current.chatMessages).toEqual([])
expect(controller.result.current.chatMessagesLoading).toBe(false)
})
+
+it('settles a superseded rejection without settling or duplicating its in-flight successor', async () => {
+ const clerum = installMockClerum()
+ clerum.chat.getIndex.mockImplementation(agent => clerum.readIndex(agent))
+ 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))
+ const old = deferred<void>()
+ const successor = deferred<void>()
+ clerum.chat.setLastActive
+ .mockImplementationOnce(() => old.promise)
+ .mockImplementationOnce(() => successor.promise)
+ let first!: Promise<void>
+ let second!: Promise<void>
+ act(() => {
+ first = controller.result.current.switchToChat('agent-x', 'chat-1', {
+ onCurrentError: cause => controller.spies.pushToast(String(cause), 'error'),
+ })
+ controller.result.current.setPendingChatSelection('agent-x', 'chat-2', { deferSwitch: true })
+ second = controller.result.current.switchToChat('agent-x', 'chat-2', {
+ onCurrentError: cause => controller.spies.pushToast(String(cause), 'error'),
+ })
+ })
+ const observed = first.catch(cause => cause)
+ const error = new Error('superseded first switch failed')
+ await act(async () => {
+ old.reject(error)
+ expect(await observed).toBe(error)
+ })
+ expect(controller.result.current.activeChatId).toBe('chat-2')
+ expect(controller.result.current.chatMessagesLoading).toBe(true)
+ const catalog = deferred<Awaited<ReturnType<typeof clerum.readIndex>>>()
+ clerum.chat.getIndex.mockReturnValueOnce(catalog.promise)
+ act(() => controller.rerender({ navItem: DESKTOP_ROUTES.apps }))
+ await act(async () => catalog.resolve(await clerum.readIndex('agent-x')))
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ expect(clerum.chat.setLastActive.mock.calls.map(([, id]) => id)).toEqual(['chat-1', 'chat-2'])
+ expect(controller.result.current.chatMessagesLoading).toBe(true)
+ expect(controller.result.current.chatMessages).toEqual([])
+ await act(async () => {
+ successor.resolve()
+ expect(await second).toBeUndefined()
+ })
+ 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).toHaveBeenCalledTimes(2)
+ expect(controller.spies.pushToast).not.toHaveBeenCalled()
+})
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index a78920ea5..663a5bd93 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)
- // Admission closes at settlement. An effect that captured this execution
- // while in flight may still await its result after its catalog read.
+ // Effects can share an in-flight execution or its successful same-intent
+ // result. A previously rejected execution instead permits an independent retry.
const pendingChatSwitchRequestRef = useRef<PendingChatSwitchRequest | null>(null)
const activeChatSwitchRequestRef = useRef<symbol | null>(null)
const activeChatSwitchKeyRef = useRef<string | null>(null)
@@ -1222,15 +1222,17 @@ export function useAgentChatController({
chatId,
revision: selectionIntentRevision,
scope: currentAuthScopeRef.current,
- settled: false,
+ state: 'pending',
promise,
}
pendingChatSwitchRequestRef.current = request
try {
await promise
- } finally {
- // Mark this execution, not whatever successor now occupies the ref.
- request.settled = true
+ // Settle this execution, not whatever successor now occupies the ref.
+ request.state = 'fulfilled'
+ } catch (error) {
+ request.state = 'rejected'
+ throw error
}
},
[
@@ -1467,7 +1469,7 @@ export function useAgentChatController({
const pendingSwitch = pendingChatSwitchRequestRef.current
const sharesPendingSwitch = Boolean(
requestedSelection?.mode === 'specific' &&
- !pendingSwitch?.settled &&
+ pendingSwitch?.state !== 'rejected' &&
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 9577d8feb..563971d80 100644
--- a/desktop-app/ui/src/lib/chatSwitchRequest.types.ts
+++ b/desktop-app/ui/src/lib/chatSwitchRequest.types.ts
@@ -3,6 +3,6 @@ export interface PendingChatSwitchRequest {
chatId: string
revision: number
scope: string
- settled: boolean
+ state: 'pending' | 'fulfilled' | 'rejected'
promise: Promise<void>
}