Skip to content

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

TypeScript

fix(desktop): preserve same-chat stream recovery

Josuéa2d3715ce93a
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/resendGhostTerminal.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/resendGhostTerminal.test.tsx
index 56af93c3f..f85dfe76f 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/resendGhostTerminal.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/resendGhostTerminal.test.tsx
@@ -32,6 +32,14 @@ async function settleMount() {
await waitFor(() => expect(clerum.chat.getIndex).toHaveBeenCalled())
}
+function deferred<T>() {
+ let resolve!: (value: T) => void
+ const promise = new Promise<T>(resolvePromise => {
+ resolve = resolvePromise
+ })
+ return { promise, resolve }
+}
+
describe('ghost-terminal Resend (D.4 AC11)', () => {
it('exposes failedAgentSend with the original input when the stream closes without a terminal', async () => {
clerum.rpc.invokeHostMessage.mockResolvedValue({ taskId: 'task-ghost' })
@@ -79,4 +87,45 @@ describe('ghost-terminal Resend (D.4 AC11)', () => {
// No assistant reply persisted for the ghost terminal (D.3 behavior preserved).
expect(clerum.chat.appendMessages.mock.calls.length).toBe(appendCallsBefore)
})
+
+ it('keeps loud stream-loss recovery alive when the active chat is reopened', async () => {
+ clerum.rpc.invokeHostMessage.mockResolvedValue({ taskId: 'task-same-chat' })
+ clerum.rpc.getTaskResult.mockRejectedValue(new Error('404 not found'))
+ const recovery = deferred<Awaited<ReturnType<typeof clerum.rpc.loadSessionMessages>>>()
+ clerum.rpc.loadSessionMessages.mockImplementation(() => recovery.promise)
+ const { result } = renderController()
+ await settleMount()
+
+ await act(async () => {
+ await result.current.handleSendAgentMessage('keep the recovery loud')
+ })
+ await waitFor(() => expect(clerum.hasProgressHandler('task-same-chat')).toBe(true))
+ const chatId = result.current.activeChatId
+ expect(chatId).toBeTruthy()
+
+ act(() => {
+ clerum.emitTaskProgress('task-same-chat', { type: 'closed' })
+ })
+ await waitFor(() => expect(clerum.rpc.loadSessionMessages).toHaveBeenCalledTimes(1))
+
+ let reopen!: Promise<void>
+ act(() => {
+ reopen = result.current.switchToChat('agent-x', chatId!)
+ })
+ recovery.resolve({
+ agent: 'agent-x',
+ chatId: chatId!,
+ state: 'idle',
+ turns: [],
+ })
+ await act(async () => {
+ await reopen
+ })
+
+ await waitFor(() =>
+ expect(result.current.failedAgentSend?.content).toBe('keep the recovery loud')
+ )
+ expect(result.current.agentError).toBeTruthy()
+ expect(clerum.rpc.getTaskResult).toHaveBeenCalledWith('agent-x', 'task-same-chat', ['agent-x'])
+ })
})
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index 2e340e192..3199427eb 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -792,7 +792,14 @@ export function useAgentChatController({
const switchRequest = Symbol(`${agentRef}:${chatId}`)
activeChatSwitchRequestRef.current = switchRequest
const key = makeTaskKey(agentRef, chatId)
- reconcileChatRef.current!.supersede(key)
+ const visibleBeforeSwitch = activeChatVisibilityRef.current
+ const reopeningActiveChat =
+ visibleBeforeSwitch.selectedAgent === agentRef &&
+ visibleBeforeSwitch.activeChatId === chatId
+ // A same-chat click must coalesce onto an already-running loud recovery.
+ // Superseding it would discard the stream-loss caller that owns the Resend
+ // or durable-result UX. Genuine chat changes still cancel stale work.
+ if (!reopeningActiveChat) reconcileChatRef.current!.supersede(key)
activeChatVisibilityRef.current = {
...activeChatVisibilityRef.current,
activeChatId: chatId,