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-l3 prove switch admission and scope guards

Josué2a8962812ff1
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/mockClerum.ts b/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/mockClerum.ts
index d0dd4986e..b36b8f1c8 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/mockClerum.ts
+++ b/desktop-app/ui/src/hooks/domain/__tests__/__fixtures__/mockClerum.ts
@@ -79,6 +79,8 @@ export interface MockClerum {
persistedMessages: (agentRef: string, chatId: string) => Promise<Array<Record<string, unknown>>>
/** The index a real ChatStore read produces for one agent. */
readIndex: (agentRef: string) => Promise<ChatIndex>
+ /** Persist selection through the same producer used by chat:setLastActive. */
+ persistLastActive: (agentRef: string, chatId: string) => Promise<void>
}
export function installMockClerum(): MockClerum {
@@ -181,6 +183,8 @@ export function installMockClerum(): MockClerum {
persistedMessages: async (agentRef: string, chatId: string) =>
(await store.loadMessages(agentRef, chatId)) as unknown as Array<Record<string, unknown>>,
readIndex: (agentRef: string) => store.getIndex(agentRef),
+ persistLastActive: (agentRef: string, chatId: string) =>
+ store.setLastActiveChatId(agentRef, chatId),
}
Object.defineProperty(window, 'clerum', {
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/createRecoveryScopeWindow.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/createRecoveryScopeWindow.test.tsx
index 2f8df3c67..8e612eee1 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/createRecoveryScopeWindow.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/createRecoveryScopeWindow.test.tsx
@@ -18,7 +18,20 @@ afterEach(() => {
it('does not restore an old-team chat when create rejects in the new render layout effect', async () => {
const clerum = installMockClerum()
+ await clerum.chat.create('agent-x', 'chat-a')
await clerum.chat.create('agent-x', 'chat-b')
+ await clerum.persistLastActive('agent-x', 'chat-a')
+ clerum.chat.setLastActive.mockImplementation(clerum.persistLastActive)
+ const loadMessages = clerum.chat.loadMessages.getMockImplementation()!
+ const restoredHistory = deferred<void>()
+ clerum.chat.loadMessages.mockImplementation(async (...args) => {
+ try {
+ return await loadMessages(...args)
+ } finally {
+ restoredHistory.resolve()
+ }
+ })
+ expect((await clerum.readIndex('agent-x')).lastActiveChatId).toBe('chat-a')
await clerum.chat.upsertMessages('agent-x', 'chat-b', [
{ id: 'old-team-history', role: 'user', content: 'team-one history', timestamp: 1 },
])
@@ -74,9 +87,18 @@ it('does not restore an old-team chat when create rejects in the new render layo
await layout.promise
expect(await rejected.promise).toBe(createError)
await waitFor(() => expect(app.result.current.team).toBe('team-2'))
- expect(clerum.chat.setLastActive).not.toHaveBeenCalled()
- expect(app.result.current.controller.activeChatId).toBeNull()
- expect(app.result.current.controller.chatMessages).toEqual([])
- expect(app.result.current.controller.chatMessagesLoading).toBe(false)
- expect(params.navItem).toBe(DESKTOP_ROUTES.agents)
+ try {
+ expect((await clerum.readIndex('agent-x')).lastActiveChatId).toBe('chat-a')
+ expect(clerum.chat.setLastActive).not.toHaveBeenCalled()
+ expect(app.result.current.controller.activeChatId).toBeNull()
+ expect(app.result.current.controller.chatMessages).toEqual([])
+ expect(app.result.current.controller.chatMessagesLoading).toBe(false)
+ expect(params.navItem).toBe(DESKTOP_ROUTES.agents)
+ } finally {
+ // A guard-removal run can launch real restore I/O; let that producer finish
+ // before afterEach removes the bridge, preserving behavioral failure proof.
+ if (clerum.chat.setLastActive.mock.calls.length) {
+ await act(async () => await restoredHistory.promise)
+ }
+ }
})
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/notificationSwitchErrors.characterization.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/notificationSwitchErrors.characterization.test.tsx
index 229ab192a..3ea294664 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/notificationSwitchErrors.characterization.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/notificationSwitchErrors.characterization.test.tsx
@@ -66,6 +66,10 @@ describe('notification switch error ownership', () => {
)
const status = app.result.current.statusText
const toasts = app.result.current.toasts
+ const activeTabId = app.result.current.workspaceTabs.activeTabId
+ expect(
+ app.result.current.workspaceTabs.tabs.find(tab => tab.id === activeTabId)?.chat
+ ).toEqual({ agentRef: 'agent-x', chatId: 'chat-3' })
await act(async () => {
gate.reject(new Error('obsolete notification failed'))
await notification
@@ -75,6 +79,7 @@ describe('notification switch error ownership', () => {
expect.objectContaining({ id: 'chat-3-history' }),
])
expect(app.result.current.chatMessagesLoading).toBe(false)
+ expect(app.result.current.workspaceTabs.activeTabId).toBe(activeTabId)
expect(app.result.current.statusText).toBe(status)
expect(app.result.current.toasts).toEqual(toasts)
} finally {
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/pendingSwitchScope.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/pendingSwitchScope.test.tsx
index 55a92ed3b..b59f3b3e9 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/pendingSwitchScope.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/pendingSwitchScope.test.tsx
@@ -1,9 +1,13 @@
// @vitest-environment jsdom
+import { useLayoutEffect, useState } from 'react'
import { afterEach, expect, it, vi } from 'vitest'
-import { act, cleanup, waitFor } from '@testing-library/react'
+import { AgentTaskTrackerProvider } from '@contexts/AgentTaskTrackerContext'
+import { act, cleanup, renderHook, waitFor } from '@testing-library/react'
import { DESKTOP_ROUTES } from '@constants/navigation'
+import { useAgentChatController } from '../useAgentChatController'
import { deferred } from './__fixtures__/catalogFixtures'
import { renderController } from './__fixtures__/controllerHarness'
+import { useHarnessHostAuthority } from './__fixtures__/hostAuthorityHarness'
import { installMockClerum, uninstallMockClerum } from './__fixtures__/mockClerum'
afterEach(() => {
@@ -12,6 +16,73 @@ afterEach(() => {
uninstallMockClerum()
})
+it('suppresses old-team switch feedback when rejection occurs in the new render layout effect', async () => {
+ const clerum = installMockClerum()
+ await clerum.chat.create('agent-x', 'chat-b')
+ await clerum.chat.upsertMessages('agent-x', 'chat-b', [
+ { id: 'scope-history', role: 'user', content: 'persisted team-one history', timestamp: 1 },
+ ])
+ const seed = renderController({ loadMenuData: false })
+ const { params, spies } = seed
+ seed.unmount()
+ const gate = deferred<void>()
+ const entered = deferred<void>()
+ const layout = deferred<void>()
+ const error = new Error('previous-team switch rejected')
+ let rejectInLayout = false
+ const app = renderHook(
+ () => {
+ const [team, setTeam] = useState('team-1')
+ const authority = useHarnessHostAuthority()
+ const controller = useAgentChatController({
+ ...params,
+ currentTeamId: team,
+ chatAuthorityTeamId: team,
+ isHostAccessBlocked: authority.isHostAccessBlocked,
+ getHostAuthorityEpoch: authority.getHostAuthorityEpoch,
+ hostAuthorityRevision: authority.revision,
+ onHostAccessRevoked: authority.onHostAccessRevoked,
+ onHostAuthorityUncertain: authority.onHostAuthorityUncertain,
+ } as Parameters<typeof useAgentChatController>[0])
+ useLayoutEffect(() => {
+ if (team === 'team-2' && rejectInLayout) {
+ gate.reject(error)
+ layout.resolve()
+ }
+ }, [team])
+ return { controller, team, setTeam }
+ },
+ { wrapper: ({ children }) => <AgentTaskTrackerProvider>{children}</AgentTaskTrackerProvider> }
+ )
+ await waitFor(() => expect(app.result.current.controller.chatListLoading).toBe(false))
+ clerum.chat.setLastActive.mockImplementationOnce(async () => {
+ entered.resolve()
+ await gate.promise
+ })
+ let request!: Promise<void>
+ act(() => {
+ request = app.result.current.controller.switchToChat('agent-x', 'chat-b', {
+ onCurrentError: failure =>
+ spies.pushToast(
+ `Could not open conversation: ${failure instanceof Error ? failure.message : String(failure)}`,
+ 'error'
+ ),
+ })
+ })
+ const observed = request.catch(failure => failure)
+ await entered.promise
+ rejectInLayout = true
+ // Normal-priority commit: layout rejects before passive effects advance the
+ // selection revision. Only render-updated scope identity can suppress feedback.
+ app.result.current.setTeam('team-2')
+ await layout.promise
+ expect(await observed).toBe(error)
+ await waitFor(() => expect(app.result.current.team).toBe('team-2'))
+ expect(spies.pushToast).not.toHaveBeenCalled()
+ expect(app.result.current.controller.chatMessages).toEqual([])
+ expect(app.result.current.controller.chatMessagesLoading).toBe(false)
+})
+
it('starts new scoped history instead of joining the old-team pending switch', async () => {
const clerum = installMockClerum()
await clerum.chat.create('agent-x', 'chat-b')
@@ -32,7 +103,11 @@ it('starts new scoped history instead of joining the old-team pending switch', a
act(() => {
controller.result.current.setPendingChatSelection('agent-x', 'chat-b', { deferSwitch: true })
request = controller.result.current.switchToChat('agent-x', 'chat-b', {
- onCurrentError: () => undefined,
+ onCurrentError: failure =>
+ controller.spies.pushToast(
+ `Could not open conversation: ${failure instanceof Error ? failure.message : String(failure)}`,
+ 'error'
+ ),
})
})
const observed = request.catch(error => error)
@@ -56,4 +131,5 @@ it('starts new scoped history instead of joining the old-team pending switch', a
expect.objectContaining({ id: 'real-history' }),
])
})
+ expect(controller.spies.pushToast).not.toHaveBeenCalled()
})
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/switchJoinRevision.r12.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/switchJoinRevision.r12.test.tsx
new file mode 100644
index 000000000..0edc7357d
--- /dev/null
+++ b/desktop-app/ui/src/hooks/domain/__tests__/switchJoinRevision.r12.test.tsx
@@ -0,0 +1,52 @@
+// @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('does not join an old revision still in flight when a newer same-chat intent owns loading', async () => {
+ const clerum = installMockClerum()
+ await clerum.chat.create('agent-x', 'chat-2')
+ await clerum.chat.upsertMessages('agent-x', 'chat-2', [
+ { id: 'actual-chat-2', role: 'user', content: 'current intent history', timestamp: 1 },
+ ])
+ const controller = renderController({ loadMenuData: false })
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ const old = deferred<void>()
+ clerum.chat.setLastActive.mockImplementationOnce(() => old.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'),
+ })
+ })
+ const observed = request.catch(cause => cause)
+ act(() => {
+ controller.result.current.setPendingChatSelection('agent-x', 'chat-2', { deferSwitch: true })
+ controller.rerender({ navItem: DESKTOP_ROUTES.chat })
+ })
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ const error = new Error('old revision failed')
+ await act(async () => {
+ old.reject(error)
+ expect(await observed).toBe(error)
+ })
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBe('chat-2')
+ expect(controller.result.current.chatMessages).toEqual([
+ expect.objectContaining({ id: 'actual-chat-2', content: 'current intent 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 162c0dca5..cc1223b37 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -1217,23 +1217,20 @@ export function useAgentChatController({
const promise = runSwitchForIntent(agentRef, chatId, selectionIntentRevision, error => {
options.onCurrentError?.(error)
})
- const request: PendingChatSwitchRequest | null =
- readPendingSelection(agentRef)?.mode === 'specific'
- ? {
- agentRef,
- chatId,
- revision: selectionIntentRevision,
- scope: currentAuthScopeRef.current,
- settled: false,
- promise,
- }
- : null
+ const request: PendingChatSwitchRequest = {
+ agentRef,
+ chatId,
+ revision: selectionIntentRevision,
+ scope: currentAuthScopeRef.current,
+ settled: false,
+ promise,
+ }
pendingChatSwitchRequestRef.current = request
try {
await promise
} finally {
// Mark this execution, not whatever successor now occupies the ref.
- if (request) request.settled = true
+ request.settled = true
}
},
[
@@ -1241,7 +1238,6 @@ export function useAgentChatController({
clearPendingSelection,
isChatDeleted,
isHostAccessBlocked,
- readPendingSelection,
runSwitchForIntent,
]
)