Skip to content

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

TypeScript

fix(desktop-ui): keep a send whose documents never reached the Host

Alfredo Lopezed5c7c9f8d3f
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/sendAgentMessage.documents.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/sendAgentMessage.documents.test.tsx
index 350bf52c1..f786b01b2 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/sendAgentMessage.documents.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/sendAgentMessage.documents.test.tsx
@@ -275,6 +275,50 @@ describe('sendAgentMessage — a Host without file attachments (#678 D13)', () =
expect(rendered.result.current.failedAgentSend?.files).toHaveLength(2)
})
+ it('async task accepted without listing the file: keeps the snapshot after the task succeeds', async () => {
+ clerum.rpc.getTaskResult.mockResolvedValue({ status: 'completed', response: 'all done' })
+ const rendered = renderController()
+ await settleMount()
+ const file = await addReadyFile(rendered)
+ clerum.rpc.invokeHostMessage.mockResolvedValueOnce({
+ taskId: 'task-doc',
+ acceptedAttachmentIds: [],
+ })
+
+ const send = act(async () => {
+ await rendered.result.current.handleSendAgentMessage('Summarize this')
+ })
+ await waitFor(() => expect(clerum.hasProgressHandler('task-doc')).toBe(true))
+ await send
+
+ // The sent bubble lists the document by name.
+ const sent = rendered.result.current.chatMessages.find(message => message.role === 'user')
+ expect(sent?.attachments).toEqual([
+ expect.objectContaining({ id: file.id, type: 'uploaded_file', label: 'notes.txt' }),
+ ])
+
+ await act(async () => {
+ clerum.emitTaskProgress('task-doc', {
+ type: 'terminal',
+ data: { taskId: 'task-doc', status: 'completed' },
+ })
+ })
+
+ // Witness that the success branch ran: the durable reply was persisted.
+ await waitFor(() =>
+ expect(clerum.chat.appendMessages).toHaveBeenCalledWith(
+ 'agent-x',
+ expect.any(String),
+ expect.arrayContaining([
+ expect.objectContaining({ role: 'assistant', content: 'all done' }),
+ ])
+ )
+ )
+ expect(rendered.spies.pushToast).toHaveBeenCalledWith(UNSUPPORTED, 'error')
+ expect(rendered.result.current.failedAgentSend?.message).toBe(UNSUPPORTED)
+ expect(rendered.result.current.failedAgentSend?.files.map(item => item.id)).toEqual([file.id])
+ })
+
it('does not read an empty ack (an unreadable Host answer) as a dropped file', async () => {
const rendered = renderController()
await settleMount()
diff --git a/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts b/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts
index 4480b9242..c3ac2bb94 100644
--- a/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts
+++ b/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts
@@ -130,4 +130,89 @@ describe('retainedSendStore — documents the Host never received (#678 D13)', (
expect(held(store, ['chat-1/delivered'])).toEqual([])
})
+
+ function seedUndeliveredFiles() {
+ const store = createRetainedSendStore(vi.fn())
+ store.retainSendSnapshot(
+ snapshot('undelivered', 100, {
+ reason: 'host_files_unsupported',
+ failure: { message: 'The Host did not receive the files.', kind: 'upstream' },
+ })
+ )
+ store.retainSendSnapshot(snapshot('ordinary-failure', 150))
+ store.retainSendSnapshot(
+ snapshot('later', 200, { reason: 'awaiting_terminal', failure: undefined })
+ )
+ return store
+ }
+ const SEEDED = ['chat-1/undelivered', 'chat-1/ordinary-failure', 'chat-1/later']
+
+ it('a later successful send in the chat keeps an older snapshot whose files never arrived', () => {
+ const store = seedUndeliveredFiles()
+
+ store.releaseSucceededRetainedSend('agent-x', 'chat-1', 'later')
+
+ // Witness: the success did supersede the ordinary older failure.
+ expect(held(store, SEEDED)).toEqual(['chat-1/undelivered'])
+ })
+
+ it('a later successful task in the chat keeps an older snapshot whose files never arrived', () => {
+ const store = seedUndeliveredFiles()
+ store.attachTaskIdToRetainedSend('agent-x', 'chat-1', 'later', 'task-later')
+
+ store.releaseSucceededRetainedSendsForTask('task-later')
+
+ expect(held(store, SEEDED)).toEqual(['chat-1/undelivered'])
+ })
+
+ it('a discard still releases an older snapshot whose files never arrived', () => {
+ const store = seedUndeliveredFiles()
+
+ // The user dismissed the newest failure of the chat, and the ones behind it.
+ store.releaseRetainedFailuresForChat('agent-x', 'chat-1', 150)
+
+ // Witness: the send still awaiting its terminal is not a failure and stays.
+ expect(held(store, SEEDED)).toEqual(['chat-1/later'])
+ })
+
+ it('a later task failure updates the message and keeps the undelivered-files reason', () => {
+ const store = createRetainedSendStore(vi.fn())
+ store.retainSendSnapshot(
+ snapshot('with-files', 200, { reason: 'awaiting_terminal', failure: undefined })
+ )
+ store.attachTaskIdToRetainedSend('agent-x', 'chat-1', 'with-files', 'task-files')
+ store.markRetainedSendReason(
+ 'task-files',
+ 'host_files_unsupported',
+ 'The Host did not receive the files.',
+ 'upstream'
+ )
+
+ store.markRetainedSendReason('task-files', 'async_task_failed', 'The task failed.', 'upstream')
+
+ const kept = store.getRetainedSendSnapshot('agent-x', 'chat-1', 'with-files')
+ // Witness: the second mark ran and replaced the failure message.
+ expect(kept?.failure).toEqual({ message: 'The task failed.', kind: 'upstream' })
+ expect(kept?.reason).toBe('host_files_unsupported')
+ // So the task's later success still keeps it.
+ store.releaseSucceededRetainedSendsForTask('task-files')
+ expect(held(store, ['chat-1/with-files'])).toEqual(['chat-1/with-files'])
+ })
+
+ it('a later send failure updates the message and keeps the undelivered-files reason', () => {
+ const store = seedUndeliveredFiles()
+
+ store.failRetainedSend(
+ 'agent-x',
+ 'chat-1',
+ 'undelivered',
+ 'stream_lost',
+ 'The stream was lost.',
+ 'network'
+ )
+
+ const kept = store.getRetainedSendSnapshot('agent-x', 'chat-1', 'undelivered')
+ expect(kept?.failure).toEqual({ message: 'The stream was lost.', kind: 'network' })
+ expect(kept?.reason).toBe('host_files_unsupported')
+ })
})
diff --git a/desktop-app/ui/src/lib/retainedSendStore.ts b/desktop-app/ui/src/lib/retainedSendStore.ts
index 647f3d4b4..fdf68a0b5 100644
--- a/desktop-app/ui/src/lib/retainedSendStore.ts
+++ b/desktop-app/ui/src/lib/retainedSendStore.ts
@@ -11,7 +11,8 @@
* and released only on an explicit terminal outcome: its own successful reply
* or cancel, a user-initiated discard (which also drops the older failures of
* that chat), or a newer send for the same chat that reaches a successful
- * terminal, which supersedes every failure recorded before it. A snapshot still
+ * terminal, which supersedes every failure recorded before it except one whose
+ * documents the Host never received (#678 D13). A snapshot still
* awaiting its own terminal is never released by another send. Receiving a
* `taskId` is NOT a terminal acknowledgement — a started task can still fail —
* so it never releases the snapshot by itself.
@@ -129,17 +130,22 @@ export function createRetainedSendStore(changed: () => void) {
* Releases the failed snapshots of one chat recorded at or before
* `upToTimestamp`. Snapshots without a failure are still awaiting their own
* terminal and stay held: that send can still fail, and then its snapshot is
- * the only copy of the payload.
+ * the only copy of the payload. A later success passes
+ * `keepUndeliveredFiles`: it supersedes an older failure, but not documents
+ * the Host never received, which exist nowhere else. A user discard releases
+ * those too.
*/
function releaseRetainedFailuresForChat(
agentRef: string,
chatId: string | null,
- upToTimestamp: number
+ upToTimestamp: number,
+ options: { keepUndeliveredFiles?: boolean } = {}
): void {
let released = false
for (const [key, snapshot] of snapshots) {
if (snapshot.agentRef !== agentRef || snapshot.chatId !== chatId) continue
if (!snapshot.failure || snapshot.timestamp > upToTimestamp) continue
+ if (options.keepUndeliveredFiles && snapshot.reason === 'host_files_unsupported') continue
snapshots.delete(key)
released = true
}
@@ -158,7 +164,9 @@ export function createRetainedSendStore(changed: () => void) {
): void {
const succeeded = snapshots.get(keyFor(agentRef, chatId, userMessageId))
if (succeeded) {
- releaseRetainedFailuresForChat(succeeded.agentRef, succeeded.chatId, succeeded.timestamp)
+ releaseRetainedFailuresForChat(succeeded.agentRef, succeeded.chatId, succeeded.timestamp, {
+ keepUndeliveredFiles: true,
+ })
}
releaseRetainedSend(agentRef, chatId, userMessageId)
}
@@ -173,11 +181,25 @@ export function createRetainedSendStore(changed: () => void) {
snapshot => snapshot.taskId === taskId && snapshot.reason !== 'host_files_unsupported'
)
for (const snapshot of succeeded) {
- releaseRetainedFailuresForChat(snapshot.agentRef, snapshot.chatId, snapshot.timestamp)
+ releaseRetainedFailuresForChat(snapshot.agentRef, snapshot.chatId, snapshot.timestamp, {
+ keepUndeliveredFiles: true,
+ })
releaseRetainedSend(snapshot.agentRef, snapshot.chatId, snapshot.userMessageId)
}
}
+ /**
+ * A snapshot whose documents the Host never received keeps that reason when a
+ * later failure is recorded: the reason is what stops a later success from
+ * releasing files that exist nowhere else. Only the shown failure changes.
+ */
+ function nextReason(
+ snapshot: RetainedSendSnapshot,
+ reason: RetainedSendReason
+ ): RetainedSendReason {
+ return snapshot.reason === 'host_files_unsupported' ? snapshot.reason : reason
+ }
+
/**
* Records why a snapshot is still held once the async outcome is known. Keeps
* the payload and identity untouched; only the recovery code changes.
@@ -190,7 +212,11 @@ export function createRetainedSendStore(changed: () => void) {
): void {
for (const [key, snapshot] of snapshots) {
if (snapshot.taskId !== taskId) continue
- snapshots.set(key, { ...snapshot, reason, failure: { message, kind } })
+ snapshots.set(key, {
+ ...snapshot,
+ reason: nextReason(snapshot, reason),
+ failure: { message, kind },
+ })
changed()
}
}
@@ -212,7 +238,11 @@ export function createRetainedSendStore(changed: () => void) {
const key = keyFor(agentRef, chatId, userMessageId)
const existing = snapshots.get(key)
if (!existing) return
- snapshots.set(key, { ...existing, reason, failure: { message, kind } })
+ snapshots.set(key, {
+ ...existing,
+ reason: nextReason(existing, reason),
+ failure: { message, kind },
+ })
changed()
}
return {