Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): keep the remembered transcript when the disk read fails
Alfredo Lopez73517f7ee0b4
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.transcriptMemo.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.transcriptMemo.test.tsxindex 546d9d465..f04f34118 100644--- a/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.transcriptMemo.test.tsx+++ b/desktop-app/ui/src/hooks/domain/__tests__/switchToChat.transcriptMemo.test.tsx@@ -275,6 +275,52 @@ describe('switchToChat transcript memo (#1091 A9)', () => {})+ it.each(HOSTS)(+ 'b4. %s: a failed disk read keeps the remembered transcript instead of treating it as a tombstone',+ async host => {+ const disk = { 'chat-a': transcript('chat-a'), 'chat-b': transcript('chat-b') }+ serveDisk(disk)+ const controller = mount(host)+ await settleMount()+ await open(controller, host, 'chat-a')+ await open(controller, host, 'chat-b')+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)+ const newestPageReads = () =>+ clerum.chat.loadMessages.mock.calls.filter(+ ([, chatId, limit, offset]) => chatId === 'chat-a' && limit === 80 && offset === undefined+ ).length+ const readsBefore = newestPageReads()+ clerum.chat.loadMessages.mockImplementationOnce(() =>+ Promise.reject(new Error('EIO: newest chat page unreadable'))+ )++ const reopenStart = frames.length+ await open(controller, host, 'chat-a')+ // Witnesses: the reopen read the newest disk page and that read failed.+ expect(newestPageReads()).toBeGreaterThan(readsBefore)+ expect(warn).toHaveBeenCalledWith(+ '[chat-history] failed to load local messages',+ expect.objectContaining({ agentRef: host, chatId: 'chat-a' })+ )+ expectOnlyTranscript(frames.slice(reopenStart), 'chat-a', disk['chat-a'])++ // The entry survived the failed read: the next reopen is still remembered.+ await open(controller, host, 'chat-b')+ const chatARead = deferred<AgentChatMessage[]>()+ clerum.chat.loadMessages.mockImplementationOnce(() => chatARead.promise)+ const againStart = frames.length+ let again!: Promise<void>+ await act(async () => {+ again = controller.result.current.switchToChat(host, 'chat-a')+ })+ expectOnlyTranscript(frames.slice(againStart), 'chat-a', disk['chat-a'])+ await act(async () => {+ chatARead.resolve(disk['chat-a'])+ await again+ })+ }+ )+it.each(HOSTS)('c. %s: when the disk page matches, the remembered list and paging offset are kept',async host => {diff --git a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts b/desktop-app/ui/src/hooks/domain/useAgentChatController.tsindex c6bbc3934..81019914c 100644--- a/desktop-app/ui/src/hooks/domain/useAgentChatController.ts+++ b/desktop-app/ui/src/hooks/domain/useAgentChatController.ts@@ -1122,6 +1122,7 @@ export function useAgentChatController({// PHASE 1 — render only the newest local page. The authoritative reconcile// below requests a delta after the newest cached server turn.let cached: Awaited<ReturnType<typeof chatStore.loadMessages>> = []+ let localReadFailed = falsetry {cached = canReuseActiveSwitch? ([...chatMessagesRef.current] as AgentChatMessage[])@@ -1131,6 +1132,7 @@ export function useAgentChatController({LOCAL_MESSAGE_PAGE_SIZE)) as AgentChatMessage[])} catch (error) {+ localReadFailed = trueconsole.warn('[chat-history] failed to load local messages', { agentRef, chatId, error })}// #1091 A9: the remembered transcript is already on screen. Keep it only@@ -1138,15 +1140,17 @@ export function useAgentChatController({// e.g. a reply persisted while the chat was hidden. An empty page also// wins: `loadMessages` returns [] for a durably tombstoned chat, which// this session may not have loaded into `deletedChatIdsByAgentRef` yet,- // and recording [] drops the memo entry. The Host reconcile below runs- // in both cases.+ // and recording [] drops the memo entry. A rejected read is not an empty+ // page: it says nothing about the chat, so the remembered transcript+ // stays. The Host reconcile below runs in every case.const keepRemembered =remembered !== undefined &&- cached.length > 0 &&- sameMessageSequence(- remembered.messages.slice(-cached.length),- cached as AgentChatMessage[]- )+ (localReadFailed ||+ (cached.length > 0 &&+ sameMessageSequence(+ remembered.messages.slice(-cached.length),+ cached as AgentChatMessage[]+ )))if (keepRemembered) {cached = remembered.messages as typeof cachedif (!isStillActive()) return