Skip to content

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

TypeScript

fix(desktop-ui): r7-m1 settle loading per selection intent

Josuéb121d8f32979
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
new file mode 100644
index 000000000..7a99c0596
--- /dev/null
+++ b/desktop-app/ui/src/hooks/domain/__tests__/selectionLoadingOwnership.r7.characterization.test.tsx
@@ -0,0 +1,163 @@
+// @vitest-environment jsdom
+import { afterEach, beforeEach, describe, expect, it, vi } 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(() => {
+ vi.restoreAllMocks()
+ uninstallMockClerum()
+})
+
+function holdLastActive(chatId: string) {
+ const original = clerum.chat.setLastActive.getMockImplementation()
+ if (!original) throw new Error('Expected the setLastActive test implementation')
+ const entered = deferred<void>()
+ const release = deferred<void>()
+ const returned = deferred<void>()
+ let held = false
+ clerum.chat.setLastActive.mockImplementation(async (agentRef, selectedChatId) => {
+ if (agentRef === 'agent-x' && selectedChatId === chatId && !held) {
+ held = true
+ entered.resolve()
+ await release.promise
+ }
+ await original(agentRef, selectedChatId)
+ if (held && selectedChatId === chatId) returned.resolve()
+ })
+ return { entered, release, returned }
+}
+
+describe('Round 7 selection loading ownership', () => {
+ it('settles a held implicit switch when a newer no-chat intent arrives', async () => {
+ await clerum.chat.create('agent-x', 'chat-a')
+ clerum.chat.getIndex.mockImplementation(agentRef => clerum.readIndex(agentRef))
+ const heldSwitch = holdLastActive('chat-a')
+ const controller = renderController({ navItem: 'chat', loadMenuData: false })
+
+ try {
+ await act(async () => {
+ await heldSwitch.entered.promise
+ })
+ expect(controller.result.current.activeChatId).toBe('chat-a')
+ expect(controller.result.current.chatMessagesLoading).toBe(true)
+
+ 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([])
+
+ 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([])
+ } finally {
+ heldSwitch.release.resolve()
+ await act(async () => {
+ await heldSwitch.returned.promise
+ })
+ 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', [
+ { id: 'chat-b-message', role: 'user', content: 'chat B history', timestamp: 2 },
+ ])
+ await clerum.chat.create('agent-x', 'chat-a')
+ clerum.chat.getIndex.mockImplementation(agentRef => clerum.readIndex(agentRef))
+ const heldSwitch = holdLastActive('chat-a')
+ const controller = renderController({ navItem: 'chat', loadMenuData: false })
+
+ try {
+ await act(async () => {
+ await heldSwitch.entered.promise
+ })
+ act(() => controller.result.current.setPendingChatSelection('agent-x', 'chat-b'))
+
+ await waitFor(() => {
+ expect(controller.result.current.activeChatId).toBe('chat-b')
+ expect(controller.result.current.chatMessages).toEqual(
+ expect.arrayContaining([expect.objectContaining({ id: 'chat-b-message' })])
+ )
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ })
+
+ await act(async () => {
+ heldSwitch.release.resolve()
+ await heldSwitch.returned.promise
+ })
+ expect(controller.result.current.activeChatId).toBe('chat-b')
+ expect(controller.result.current.chatMessages).toEqual(
+ expect.arrayContaining([expect.objectContaining({ id: 'chat-b-message' })])
+ )
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+ } finally {
+ heldSwitch.release.resolve()
+ await act(async () => {
+ await heldSwitch.returned.promise
+ })
+ controller.unmount()
+ }
+ })
+
+ it('settles the current spinner when a newer chat switch is Host-blocked', async () => {
+ await clerum.chat.create('agent-x', 'chat-a')
+ await clerum.chat.create('agent-x', 'chat-b')
+ const heldSwitch = holdLastActive('chat-a')
+ let hostBlocked = false
+ const controller = renderController(
+ {
+ isHostAccessBlocked: () => hostBlocked,
+ loadMenuData: false,
+ },
+ {}
+ )
+ let switchPromise!: Promise<void>
+
+ try {
+ await waitFor(() => expect(controller.result.current.chatListLoading).toBe(false))
+ act(() => {
+ switchPromise = controller.result.current.switchToChat('agent-x', 'chat-a')
+ })
+ await act(async () => {
+ await heldSwitch.entered.promise
+ })
+ expect(controller.result.current.chatMessagesLoading).toBe(true)
+
+ hostBlocked = true
+ await act(async () => {
+ await controller.result.current.switchToChat('agent-x', 'chat-b')
+ })
+ expect(controller.result.current.activeChatId).toBe('chat-a')
+ expect(controller.result.current.chatMessagesLoading).toBe(false)
+
+ 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()
+ } finally {
+ heldSwitch.release.resolve()
+ await act(async () => {
+ await switchPromise?.catch(() => undefined)
+ })
+ controller.unmount()
+ }
+ })
+})
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index 56ad408d5..9202c49c2 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -448,10 +448,6 @@ export function useAgentChatController({
const [activeChatId, setActiveChatId] = useState<string | null>(null)
const selectionIntentRevisionRef = useRef(0)
- const beginSelectionIntent = useCallback(() => {
- selectionIntentRevisionRef.current += 1
- return selectionIntentRevisionRef.current
- }, [])
// Fase 2b (§4.1): the SessionFSM is now the SINGLE writer of the per-chat
// session projection. `sessionStateByChatKey` (the public contract) is derived
// from the reducer's map via `useSyncExternalStore` + `projectSessionState`.
@@ -527,7 +523,28 @@ export function useAgentChatController({
isChatDeleted,
} = chatListCtl
const [chatMessages, setChatMessages] = useState<AgentChatMessage[]>([])
- const [chatMessagesLoading, setChatMessagesLoading] = useState(false)
+ const [chatMessagesLoading, setChatMessagesLoadingState] = useState(false)
+ const chatMessagesLoadingRef = useRef(false)
+ const setChatMessagesLoading = useCallback((loading: boolean) => {
+ chatMessagesLoadingRef.current = loading
+ setChatMessagesLoadingState(loading)
+ }, [])
+ const beginSelectionIntent = useCallback(
+ (startsHistoryLoad = false) => {
+ selectionIntentRevisionRef.current += 1
+ if (!startsHistoryLoad) setChatMessagesLoading(false)
+ return selectionIntentRevisionRef.current
+ },
+ [setChatMessagesLoading]
+ )
+ const settleChatMessagesLoading = useCallback(
+ (selectionIntentRevision: number) => {
+ if (selectionIntentRevisionRef.current !== selectionIntentRevision) return false
+ setChatMessagesLoading(false)
+ return true
+ },
+ [setChatMessagesLoading]
+ )
const [hasOlderMessages, setHasOlderMessages] = useState(false)
const [olderMessagesLoading, setOlderMessagesLoading] = useState(false)
const chatMessagesRef = useRef<AgentChatMessage[]>([])
@@ -957,7 +974,6 @@ export function useAgentChatController({
resetReplyNotificationDedupe()
chatStore.clearCachedRemoteData()
setChatMessages([])
- setChatMessagesLoading(false)
setHasOlderMessages(false)
cancelOlderMessagesLoad()
loadedLocalMessageCountRef.current = 0
@@ -981,7 +997,10 @@ export function useAgentChatController({
// chat, and a task in flight is rejoined via the tracker D.3 already mounts.
const switchToChatForIntent = useCallback(
async (agentRef: string, chatId: string, selectionIntentRevision: number) => {
- if (isHostAccessBlocked(agentRef) || isChatDeleted(agentRef, chatId)) return
+ if (isHostAccessBlocked(agentRef) || isChatDeleted(agentRef, chatId)) {
+ settleChatMessagesLoading(selectionIntentRevision)
+ return
+ }
const key = makeTaskKey(agentRef, chatId)
const visibleBeforeSwitch = activeChatVisibilityRef.current
const reopeningActiveChat =
@@ -1023,12 +1042,11 @@ export function useAgentChatController({
setAgentError(null)
setFailedAgentSend(null)
await chatStore.setLastActive(agentRef, chatId)
- if (
- selectionIntentRevisionRef.current !== selectionIntentRevision ||
- isHostAccessBlocked(agentRef) ||
- isChatDeleted(agentRef, chatId)
- )
+ if (selectionIntentRevisionRef.current !== selectionIntentRevision) return
+ if (isHostAccessBlocked(agentRef) || isChatDeleted(agentRef, chatId)) {
+ settleChatMessagesLoading(selectionIntentRevision)
return
+ }
unfillableServerGapUpperBoundsRef.current.delete(key)
@@ -1040,6 +1058,8 @@ export function useAgentChatController({
const isStillActive = () => {
const visible = activeChatVisibilityRef.current
return (
+ selectionIntentRevisionRef.current === selectionIntentRevision &&
+ !isHostAccessBlocked(agentRef) &&
activeChatSwitchRequestRef.current === switchRequest &&
visible.selectedAgent === agentRef &&
visible.activeChatId === chatId
@@ -1085,7 +1105,7 @@ export function useAgentChatController({
// Phase 1 rendered the cache → clear the blocking spinner. The reconcile
// runs under the `syncing` indicator (RECONCILE_STARTED, dispatched by the
// reconcile gate) instead.
- setChatMessagesLoading(false)
+ settleChatMessagesLoading(selectionIntentRevision)
// D.5: opening the chat clears its "completed_unread" badge. CHAT_OPENED
// resets the FSM's `unreadTerminal` flag so a LATER hidden terminal re-marks
// (without it the flag is write-once-true and the badge never reappears) and
@@ -1143,6 +1163,7 @@ export function useAgentChatController({
scrollChatToBottom,
clearUnreadInList,
cancelOlderMessagesLoad,
+ settleChatMessagesLoading,
chatStore.setLastActive,
chatStore.loadMessages,
chatStore.clearUnreadTerminal,
@@ -1151,7 +1172,7 @@ export function useAgentChatController({
const switchToChat = useCallback(
async (agentRef: string, chatId: string) => {
- const selectionIntentRevision = beginSelectionIntent()
+ const selectionIntentRevision = beginSelectionIntent(true)
clearPendingSelection(agentRef, chatId)
await switchToChatForIntent(agentRef, chatId, selectionIntentRevision)
},
@@ -1351,6 +1372,7 @@ export function useAgentChatController({
// Agent selection → load chats
useEffect(() => {
if (!selectedAgent || isHostAccessBlocked(selectedAgent)) {
+ beginSelectionIntent()
autoSelectedChatIdRef.current = null
activeChatVisibilityRef.current = {
...activeChatVisibilityRef.current,
@@ -1363,7 +1385,6 @@ export function useAgentChatController({
setChatListLoading(false)
activeChatLocalPageRequestRef.current = null
setChatMessages([])
- setChatMessagesLoading(false)
setHasOlderMessages(false)
cancelOlderMessagesLoad()
loadedLocalMessageCountRef.current = 0
@@ -1404,7 +1425,7 @@ export function useAgentChatController({
setActiveChatId(null)
activeChatLocalPageRequestRef.current = null
setChatMessages([])
- setChatMessagesLoading(false)
+ settleChatMessagesLoading(selectionIntentRevision)
setHasOlderMessages(false)
cancelOlderMessagesLoad()
loadedLocalMessageCountRef.current = 0
@@ -1419,7 +1440,7 @@ export function useAgentChatController({
// selection or replay this older request after the list load completes.
if (selectionIntentRevisionRef.current !== selectionIntentRevision) return
if (!result) {
- setChatMessagesLoading(false)
+ settleChatMessagesLoading(selectionIntentRevision)
return
}
const { merged } = result
@@ -1437,7 +1458,7 @@ export function useAgentChatController({
if (requestedSelection) {
clearPendingSelection(selectedAgent)
}
- setChatMessagesLoading(false)
+ settleChatMessagesLoading(selectionIntentRevision)
return
}
@@ -1447,7 +1468,7 @@ export function useAgentChatController({
clearPendingSelection(selectedAgent)
if (requestedSelection?.mode === 'none') {
- setChatMessagesLoading(false)
+ settleChatMessagesLoading(selectionIntentRevision)
return
}
if (requestedSelection?.mode === 'latest') {
@@ -1456,9 +1477,7 @@ export function useAgentChatController({
autoSelectedChatIdRef.current = latest.id
await switchToChatForIntent(selectedAgent, latest.id, selectionIntentRevision)
}
- if (selectionIntentRevisionRef.current === selectionIntentRevision) {
- setChatMessagesLoading(false)
- }
+ settleChatMessagesLoading(selectionIntentRevision)
return
}
if (requestedSelection?.mode === 'specific') {
@@ -1480,20 +1499,20 @@ export function useAgentChatController({
await switchToChatForIntent(selectedAgent, latest.id, selectionIntentRevision)
}
}
- if (selectionIntentRevisionRef.current === selectionIntentRevision) {
- setChatMessagesLoading(false)
- }
+ settleChatMessagesLoading(selectionIntentRevision)
})()
return () => {
cancelled = true
}
}, [
+ beginSelectionIntent,
cancelOlderMessagesLoad,
currentTeamId,
navItem,
selectedAgent,
isHostAccessBlocked,
+ settleChatMessagesLoading,
switchToChatForIntent,
])
@@ -2038,7 +2057,12 @@ export function useAgentChatController({
})
if (kind === 'revoked') {
hideAgent(agentRef)
- const view = activeChatVisibilityRef.current
+ }
+ const view = activeChatVisibilityRef.current
+ if (view.selectedAgent === agentRef) {
+ beginSelectionIntent()
+ }
+ if (kind === 'revoked') {
if (agentSendSetupOwnerRef.current?.agentRef === agentRef) {
agentSendSetupOwnerRef.current = null
agentSendInFlightRef.current = false
@@ -2052,7 +2076,6 @@ export function useAgentChatController({
setActiveChatId(null)
activeChatLocalPageRequestRef.current = null
setChatMessages([])
- setChatMessagesLoading(false)
setHasOlderMessages(false)
cancelOlderMessagesLoad()
}
@@ -2068,6 +2091,7 @@ export function useAgentChatController({
clearList,
resetComposerAttachments,
cancelOlderMessagesLoad,
+ beginSelectionIntent,
onHostAccessRevoked,
onHostAuthorityUncertain,
]
@@ -3858,7 +3882,18 @@ export function useAgentChatController({
} = {}
) => {
if (isHostAccessBlocked(agentName) || (chatId && isChatDeleted(agentName, chatId))) return
- beginSelectionIntent()
+ const selectsSpecificChat = Boolean(
+ chatId && !options.selectLatest && !options.suppressAutoSelect
+ )
+ if (selectsSpecificChat && selectedAgent === agentName && navItem === DESKTOP_ROUTES.chat) {
+ clearPendingSelection(agentName)
+ if (options.title) {
+ upsertProvisionalEntry(chatId!, options.title, options.isRemote === true)
+ }
+ void switchToChat(agentName, chatId!)
+ return
+ }
+ beginSelectionIntent(selectsSpecificChat)
if (options.selectLatest) {
writePendingSelection(agentName, { mode: 'latest', chatId: null })
return
@@ -3894,9 +3929,13 @@ export function useAgentChatController({
[
cancelOlderMessagesLoad,
beginSelectionIntent,
+ clearPendingSelection,
currentTeamId,
writePendingSelection,
upsertProvisionalEntry,
+ selectedAgent,
+ navItem,
+ switchToChat,
isHostAccessBlocked,
isChatDeleted,
]
@@ -3912,7 +3951,6 @@ export function useAgentChatController({
setActiveChatId(null)
activeChatLocalPageRequestRef.current = null
setChatMessages([])
- setChatMessagesLoading(false)
setHasOlderMessages(false)
cancelOlderMessagesLoad()
loadedLocalMessageCountRef.current = 0