Skip to content

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

TypeScript

fix(desktop): keep a provisional selection mark only at its own revision

Alfredo Lopezbdccf042f322
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.provisionalSelection.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.provisionalSelection.test.tsx
index 3362697a3..3a86b8fc6 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.provisionalSelection.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.provisionalSelection.test.tsx
@@ -7,7 +7,9 @@
* tests j, k and m in `switchToChat.transcriptMemo.test.tsx`; row 14 (blank
* path) is #976's `createChatFailure*` suites. Rows 15 and 16 cover a newer
* selection intent that supersedes the click without switching, on the same
- * Host and across Hosts. Every case runs for a
+ * Host and across Hosts. Rows 17 and 18 check that the superseded mark stays
+ * dead when a later click or effect reuses the same switch request. Every case
+ * runs for a
* stateful and a stateless Host.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
@@ -18,7 +20,7 @@ import {
setComposerDraft,
} from '@lib/composerDraftStore'
import type { AgentChatMessage } from '../../../uiTypes'
-import { deferred } from './__fixtures__/catalogFixtures'
+import { deferred, localIndex } from './__fixtures__/catalogFixtures'
import { type RenderControllerResult, renderController } from './__fixtures__/controllerHarness'
import { ipcGenericForbidden } from './__fixtures__/ipcErrors'
import { type MockClerum, installMockClerum, uninstallMockClerum } from './__fixtures__/mockClerum'
@@ -513,4 +515,83 @@ describe('provisional selection (#1091 A17)', () => {
expect(getComposerDraft('chat-a', host)).toBe('')
}
)
+
+ /** Rows 17/18 setup: row 15, then a message sent and rendered in A. */
+ async function sendAfterSupersededClick(host: string) {
+ const { controller } = await rememberA(host)
+ const { write, click } = await clickHeld(controller, host)
+ const getIndexCalls = clerum.chat.getIndex.mock.calls.length
+ clerum.chat.getIndex.mockRejectedValue(new Error('catalog unavailable'))
+ act(() => controller.result.current.setPendingChatSelection(host, 'chat-a'))
+ act(() => controller.rerender({ navItem: 'chat' }))
+ await waitFor(() =>
+ expect(clerum.chat.getIndex.mock.calls.length).toBeGreaterThan(getIndexCalls)
+ )
+ await waitFor(() => expect(controller.result.current.chatMessagesLoading).toBe(false))
+ await act(async () => {
+ write.resolve()
+ await click
+ })
+ clerum.chat.getIndex.mockResolvedValue(
+ localIndex([
+ { id: 'chat-a', title: 'A' },
+ { id: 'chat-b', title: 'B' },
+ ]) as never
+ )
+ act(() => setComposerDraft('chat-a', 'hello', host))
+ await act(async () => {
+ await controller.result.current.handleSendAgentMessage('hello')
+ })
+ // Liveness: the message was dispatched once and rendered.
+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(1)
+ expect(shows(controller)).toContain('hello')
+ return controller
+ }
+
+ it.each(HOSTS)(
+ '17. %s: a later same-row click whose write fails does not revive the superseded mark',
+ async host => {
+ const controller = await sendAfterSupersededClick(host)
+ const shown = shows(controller)
+ const writesBefore = clerum.chat.setLastActive.mock.calls.length
+ const reclickError = vi.fn()
+ const reclick = await clickHeld(controller, host, { onCurrentError: reclickError })
+ // Witness: the reclick issued its own write and did not repaint.
+ expect(clerum.chat.setLastActive.mock.calls.length).toBe(writesBefore + 1)
+ expect(shows(controller)).toEqual(shown)
+ await act(async () => {
+ reclick.write.reject(new Error('last-active write failed'))
+ await reclick.click
+ })
+ // Witness: the failure reached the caller, once.
+ expect(reclickError).toHaveBeenCalledTimes(1)
+ expect(controller.result.current.activeChatId).toBe('chat-a')
+ expect(shows(controller)).toEqual(shown)
+ }
+ )
+
+ it.each(HOSTS)(
+ '18. %s: a later effect-driven switch whose write fails does not revive the superseded mark',
+ async host => {
+ const controller = await sendAfterSupersededClick(host)
+ const shown = shows(controller)
+ const writesBefore = clerum.chat.setLastActive.mock.calls.length
+ clerum.chat.setLastActive.mockRejectedValueOnce(new Error('last-active write failed'))
+ // A same-chat notification whose catalog load now succeeds: the switch runs in the effect.
+ act(() => controller.result.current.setPendingChatSelection(host, 'chat-a'))
+ act(() => controller.rerender({ navItem: 'chat' }))
+ // Witness: the effect's switch issued its write and its failure was reported.
+ await waitFor(() =>
+ expect(clerum.chat.setLastActive.mock.calls.length).toBe(writesBefore + 1)
+ )
+ await waitFor(() =>
+ expect(controller.spies.pushToast).toHaveBeenCalledWith(
+ expect.stringContaining('Could not open conversation'),
+ 'error'
+ )
+ )
+ expect(controller.result.current.activeChatId).toBe('chat-a')
+ expect(shows(controller)).toEqual(shown)
+ }
+ )
})
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index 82bfe1c10..75e79f25b 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -559,15 +559,15 @@ export function useAgentChatController({
// #1091 A17: a chat shown from memory before its last-active write and first
// local page settled. Without the memo this state was the blank view with the
// spinner, and every path below relied on that. `revision` is the selection
- // intent that owns it; a same-row click takes it over. A newer intent that
- // never reaches a switch (a pending selection whose catalog load fails) leaves
- // the mark in place, so the revision check retires it for the guards.
+ // intent that owns it. Invariant: the mark exists only at the current
+ // revision. `beginSelectionIntent` retires it on every advance, and a
+ // same-row click that finds it live passes it on to its own intent as a new
+ // object, so a superseded mark can never be revived by a later click.
const provisionalSelectionRef = useRef<ProvisionalSelection | null>(null)
const provisionalSelectionFor = useCallback((agentRef: string | null, chatId: string | null) => {
const selection = provisionalSelectionRef.current
return selection &&
selection.request === activeChatSwitchRequestRef.current &&
- selection.revision === selectionIntentRevisionRef.current &&
selection.agentRef === agentRef &&
selection.chatId === chatId
? selection
@@ -593,7 +593,9 @@ export function useAgentChatController({
}, [cancelOlderMessagesLoad])
const beginSelectionIntent = useCallback(
(startsHistoryLoad = false) => {
- if (!startsHistoryLoad) withdrawProvisionalSelection()
+ // A switch that follows keeps the view; it inherits a live mark itself.
+ if (startsHistoryLoad) provisionalSelectionRef.current = null
+ else withdrawProvisionalSelection()
selectionIntentRevisionRef.current += 1
if (!startsHistoryLoad) {
setChatMessagesLoading(false)
@@ -1119,7 +1121,12 @@ export function useAgentChatController({
// rejoin. No `isRemote` branch — the server is the source of truth for every
// 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) => {
+ async (
+ agentRef: string,
+ chatId: string,
+ selectionIntentRevision: number,
+ inherited: ProvisionalSelection | null = null
+ ) => {
const deleted = isChatDeleted(agentRef, chatId)
if (deleted) transcriptMemo.delete(chatTranscriptMemoKey(agentRef, chatId))
if (deleted || isHostAccessBlocked(agentRef)) {
@@ -1163,8 +1170,8 @@ export function useAgentChatController({
provisionalSelectionRef.current = remembered
? { request: switchRequest, revision: selectionIntentRevision, agentRef, chatId }
: null
- } else if (provisionalSelectionRef.current?.request === switchRequest) {
- provisionalSelectionRef.current.revision = selectionIntentRevision
+ } else if (inherited?.request === switchRequest) {
+ provisionalSelectionRef.current = { ...inherited, revision: selectionIntentRevision }
}
const ownsProvisionalSelection = () =>
provisionalSelectionRef.current?.request === switchRequest &&
@@ -1351,11 +1358,12 @@ export function useAgentChatController({
chatId: string,
revision: number,
onCurrentError: (error: unknown) => void,
- isRelevant: () => boolean = () => true
+ isRelevant: () => boolean = () => true,
+ inherited: ProvisionalSelection | null = null
) => {
const scope = currentAuthScopeRef.current
return runChatSwitchRequest(
- () => switchToChatForIntent(agentRef, chatId, revision),
+ () => switchToChatForIntent(agentRef, chatId, revision, inherited),
() =>
selectionIntentRevisionRef.current === revision &&
currentAuthScopeRef.current === scope &&
@@ -1379,11 +1387,20 @@ export function useAgentChatController({
// 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
+ // A same-row click takes over the live mark of the click it supersedes.
+ const inherited = provisionalSelectionFor(agentRef, chatId)
const selectionIntentRevision = beginSelectionIntent(true)
clearPendingSelection(agentRef, chatId)
- const promise = runSwitchForIntent(agentRef, chatId, selectionIntentRevision, error => {
- options.onCurrentError?.(error)
- })
+ const promise = runSwitchForIntent(
+ agentRef,
+ chatId,
+ selectionIntentRevision,
+ error => {
+ options.onCurrentError?.(error)
+ },
+ undefined,
+ inherited
+ )
const request: PendingChatSwitchRequest = {
agentRef,
chatId,
@@ -1407,6 +1424,7 @@ export function useAgentChatController({
clearPendingSelection,
isChatDeleted,
isHostAccessBlocked,
+ provisionalSelectionFor,
runSwitchForIntent,
]
)