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 cache replacement

Josuée638083b39d0
diff --git a/desktop-app/src/__tests__/chatStore.test.ts b/desktop-app/src/__tests__/chatStore.test.ts
index 369ff1c53..f8671d8ae 100644
--- a/desktop-app/src/__tests__/chatStore.test.ts
+++ b/desktop-app/src/__tests__/chatStore.test.ts
@@ -3,6 +3,7 @@ import { promises as fs } from 'node:fs'
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'
import { ChatStore } from '../chatStore.js'
+import { turnsToChatMessages } from '../serverTurnAdapter.js'
import type { ChatMessage } from '../types.js'
let tempDir: string
@@ -934,6 +935,53 @@ describe('messages', () => {
).toEqual(['turn-1-user', 'optimistic-user'])
})
+ it('replaces a settled legacy turnless cache without persisting duplicate history', async () => {
+ await store.createChat('agent-1', 'legacy-turnless')
+ await store.saveMessages('agent-1', 'legacy-turnless', [
+ { 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 },
+ ])
+
+ const authoritative = turnsToChatMessages([
+ {
+ number: 1,
+ user_input: 'q1',
+ response: 'a1',
+ started_at: '2026-01-01T00:00:01.000Z',
+ completed_at: '2026-01-01T00:00:02.000Z',
+ },
+ {
+ number: 2,
+ user_input: 'q2',
+ response: 'a2',
+ started_at: '2026-01-01T00:00:03.000Z',
+ completed_at: '2026-01-01T00:00:04.000Z',
+ },
+ {
+ number: 3,
+ user_input: 'q3',
+ response: 'a3',
+ started_at: '2026-01-01T00:00:05.000Z',
+ completed_at: '2026-01-01T00:00:06.000Z',
+ },
+ ])
+
+ await store.replaceMessages('agent-1', 'legacy-turnless', authoritative)
+
+ expect(
+ (await store.loadMessages('agent-1', 'legacy-turnless')).map(message => message.id)
+ ).toEqual([
+ 'turn-1-user',
+ 'turn-1-assistant',
+ 'turn-2-user',
+ 'turn-2-assistant',
+ 'turn-3-user',
+ 'turn-3-assistant',
+ ])
+ })
+
it('keeps unseen numbered history while replacing completed local-only echoes', async () => {
await store.createChat('agent-1', 'reconcile-upsert')
await store.saveMessages('agent-1', 'reconcile-upsert', [
diff --git a/desktop-app/src/chatMessageMerge.ts b/desktop-app/src/chatMessageMerge.ts
index f0373d219..fb71d0502 100644
--- a/desktop-app/src/chatMessageMerge.ts
+++ b/desktop-app/src/chatMessageMerge.ts
@@ -89,8 +89,20 @@ function insertUnmatchedTurnlessIncoming(
export function mergeAuthoritativeServerMessages(
existing: ChatMessage[],
incoming: ChatMessage[],
- options: { activeTaskIds?: ReadonlySet<string> } = {}
+ options: {
+ activeTaskIds?: ReadonlySet<string>
+ replaceLegacyTurnlessWindow?: boolean
+ } = {}
): ChatMessage[] {
+ if (options.replaceLegacyTurnlessWindow) {
+ const durableLocalMessages = existing.filter(
+ message => message.role === 'system' || message.isError || message.preserveLocal
+ )
+ return mergeAuthoritativeServerMessages(durableLocalMessages, incoming, {
+ activeTaskIds: options.activeTaskIds,
+ })
+ }
+
const authoritative = incoming
.filter(message => messageServerTurnNumber(message) !== undefined)
.sort((left, right) => {
diff --git a/desktop-app/src/chatStore.ts b/desktop-app/src/chatStore.ts
index 7d2520f10..eb32e35ee 100644
--- a/desktop-app/src/chatStore.ts
+++ b/desktop-app/src/chatStore.ts
@@ -1659,8 +1659,14 @@ export class ChatStore {
const existingMessages = existingMeta
? await this.readMessagesFromPagedMeta(agentRef, chatId, existingMeta)
: []
+ const replaceLegacyTurnlessWindow =
+ existingMessages.length > 0 &&
+ !options.activeTaskIds?.length &&
+ existingMessages.every(message => messageServerTurnNumber(message) === undefined) &&
+ messages.some(message => messageServerTurnNumber(message) !== undefined)
const merged = mergeAuthoritativeServerMessages(existingMessages, messages, {
activeTaskIds: options.activeTaskIds ? new Set(options.activeTaskIds) : undefined,
+ replaceLegacyTurnlessWindow,
})
const indexedCount = await this.indexedMessageCount(agentRef, chatId)
await this.writePagedChatUnlocked(agentRef, chatId, merged, {
diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
index 3199427eb..2fe14f469 100644
--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts
@@ -203,9 +203,13 @@ function mergeUniqueMessages(
function mergeServerMessages(
existing: AgentChatMessage[],
incoming: AgentChatMessage[],
- activeTaskIds?: ReadonlySet<string>
+ activeTaskIds?: ReadonlySet<string>,
+ replaceLegacyTurnlessWindow = false
): AgentChatMessage[] {
- return mergeAuthoritativeServerMessages(existing, incoming, { activeTaskIds })
+ return mergeAuthoritativeServerMessages(existing, incoming, {
+ activeTaskIds,
+ replaceLegacyTurnlessWindow,
+ })
}
function sameMessageSequence(a: AgentChatMessage[], b: AgentChatMessage[]): boolean {
@@ -1491,11 +1495,12 @@ export function useAgentChatController({
const replaceSettledLegacyCache =
localMessages.length > 0 && !localHasServerTurns && !resp.activeTaskId
const rendered =
- localMessages.length && !replaceSettledLegacyCache
+ localMessages.length > 0
? mergeServerMessages(
localMessages,
hydratedWithAttachments,
- resp.activeTaskId ? new Set([resp.activeTaskId]) : undefined
+ resp.activeTaskId ? new Set([resp.activeTaskId]) : undefined,
+ replaceSettledLegacyCache
)
: hydratedWithAttachments
const hasOlderAfterMerge =