Skip to content

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

TypeScript

fix(desktop-ui): r9-m1 preserve implicit chat selection

Josué766bb895841e
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/selectionOwnership.r9.characterization.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/selectionOwnership.r9.characterization.test.tsx
new file mode 100644
index 000000000..5e3d22b10
--- /dev/null
+++ b/desktop-app/ui/src/hooks/domain/__tests__/selectionOwnership.r9.characterization.test.tsx
@@ -0,0 +1,52 @@
+// @vitest-environment jsdom
+import { afterEach, beforeEach, describe, expect, it } from 'vitest'
+import { act, waitFor } from '@testing-library/react'
+import { deferred } from './__fixtures__/catalogFixtures'
+import { renderController } from './__fixtures__/controllerHarness'
+import { type MockClerum, installMockClerum, uninstallMockClerum } from './__fixtures__/mockClerum'
+
+let clerum: MockClerum
+;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
+
+beforeEach(() => {
+ clerum = installMockClerum()
+})
+
+afterEach(() => {
+ uninstallMockClerum()
+})
+
+describe('Round 9 selection ownership', () => {
+ it('keeps an implicit latest load when a same-agent no-chat notification arrives first', async () => {
+ await clerum.chat.create('agent-x', 'chat-newest')
+ await clerum.chat.upsertMessages('agent-x', 'chat-newest', [
+ { id: 'newest-message', role: 'user', content: 'newest history', timestamp: 1 },
+ ])
+ const index = await clerum.readIndex('agent-x')
+ const indexLoad = deferred<Awaited<ReturnType<typeof clerum.chat.getIndex>>>()
+ clerum.chat.getIndex.mockReturnValue(indexLoad.promise)
+ const controller = renderController({ navItem: 'chat', loadMenuData: false })
+
+ try {
+ await waitFor(() => expect(clerum.chat.getIndex).toHaveBeenCalled())
+ expect(controller.result.current.activeChatId).toBeNull()
+
+ act(() => controller.result.current.setPendingChatSelection('agent-x', null))
+
+ await act(async () => {
+ indexLoad.resolve(index)
+ })
+
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBe('chat-newest')
+ expect(controller.result.current.chatMessages).toEqual(
+ expect.arrayContaining([expect.objectContaining({ id: 'newest-message' })])
+ )
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ })
+ } finally {
+ indexLoad.resolve(index)
+ controller.unmount()
+ }
+ })
+})
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index cf51c8c65..8cf9b2c43 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -3912,13 +3912,11 @@ export function useAgentChatController({
const selectsSpecificChat = Boolean(
chatId && !options.selectLatest && !options.suppressAutoSelect
)
- const preservesVisibleChat = Boolean(
+ const preservesCurrentChatRoute = Boolean(
!chatId &&
!options.selectLatest &&
selectedAgent === agentName &&
- navItem === DESKTOP_ROUTES.chat &&
- activeChatVisibilityRef.current.selectedAgent === agentName &&
- activeChatVisibilityRef.current.activeChatId
+ navItem === DESKTOP_ROUTES.chat
)
if (
selectsSpecificChat &&
@@ -3939,11 +3937,11 @@ export function useAgentChatController({
return
}
// 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)
+ // while the chat route continues an implicit/latest or visible history
+ // load. 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 (!preservesCurrentChatRoute) beginSelectionIntent(selectsSpecificChat)
if (options.selectLatest) {
writePendingSelection(agentName, { mode: 'latest', chatId: null })
return