Skip to content

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

TypeScript

fix(desktop-ui): r8-m1 preserve visible chat history ownership

Josuébb616bd519a9
diff --git a/desktop-app/ui/src/hooks/__tests__/useAppController.chatDrawer.test.tsx b/desktop-app/ui/src/hooks/__tests__/useAppController.chatDrawer.test.tsx
index 82ec59040..03921fa72 100644
--- a/desktop-app/ui/src/hooks/__tests__/useAppController.chatDrawer.test.tsx
+++ b/desktop-app/ui/src/hooks/__tests__/useAppController.chatDrawer.test.tsx
@@ -6,6 +6,7 @@ import {
installAppControllerClerum,
renderAppController,
} from '../domain/__tests__/__fixtures__/appControllerHarness'
+import { deferred } from '../domain/__tests__/__fixtures__/catalogFixtures'
import { uninstallMockClerum } from '../domain/__tests__/__fixtures__/mockClerum'
;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
@@ -211,4 +212,85 @@ describe('useAppController — chat drawer keepNavItem', () => {
expect(app.result.current.activeChatId).toBe('chat-2')
await waitFor(() => expect(app.result.current.chatMessagesLoading).toBe(false))
})
+
+ it('preserves the visible conversation while a no-chat notification is opened', async () => {
+ const { clerum } = installAppControllerClerum({ agentNames: ['agent-x'] })
+ await clerum.chat.create('agent-x', 'chat-1')
+ await clerum.chat.upsertMessages('agent-x', 'chat-1', [
+ { id: 'chat-1-message', role: 'user', content: 'visible chat history', timestamp: 1 },
+ ])
+ const originalSetLastActive = clerum.chat.setLastActive.getMockImplementation()
+ if (!originalSetLastActive) throw new Error('Expected the mock setLastActive implementation')
+ const entered = deferred<void>()
+ const release = deferred<void>()
+ const returned = deferred<void>()
+ let held = false
+ clerum.chat.setLastActive.mockImplementation(async (agentRef, chatId) => {
+ if (agentRef === 'agent-x' && chatId === 'chat-1' && !held) {
+ held = true
+ entered.resolve()
+ await release.promise
+ }
+ await originalSetLastActive(agentRef, chatId)
+ if (held && chatId === 'chat-1') returned.resolve()
+ })
+ const app = renderAppController()
+ unmount = app.unmount
+
+ try {
+ await waitFor(() => expect(app.result.current.isAuthenticated).toBe(true))
+ await waitFor(() => expect(app.result.current.initialExperienceLoading).toBe(false))
+
+ act(() => {
+ app.result.current.handleSelectChatAgent('agent-x', {
+ chatId: 'chat-1',
+ selectLatest: false,
+ })
+ })
+ await act(async () => {
+ await entered.promise
+ })
+ expect(app.result.current.activeChatId).toBe('chat-1')
+ expect(app.result.current.chatMessagesLoading).toBe(true)
+
+ await act(async () => {
+ await app.result.current.handleOpenNotification({
+ id: 'n-no-chat',
+ kind: 'approval_required',
+ agentName: 'agent-x',
+ teamId: 'team-1',
+ text: 'Open agent notification',
+ timestamp: Date.now(),
+ read: false,
+ approval: { taskId: 't1', requestId: 'r1' },
+ } as Parameters<typeof app.result.current.handleOpenNotification>[0])
+ })
+ const loadingAfterNotification = app.result.current.chatMessagesLoading
+
+ await act(async () => {
+ release.resolve()
+ await returned.promise
+ })
+ await waitFor(() => expect(app.result.current.chatMessagesLoading).toBe(false))
+
+ expect({
+ activeChatId: app.result.current.activeChatId,
+ loadingAfterNotification,
+ messages: app.result.current.activeMessages,
+ loadingAfter: app.result.current.chatMessagesLoading,
+ }).toEqual({
+ activeChatId: 'chat-1',
+ loadingAfterNotification: true,
+ messages: expect.arrayContaining([expect.objectContaining({ id: 'chat-1-message' })]),
+ loadingAfter: false,
+ })
+ } finally {
+ release.resolve()
+ await act(async () => {
+ await returned.promise
+ })
+ app.unmount()
+ unmount = null
+ }
+ })
})
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 9bc38df49..0d104fcee 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
@@ -57,8 +57,11 @@ function holdLocalMessages(chatId: string) {
}
describe('Round 7 selection loading ownership', () => {
- it('settles a held implicit switch when a newer no-chat intent arrives', async () => {
+ it('preserves visible chat history loading when a no-chat intent does not change the view', async () => {
await clerum.chat.create('agent-x', 'chat-a')
+ await clerum.chat.upsertMessages('agent-x', 'chat-a', [
+ { id: 'chat-a-message', role: 'user', content: 'chat A history', timestamp: 1 },
+ ])
clerum.chat.getIndex.mockImplementation(agentRef => clerum.readIndex(agentRef))
const heldSwitch = holdLastActive('chat-a')
const controller = renderController({ navItem: 'chat', loadMenuData: false })
@@ -73,16 +76,25 @@ describe('Round 7 selection loading ownership', () => {
act(() => controller.result.current.setPendingChatSelection('agent-x', null))
expect(controller.result.current.activeChatId).toBe('chat-a')
- expect(controller.result.current.chatMessagesLoading).toBe(false)
expect(controller.result.current.chatMessages).toEqual([])
+ const loadingWhileHeld = controller.result.current.chatMessagesLoading
await act(async () => {
heldSwitch.release.resolve()
await heldSwitch.returned.promise
})
- expect(controller.result.current.activeChatId).toBe('chat-a')
- expect(controller.result.current.chatMessagesLoading).toBe(false)
- expect(controller.result.current.chatMessages).toEqual([])
+ await waitFor(() => expect(controller.result.current.chatMessagesLoading).toBe(false))
+ expect({
+ activeChatId: controller.result.current.activeChatId,
+ loadingWhileHeld,
+ messages: controller.result.current.chatMessages,
+ loadingAfter: controller.result.current.chatMessagesLoading,
+ }).toEqual({
+ activeChatId: 'chat-a',
+ loadingWhileHeld: true,
+ messages: expect.arrayContaining([expect.objectContaining({ id: 'chat-a-message' })]),
+ loadingAfter: false,
+ })
} finally {
heldSwitch.release.resolve()
await act(async () => {
@@ -91,6 +103,70 @@ describe('Round 7 selection loading ownership', () => {
controller.unmount()
}
})
+
+ it('keeps the requested chat selected while its local history is slow', async () => {
+ await clerum.chat.create('agent-x', 'chat-a')
+ await clerum.chat.upsertMessages('agent-x', 'chat-a', [
+ { id: 'chat-a-message', role: 'user', content: 'chat A history', timestamp: 1 },
+ ])
+ await clerum.chat.create('agent-x', 'chat-b')
+ await clerum.chat.upsertMessages('agent-x', 'chat-b', [
+ { id: 'chat-b-message', role: 'user', content: 'chat B history', timestamp: 2 },
+ ])
+ clerum.chat.getIndex.mockImplementation(agentRef => clerum.readIndex(agentRef))
+ const controller = renderController({ navItem: 'chat', loadMenuData: false })
+ const heldHistory = holdLocalMessages('chat-b')
+ let historyEntered = false
+ let switchPromise!: Promise<void>
+
+ try {
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ await act(async () => {
+ await controller.result.current.switchToChat('agent-x', 'chat-a')
+ })
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBe('chat-a')
+ expect(controller.result.current.chatMessages).toEqual(
+ expect.arrayContaining([expect.objectContaining({ id: 'chat-a-message' })])
+ )
+ })
+
+ act(() => {
+ switchPromise = controller.result.current.handleSelectChat('chat-b')
+ })
+ await act(async () => {
+ await heldHistory.entered.promise
+ historyEntered = true
+ })
+ expect({
+ activeChatId: controller.result.current.activeChatId,
+ messages: controller.result.current.chatMessages,
+ loading: controller.result.current.chatMessagesLoading,
+ }).toEqual({ activeChatId: 'chat-b', messages: [], loading: true })
+
+ await act(async () => {
+ heldHistory.release.resolve()
+ await heldHistory.returned.promise
+ await switchPromise
+ })
+ expect({
+ activeChatId: controller.result.current.activeChatId,
+ messages: controller.result.current.chatMessages,
+ loading: controller.result.current.chatMessagesLoading,
+ }).toEqual({
+ activeChatId: 'chat-b',
+ messages: expect.arrayContaining([expect.objectContaining({ id: 'chat-b-message' })]),
+ loading: false,
+ })
+ } finally {
+ heldHistory.release.resolve()
+ await act(async () => {
+ if (historyEntered) await heldHistory.returned.promise
+ await switchPromise?.catch(() => undefined)
+ })
+ controller.unmount()
+ }
+ })
it('loads a newer specific chat when it supersedes a held implicit switch', async () => {
await clerum.chat.create('agent-x', 'chat-b')
await clerum.chat.upsertMessages('agent-x', 'chat-b', [
@@ -159,14 +235,17 @@ describe('Round 7 selection loading ownership', () => {
}
})
- it('settles the current spinner when a newer chat switch is Host-blocked', async () => {
+ it('keeps a valid history load when a newer target is Host-blocked', async () => {
await clerum.chat.create('agent-x', 'chat-a')
- await clerum.chat.create('agent-x', 'chat-b')
+ await clerum.chat.upsertMessages('agent-x', 'chat-a', [
+ { id: 'chat-a-message', role: 'user', content: 'chat A history', timestamp: 1 },
+ ])
+ await clerum.chat.create('agent-y', 'chat-b')
const heldSwitch = holdLastActive('chat-a')
let hostBlocked = false
const controller = renderController(
{
- isHostAccessBlocked: () => hostBlocked,
+ isHostAccessBlocked: agentRef => agentRef === 'agent-y' && hostBlocked,
loadMenuData: false,
},
{}
@@ -185,19 +264,31 @@ describe('Round 7 selection loading ownership', () => {
hostBlocked = true
await act(async () => {
- await controller.result.current.switchToChat('agent-x', 'chat-b')
+ await controller.result.current.switchToChat('agent-y', 'chat-b')
})
expect(controller.result.current.activeChatId).toBe('chat-a')
- expect(controller.result.current.chatMessagesLoading).toBe(false)
+ const loadingWhileHeld = controller.result.current.chatMessagesLoading
await act(async () => {
heldSwitch.release.resolve()
await switchPromise
})
- expect(controller.result.current.activeChatId).toBe('chat-a')
- expect(controller.result.current.chatMessages).toEqual([])
- expect(controller.result.current.chatMessagesLoading).toBe(false)
- expect(clerum.chat.loadMessages).not.toHaveBeenCalled()
+ expect({
+ activeChatId: controller.result.current.activeChatId,
+ loadingWhileHeld,
+ messages: controller.result.current.chatMessages,
+ loadingAfter: controller.result.current.chatMessagesLoading,
+ }).toEqual({
+ activeChatId: 'chat-a',
+ loadingWhileHeld: true,
+ messages: expect.arrayContaining([expect.objectContaining({ id: 'chat-a-message' })]),
+ loadingAfter: false,
+ })
+ expect(
+ clerum.chat.loadMessages.mock.calls.some(
+ ([agentRef, chatId]) => agentRef === 'agent-y' && chatId === 'chat-b'
+ )
+ ).toBe(false)
} finally {
heldSwitch.release.resolve()
await act(async () => {
@@ -207,8 +298,11 @@ describe('Round 7 selection loading ownership', () => {
}
})
- it('settles the current spinner when a newer chat switch targets a deleted chat', async () => {
+ it('keeps a valid history load when a newer target has been deleted', async () => {
await clerum.chat.create('agent-x', 'chat-a')
+ await clerum.chat.upsertMessages('agent-x', 'chat-a', [
+ { id: 'chat-a-message', role: 'user', content: 'chat A history', timestamp: 1 },
+ ])
await clerum.chat.create('agent-x', 'chat-b')
const heldSwitch = holdLastActive('chat-a')
const controller = renderController({ loadMenuData: false })
@@ -230,15 +324,23 @@ describe('Round 7 selection loading ownership', () => {
await controller.result.current.switchToChat('agent-x', 'chat-b')
})
expect(controller.result.current.activeChatId).toBe('chat-a')
- expect(controller.result.current.chatMessagesLoading).toBe(false)
+ const loadingWhileHeld = controller.result.current.chatMessagesLoading
await act(async () => {
heldSwitch.release.resolve()
await switchPromise
})
- expect(controller.result.current.activeChatId).toBe('chat-a')
- expect(controller.result.current.chatMessages).toEqual([])
- expect(controller.result.current.chatMessagesLoading).toBe(false)
+ expect({
+ activeChatId: controller.result.current.activeChatId,
+ loadingWhileHeld,
+ messages: controller.result.current.chatMessages,
+ loadingAfter: controller.result.current.chatMessagesLoading,
+ }).toEqual({
+ activeChatId: 'chat-a',
+ loadingWhileHeld: true,
+ messages: expect.arrayContaining([expect.objectContaining({ id: 'chat-a-message' })]),
+ loadingAfter: false,
+ })
expect(
clerum.chat.loadMessages.mock.calls.some(
([agentRef, chatId]) => agentRef === 'agent-x' && chatId === 'chat-b'
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index 8a43f4ad2..40e715fcb 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -1176,11 +1176,21 @@ export function useAgentChatController({
const switchToChat = useCallback(
async (agentRef: string, chatId: string) => {
+ // A rejected target does not take ownership of the visible view. Check
+ // these guards before advancing the history-selection revision so a
+ // blocked or deleted chat cannot cancel another chat's pending load.
+ if (isHostAccessBlocked(agentRef) || isChatDeleted(agentRef, chatId)) return
const selectionIntentRevision = beginSelectionIntent(true)
clearPendingSelection(agentRef, chatId)
await switchToChatForIntent(agentRef, chatId, selectionIntentRevision)
},
- [beginSelectionIntent, clearPendingSelection, switchToChatForIntent]
+ [
+ beginSelectionIntent,
+ clearPendingSelection,
+ isChatDeleted,
+ isHostAccessBlocked,
+ switchToChatForIntent,
+ ]
)
// §5d: chat CRUD (handleCreateChat / handleRename[ForAgent] / handleDelete[ForAgent])
@@ -3940,6 +3950,14 @@ export function useAgentChatController({
const selectsSpecificChat = Boolean(
chatId && !options.selectLatest && !options.suppressAutoSelect
)
+ const preservesVisibleChat = Boolean(
+ !chatId &&
+ !options.selectLatest &&
+ selectedAgent === agentName &&
+ navItem === DESKTOP_ROUTES.chat &&
+ activeChatVisibilityRef.current.selectedAgent === agentName &&
+ activeChatVisibilityRef.current.activeChatId
+ )
if (
selectsSpecificChat &&
!options.deferSwitch &&
@@ -3953,7 +3971,12 @@ export function useAgentChatController({
void switchToChat(agentName, chatId!)
return
}
- beginSelectionIntent(selectsSpecificChat)
+ // A same-agent no-chat notification can record a pending none selection
+ // while the chat route continues showing this conversation. The explicit
+ // clear path owns blanking the view; do not steal its history completion
+ // here. Route/agent changes and specific/latest selections still advance
+ // selection ownership before their effects run.
+ if (!preservesVisibleChat) beginSelectionIntent(selectsSpecificChat)
if (options.selectLatest) {
writePendingSelection(agentName, { mode: 'latest', chatId: null })
return