Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop-app): require turn identity for image echo collapse

Mfdbd59ac0a6d
diff --git a/desktop-app/src/chatMessageMerge.ts b/desktop-app/src/chatMessageMerge.ts
index 7b78be2fe..c7214d8d0 100644
--- a/desktop-app/src/chatMessageMerge.ts
+++ b/desktop-app/src/chatMessageMerge.ts
@@ -93,51 +93,17 @@ 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))
+function hasByteBearingUploadedImage(message: ChatMessage): boolean {
+ // Parsed server image chips expose only filename and list position. Those
+ // fields cannot distinguish different bytes in same-name prompts. A
+ // content-only collapse has no stronger turn identity, so the local image
+ // must remain on its own bubble. Response files are assistant output
+ // artifacts and retain their established collapse behavior.
+ return Boolean(
+ message.attachments?.some(
+ attachment => attachment.type === 'uploaded_file' && Boolean(attachment.dataBase64)
)
- if (index < 0) return false
- unpaired.splice(index, 1)
- }
- return true
+ )
}
function preferredServerMessage(
@@ -455,7 +421,7 @@ export function mergeAuthoritativeServerMessages(
: matchesAuthoritative(previousAuthoritative)
? previousAuthoritative
: undefined
- if (echoRow && hasProvenCollapsedImageContext(echoRow, message, authoritative, existing)) {
+ if (echoRow && !hasByteBearingUploadedImage(message)) {
dropLocalEcho(message, echoRow)
}
}
@@ -479,8 +445,8 @@ export function mergeAuthoritativeServerMessages(
// 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. Byte-bearing
- // images reach this path only after unique text and server image context
- // prove their association. Keep non-reference artifacts and toolSteps. It
+ // uploaded images never reach this path on a content-only collapse;
+ // response-file output artifacts and toolSteps can still be kept. It
// deliberately
// does NOT use preferredServerMessage/copyLocalMetadata here: that path also
// copies `task_id` (echo identity), which is correct for D-1 replacement (the
diff --git a/desktop-app/ui/docs/attachment-reconciliation-provenance.md b/desktop-app/ui/docs/attachment-reconciliation-provenance.md
index 6dbd20244..187ef47a9 100644
--- a/desktop-app/ui/docs/attachment-reconciliation-provenance.md
+++ b/desktop-app/ui/docs/attachment-reconciliation-provenance.md
@@ -4,10 +4,10 @@
- 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.
+- Server-parsed image chips currently carry a filename but no turn-scoped image identity or byte digest. Their parsed IDs also derive from list position and filename. Neither a matching filename, parsed ID, nor matching multiplicity proves that two images have the same bytes or belong to the same prompt. 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.
+- A content-only collapse must not transfer byte-bearing uploaded images with the current server payload. Keep their local bubble even if role, text, filenames, and chip counts match. Byte recovery remains allowed when turn number plus role, deterministic server message ID, or a unique live-task binding identifies the same turn. A future content-only transfer would require an independently verified turn-scoped image identity or content digest on both sides.
+- This restriction applies to uploaded input images. A `response_file` is an assistant output artifact, not a Resend input; a settled assistant echo carrying its bytes may still collapse under the adjacent-turn rule and retain that artifact on the surviving assistant row. Label-only legacy uploads may also collapse and retain their unrestorable warning. Text-only echoes and other non-reference artifacts keep their existing collapse behavior.
- 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.ts
index 3ce940113..91585acff 100644
--- a/desktop-app/ui/src/hooks/__tests__/sessionAdapter.test.ts
+++ b/desktop-app/ui/src/hooks/__tests__/sessionAdapter.test.ts
@@ -1,6 +1,10 @@
import { describe, expect, it } from 'vitest'
import { mergeAuthoritativeServerMessages } from '../../../../src/chatMessageMerge'
import type { ChatMessage } from '../../../../src/types'
+import {
+ buildChatMessageAttachments,
+ buildResponseFileAttachments,
+} from '../../lib/chatMessageAttachments'
import {
buildComposerReferencesPromptSection,
buildComposerRequestContent,
@@ -322,44 +326,144 @@ describe('turnsToChatMessages', () => {
expect(buildComposerResendDraft(once[1]!).imageAttachments[0]?.dataBase64).toBe('Ag==')
})
- it('recovers image bytes for a unique text echo with matching server image context', () => {
+ it('keeps same-name bytes on their own prompt when only a content match exists', () => {
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',
+ user_input: 'repeat\n\n[Attached images]\n- shared.png',
started_at: new Date(3).toISOString(),
},
])
+ const firstImage = buildChatMessageAttachments(
+ [
+ {
+ id: 'first-image',
+ name: 'shared.png',
+ mimeType: 'image/png',
+ dataBase64: 'AQ==',
+ sizeBytes: 1,
+ previewDataUrl: 'data:image/png;base64,AQ==',
+ },
+ ],
+ []
+ )[0]!
+ const secondImage = buildChatMessageAttachments(
+ [
+ {
+ id: 'second-image',
+ name: 'shared.png',
+ mimeType: 'image/png',
+ dataBase64: 'Ag==',
+ sizeBytes: 1,
+ previewDataUrl: 'data:image/png;base64,Ag==',
+ },
+ ],
+ []
+ )[0]!
+ expect(firstImage.dataBase64).not.toBe(secondImage.dataBase64)
+ const firstTurn: ChatMessage = {
+ id: 'turn-1-user',
+ role: 'user',
+ content: 'repeat',
+ timestamp: 1,
+ serverTurnNumber: 1,
+ attachments: [firstImage],
+ }
const localEcho: ChatMessage = {
- id: 'second-local-echo',
+ id: 'first-prompt-idle-echo',
role: 'user',
content: 'repeat',
timestamp: 2,
+ attachments: [firstImage],
+ }
+ const existing = [firstTurn, localEcho, incoming[0]!]
+ const once = mergeAuthoritativeServerMessages(existing, 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',
+ 'first-prompt-idle-echo',
+ 'turn-2-user',
+ ])
+ expect(buildComposerResendDraft(once[0]!).imageAttachments[0]?.dataBase64).toBe('AQ==')
+ expect(buildComposerResendDraft(once[1]!).imageAttachments[0]?.dataBase64).toBe('AQ==')
+ expect(buildComposerResendDraft(once[2]!).imageAttachments).toEqual([])
+ expect(once[2]?.attachments).toMatchObject([{ type: 'uploaded_file', label: 'shared.png' }])
+ })
+
+ it('recovers image bytes when the local and server message have the same turn identity', () => {
+ const incoming = turnsToChatMessages([
+ {
+ number: 2,
+ user_input: 'repeat\n\n[Attached images]\n- shared.png',
+ started_at: new Date(3).toISOString(),
+ },
+ ])
+ const local: ChatMessage = {
+ ...incoming[0]!,
+ attachments: buildChatMessageAttachments(
+ [
+ {
+ id: 'same-turn-image',
+ name: 'shared.png',
+ mimeType: 'image/png',
+ dataBase64: 'Ag==',
+ sizeBytes: 1,
+ previewDataUrl: 'data:image/png;base64,Ag==',
+ },
+ ],
+ []
+ ),
+ }
+ const merged = mergeAuthoritativeServerMessages([local], incoming)
+ expect(merged.map(message => message.id)).toEqual(['turn-2-user'])
+ expect(merged[0]?.attachments).toMatchObject([
+ { type: 'uploaded_file', label: 'shared.png', dataBase64: 'Ag==' },
+ ])
+ expect(buildComposerResendDraft(merged[0]!).imageAttachments[0]?.dataBase64).toBe('Ag==')
+ expect(mergeAuthoritativeServerMessages(merged, incoming)).toEqual(merged)
+ })
+
+ it('collapses a settled assistant echo with producer-built response file bytes', () => {
+ const incoming = turnsToChatMessages([
+ { number: 1, user_input: 'first', response: 'one', started_at: new Date(1).toISOString() },
+ { number: 2, user_input: 'second', response: 'done', started_at: new Date(3).toISOString() },
+ ])
+ const responseFile = buildResponseFileAttachments({
attachments: [
{
- id: 'second-image',
- type: 'uploaded_file',
- label: 'second.png',
- filename: 'second.png',
- mimeType: 'image/png',
+ id: 'result',
+ kind: 'file',
+ filename: 'result.txt',
+ mimeType: 'text/plain',
encoding: 'base64',
- dataBase64: 'Ag==',
- sizeBytes: 1,
+ dataBase64: 'b2s=',
+ sizeBytes: 2,
},
],
+ })[0]!
+ const localEcho: ChatMessage = {
+ id: 'settled-assistant-echo',
+ role: 'assistant',
+ content: 'done',
+ timestamp: 2,
+ attachments: [responseFile],
}
- const merged = mergeAuthoritativeServerMessages(
- [incoming[0]!, localEcho, incoming[1]!],
+ const once = mergeAuthoritativeServerMessages(
+ [incoming[1]!, localEcho, incoming[3]!],
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(once.filter(message => message.role === 'assistant')).toHaveLength(2)
+ expect(once.some(message => message.id === 'settled-assistant-echo')).toBe(false)
+ expect(once.find(message => message.id === 'turn-2-assistant')?.attachments).toEqual([
+ responseFile,
])
- expect(buildComposerResendDraft(merged[1]!).imageAttachments[0]?.dataBase64).toBe('Ag==')
- expect(mergeAuthoritativeServerMessages(merged, incoming)).toEqual(merged)
+ expect(mergeAuthoritativeServerMessages(once, incoming)).toEqual(once)
})
it('preserves turn order for multi-turn transcripts', () => {