Skip to content

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

TypeScript

fix(desktop-ui): r13-m1 separate deletion and view feedback ownership

Josuéd86d20ca1db4
diff --git a/desktop-app/ui/src/hooks/__tests__/deleteStorageFeedback.r12.test.tsx b/desktop-app/ui/src/hooks/__tests__/deleteStorageFeedback.r12.test.tsx
index 98d80fa11..24b658944 100644
--- a/desktop-app/ui/src/hooks/__tests__/deleteStorageFeedback.r12.test.tsx
+++ b/desktop-app/ui/src/hooks/__tests__/deleteStorageFeedback.r12.test.tsx
@@ -3,11 +3,13 @@ import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import { useNavigationContext } from '@contexts/NavigationContext'
import { useNotificationsContext } from '@contexts/NotificationsContext'
import { act, cleanup, fireEvent, screen, waitFor, within } from '@testing-library/react'
+import { promises as fs } from 'node:fs'
import { SidebarNav } from '@components/SidebarNav'
import { ChatThread } from '@components/agents/ChatThread'
import { installAppControllerClerum } from '../domain/__tests__/__fixtures__/appControllerHarness'
import { deferred } from '../domain/__tests__/__fixtures__/catalogFixtures'
import { renderChatActionConsumer } from '../domain/__tests__/__fixtures__/chatActionConsumerHarness'
+import { ipcHostAccessRevoked } from '../domain/__tests__/__fixtures__/ipcErrors'
import { uninstallMockClerum } from '../domain/__tests__/__fixtures__/mockClerum'
beforeEach(() => {
@@ -52,14 +54,27 @@ function StorageDeleteSidebar() {
)
}
-async function mountStorageDelete() {
- const { clerum } = installAppControllerClerum()
- for (const chat of ['chat-1', 'chat-2']) {
+async function mountStorageDelete(target = 'chat-2', withOtherAgent = false) {
+ const { clerum, handle } = installAppControllerClerum({
+ agentNames: withOtherAgent ? ['agent-x', 'agent-y'] : ['agent-x'],
+ })
+ const runtimeConfig = await window.clerum.auth.getRuntimeConfigState()
+ vi.mocked(window.clerum.auth.getRuntimeConfigState).mockResolvedValue({
+ ...runtimeConfig,
+ envKey: 'delete-feedback-fixture',
+ })
+ for (const chat of ['chat-1', 'chat-2', 'chat-3']) {
await clerum.chat.create('agent-x', chat)
await clerum.chat.upsertMessages('agent-x', chat, [
{ id: `${chat}-history`, role: 'user', content: `${chat} real history`, timestamp: 1 },
])
}
+ if (withOtherAgent) {
+ await clerum.chat.create('agent-y', 'other-chat')
+ await clerum.chat.upsertMessages('agent-y', 'other-chat', [
+ { id: 'other-history', role: 'user', content: 'other agent real history', timestamp: 1 },
+ ])
+ }
clerum.chat.getIndex.mockImplementation(agent => clerum.readIndex(agent))
const app = await renderChatActionConsumer(
<>
@@ -70,13 +85,13 @@ async function mountStorageDelete() {
act(() => app.getVm().handleSelectChatAgent('agent-x', { chatId: 'chat-1', selectLatest: false }))
await waitFor(() => expect(screen.getByText('chat-1 real history')).toBeTruthy())
await waitFor(() => expect(app.getVm().sessionStateByChatId['chat-1']?.syncing).toBe(false))
- await waitFor(() => expect(app.getVm().latestChatSessions).toHaveLength(2))
- const row = app.getVm().latestChatSessions.findIndex(chat => chat.id === 'chat-1')
+ await waitFor(() => expect(app.getVm().latestChatSessions).toHaveLength(withOtherAgent ? 4 : 3))
+ const row = app.getVm().latestChatSessions.findIndex(chat => chat.id === target)
const title = app.getVm().latestChatSessions[row]!.title
fireEvent.click(screen.getAllByRole('button', { name: `Session options for ${title}` })[row]!)
fireEvent.click(screen.getByRole('menuitem', { name: 'Delete' }))
await screen.findByRole('dialog', { name: 'Delete session?' })
- return { app, clerum }
+ return { app, clerum, handle }
}
function confirmStorageDelete() {
@@ -102,76 +117,243 @@ it('reports a current storage deletion failure once and retains the actual conve
])
expect(screen.getByText('chat-1 real history')).toBeTruthy()
expect(app.getVm().chatMessagesLoading).toBe(false)
- expect((await clerum.readIndex('agent-x')).chats.some(chat => chat.id === 'chat-1')).toBe(true)
+ expect((await clerum.readIndex('agent-x')).chats.some(chat => chat.id === 'chat-2')).toBe(true)
+ expect(app.getVm().latestChatSessions.some(chat => chat.id === 'chat-2')).toBe(true)
})
-it('suppresses stale storage deletion feedback after newer navigation owns real history', async () => {
+it.each([false, true])(
+ 'reports a failed explicit deletion after same-scope navigation (otherAgent=%s)',
+ async otherAgent => {
+ const { app, clerum } = await mountStorageDelete('chat-2', otherAgent)
+ const entered = deferred<void>()
+ const held = deferred<Awaited<ReturnType<typeof clerum.chat.delete>>>()
+ clerum.chat.delete.mockImplementationOnce(async () => {
+ entered.resolve()
+ return held.promise
+ })
+ confirmStorageDelete()
+ await act(async () => {
+ await entered.promise
+ })
+ const agent = otherAgent ? 'agent-y' : 'agent-x'
+ const chatId = otherAgent ? 'other-chat' : 'chat-3'
+ const history = otherAgent ? 'other agent real history' : 'chat-3 real history'
+ act(() => app.getVm().handleSelectChatAgent(agent, { chatId, selectLatest: false }))
+ await waitFor(() => expect(screen.getByText(history)).toBeTruthy())
+ await waitFor(() => expect(app.getVm().sessionStateByChatId[chatId]?.syncing).toBe(false))
+ const status = { text: app.getVm().statusText, tone: app.getVm().statusTone }
+ await act(async () => {
+ held.reject(new Error('requested storage deletion failed'))
+ await held.promise.catch(() => undefined)
+ })
+ expect(app.getVm().selectedAgent).toBe(agent)
+ expect(app.getVm().activeChatId).toBe(chatId)
+ expect(app.getVm().activeMessages.map(message => message.content)).toEqual([history])
+ expect(screen.getByText(history)).toBeTruthy()
+ expect(app.getVm().chatMessagesLoading).toBe(false)
+ expect(app.getVm().toasts).toHaveLength(1)
+ expect(screen.getAllByRole('alert')).toHaveLength(1)
+ expect(screen.getByRole('alert').textContent).toContain('Could not delete the chat')
+ expect({ text: app.getVm().statusText, tone: app.getVm().statusTone }).toEqual(status)
+ expect((await clerum.readIndex('agent-x')).chats.some(chat => chat.id === 'chat-2')).toBe(true)
+ expect(app.getVm().latestChatSessions.some(chat => chat.id === 'chat-2')).toBe(true)
+ }
+)
+
+it.each([false, true])(
+ 'delete access confirmation remains observable across navigation (navigated=%s)',
+ async navigated => {
+ const { app, clerum } = await mountStorageDelete()
+ const held = deferred<Awaited<ReturnType<typeof clerum.chat.captureDeleteFence>>>()
+ clerum.chat.captureDeleteFence.mockReturnValueOnce(held.promise)
+ const error = new Error('confirmation failed')
+ let request!: Promise<unknown>
+ act(() => {
+ request = app.getVm().captureChatDeleteFence('agent-x')
+ })
+ const observed = request.catch(cause => cause)
+ if (navigated) {
+ act(() =>
+ app.getVm().handleSelectChatAgent('agent-x', { chatId: 'chat-3', selectLatest: false })
+ )
+ await waitFor(() => expect(screen.getByText('chat-3 real history')).toBeTruthy())
+ await waitFor(() => expect(app.getVm().sessionStateByChatId['chat-3']?.syncing).toBe(false))
+ }
+ const status = { text: app.getVm().statusText, tone: app.getVm().statusTone }
+ await act(async () => {
+ held.reject(error)
+ expect(await observed).toBe(error)
+ })
+ expect(app.getVm().toasts).toHaveLength(1)
+ expect(screen.getAllByRole('alert')).toHaveLength(1)
+ expect(screen.getByRole('alert').textContent).toContain('Could not confirm chat access')
+ expect(app.getVm().activeChatId).toBe(navigated ? 'chat-3' : 'chat-1')
+ expect(app.getVm().activeMessages.map(message => message.content)).toEqual([
+ `${navigated ? 'chat-3' : 'chat-1'} real history`,
+ ])
+ expect(app.getVm().chatMessagesLoading).toBe(false)
+ expect({ text: app.getVm().statusText, tone: app.getVm().statusTone }).toEqual(status)
+ expect((await clerum.readIndex('agent-x')).chats).toHaveLength(3)
+ }
+)
+
+it.each(['storage', 'confirmation'] as const)(
+ 'suppresses %s failure only after its originating authority scope is invalidated',
+ async phase => {
+ const { app, clerum } = await mountStorageDelete()
+ const held = deferred<never>()
+ let observed: Promise<unknown>
+ if (phase === 'storage') {
+ const deletion = await app.getVm().captureChatDeleteFence('agent-x')
+ clerum.chat.delete.mockReturnValueOnce(held.promise)
+ observed = app.getVm().handleDeleteChat('chat-2', deletion)
+ } else {
+ clerum.chat.captureDeleteFence.mockReturnValueOnce(held.promise)
+ observed = app.getVm().captureChatDeleteFence('agent-x')
+ }
+ const settled = observed.catch(error => error)
+ await act(async () => {
+ await app.getVm().handleEnsureTeamContext({ teamId: 'team-2' })
+ })
+ await waitFor(() => expect(app.getVm().currentTeamId).toBe('team-2'))
+ act(() =>
+ app.getVm().handleSelectChatAgent('agent-x', { chatId: 'chat-3', selectLatest: false })
+ )
+ await waitFor(() => expect(screen.getByText('chat-3 real history')).toBeTruthy())
+ await waitFor(() => expect(app.getVm().chatMessagesLoading).toBe(false))
+ await waitFor(() => expect(app.getVm().sessionStateByChatId['chat-3']?.syncing).toBe(false))
+ const status = { text: app.getVm().statusText, tone: app.getVm().statusTone }
+ const error = new Error(`old-team ${phase} failure`)
+ await act(async () => {
+ held.reject(error)
+ expect(await settled).toBe(phase === 'confirmation' ? error : undefined)
+ })
+ expect(app.getVm().toasts).toEqual([])
+ expect(screen.queryAllByRole('alert')).toEqual([])
+ expect(app.getVm().activeChatId).toBe('chat-3')
+ expect(app.getVm().activeMessages.map(message => message.content)).toEqual([
+ 'chat-3 real history',
+ ])
+ expect(screen.getByText('chat-3 real history')).toBeTruthy()
+ expect(app.getVm().chatMessagesLoading).toBe(false)
+ expect({ text: app.getVm().statusText, tone: app.getVm().statusTone }).toEqual(status)
+ expect((await clerum.readIndex('agent-x')).chats.some(chat => chat.id === 'chat-2')).toBe(true)
+ }
+)
+
+it('does not report an old-scope confirmation cancellation into the new team', async () => {
const { app, clerum } = await mountStorageDelete()
- const entered = deferred<void>()
- const held = deferred<Awaited<ReturnType<typeof clerum.chat.delete>>>()
- clerum.chat.delete.mockImplementationOnce(async () => {
- entered.resolve()
- return held.promise
+ const deletion = await app.getVm().captureChatDeleteFence('agent-x')
+ await act(async () => {
+ await app.getVm().handleEnsureTeamContext({ teamId: 'team-2' })
})
- confirmStorageDelete()
+ await waitFor(() => expect(app.getVm().currentTeamId).toBe('team-2'))
+ act(() => app.getVm().handleSelectChatAgent('agent-x', { chatId: 'chat-3', selectLatest: false }))
+ await waitFor(() => expect(screen.getByText('chat-3 real history')).toBeTruthy())
+ await waitFor(() => expect(app.getVm().sessionStateByChatId['chat-3']?.syncing).toBe(false))
await act(async () => {
- await entered.promise
+ await app.getVm().handleDeleteChatForAgent('agent-x', 'chat-2', deletion)
})
- act(() => app.getVm().handleSelectChatAgent('agent-x', { chatId: 'chat-2', selectLatest: false }))
- await waitFor(() => expect(screen.getByText('chat-2 real history')).toBeTruthy())
- await waitFor(() => expect(app.getVm().sessionStateByChatId['chat-2']?.syncing).toBe(false))
+ expect(clerum.chat.delete).not.toHaveBeenCalled()
+ expect(app.getVm().toasts).toEqual([])
+ expect(screen.queryAllByRole('alert')).toEqual([])
+ expect(app.getVm().activeChatId).toBe('chat-3')
+ expect(app.getVm().activeMessages.map(message => message.content)).toEqual([
+ 'chat-3 real history',
+ ])
+ expect(app.getVm().chatMessagesLoading).toBe(false)
+ expect((await clerum.readIndex('agent-x')).chats).toHaveLength(3)
+})
+
+it('same-scope Host cancellation reports once while another agent stays visible', async () => {
+ const { app, clerum } = await mountStorageDelete('chat-2', true)
+ const deletion = await app.getVm().captureChatDeleteFence('agent-x')
+ clerum.rpc.loadSessionMessages.mockRejectedValueOnce(
+ await ipcHostAccessRevoked('rpc:loadSessionMessages')
+ )
+ act(() => app.getVm().handleSelectChatAgent('agent-x', { chatId: 'chat-3', selectLatest: false }))
+ await waitFor(() => expect(app.getVm().selectedAgent).toBe(null))
+ act(() =>
+ app.getVm().handleSelectChatAgent('agent-y', { chatId: 'other-chat', selectLatest: false })
+ )
+ await waitFor(() => expect(screen.getByText('other agent real history')).toBeTruthy())
+ await waitFor(() => expect(app.getVm().sessionStateByChatId['other-chat']?.syncing).toBe(false))
const status = { text: app.getVm().statusText, tone: app.getVm().statusTone }
await act(async () => {
- held.reject(new Error('obsolete storage deletion failed'))
- await held.promise.catch(() => undefined)
+ await app.getVm().handleDeleteChatForAgent('agent-x', 'chat-2', deletion)
})
- expect(app.getVm().activeChatId).toBe('chat-2')
+ expect(clerum.chat.delete).not.toHaveBeenCalled()
+ expect(app.getVm().toasts).toHaveLength(1)
+ expect(screen.getAllByRole('alert')).toHaveLength(1)
+ expect(screen.getByRole('alert').textContent).toContain('Delete cancelled')
+ expect(app.getVm().selectedAgent).toBe('agent-y')
+ expect(app.getVm().activeChatId).toBe('other-chat')
expect(app.getVm().activeMessages.map(message => message.content)).toEqual([
- 'chat-2 real history',
+ 'other agent real history',
])
- expect(screen.getByText('chat-2 real history')).toBeTruthy()
expect(app.getVm().chatMessagesLoading).toBe(false)
- expect(app.getVm().toasts).toEqual([])
- expect(screen.queryAllByRole('alert')).toEqual([])
expect({ text: app.getVm().statusText, tone: app.getVm().statusTone }).toEqual(status)
- expect((await clerum.readIndex('agent-x')).chats.some(chat => chat.id === 'chat-1')).toBe(true)
+ expect((await clerum.readIndex('agent-x')).chats).toHaveLength(3)
})
it.each([false, true])(
- 'delete access confirmation reports only current failure (stale=%s)',
- async stale => {
+ 'successful deferred cleanup reports by operation scope, not navigation (scopeChanged=%s)',
+ async scopeChanged => {
const { app, clerum } = await mountStorageDelete()
- const held = deferred<Awaited<ReturnType<typeof clerum.chat.captureDeleteFence>>>()
- clerum.chat.captureDeleteFence.mockReturnValueOnce(held.promise)
- const error = new Error('confirmation failed')
- let request!: Promise<unknown>
- act(() => {
- request = app.getVm().captureChatDeleteFence('agent-x')
+ const actualRm = fs.rm
+ vi.spyOn(fs, 'rm').mockImplementation((path, options) => {
+ if (String(path).endsWith('/agent-x/chat-2.json')) {
+ return Promise.reject(Object.assign(new Error('cleanup denied'), { code: 'EACCES' }))
+ }
+ return actualRm(path, options)
})
- const observed = request.catch(cause => cause)
- if (stale) {
- act(() =>
- app.getVm().handleSelectChatAgent('agent-x', { chatId: 'chat-2', selectLatest: false })
- )
- await waitFor(() => expect(screen.getByText('chat-2 real history')).toBeTruthy())
- await waitFor(() => expect(app.getVm().sessionStateByChatId['chat-2']?.syncing).toBe(false))
+ const deleteFromStore = clerum.chat.delete.getMockImplementation()!
+ const produced = deferred<Awaited<ReturnType<typeof clerum.chat.delete>>>()
+ const deliver = deferred<void>()
+ clerum.chat.delete.mockImplementationOnce(async (...args) => {
+ const result = await deleteFromStore(...args)
+ produced.resolve(result)
+ await deliver.promise
+ return result
+ })
+ confirmStorageDelete()
+ await act(async () => {
+ expect((await produced.promise).cleanupPending).toBe(true)
+ })
+ if (scopeChanged) {
+ await act(async () => {
+ await app.getVm().handleEnsureTeamContext({ teamId: 'team-2' })
+ })
+ await waitFor(() => expect(app.getVm().currentTeamId).toBe('team-2'))
}
+ act(() =>
+ app.getVm().handleSelectChatAgent('agent-x', { chatId: 'chat-3', selectLatest: false })
+ )
+ await waitFor(() => expect(screen.getByText('chat-3 real history')).toBeTruthy())
+ await waitFor(() => expect(app.getVm().sessionStateByChatId['chat-3']?.syncing).toBe(false))
const status = { text: app.getVm().statusText, tone: app.getVm().statusTone }
await act(async () => {
- held.reject(error)
- expect(await observed).toBe(error)
+ deliver.resolve()
})
- expect(app.getVm().toasts).toHaveLength(stale ? 0 : 1)
- expect(screen.queryAllByRole('alert')).toHaveLength(stale ? 0 : 1)
- if (!stale)
- expect(screen.getByRole('alert').textContent).toContain('Could not confirm chat access')
- expect(app.getVm().activeChatId).toBe(stale ? 'chat-2' : 'chat-1')
+ expect(app.getVm().toasts).toHaveLength(scopeChanged ? 0 : 1)
+ expect(screen.queryAllByRole('alert')).toEqual([])
+ if (!scopeChanged) {
+ expect(app.getVm().toasts[0]?.tone).toBe('info')
+ expect(
+ screen.getByText(
+ 'Chat removed. Local transcript cleanup will retry when this team is active.'
+ )
+ ).toBeTruthy()
+ expect(app.getVm().latestChatSessions.some(chat => chat.id === 'chat-2')).toBe(false)
+ }
+ expect(app.getVm().activeChatId).toBe('chat-3')
expect(app.getVm().activeMessages.map(message => message.content)).toEqual([
- `${stale ? 'chat-2' : 'chat-1'} real history`,
+ 'chat-3 real history',
])
expect(app.getVm().chatMessagesLoading).toBe(false)
- if (stale)
- expect({ text: app.getVm().statusText, tone: app.getVm().statusTone }).toEqual(status)
- expect((await clerum.readIndex('agent-x')).chats).toHaveLength(2)
+ expect({ text: app.getVm().statusText, tone: app.getVm().statusTone }).toEqual(status)
+ const index = await clerum.readIndex('agent-x')
+ expect(index.chats.some(chat => chat.id === 'chat-2')).toBe(false)
+ expect(index.pendingChatCleanup?.some(entry => entry.chatId === 'chat-2')).toBe(true)
}
)
diff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.ts
index 18d0ab54c..10c12b2aa 100644
--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts
+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts
@@ -1422,7 +1422,6 @@ export function useChatListController({
}
const hostAuthorityEpoch = getHostAuthorityEpoch(agentRef)
const requestScope = currentAuthorityScopeRef.current
- const requestRevision = host.current?.getSelectionIntentRevision()
try {
const fence = await chatStore.captureDeleteFence(authorityScope)
if (
@@ -1434,10 +1433,9 @@ export function useChatListController({
}
return { agentRef, fence, hostAuthorityEpoch }
} catch (error) {
- if (
- currentAuthorityScopeRef.current === requestScope &&
- host.current?.getSelectionIntentRevision() === requestRevision
- ) {
+ // Confirmation belongs to the destructive operation, not the visible
+ // conversation. Same-scope navigation must not hide its failure.
+ if (currentAuthorityScopeRef.current === requestScope) {
host.current?.pushToast('Could not confirm chat access. Please try again.', 'error')
}
throw error
@@ -1459,16 +1457,19 @@ export function useChatListController({
deletion.hostAuthorityEpoch !== getHostAuthorityEpoch(agentRef) ||
isHostAccessBlocked(agentRef)
) {
- host.current?.pushToast(
- 'Delete cancelled because chat access changed. Please try again.',
- 'error'
- )
+ // A changed Host/epoch cancels the operation within its own scope. An
+ // old team's confirmation must not present into the new team's UI.
+ if (isAuthenticated && sameAuthorityScope(deletion.fence.authorityScope, authorityScope)) {
+ host.current?.pushToast(
+ 'Delete cancelled because chat access changed. Please try again.',
+ 'error'
+ )
+ }
return
}
const requestGeneration = requestGenerationRef.current
const authorityScopeGeneration = authorityScopeGenerationRef.current
const authorityScopeAtDelete = currentAuthorityScopeRef.current
- const selectionRevisionAtDelete = host.current?.getSelectionIntentRevision()
const stillOwned = () =>
authorityScopeGenerationRef.current === authorityScopeGeneration &&
currentAuthorityScopeRef.current === authorityScopeAtDelete
@@ -1485,10 +1486,7 @@ export function useChatListController({
try {
result = await chatStore.deleteChat(agentRef, chatId, deletion.fence)
} catch {
- if (
- stillOwned() &&
- host.current?.getSelectionIntentRevision() === selectionRevisionAtDelete
- ) {
+ if (stillOwned()) {
host.current?.pushToast('Could not delete the chat. Please try again.', 'error')
}
return