Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): require provenance for collapsed echo images
M55ed7758e6e3
diff --git a/desktop-app/src/__tests__/chatMessageMerge.test.ts b/desktop-app/src/__tests__/chatMessageMerge.test.tsindex d9268f5fd..59d2289f1 100644--- a/desktop-app/src/__tests__/chatMessageMerge.test.ts+++ b/desktop-app/src/__tests__/chatMessageMerge.test.ts@@ -169,7 +169,7 @@ describe('mergeAuthoritativeServerMessages image byte gap-fill', () => {])})- it('appends a local-only image with bytes when a settled echo collapses onto a server reference row', () => {+ it('keeps an unproven local image on its own idle echo', () => {const serverPlugin: ChatMessageAttachment = {id: 'server-plugin',type: 'plugin',@@ -211,9 +211,14 @@ describe('mergeAuthoritativeServerMessages image byte gap-fill', () => {{ activeTaskIds: new Set() })- expect(merged.map(message => message.id)).toEqual(['turn-1-user', 'turn-2-user'])- expect(merged[1]?.attachments).toEqual([serverPlugin, imageWithBytes])- expect(merged[1]?.task_id).toBeUndefined()+ expect(merged.map(message => message.id)).toEqual([+ 'turn-1-user',+ 'optimistic-user',+ 'turn-2-user',+ ])+ expect(merged[1]?.attachments).toContainEqual(imageWithBytes)+ expect(merged[2]?.attachments).toEqual([serverPlugin])+ expect(merged[2]?.task_id).toBeUndefined()})})diff --git a/desktop-app/src/chatMessageMerge.ts b/desktop-app/src/chatMessageMerge.tsindex d47279e91..7b78be2fe 100644--- a/desktop-app/src/chatMessageMerge.ts+++ b/desktop-app/src/chatMessageMerge.ts@@ -93,6 +93,53 @@ function collapseSafeAttachments()}+function hasProvenCollapsedImageContext(+ server: ChatMessage,+ local: ChatMessage,+ authoritative: ChatMessage[],+ existing: ChatMessage[]+): boolean {+ const byteAttachments =+ local.attachments?.filter(attachment => Boolean(attachment.dataBase64)) ?? []+ if (!byteAttachments.length) return true+ if (byteAttachments.some(attachment => attachment.type !== 'uploaded_file')) return false++ // Content is only an identity hint when neither side has another claimant.+ if (+ authoritative.filter(row => row.role === local.role && row.content === local.content).length !==+ 1 ||+ existing.filter(+ row =>+ messageServerTurnNumber(row) === undefined &&+ row.role === local.role &&+ row.content === local.content &&+ !row.isError &&+ !row.preserveLocal+ ).length !== 1+ ) {+ return false+ }++ // The server must explicitly name every image. Pair repeated names by+ // occurrence, as mergeAttachmentChips does; a missing or extra chip is not+ // evidence that these local bytes belong to this server turn.+ const serverImages =+ server.attachments?.filter(attachment => attachment.type === 'uploaded_file') ?? []+ if (serverImages.length !== byteAttachments.length) return false+ const unpaired = [...serverImages]+ for (const localImage of byteAttachments) {+ const index = unpaired.findIndex(+ serverImage =>+ serverImage.id === localImage.id ||+ (attachmentName(serverImage) !== '' &&+ attachmentName(serverImage) === attachmentName(localImage))+ )+ if (index < 0) return false+ unpaired.splice(index, 1)+ }+ return true+}+function preferredServerMessage(server: ChatMessage,local: ChatMessage | undefined,@@ -408,7 +455,9 @@ export function mergeAuthoritativeServerMessages(: matchesAuthoritative(previousAuthoritative)? previousAuthoritative: undefined- if (echoRow) dropLocalEcho(message, echoRow)+ if (echoRow && hasProvenCollapsedImageContext(echoRow, message, authoritative, existing)) {+ dropLocalEcho(message, echoRow)+ }}continue}@@ -429,8 +478,10 @@ export function mergeAuthoritativeServerMessages(// Merge safe side metadata of a collapsed idle echo onto its authoritative// slot row (§6.2, R2-M1). A same-text collapse does not prove that a local// plugin/file reference belonged to this server turn; Resend must not turn- // that unverified reference into a new prompt attachment. Keep byte-bearing- // files and non-reference artifacts, plus toolSteps. It deliberately+ // that unverified reference into a new prompt attachment. Byte-bearing+ // images reach this path only after unique text and server image context+ // prove their association. Keep non-reference artifacts and toolSteps. It+ // deliberately// does NOT use preferredServerMessage/copyLocalMetadata here: that path also// copies `task_id` (echo identity), which is correct for D-1 replacement (the// server IS the live task's echo) but WRONG for an idle collapse — the task isdiff --git a/desktop-app/ui/docs/attachment-reconciliation-provenance.md b/desktop-app/ui/docs/attachment-reconciliation-provenance.mdnew file mode 100644index 000000000..6dbd20244--- /dev/null+++ b/desktop-app/ui/docs/attachment-reconciliation-provenance.md@@ -0,0 +1,13 @@+# Attachment reconciliation provenance mini-spec++## Identity and authority++- A matching server turn number and role, or the same deterministic server message ID, identifies the same turn. A uniquely bound live task can also replace its own optimistic turn. In these cases the server owns the visible chips and local upload bytes may enrich matching image chips or remain as local byte-bearing uploads.+- A content-only idle echo has no turn identity. Equal role and text alone is only a candidate match. It is unambiguous only when exactly one authoritative turn of that role has that text in the reconciliation input and exactly one local idle echo claims it.+- For a content-only collapse, server-parsed attachment context must independently identify every local image whose bytes would move: the server must have a matching uploaded-file chip, paired by stable ID where possible or by type, name, and occurrence with equal multiplicity. Server chips remain authoritative. A plugin or file reference on the local echo never becomes a server reference through text coincidence.++## Transfer and ambiguity++- Transfer image bytes across a content-only collapse only when both the unique-text condition and matching server-parsed image context hold. Keep the server chip identity and order; add only the local bytes and missing byte metadata.+- If either proof is missing, do not move the image or collapse its local bubble. The image stays with its own turn so Resend cannot attach it to a different server turn. Text-only echoes and non-reference artifacts may still collapse under the existing adjacent-turn rule.+- Reconciliation is idempotent: repeating it with the same server turns must neither duplicate attachments nor move bytes to another turn. A later server response with stronger identity may safely reconcile a previously retained local bubble.diff --git a/desktop-app/ui/src/hooks/__tests__/sessionAdapter.test.ts b/desktop-app/ui/src/hooks/__tests__/sessionAdapter.test.tsindex 55836eeab..3ce940113 100644--- a/desktop-app/ui/src/hooks/__tests__/sessionAdapter.test.ts+++ b/desktop-app/ui/src/hooks/__tests__/sessionAdapter.test.ts@@ -248,14 +248,118 @@ describe('turnsToChatMessages', () => {incoming,{ activeTaskIds: new Set() })- expect(merged.map(message => message.id)).toEqual(['turn-1-user', 'turn-2-user'])- const serverUser = merged[1]!- expect(serverUser.attachments).toMatchObject([{ type: 'uploaded_file', dataBase64: 'AQ==' }])+ expect(merged.map(message => message.id)).toEqual(['turn-1-user', 'idle-echo', 'turn-2-user'])+ const serverUser = merged[2]!+ expect(serverUser.attachments).toBeUndefined()const draft = buildComposerResendDraft(serverUser)expect(draft.content).toBe('repeat')- expect(draft.imageAttachments).toHaveLength(1)+ expect(draft.imageAttachments).toHaveLength(0)expect(draft.referenceAttachments).toEqual([])expect(draft.unrestorable).toEqual([])+ expect(buildComposerResendDraft(merged[1]!).imageAttachments[0]?.dataBase64).toBe('AQ==')+ })++ it('does not move either image across ambiguous same-text prompts, including on repeated reconciliation', () => {+ const incoming = turnsToChatMessages([+ {+ number: 1,+ user_input: 'repeat\n\n[Attached images]\n- first.png',+ started_at: new Date(1).toISOString(),+ },+ {+ number: 2,+ user_input: 'repeat\n\n[Attached images]\n- second.png',+ started_at: new Date(3).toISOString(),+ },+ ])+ const localEcho: ChatMessage = {+ id: 'second-local-echo',+ role: 'user',+ content: 'repeat',+ timestamp: 2,+ attachments: [+ {+ id: 'second-image',+ type: 'uploaded_file',+ label: 'second.png',+ filename: 'second.png',+ mimeType: 'image/png',+ encoding: 'base64',+ dataBase64: 'Ag==',+ sizeBytes: 1,+ },+ ],+ }+ const firstLocal: ChatMessage = {+ ...incoming[0]!,+ attachments: [+ {+ id: 'first-image',+ type: 'uploaded_file',+ label: 'first.png',+ filename: 'first.png',+ mimeType: 'image/png',+ encoding: 'base64',+ dataBase64: 'AQ==',+ sizeBytes: 1,+ },+ ],+ }+ const once = mergeAuthoritativeServerMessages([firstLocal, localEcho, incoming[1]!], incoming, {+ activeTaskIds: new Set(),+ })+ const twice = mergeAuthoritativeServerMessages(once, incoming, { activeTaskIds: new Set() })+ expect(twice).toEqual(once)+ expect(once.map(message => message.id)).toEqual([+ 'turn-1-user',+ 'second-local-echo',+ 'turn-2-user',+ ])+ expect(+ buildComposerResendDraft(once[0]!).imageAttachments.map(image => image.dataBase64)+ ).toEqual(['AQ=='])+ expect(buildComposerResendDraft(once[2]!).imageAttachments).toEqual([])+ expect(buildComposerResendDraft(once[1]!).imageAttachments[0]?.dataBase64).toBe('Ag==')+ })++ it('recovers image bytes for a unique text echo with matching server image context', () => {+ const incoming = turnsToChatMessages([+ { number: 1, user_input: 'first', started_at: new Date(1).toISOString() },+ {+ number: 2,+ user_input: 'repeat\n\n[Attached images]\n- second.png',+ started_at: new Date(3).toISOString(),+ },+ ])+ const localEcho: ChatMessage = {+ id: 'second-local-echo',+ role: 'user',+ content: 'repeat',+ timestamp: 2,+ attachments: [+ {+ id: 'second-image',+ type: 'uploaded_file',+ label: 'second.png',+ filename: 'second.png',+ mimeType: 'image/png',+ encoding: 'base64',+ dataBase64: 'Ag==',+ sizeBytes: 1,+ },+ ],+ }+ const merged = mergeAuthoritativeServerMessages(+ [incoming[0]!, localEcho, incoming[1]!],+ incoming,+ { activeTaskIds: new Set() }+ )+ expect(merged.map(message => message.id)).toEqual(['turn-1-user', 'turn-2-user'])+ expect(merged[1]?.attachments).toMatchObject([+ { type: 'uploaded_file', label: 'second.png', dataBase64: 'Ag==' },+ ])+ expect(buildComposerResendDraft(merged[1]!).imageAttachments[0]?.dataBase64).toBe('Ag==')+ expect(mergeAuthoritativeServerMessages(merged, incoming)).toEqual(merged)})it('preserves turn order for multi-turn transcripts', () => {