Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): queue parked cancel-restorations per chat (R3-M4)
Mf56365fedfe8
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsxindex a7c558235..c2d834c0b 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx@@ -35,6 +35,22 @@ const pluginReference: ComposerPluginReference = {name: 'helper',label: 'helper plugin',}+/** A SECOND payload, for the R3-M4 "two batches" case. */+const secondImage: ComposerImageAttachment = {+ id: 'input-image-two',+ name: 'input-two.png',+ mimeType: 'image/png',+ dataBase64: 'ZGVm',+ sizeBytes: 3,+ previewDataUrl: 'data:image/png;base64,ZGVm',+}+const secondReference: ComposerPluginReference = {+ id: 'plugin-ref-two',+ type: 'plugin',+ namespace: 'ns',+ name: 'helper-two',+ label: 'helper plugin two',+}const catalog: HostModelsResult = {provider: 'zai',hostDefault: 'glm-5.3-flash',@@ -293,6 +309,97 @@ describe('STORY-38 — attachments survive message cancel', () => {expect(keptToastCall(rendered.spies)?.[0]).toBe('Attachments kept')})+ it.each([+ ['first settles first', [0, 1]],+ ['second settles first', [1, 0]],+ ])(+ 'keeps BOTH restorations when two cancels settle while another chat is viewed (R3-M4, %s)',+ async (_name, settlementOrder) => {+ // Two DISTINCT cancellations for the SAME originating chat settle while+ // the user views a different chat. Both batches are parked under that+ // chat's identity: the later one must NOT overwrite the earlier one, or+ // the first batch's attachments are lost for good.+ const cancelDeferreds: Array<() => void> = []+ clerum.rpc.cancelTask.mockImplementation(+ () =>+ new Promise<void>(resolve => {+ cancelDeferreds.push(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-first')+ const originChat = rendered.result.current.activeChatId++ // The first task FAILS: its payload stays retained (that is what the+ // cancel restores) and the chat is free to run a SECOND task.+ clerum.rpc.getTaskResult.mockResolvedValue({ status: 'failed', error: 'the task failed' })+ await act(async () => {+ clerum.emitTaskProgress('task-first', { type: 'closed' })+ })+ await waitFor(() => expect(clerum.rpc.getTaskResult).toHaveBeenCalled())++ // The first send auto-created the chat, and the visual send guard reads+ // the CHAT-scoped model selection: reload it for the second send.+ await act(async () => {+ await loadHostModels(modelTransport, 'agent-x', rendered.result.current.activeChatId, {+ force: true,+ })+ })++ // A SECOND send, with its own payload, in the SAME chat.+ act(() => rendered.result.current.handleAddComposerImageAttachments([secondImage]))+ act(() => rendered.result.current.handleAddComposerReferenceAttachments([secondReference]))+ await sendAsync(rendered.result, 'task-second')++ // Move to another chat, then let both cancels settle there.+ await act(async () => {+ await rendered.result.current.switchToChat('agent-x', 'another-chat')+ })+ let firstCancel!: Promise<void>+ let secondCancel!: Promise<void>+ act(() => {+ firstCancel = rendered.result.current.cancelTask('task-first')+ secondCancel = rendered.result.current.cancelTask('task-second')+ })+ expect(cancelDeferreds).toHaveLength(2)+ await act(async () => {+ for (const index of settlementOrder) cancelDeferreds[index]!()+ await Promise.all([firstCancel, secondCancel])+ })++ // Nothing landed in the other chat's composer.+ expect(rendered.result.current.composerImageAttachments).toHaveLength(0)+ expect(rendered.result.current.composerReferenceAttachments).toHaveLength(0)++ // Returning to the origin chat applies BOTH parked batches: no+ // attachment was overwritten, whichever order they settled in.+ await act(async () => {+ await rendered.result.current.switchToChat('agent-x', originChat!)+ })+ expect(rendered.result.current.composerImageAttachments).toHaveLength(2)+ expect(rendered.result.current.composerReferenceAttachments).toHaveLength(2)+ expect(rendered.result.current.composerImageAttachments.map(item => item.id).sort()).toEqual(+ [image.id, secondImage.id].sort()+ )+ // Each batch brought its own toast, bound to the same agent + chat.+ const keptToasts = rendered.spies.pushToast.mock.calls.filter(([message]) =>+ String(message).startsWith('Attachments kept')+ )+ expect(keptToasts).toHaveLength(2)+ for (const [, , options] of keptToasts) {+ expect((options as { action?: { label: string } } | undefined)?.action?.label).toBe(+ 'Discard all'+ )+ }+ }+ )+it.each([['success answer', 'resolve'],['404 answer', '404'],diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.tsindex 1971edd08..eb6f0c8cf 100644--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts@@ -719,14 +719,21 @@ export function useAgentChatController({// composer state (cleared on session/agent-scope reset, never persisted): a// cancel settling while another chat is viewed PRESERVES the kept// attachments and applies them when the originating chat is active again.+ //+ // R3-M4 — the parked value is a QUEUE of restoration batches, not a single+ // batch: two cancellations settling for the SAME originating chat while the+ // user views another chat must not overwrite one another. Each batch keeps+ // its own attachments, its own caps and its own toast.const parkedRestorationsRef = useRef(new Map<string,{agentRef: stringchatId: string | null- images: ComposerImageAttachment[]- references: ComposerReferenceAttachment[]+ batches: Array<{+ images: ComposerImageAttachment[]+ references: ComposerReferenceAttachment[]+ }>}>())@@ -930,13 +937,19 @@ export function useAgentChatController({// R2-M1 — a parked cancel-restoration applies when its originating chat// becomes active again (runs after the visibility sync above, so the ref it// guards through is current).+ //+ // R3-M4 — EVERY parked batch is applied, in the order the cancellations+ // settled. A single batch would let a later cancellation overwrite an+ // earlier one's attachments, losing them for good.useEffect(() => {if (!selectedAgent) returnconst key = `${selectedAgent}::${activeChatId}`const parked = parkedRestorationsRef.current.get(key)if (!parked) returnparkedRestorationsRef.current.delete(key)- applyKeptAttachments(parked.agentRef, parked.chatId, parked.images, parked.references)+ for (const batch of parked.batches) {+ applyKeptAttachments(parked.agentRef, parked.chatId, batch.images, batch.references)+ }}, [activeChatId, selectedAgent, applyKeptAttachments])// Cleanup activity streams on unmount@@ -3616,12 +3629,22 @@ export function useAgentChatController({if (!stillViewingOrigin) {// Preserve: the task snapshot is released by the caller, but the kept// payload survives here until its chat is viewed again.- parkedRestorationsRef.current.set(`${agentRef}::${originChatId}`, {- agentRef,- chatId: originChatId,- images: preparedImages,- references,- })+ //+ // R3-M4: the batch is APPENDED to the chat's queue. Two+ // cancellations settling for the same originating chat while another+ // chat is viewed must both survive — the later one does not replace+ // the earlier one's attachments.+ const key = `${agentRef}::${originChatId}`+ const parked = parkedRestorationsRef.current.get(key)+ if (parked) {+ parked.batches.push({ images: preparedImages, references })+ } else {+ parkedRestorationsRef.current.set(key, {+ agentRef,+ chatId: originChatId,+ batches: [{ images: preparedImages, references }],+ })+ }return}applyKeptAttachments(agentRef, originChatId, preparedImages, references)