Skip to content

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

TypeScript

fix(desktop): preserve legacy turnless caches during reconcile

Josué26e244f26b7d
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsx
index 6a622f4cd..707f3ffbf 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.unified.test.tsx
@@ -191,6 +191,43 @@ describe('switchToChat (unified, D.4)', () => {
expect(result.current.chatMessages).toHaveLength(60)
})
+ it('replaces a settled legacy turnless cache instead of persisting duplicate server history', async () => {
+ clerum.chat.loadMessages.mockResolvedValue([
+ { id: 'legacy-q1', role: 'user', content: 'q1', timestamp: 1 },
+ { id: 'legacy-a1', role: 'assistant', content: 'a1', timestamp: 2 },
+ { id: 'legacy-q2', role: 'user', content: 'q2', timestamp: 3 },
+ { id: 'legacy-a2', role: 'assistant', content: 'a2', timestamp: 4 },
+ ])
+ clerum.rpc.loadSessionMessages.mockResolvedValue({
+ agent: 'agent-x',
+ chatId: 'legacy-cache',
+ state: 'idle',
+ totalTurns: 3,
+ turns: [turn(1, 'q1', 'a1'), turn(2, 'q2', 'a2'), turn(3, 'q3', 'a3')],
+ })
+ const { result } = renderController()
+ await settleMount()
+
+ await act(async () => {
+ await result.current.switchToChat('agent-x', 'legacy-cache')
+ })
+
+ expect(result.current.chatMessages.map(message => message.id)).toEqual([
+ 'turn-1-user',
+ 'turn-1-assistant',
+ 'turn-2-user',
+ 'turn-2-assistant',
+ 'turn-3-user',
+ 'turn-3-assistant',
+ ])
+ expect(clerum.chat.replaceMessages).toHaveBeenCalledWith(
+ 'agent-x',
+ 'legacy-cache',
+ expect.arrayContaining([expect.objectContaining({ id: 'turn-3-assistant' })])
+ )
+ expect(clerum.chat.replaceMessages.mock.calls.at(-1)?.[2]).toHaveLength(6)
+ })
+
it('Phase 2 persists the merged server delta to a durable-ID cache', async () => {
clerum.chat.loadMessages.mockResolvedValue([
{ id: 'turn-1-user', role: 'user' as const, content: 'q1', timestamp: 1 },
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index b7bcdf3a6..ec52dc164 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -1462,7 +1462,14 @@ export function useAgentChatController({
if (!isActive() || tracker.get(chatKey)) {
return { rendered: localMessages, replaced: false, cached: localMessages }
}
- const rendered = localMessages.length
+ // Pre-paging caches have no server-turn identity, so ordinary local
+ // user/assistant rows cannot be compared safely with an authoritative
+ // window. Preserve the old replace behavior for settled sessions instead
+ // of persisting both copies. A reported live task keeps the merge path so
+ // its optimistic bubbles remain visible until task-scoped reconciliation.
+ const replaceSettledLegacyCache =
+ localMessages.length > 0 && !localHasServerTurns && !resp.activeTaskId
+ const rendered = localMessages.length && !replaceSettledLegacyCache
? mergeServerMessages(
localMessages,
hydratedWithAttachments,