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 documents the Host never received across releases

Alfredo Lopez8323cdefdada
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/retainedSendRelease.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/retainedSendRelease.test.tsx
index a85c4f9a1..cd9017d9a 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/retainedSendRelease.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/retainedSendRelease.test.tsx
@@ -22,6 +22,8 @@ type RetainedSendStore = ReturnType<
const captured = vi.hoisted(() => ({
stores: [] as RetainedSendStore[],
retained: [] as RetainedSendSnapshot[],
+ /** Task ids the controller asked the store to release as ended. */
+ releasedTasks: [] as string[],
}))
vi.mock('@lib/retainedSendStore', async importOriginal => {
@@ -34,7 +36,11 @@ vi.mock('@lib/retainedSendStore', async importOriginal => {
captured.retained.push(snapshot)
store.retainSendSnapshot(snapshot)
}
- const wrapped = { ...store, retainSendSnapshot }
+ const releaseRetainedSendsForTask = (taskId: string) => {
+ captured.releasedTasks.push(taskId)
+ store.releaseRetainedSendsForTask(taskId)
+ }
+ const wrapped = { ...store, retainSendSnapshot, releaseRetainedSendsForTask }
captured.stores.push(wrapped)
return wrapped
},
@@ -52,6 +58,7 @@ beforeEach(() => {
uuidCounter = 0
captured.stores.length = 0
captured.retained.length = 0
+ captured.releasedTasks.length = 0
vi.spyOn(globalThis.crypto, 'randomUUID').mockImplementation(
() => `uuid-${++uuidCounter}` as `${string}-${string}-${string}-${string}-${string}`
)
@@ -211,27 +218,27 @@ describe('retained send release on terminal outcomes (#654 M6)', () => {
})
})
-describe('older failures of a chat (#654 M2)', () => {
- /** Sends one message whose POST throws, leaving a retained failure. */
- async function sendFailing(result: ReturnType<typeof renderController>['result'], text: string) {
- clerum.rpc.invokeHostMessage.mockRejectedValueOnce(new Error('network down'))
- await act(async () => {
- await result.current.handleSendAgentMessage(text)
- })
- expect(result.current.failedAgentSend?.content).toBe(text)
- }
+/** Sends one message whose POST throws, leaving a retained failure. */
+async function sendFailing(result: ReturnType<typeof renderController>['result'], text: string) {
+ clerum.rpc.invokeHostMessage.mockRejectedValueOnce(new Error('network down'))
+ await act(async () => {
+ await result.current.handleSendAgentMessage(text)
+ })
+ expect(result.current.failedAgentSend?.content).toBe(text)
+}
- /** Failed snapshots still held by the controller's store. */
- function heldFailures(): RetainedSendSnapshot[] {
- expect(captured.stores).toHaveLength(1)
- const [store] = captured.stores
- return captured.retained
- .map(retained =>
- store!.getRetainedSendSnapshot(retained.agentRef, retained.chatId, retained.userMessageId)
- )
- .filter((snapshot): snapshot is RetainedSendSnapshot => Boolean(snapshot?.failure))
- }
+/** Failed snapshots still held by the controller's store. */
+function heldFailures(): RetainedSendSnapshot[] {
+ expect(captured.stores).toHaveLength(1)
+ const [store] = captured.stores
+ return captured.retained
+ .map(retained =>
+ store!.getRetainedSendSnapshot(retained.agentRef, retained.chatId, retained.userMessageId)
+ )
+ .filter((snapshot): snapshot is RetainedSendSnapshot => Boolean(snapshot?.failure))
+}
+describe('older failures of a chat (#654 M2)', () => {
it('hides an older failure once a later synchronous send succeeds', async () => {
const { result, spies } = renderController()
await settleMount()
@@ -330,3 +337,189 @@ describe('older failures of a chat (#654 M2)', () => {
expect(result.current.agentError).toBe(blocker)
})
})
+
+/**
+ * jozer-rami review 5445124702 (A15 items 2 and 3) — documents the Host never
+ * received exist nowhere else, so ending a task or dismissing a newer failure
+ * must not drop them. The snapshot stays visible, offers Recover files, and
+ * nothing is sent again.
+ */
+describe('documents the Host never received (#678 A15)', () => {
+ type Result = ReturnType<typeof renderController>['result']
+
+ /** Adds one document through the public action and waits until it is read. */
+ async function addReadyFile(result: Result, name: string) {
+ act(() => {
+ result.current.handleAddComposerFiles(
+ [new File([new TextEncoder().encode(`${name} body`)], name, { type: 'text/plain' })],
+ ''
+ )
+ })
+ await waitFor(() =>
+ expect(result.current.composerFileAttachments.map(item => item.status)).toEqual(['ready'])
+ )
+ return result.current.composerFileAttachments[0]!
+ }
+
+ /** A synchronous send the Host answered without admitting its document. */
+ async function sendDropped(result: Result, text: string, name: string) {
+ const file = await addReadyFile(result, name)
+ clerum.rpc.invokeHostMessage.mockResolvedValueOnce({
+ response: 'done',
+ acceptedAttachmentIds: [],
+ })
+ await act(async () => {
+ await result.current.handleSendAgentMessage(text)
+ })
+ expect(result.current.failedAgentSend).toMatchObject({
+ content: text,
+ answeredWithoutFiles: true,
+ })
+ return file
+ }
+
+ /** An async task the Host accepted without admitting its document. */
+ async function sendAsyncDropped(result: Result, taskId: string) {
+ const file = await addReadyFile(result, 'notes.txt')
+ clerum.rpc.invokeHostMessage.mockResolvedValueOnce({ taskId, acceptedAttachmentIds: [] })
+ const send = act(async () => {
+ await result.current.handleSendAgentMessage('keep this payload')
+ })
+ await waitFor(() => expect(clerum.hasProgressHandler(taskId)).toBe(true), {
+ timeout: WIRING_TIMEOUT_MS,
+ })
+ await send
+ // Liveness: the drop was recorded on the task's snapshot.
+ expect(heldSnapshot()).toMatchObject({ taskId, reason: 'host_files_dropped' })
+ expect(result.current.failedAgentSend?.answeredWithoutFiles).toBe(true)
+ return file
+ }
+
+ /** The kept snapshot offers its files back and never sends the text again. */
+ async function expectFilesRecoverableAndNotResent(result: Result, fileId: string) {
+ expect(result.current.failedAgentSend).toMatchObject({
+ content: 'keep this payload',
+ answeredWithoutFiles: true,
+ })
+ await act(async () => {
+ await result.current.handleRetryFailedAgentSend()
+ })
+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(1)
+
+ act(() => result.current.handleRecoverFailedAgentSend())
+
+ // Recover brings the document back and releases what it recovered.
+ expect(result.current.composerFileAttachments.map(item => item.id)).toEqual([fileId])
+ expect(heldSnapshot()).toBeUndefined()
+ expect(result.current.failedAgentSend).toBeNull()
+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(1)
+ }
+
+ it('a discard keeps an older snapshot whose documents never reached the Host', async () => {
+ const { result } = renderController()
+ await settleMount()
+ const firstFile = await sendDropped(result, 'first drop', 'first.txt')
+ await sendFailing(result, 'plain failure')
+ await sendDropped(result, 'second drop', 'second.txt')
+ expect(heldFailures().map(snapshot => snapshot.content)).toEqual([
+ 'first drop',
+ 'plain failure',
+ 'second drop',
+ ])
+
+ act(() => {
+ result.current.handleDiscardFailedAgentSend()
+ })
+
+ // Witness: the visible drop and the plain failure behind it are released.
+ expect(heldFailures().map(snapshot => snapshot.content)).toEqual(['first drop'])
+ // The older drop is now the visible failure and still offers its file back.
+ expect(result.current.failedAgentSend).toMatchObject({
+ content: 'first drop',
+ answeredWithoutFiles: true,
+ })
+ expect(result.current.failedAgentSend?.files.map(item => item.id)).toEqual([firstFile.id])
+ })
+
+ it('recovering the files of the newest drop keeps an older drop recoverable', async () => {
+ const { result } = renderController()
+ await settleMount()
+ await sendDropped(result, 'first drop', 'first.txt')
+ const secondFile = await sendDropped(result, 'second drop', 'second.txt')
+
+ act(() => result.current.handleRecoverFailedAgentSend())
+
+ // Witness: the recovered drop is released and its document is back.
+ expect(result.current.composerFileAttachments.map(item => item.id)).toEqual([secondFile.id])
+ expect(heldFailures().map(snapshot => snapshot.content)).toEqual(['first drop'])
+ expect(result.current.failedAgentSend).toMatchObject({
+ content: 'first drop',
+ answeredWithoutFiles: true,
+ })
+ expect(clerum.rpc.invokeHostMessage).toHaveBeenCalledTimes(2)
+ })
+
+ it('a cancel keeps the snapshot of a task whose documents never reached the Host', async () => {
+ const { result } = renderController()
+ await settleMount()
+ const file = await sendAsyncDropped(result, 'task-cancel-doc')
+
+ await act(async () => {
+ await result.current.cancelTask('task-cancel-doc')
+ })
+
+ // Witness: the cancel succeeded and released the task in the store.
+ expect(clerum.rpc.cancelTask).toHaveBeenCalledWith('agent-x', 'task-cancel-doc')
+ expect(captured.releasedTasks).toEqual(['task-cancel-doc'])
+ expect(heldSnapshot()).toMatchObject({ reason: 'host_files_dropped' })
+ await expectFilesRecoverableAndNotResent(result, file.id)
+ })
+
+ it('a cancel of a task already gone keeps the snapshot whose documents never reached the Host', async () => {
+ clerum.rpc.cancelTask.mockRejectedValue(new Error('404 Not Found'))
+ const { result, spies } = renderController()
+ await settleMount()
+ const file = await sendAsyncDropped(result, 'task-gone-doc')
+
+ await act(async () => {
+ await result.current.cancelTask('task-gone-doc')
+ })
+
+ // Witness: the 404 branch ran and released the task in the store.
+ expect(spies.pushToast).toHaveBeenCalledWith('That task is no longer active.', 'info')
+ expect(captured.releasedTasks).toEqual(['task-gone-doc'])
+ expect(heldSnapshot()).toMatchObject({ reason: 'host_files_dropped' })
+ await expectFilesRecoverableAndNotResent(result, file.id)
+ })
+
+ it('a lost stream the turn already covered keeps the snapshot whose documents never reached the Host', async () => {
+ const { result } = renderController()
+ await settleMount()
+ const file = await sendAsyncDropped(result, 'task-noop-doc')
+ // The local transcript already holds the task's answer, so the reconcile
+ // after the stream loss settles as `noop`.
+ const chatId = result.current.activeChatId
+ expect(chatId).toEqual(expect.any(String))
+ await act(async () => {
+ await clerum.chat.appendMessages('agent-x', chatId, [
+ {
+ id: 'covered-answer',
+ role: 'assistant',
+ content: 'already answered',
+ timestamp: Date.now(),
+ task_id: 'task-noop-doc',
+ },
+ ])
+ })
+
+ await act(async () => {
+ clerum.emitTaskProgress('task-noop-doc', { type: 'closed' })
+ })
+
+ // Witness: the noop branch ran and released the task in the store.
+ await waitFor(() => expect(captured.releasedTasks).toEqual(['task-noop-doc']))
+ expect(clerum.rpc.loadSessionMessages).toHaveBeenCalled()
+ expect(heldSnapshot()).toMatchObject({ reason: 'host_files_dropped' })
+ await expectFilesRecoverableAndNotResent(result, file.id)
+ })
+})
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index 40da25d7c..79394edf9 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -2472,7 +2472,8 @@ export function useAgentChatController({
// mid-reconcile — settle without re-rendering (a reopen reconcile
// re-derives if a newer send didn't already take over via R1).
// #654 M6 — either way this task will never produce another terminal,
- // so its retained payload has no reader left.
+ // so its retained payload has no reader left, except documents the
+ // Host never received (kept for Recover files).
releaseRetainedSendsForTask(state.taskId)
dropActivity()
setIdle()
@@ -3622,11 +3623,14 @@ export function useAgentChatController({
visibleFailure.userMessageId
)
// The visible failure is the newest one of this chat; the older failures
- // behind it would otherwise resurface one by one after each discard.
+ // behind it would otherwise resurface one by one after each discard. An
+ // older snapshot holding documents the Host never received stays: those
+ // files exist nowhere else, so it resurfaces with Recover files.
releaseRetainedFailuresForChat(
visibleFailure.agentRef,
visibleFailure.chatId ?? null,
- visibleFailure.timestamp
+ visibleFailure.timestamp,
+ { keepUndeliveredFiles: true }
)
}
setFailedAgentSend(null)
@@ -3737,7 +3741,9 @@ export function useAgentChatController({
await window.clerum.rpc.cancelTask(hostRef, taskId)
markCancelled('Cancelled by user.')
// #654 M6 — a cancel is terminal by intent: the user does not want this
- // payload resent, so nothing will read the retained snapshot again.
+ // payload resent, so nothing will read the retained snapshot again. A
+ // snapshot holding documents the Host never received stays for Recover
+ // files (see releaseRetainedSendsForTask).
releaseRetainedSendsForTask(taskId)
} catch (err) {
const message = err instanceof Error ? err.message : String(err)
diff --git a/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts b/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts
index f7524239d..e32959046 100644
--- a/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts
+++ b/desktop-app/ui/src/lib/__tests__/retainedSendStore.test.ts
@@ -124,12 +124,31 @@ describe('retainedSendStore — documents the Host never received (#678 D13)', (
expect(failed).not.toHaveProperty('undeliveredFileIds')
})
+ // Twin of the test above (A15 Nit B): a lost stream says nothing about the
+ // answer, so the text may have been answered and a retry could send it again.
+ it('keeps a drop as answered without its files when the stream is lost afterwards', () => {
+ 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_dropped', 'Dropped.', 'upstream', ['b'])
+
+ store.markRetainedSendReason('task-files', 'stream_lost', 'The stream was lost.', 'upstream')
+
+ const lost = store.getRetainedSendSnapshot('agent-x', 'chat-1', 'with-files')
+ // Witness: the second mark ran and replaced the failure message.
+ expect(lost?.failure).toEqual({ message: 'The stream was lost.', kind: 'upstream' })
+ expect(lost?.reason).toBe('host_files_dropped')
+ expect(lost?.undeliveredFileIds).toEqual(['b'])
+ })
+
// Both reasons hold documents that exist nowhere else: a rejected send
// (nothing answered) and a message answered without its documents.
describe.each(['host_files_unsupported', 'host_files_dropped'] as const)('%s', reason => {
- // A failure recorded after the drop means the text was not answered
- // either: the snapshot becomes a rejected send, so the files stay held and
- // the text and Retry come back.
+ // A failure other than a lost stream recorded after the drop means the
+ // text was not answered either: the snapshot becomes a rejected send, so
+ // the files stay held and the text and Retry come back.
const reasonAfterFailure = 'host_files_unsupported'
it('keeps the snapshot of a task that succeeded without its files', () => {
@@ -192,16 +211,54 @@ describe('retainedSendStore — documents the Host never received (#678 D13)', (
expect(held(store, SEEDED)).toEqual(['chat-1/undelivered'])
})
- it('a discard still releases an older snapshot whose files never arrived', () => {
+ it('releasing failures without keepUndeliveredFiles also 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('releasing failures with keepUndeliveredFiles keeps an older snapshot whose files never arrived', () => {
+ const store = seedUndeliveredFiles()
+
+ store.releaseRetainedFailuresForChat('agent-x', 'chat-1', 150, { keepUndeliveredFiles: true })
+
+ // Witness: the ordinary failure recorded before the same timestamp is released.
+ expect(held(store, SEEDED)).toEqual(['chat-1/undelivered', 'chat-1/later'])
+ })
+
+ // A15 item 3: a cancel, or a stream loss the turn already covered, ends
+ // the task, but the documents the Host never received exist nowhere else.
+ it('releasing a task keeps its snapshot whose files never arrived', () => {
+ const changed = vi.fn()
+ const store = createRetainedSendStore(changed)
+ store.retainSendSnapshot(
+ snapshot('plain', 100, { reason: 'awaiting_terminal', failure: undefined })
+ )
+ store.retainSendSnapshot(
+ snapshot('with-files', 200, {
+ reason,
+ failure: { message: 'The Host did not receive the files.', kind: 'upstream' },
+ })
+ )
+ store.attachTaskIdToRetainedSend('agent-x', 'chat-1', 'plain', 'task-shared')
+ store.attachTaskIdToRetainedSend('agent-x', 'chat-1', 'with-files', 'task-shared')
+ changed.mockClear()
+
+ store.releaseRetainedSendsForTask('task-shared')
+
+ // Witness: the plain snapshot of the same task is released.
+ expect(changed).toHaveBeenCalledTimes(1)
+ expect(held(store, ['chat-1/plain', 'chat-1/with-files'])).toEqual(['chat-1/with-files'])
+ // The kept snapshot is still the visible failure of its chat.
+ expect(store.getLatestRetainedSendSnapshotForChat('agent-x', 'chat-1')).toMatchObject({
+ userMessageId: 'with-files',
+ reason,
+ })
+ })
+
it('a later task failure updates the message and keeps the files held as a rejected send', () => {
const store = createRetainedSendStore(vi.fn())
store.retainSendSnapshot(
@@ -234,6 +291,25 @@ describe('retainedSendStore — documents the Host never received (#678 D13)', (
it('a later send failure updates the message and keeps the files held as a rejected send', () => {
const store = seedUndeliveredFiles()
+ store.failRetainedSend(
+ 'agent-x',
+ 'chat-1',
+ 'undelivered',
+ 'post_failed',
+ 'The send failed.',
+ 'network'
+ )
+
+ const kept = store.getRetainedSendSnapshot('agent-x', 'chat-1', 'undelivered')
+ expect(kept?.failure).toEqual({ message: 'The send failed.', kind: 'network' })
+ expect(kept?.reason).toBe(reasonAfterFailure)
+ })
+
+ // A15 Nit B: a lost stream does not tell whether the text was answered, so
+ // the snapshot keeps the reason it had.
+ it('a later stream loss updates the message and keeps the reason the snapshot had', () => {
+ const store = seedUndeliveredFiles()
+
store.failRetainedSend(
'agent-x',
'chat-1',
@@ -244,8 +320,9 @@ describe('retainedSendStore — documents the Host never received (#678 D13)', (
)
const kept = store.getRetainedSendSnapshot('agent-x', 'chat-1', 'undelivered')
+ // Witness: the failure was recorded.
expect(kept?.failure).toEqual({ message: 'The stream was lost.', kind: 'network' })
- expect(kept?.reason).toBe(reasonAfterFailure)
+ expect(kept?.reason).toBe(reason)
})
})
})
diff --git a/desktop-app/ui/src/lib/retainedSendStore.ts b/desktop-app/ui/src/lib/retainedSendStore.ts
index a8969495a..33aac2180 100644
--- a/desktop-app/ui/src/lib/retainedSendStore.ts
+++ b/desktop-app/ui/src/lib/retainedSendStore.ts
@@ -11,8 +11,9 @@
* 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 except one whose
- * documents the Host never received (#678 D13). A snapshot still
+ * terminal, which supersedes every failure recorded before it. Documents the
+ * Host never received are the exception (#678 D13): only a discard, recovery or
+ * retry of that snapshot itself, or a store reset, releases them. 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.
@@ -131,10 +132,15 @@ export function createRetainedSendStore(changed: () => void) {
if (snapshots.delete(keyFor(agentRef, chatId, userMessageId))) changed()
}
+ /**
+ * The task ended without a reply to recover (a cancel, or a lost stream the
+ * turn already covered). A snapshot holding documents the Host never received
+ * stays with its failure and reason: those files exist nowhere else.
+ */
function releaseRetainedSendsForTask(taskId: string): void {
let released = false
for (const [key, snapshot] of snapshots) {
- if (snapshot.taskId === taskId) {
+ if (snapshot.taskId === taskId && !holdsUndeliveredFiles(snapshot.reason)) {
snapshots.delete(key)
released = true
}
@@ -146,10 +152,9 @@ 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. 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.
+ * the only copy of the payload. A later success and a user discard pass
+ * `keepUndeliveredFiles`: they supersede an older failure, but not documents
+ * the Host never received, which exist nowhere else.
*/
function releaseRetainedFailuresForChat(
agentRef: string,
@@ -207,15 +212,22 @@ export function createRetainedSendStore(changed: () => void) {
/**
* A snapshot whose documents the Host never received keeps holding them when a
* later failure is recorded: the reason is what stops a later success from
- * releasing files that exist nowhere else. A failure after `host_files_dropped`
- * means the text was not answered either, so the snapshot becomes a rejected
- * send (`host_files_unsupported`) and Retry and text recovery come back.
+ * releasing files that exist nowhere else. Any other failure after
+ * `host_files_dropped` means the text was not answered either, so the snapshot
+ * becomes a rejected send (`host_files_unsupported`) and Retry and text
+ * recovery come back. A lost stream (`stream_lost`) does not say whether the
+ * text was answered, so the snapshot stays `host_files_dropped`: only the
+ * files come back, and the text is never sent again.
*/
function nextReason(
snapshot: RetainedSendSnapshot,
reason: RetainedSendReason
): RetainedSendReason {
- if (snapshot.reason === 'host_files_dropped' && reason !== 'host_files_dropped') {
+ if (
+ snapshot.reason === 'host_files_dropped' &&
+ reason !== 'host_files_dropped' &&
+ reason !== 'stream_lost'
+ ) {
return 'host_files_unsupported'
}
return holdsUndeliveredFiles(snapshot.reason) ? snapshot.reason : reason