Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): abort zip walks on unmount and guard cancel-restore by agent (review)
Mbdc4d7e107d1
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsxindex bde452cfc..723a5c8d4 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx@@ -248,4 +248,39 @@ describe('STORY-38 — attachments survive message cancel', () => {expect(result.current.composerImageAttachments).toHaveLength(1)expect(result.current.composerImageAttachments[0]).toMatchObject({ id: 'agent-y-image' })})++ it('does not restore attachments into another agent composer when the cancel answer lands late (L3)', async () => {+ // The cancel RPC straddles an agent switch: its answer must not drop the+ // canceled agent's attachments into the NEW agent's composer.+ let resolveCancel!: () => void+ clerum.rpc.cancelTask.mockReturnValue(+ new Promise<void>(resolve => {+ resolveCancel = resolve+ })+ )+ const rendered = renderController()+ await waitFor(() => expect(clerum.chat.getIndex).toHaveBeenCalled())+ await act(async () => {+ await loadHostModels(modelTransport, 'agent-x', null)+ })+ act(() => rendered.result.current.handleAddComposerImageAttachments([image]))+ act(() => rendered.result.current.handleAddComposerReferenceAttachments([pluginReference]))+ await sendAsync(rendered.result, 'task-slow')++ let cancelDone!: Promise<void>+ act(() => {+ cancelDone = rendered.result.current.cancelTask('task-slow')+ })+ // Switch agents while the RPC is still in flight.+ rendered.rerender({ selectedAgent: 'agent-y', agentNames: ['agent-x', 'agent-y'] })++ await act(async () => {+ resolveCancel()+ await cancelDone+ })++ expect(rendered.result.current.composerImageAttachments).toHaveLength(0)+ expect(rendered.result.current.composerReferenceAttachments).toHaveLength(0)+ expect(keptToastCall(rendered.spies)).toBeUndefined()+ })})diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.tsindex 87e5e7649..3244851d1 100644--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts@@ -3510,6 +3510,11 @@ export function useAgentChatController({*/const restoreComposerAttachmentsAfterCancel = useCallback((taskId: string, agentRef: string) => {+ // The awaited cancel RPC can straddle an agent switch: the attachments+ // belong to the agent that was canceled, so they must never land in the+ // composer of whatever agent is selected by the time the answer arrives+ // (mirrors the Discard-all guard below).+ if (activeChatVisibilityRef.current.selectedAgent !== agentRef) returnconst snapshots = getRetainedSendsForTask(taskId)const images = snapshots.flatMap(snapshot => snapshot.attachments)const references = snapshots.flatMap(snapshot => snapshot.references)diff --git a/desktop-app/ui/src/pages/FilesPage.tsx b/desktop-app/ui/src/pages/FilesPage.tsxindex e4fe544ec..9d6d182f3 100644--- a/desktop-app/ui/src/pages/FilesPage.tsx+++ b/desktop-app/ui/src/pages/FilesPage.tsx@@ -292,6 +292,15 @@ export function FilesPage({// the prefetch effect can consult it without re-arming when a job starts.const zipJobRef = useRef<string | null>(null)const zipAbortRef = useRef<AbortController | null>(null)+ // Navigating away must not leave a walk spending the shared GFS read budget+ // with no Stop reachable: unmount aborts it, so the one-job-at-a-time+ // invariant also holds across unmount/remount (a fresh mount starts fresh,+ // never alongside a zombie walk from the previous page instance).+ useEffect(() => {+ return () => {+ zipAbortRef.current?.abort()+ }+ }, [])const [draggingResourceId, setDraggingResourceId] = useState<string | null>(null)const [dragOverFolderId, setDragOverFolderId] = useState<string | null>(null)const [movingResourceId, setMovingResourceId] = useState<string | null>(null)diff --git a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsxindex 806c41118..2afd863de 100644--- a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx+++ b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx@@ -3570,6 +3570,79 @@ describe('FilesPage', () => {expect(click).not.toHaveBeenCalled()})+ it('aborts a running zip walk when the page unmounts, so a remount cannot double-walk (M2)', async () => {+ // Real timers: the first walk parks on the in-flight listing itself, so+ // only the abort race can end it; the second walk's first request is+ // immediate on its own fresh throttle.+ let settleFirst!: (value: { items: unknown[]; nextCursor: null }) => void+ const listChildren = vi.fn(() => {+ if (listChildren.mock.calls.length === 1)+ return new Promise(resolve => {+ settleFirst = resolve+ })+ return Promise.resolve({ items: [], nextCursor: null })+ })+ const pushToast = vi.fn()+ Object.defineProperty(window, 'clerum', {+ configurable: true,+ value: { gfs: { listChildren, download: vi.fn() } },+ })+ const assetsRow = {+ resourceId: 'folder-1',+ rid: 'folder-1',+ gfsUri: 'gfs://main/folder-1',+ drive: 'main',+ parentResourceId: null,+ name: 'Assets',+ kind: 'directory' as const,+ path: '/Assets',+ version: 0,+ bytes: 0,+ sources: ['grant'],+ permissions: ['read'],+ coversDescendants: false,+ }+ hookMock.useGfsBrowserController.mockReturnValue({+ ...baseController(),+ accessibleResources: [assetsRow],+ })++ const first = renderFilesPage(pushToast)+ await act(async () => {+ fireEvent.click(screen.getByRole('button', { name: 'Options for Assets' }))+ })+ await act(async () => {+ fireEvent.click(screen.getByRole('menuitem', { name: 'Download as zip' }))+ })+ expect(screen.getByTestId('gfs-zip-progress')).toBeTruthy()+ expect(listChildren).toHaveBeenCalledTimes(1)++ // Navigating away: the unmount cleanup must stop the walk.+ first.unmount()+ await act(async () => {+ await new Promise(resolve => setTimeout(resolve, 0))+ })+ expect(pushToast).toHaveBeenCalledWith('Stopped preparing Assets.zip.', 'info')++ // A fresh mount must be able to start a fresh walk — with the previous+ // one terminated, never alongside a zombie spending the same budget.+ const second = renderFilesPage(pushToast)+ await act(async () => {+ fireEvent.click(screen.getByRole('button', { name: 'Options for Assets' }))+ })+ await act(async () => {+ fireEvent.click(screen.getByRole('menuitem', { name: 'Download as zip' }))+ })+ await act(async () => {+ await new Promise(resolve => setTimeout(resolve, 0))+ })++ expect(listChildren).toHaveBeenCalledTimes(2)+ expect(pushToast).toHaveBeenCalledWith('"Assets" has no downloadable files.', 'info')+ second.unmount()+ void settleFirst+ })+it('tells the user a zip is already being prepared instead of ignoring the click (L7)', async () => {vi.useFakeTimers()// The listing never settles, so the first job stays in flight.