Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): enforce the reference cap during cancel-restore (R1-M5)
Mfa2af06ac753
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsxindex 2300631d3..73663b67f 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/cancelKeepsAttachments.test.tsx@@ -411,4 +411,43 @@ describe('STORY-38 — attachments survive message cancel', () => {'Attachments kept — 2 of 3 images exceed the 20-image limit and were dropped.')})++ it('enforces the reference cap while restoring and reports the overflow (R1-M5)', async () => {+ const sentReferences = [0, 1].map(index => ({+ ...pluginReference,+ id: `sent-ref-${index}`,+ name: `helper-${index}`,+ label: `helper plugin ${index}`,+ }))+ const rendered = renderController()+ await waitFor(() => expect(clerum.chat.getIndex).toHaveBeenCalled())+ await act(async () => {+ await loadHostModels(modelTransport, 'agent-x', null)+ })+ act(() => rendered.result.current.handleAddComposerReferenceAttachments(sentReferences))+ await sendAsync(rendered.result, 'task-refs')++ // Nine more references land while the task runs: 9 held + 2 restored+ // would be 11 — one past the send-time file limit.+ const heldReferences = Array.from({ length: 9 }, (_, index) => ({+ ...pluginReference,+ id: `held-ref-${index}`,+ name: `held-${index}`,+ label: `held plugin ${index}`,+ }))+ act(() => rendered.result.current.handleAddComposerReferenceAttachments(heldReferences))+ expect(rendered.result.current.composerReferenceAttachments).toHaveLength(11 - 2)++ await act(async () => {+ await rendered.result.current.cancelTask('task-refs')+ })++ // The composer ends AT the cap (sendable), and the toast reports the one+ // reference that could not return.+ expect(rendered.result.current.composerReferenceAttachments).toHaveLength(10)+ const call = keptToastCall(rendered.spies)+ expect(call?.[0]).toBe(+ 'Attachments kept — 1 of 2 reference exceeds the 10-file limit and was dropped.'+ )+ })})diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.tsindex 2ecad6c44..429af7abe 100644--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts@@ -6,6 +6,7 @@ import {parseTaskKey,useAgentTaskTracker,} from '@contexts/AgentTaskTrackerContext'+import { FILE_REFERENCE_MAX_COUNT } from '@clerum/gfs-interaction-policy'import { COMPOSER_MAX_IMAGE_ATTACHMENTS } from '@constants/attachments'import {buildChatMessageAttachments,@@ -709,6 +710,7 @@ export function useAgentChatController({handleAddComposerReferenceAttachments,handleRemoveComposerReferenceAttachment,restoreComposerImageAttachments,+ restoreComposerReferenceAttachments,} = useComposerAttachments({ selectedAgent, clearSendError: clearComposerSendError })const { chatEndRef, scrollChatToBottom } = useChatScroll({@@ -3523,19 +3525,31 @@ export function useAgentChatController({activeChatVisibilityRef.current.selectedAgent === agentRef &&activeChatVisibilityRef.current.activeChatId === originChatIdif (!stillViewingOrigin()) return- // One merge against the live composer snapshot — the counts and the- // committed state can never disagree (R1-M4).- const { dropped } = restoreComposerImageAttachments(+ // One merge per kind against the live composer snapshot — the counts and+ // the committed state can never disagree (R1-M4), and the reference cap+ // is enforced while restoring so the composer stays sendable (R1-M5).+ const imageOutcome = restoreComposerImageAttachments(images.map(attachment => ({...attachment,previewDataUrl: `data:${attachment.mimeType};base64,${attachment.dataBase64}`,})))- handleAddComposerReferenceAttachments(references)- const message =- dropped > 0- ? `Attachments kept — ${dropped} of ${images.length} ${dropped === 1 ? 'image exceeds' : 'images exceed'} the ${COMPOSER_MAX_IMAGE_ATTACHMENTS}-image limit and ${dropped === 1 ? 'was' : 'were'} dropped.`- : 'Attachments kept'+ const referenceOutcome = restoreComposerReferenceAttachments(references)+ const droppedParts: string[] = []+ if (imageOutcome.dropped > 0) {+ droppedParts.push(+ `${imageOutcome.dropped} of ${images.length} ${imageOutcome.dropped === 1 ? 'image exceeds' : 'images exceed'} the ${COMPOSER_MAX_IMAGE_ATTACHMENTS}-image limit`+ )+ }+ if (referenceOutcome.dropped > 0) {+ droppedParts.push(+ `${referenceOutcome.dropped} of ${references.length} ${referenceOutcome.dropped === 1 ? 'reference exceeds' : 'references exceed'} the ${FILE_REFERENCE_MAX_COUNT}-file limit`+ )+ }+ const totalDropped = imageOutcome.dropped + referenceOutcome.dropped+ const message = droppedParts.length+ ? `Attachments kept — ${droppedParts.join('; ')} and ${totalDropped === 1 ? 'was' : 'were'} dropped.`+ : 'Attachments kept'pushToast(message, 'info', {action: {label: 'Discard all',@@ -3548,10 +3562,10 @@ export function useAgentChatController({},[getRetainedSendsForTask,- handleAddComposerReferenceAttachments,pushToast,resetComposerAttachments,restoreComposerImageAttachments,+ restoreComposerReferenceAttachments,])diff --git a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts b/desktop-app/ui/src/hooks/domain/useComposerAttachments.tsindex 5bd32d75d..535dd9393 100644--- a/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts+++ b/desktop-app/ui/src/hooks/domain/useComposerAttachments.ts@@ -1,4 +1,5 @@import { useCallback, useEffect, useRef, useState } from 'react'+import { FILE_REFERENCE_MAX_COUNT } from '@clerum/gfs-interaction-policy'import { COMPOSER_MAX_IMAGE_ATTACHMENTS } from '@constants/attachments'import { clearComposerDraft, clearComposerDraftAfterSend } from '@lib/composerDraftStore'import type { ComposerImageAttachment, ComposerReferenceAttachment } from '../../uiTypes'@@ -49,6 +50,45 @@ export function mergeComposerImageAttachments(return { next: accepted, kept, duplicates, dropped: incoming.length - kept - duplicates }}+export interface ComposerReferenceMergeOutcome {+ /** The reconciled composer set (existing + accepted incoming, in order). */+ next: ComposerReferenceAttachment[]+ /** Incoming references that took a slot in the composer. */+ kept: number+ /** Incoming references already held by id (not added). */+ duplicates: number+ /** Incoming references the send-time file limit leaves no room for (lost). */+ dropped: number+}++/**+ * Reconciles incoming references into the composer's reference set with the+ * add handler's id-dedupe PLUS the send-time `FILE_REFERENCE_MAX_COUNT` cap: a+ * restore that produces an unsendable composer is not a restore (spec: restore+ * semantics), so the cap is enforced while restoring, not first at send.+ */+export function mergeComposerReferenceAttachments(+ existing: ComposerReferenceAttachment[],+ incoming: ComposerReferenceAttachment[],+ nextOrder: () => number = () => 0+): ComposerReferenceMergeOutcome {+ const accepted = [...existing]+ let kept = 0+ let duplicates = 0+ for (const attachment of incoming) {+ if (accepted.some(candidate => candidate.id === attachment.id)) {+ duplicates += 1+ continue+ }+ if (accepted.length >= FILE_REFERENCE_MAX_COUNT) break+ accepted.push(+ attachment.addedOrder != null ? attachment : { ...attachment, addedOrder: nextOrder() }+ )+ kept += 1+ }+ return { next: accepted, kept, duplicates, dropped: incoming.length - kept - duplicates }+}+interface UseComposerAttachmentsParams {/** Attachments are per-agent; switching agents clears the pending composer. */selectedAgent: string | null@@ -279,6 +319,30 @@ export function useComposerAttachments({[clearSendError])+ /**+ * Restore-path reconciliation for references (R1-M5): id-dedupe plus the+ * send-time file cap, applied against the live snapshot, with counts that+ * describe exactly that reconciliation.+ */+ const restoreComposerReferenceAttachments = useCallback(+ (incoming: ComposerReferenceAttachment[]): ComposerReferenceMergeOutcome => {+ composerAttachmentRevisionRef.current += 1+ const outcome = mergeComposerReferenceAttachments(+ composerReferenceAttachmentsRef.current,+ incoming,+ () => {+ composerAttachmentOrderRef.current += 1+ return composerAttachmentOrderRef.current+ }+ )+ composerReferenceAttachmentsRef.current = outcome.next+ setComposerReferenceAttachments(outcome.next)+ clearSendError()+ return outcome+ },+ [clearSendError]+ )+return {composerImageAttachments,composerAttachmentRevisionRef,@@ -292,5 +356,6 @@ export function useComposerAttachments({handleAddComposerReferenceAttachments,handleRemoveComposerReferenceAttachment,restoreComposerImageAttachments,+ restoreComposerReferenceAttachments,}}