Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-ui): r5-l2 verify switch reporter boundaries
Josué08a88840c6bd
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/switchReportingBranches.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/switchReportingBranches.test.tsxnew file mode 100644index 000000000..847ac056b--- /dev/null+++ b/desktop-app/ui/src/hooks/domain/__tests__/switchReportingBranches.test.tsx@@ -0,0 +1,128 @@+// @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, serverSessions } from './__fixtures__/catalogFixtures'+import { renderController } from './__fixtures__/controllerHarness'+import { installMockClerum, uninstallMockClerum } from './__fixtures__/mockClerum'++afterEach(() => {+ cleanup()+ vi.restoreAllMocks()+ uninstallMockClerum()+})++it.each(['effect cleanup', 'ownerless switch'] as const)(+ 'suppresses stale effect failure after %s',+ async successor => {+ const clerum = installMockClerum()+ for (const id of ['requested', 'successor']) {+ await clerum.chat.create('agent-x', id)+ await clerum.chat.upsertMessages('agent-x', id, [+ { id: `${id}-history`, role: 'user', content: `${id} history`, timestamp: 1 },+ ])+ }+ const controller = renderController({ loadMenuData: false })+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))+ const entered = deferred<void>()+ const gate = deferred<void>()+ const setLastActive = clerum.chat.setLastActive.getMockImplementation()!+ clerum.chat.setLastActive.mockImplementation(async (agent, id) => {+ if (id === 'requested') {+ entered.resolve()+ await gate.promise+ }+ return setLastActive(agent, id)+ })+ act(() => {+ controller.result.current.setPendingChatSelection('agent-x', 'requested')+ controller.rerender({ navItem: DESKTOP_ROUTES.chat })+ })+ await entered.promise+ if (successor === 'effect cleanup') {+ act(() => controller.rerender({ navItem: DESKTOP_ROUTES.agents }))+ await waitFor(() => expect(controller.result.current.activeChatId).toBeNull())+ } else {+ await act(async () => controller.result.current.switchToChat('agent-x', 'successor'))+ }+ await act(async () => {+ gate.reject(new Error('stale effect rejected'))+ })+ expect(controller.spies.pushToast).not.toHaveBeenCalled()+ expect(controller.result.current.chatMessagesLoading).toBe(false)+ expect(controller.result.current.activeChatId).toBe(+ successor === 'effect cleanup' ? null : 'successor'+ )+ expect(controller.result.current.chatMessages).toEqual(+ successor === 'effect cleanup' ? [] : [expect.objectContaining({ id: 'successor-history' })]+ )+ }+)++it('reports the current catalog auto-selection failure exactly once', async () => {+ const clerum = installMockClerum()+ const catalog = deferred<ReturnType<typeof serverSessions>>()+ clerum.rpc.listSessions.mockReturnValue(catalog.promise)+ const controller = renderController({ navItem: DESKTOP_ROUTES.chat, loadMenuData: false })+ await waitFor(() => expect(clerum.rpc.listSessions).toHaveBeenCalled())+ clerum.chat.setLastActive.mockRejectedValueOnce(new Error('catalog destination failed'))+ await act(async () =>+ catalog.resolve(serverSessions([{ agent: 'agent-x', chatId: 'remote-newest' }]))+ )+ await waitFor(() => expect(controller.spies.pushToast).toHaveBeenCalledTimes(1))+ expect(controller.spies.pushToast).toHaveBeenCalledWith(+ 'Could not open conversation: catalog destination failed',+ 'error'+ )+ expect(controller.result.current.activeChatId).toBe('remote-newest')+ expect(controller.result.current.chatMessages).toEqual([])+ expect(controller.result.current.chatMessagesLoading).toBe(false)+})++it.each([false, true])(+ 'reports restore failure only while its request is current (superseded=%s)',+ async superseded => {+ const clerum = installMockClerum()+ for (const id of ['restore', 'successor']) {+ await clerum.chat.create('agent-x', id)+ await clerum.chat.upsertMessages('agent-x', id, [+ { id: `${id}-history`, role: 'user', content: `${id} history`, timestamp: 1 },+ ])+ }+ const controller = renderController({ loadMenuData: false })+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))+ act(() =>+ controller.result.current.setPendingChatSelection('agent-x', 'restore', { deferSwitch: true })+ )+ const gate = deferred<void>()+ const entered = deferred<void>()+ const setLastActive = clerum.chat.setLastActive.getMockImplementation()!+ clerum.chat.setLastActive.mockImplementation(async (agent, id) => {+ if (id === 'restore') {+ entered.resolve()+ await gate.promise+ }+ return setLastActive(agent, id)+ })+ const createError = new Error('create failed')+ clerum.chat.create.mockRejectedValueOnce(createError)+ await act(async () =>+ expect(controller.result.current.handleCreateChat()).rejects.toBe(createError)+ )+ await entered.promise+ if (superseded)+ await act(async () => controller.result.current.switchToChat('agent-x', 'successor'))+ await act(async () => gate.reject(new Error('restore failed')))+ expect(controller.spies.pushToast).toHaveBeenCalledTimes(superseded ? 0 : 1)+ if (!superseded)+ expect(controller.spies.pushToast).toHaveBeenCalledWith(+ 'Could not restore conversation: restore failed',+ 'error'+ )+ expect(controller.result.current.activeChatId).toBe(superseded ? 'successor' : 'restore')+ expect(controller.result.current.chatMessages).toEqual(+ superseded ? [expect.objectContaining({ id: 'successor-history' })] : []+ )+ 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.tsindex 63c993712..20b3c65ad 100644--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts@@ -1461,9 +1461,7 @@ export function useAgentChatController({pendingSwitch.agentRef === selectedAgent &&pendingSwitch.chatId === requestedSelection.chatId)- let switchStarted = falseconst switchForEffect = (chatId: string) => {- switchStarted = trueif (sharesPendingSwitch) return pendingSwitch!.promisereturn runSwitchForIntent(selectedAgent,@@ -1579,15 +1577,9 @@ export function useAgentChatController({}}settleChatMessagesLoading(selectionIntentRevision)- })().catch(error => {- if (cancelled || selectionIntentRevisionRef.current !== selectionIntentRevision) return- settleChatMessagesLoading(selectionIntentRevision)- if (switchStarted) return- liveDepsRef.current.pushToast(- `Could not open conversation: ${error instanceof Error ? error.message : String(error)}`,- 'error'- )- })+ // List failures are normalized by loadChatList; switch failures are reported+ // by their request. This continuation observes rejection without a second owner.+ })().catch(() => undefined)return () => {cancelled = truediff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.tsindex 249bddeac..c330b336a 100644--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts@@ -1070,15 +1070,16 @@ export function useChatListController({? activeChatIdAtCreate: nullif (restoreChatId) {- const restore = host.current?.switchToChat(agentRef, restoreChatId)- const restoreSelectionRevision = host.current?.getSelectionIntentRevision()- void restore?.catch(error => {- if (host.current?.getSelectionIntentRevision() !== restoreSelectionRevision) return- host.current?.pushToast(- `Could not restore conversation: ${error instanceof Error ? error.message : String(error)}`,- 'error'- )- })+ void host.current+ ?.switchToChat(agentRef, restoreChatId, {+ onCurrentError: error => {+ host.current?.pushToast(+ `Could not restore conversation: ${error instanceof Error ? error.message : String(error)}`,+ 'error'+ )+ },+ })+ .catch(() => undefined)}}throw error