Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): bind cancel-restore and discard to agent and chat (R1-H1)
Mfd306dee3d02
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsxindex 723a5c8d4..8ce418203 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx@@ -283,4 +283,80 @@ describe('STORY-38 — attachments survive message cancel', () => {expect(rendered.result.current.composerReferenceAttachments).toHaveLength(0)expect(keptToastCall(rendered.spies)).toBeUndefined()})++ it.each([+ ['success answer', 'resolve'],+ ['404 answer', '404'],+ ])(+ 'does not restore into another chat of the same agent when the cancel %s lands late (R1-H1)',+ async (_name, mode) => {+ let settleCancel!: (value?: void) => void+ clerum.rpc.cancelTask.mockReturnValue(+ new Promise<void>(resolve => {+ settleCancel = resolve+ })+ )+ if (mode === '404')+ clerum.rpc.cancelTask.mockReturnValue(+ new Promise((_resolve, reject) => {+ settleCancel = () => reject(new Error('404 Not Found'))+ })+ )+ 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-chat-sw')+ const originChat = rendered.result.current.activeChatId++ let cancelDone!: Promise<void>+ act(() => {+ cancelDone = rendered.result.current.cancelTask('task-chat-sw')+ })+ // SAME agent, DIFFERENT chat while the RPC is in flight.+ await act(async () => {+ await rendered.result.current.switchToChat('agent-x', 'another-chat')+ })+ expect(rendered.result.current.activeChatId).toBe('another-chat')++ await act(async () => {+ settleCancel()+ await cancelDone.catch(() => undefined)+ })++ // Nothing landed in the other chat's composer, and no kept-toast fired.+ expect(rendered.result.current.composerImageAttachments).toHaveLength(0)+ expect(rendered.result.current.composerReferenceAttachments).toHaveLength(0)+ expect(keptToastCall(rendered.spies)).toBeUndefined()+ // The origin chat's composer surface is the same per-agent state: empty.+ await act(async () => {+ await rendered.result.current.switchToChat('agent-x', originChat!)+ })+ expect(rendered.result.current.composerImageAttachments).toHaveLength(0)+ }+ )++ it('stale Discard all no-ops after a same-agent chat switch (R1-H1)', async () => {+ const { result, spies } = await mountedControllerWithAttachments()+ await sendAsync(result, 'task-stale-chat')++ await act(async () => {+ await result.current.cancelTask('task-stale-chat')+ })+ const staleAction = keptToastAction(spies)+ expect(staleAction?.label).toBe('Discard all')+ expect(result.current.composerImageAttachments).toHaveLength(1)++ // Same agent, different chat: the kept attachments ride along (per-agent+ // state), but the toast is bound to the chat it came from.+ await act(async () => {+ await result.current.switchToChat('agent-x', 'another-chat')+ })+ act(() => staleAction?.onAction())++ expect(result.current.composerImageAttachments).toHaveLength(1)+ })})diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.tsindex 3244851d1..8c56ae25d 100644--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts@@ -3503,22 +3503,25 @@ export function useAgentChatController({* composer so the user can reuse them. In-memory only: the composer state,* not GFS or any persisted store, owns the kept attachments.*- * The toast reports what actually survived the composer's image cap, and its- * Discard-all action is bound to the agent that was canceled: the toast can- * outlive an agent switch, and clearing the composer of a DIFFERENT agent- * would destroy that agent's own pending attachments.+ * The restore and its Discard-all action are bound to BOTH the originating+ * agent and the originating chat (the retained snapshot's chatId): the+ * awaited cancel RPC — and the toast — can outlive a switch to a different+ * agent or a different chat, and neither the attachments nor the discard may+ * act on a composer the user has navigated away from. A skipped restore+ * still releases the snapshot (a cancel is terminal by intent; see+ * work-tracker/specs/folder-zip-and-cancel-restore-budget.md).*/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)if (!images.length && !references.length) return+ const originChatId = snapshots[0]?.chatId ?? null+ const stillViewingOrigin = () =>+ activeChatVisibilityRef.current.selectedAgent === agentRef &&+ activeChatVisibilityRef.current.activeChatId === originChatId+ if (!stillViewingOrigin()) returnconst { dropped } = predictComposerImageMerge(composerImageAttachments, images)handleAddComposerImageAttachments(images.map(attachment => ({@@ -3535,10 +3538,7 @@ export function useAgentChatController({action: {label: 'Discard all',onAction: () => {- // Attachments are per-agent; chat switches do not change who owns- // them, but an agent switch does — no-op instead of clearing- // another agent's composer.- if (activeChatVisibilityRef.current.selectedAgent !== agentRef) return+ if (!stillViewingOrigin()) returnresetComposerAttachments()},},