Skip to content

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

TypeScript

fix(desktop): keep the last server title in the local chat index

Alfredo Lopezc010b9002ae5
diff --git a/desktop-app/src/__tests__/chatStore.test.ts b/desktop-app/src/__tests__/chatStore.test.ts
index 81027077f..5e45588e8 100644
--- a/desktop-app/src/__tests__/chatStore.test.ts
+++ b/desktop-app/src/__tests__/chatStore.test.ts
@@ -2973,4 +2973,64 @@ describe('upsertServerSessions', () => {
await store.createChat('agent-1', 'c1')
expect(await store.upsertServerSessions('agent-1', [])).toBe(0)
})
+
+ it('stores the server title of a cached chat on disk without moving updatedAt', async () => {
+ await store.createChat('agent-1', 'c1')
+ const before = (await store.listChats('agent-1')).find(c => c.id === 'c1')!
+ expect(before.title).toBe('New Chat')
+
+ const changed = await store.upsertServerSessions('agent-1', [
+ { chatId: 'c1', lastActivityAt: before.updatedAt, title: 'Quarterly plan' },
+ ])
+
+ expect(changed).toBe(1)
+ const restarted = new ChatStore(tempDir)
+ const after = (await restarted.listChats('agent-1')).find(c => c.id === 'c1')!
+ expect(after.title).toBe('Quarterly plan')
+ expect(after.updatedAt).toBe(before.updatedAt)
+ })
+
+ it('creates no entry for a titled server-only session and does not resurrect a deleted chat', async () => {
+ await store.createChat('agent-1', 'c1')
+ await store.createChat('agent-1', 'gone')
+ await store.deleteChat('agent-1', 'gone', TEAM_A_SCOPE)
+
+ const changed = await store.upsertServerSessions('agent-1', [
+ { chatId: 'c1', title: 'Kept' },
+ { chatId: 'server-only', title: 'Remote' },
+ { chatId: 'gone', title: 'Deleted' },
+ ])
+
+ // Witness: the cached chat in the same call was updated.
+ expect(changed).toBe(1)
+ const chats = await new ChatStore(tempDir).listChats('agent-1')
+ expect(chats.map(c => [c.id, c.title])).toEqual([['c1', 'Kept']])
+ })
+
+ it('keeps the local title when the server reports none', async () => {
+ await store.createChat('agent-1', 'c1')
+ await store.upsertServerSessions('agent-1', [{ chatId: 'c1', title: 'First' }])
+
+ const changed = await store.upsertServerSessions('agent-1', [
+ { chatId: 'c1' },
+ { chatId: 'c1', title: '' },
+ ])
+
+ expect(changed).toBe(0)
+ expect((await store.listChats('agent-1')).find(c => c.id === 'c1')!.title).toBe('First')
+ })
+
+ it('saves the index once for a new title and not again for the same title', async () => {
+ await store.createChat('agent-1', 'c1')
+ const saveIndex = vi.spyOn(
+ store as unknown as { saveIndex: (agentRef: string, index: unknown) => Promise<void> },
+ 'saveIndex'
+ )
+
+ expect(await store.upsertServerSessions('agent-1', [{ chatId: 'c1', title: 'Plan' }])).toBe(1)
+ expect(saveIndex).toHaveBeenCalledTimes(1)
+
+ expect(await store.upsertServerSessions('agent-1', [{ chatId: 'c1', title: 'Plan' }])).toBe(0)
+ expect(saveIndex).toHaveBeenCalledTimes(1)
+ })
})
diff --git a/desktop-app/src/chatStore.ts b/desktop-app/src/chatStore.ts
index 4b15d370d..7fbe0810b 100644
--- a/desktop-app/src/chatStore.ts
+++ b/desktop-app/src/chatStore.ts
@@ -2250,14 +2250,19 @@ export class ChatStore {
* Deliberately does NOT create entries for server-only sessions: the sidebar's
* in-memory merge (`useChatListController`) already surfaces those, and a local
* `deleteChat` doesn't remove the server session — eagerly re-persisting it
- * would resurrect a chat the user just deleted. Titles are never touched here:
- * the server title is applied by the in-memory §2.2 catalog merge in
- * `useChatListController`, not persisted into this local index. Returns the
- * count of entries reconciled.
+ * would resurrect a chat the user just deleted.
+ *
+ * `title`, when present, is the server's own title for the session. It is
+ * stored so a cold start paints the last title the server reported instead of
+ * the hydration placeholder; the next catalog page overwrites it, so it never
+ * diverges from the server for longer than one load. The caller omits it while
+ * a local rename is pending. A title change does not touch `updatedAt`: the
+ * sidebar order stays driven by server activity. Returns the count of entries
+ * changed.
*/
async upsertServerSessions(
agentRef: string,
- sessions: Array<{ chatId: string; lastActivityAt?: string }>
+ sessions: Array<{ chatId: string; lastActivityAt?: string; title?: string }>
): Promise<number> {
if (!sessions.length) return 0
return this.serializeIndex(agentRef, async () => {
@@ -2267,12 +2272,20 @@ export class ChatStore {
for (const session of sessions) {
const chat = byId.get(session.chatId)
if (!chat) continue // server-only session — do not resurrect a local delete
+ let chatChanged = false
const lastActivityAt = session.lastActivityAt
- if (!lastActivityAt) continue
- if (new Date(lastActivityAt).getTime() > new Date(chat.updatedAt).getTime()) {
+ if (
+ lastActivityAt &&
+ new Date(lastActivityAt).getTime() > new Date(chat.updatedAt).getTime()
+ ) {
chat.updatedAt = lastActivityAt
- changed += 1
+ chatChanged = true
+ }
+ if (session.title && session.title !== chat.title) {
+ chat.title = session.title
+ chatChanged = true
}
+ if (chatChanged) changed += 1
}
if (changed > 0) await this.saveIndex(agentRef, index)
return changed
diff --git a/desktop-app/src/ipc.ts b/desktop-app/src/ipc.ts
index 4c77b8899..c7ffec005 100644
--- a/desktop-app/src/ipc.ts
+++ b/desktop-app/src/ipc.ts
@@ -1856,7 +1856,7 @@ export function registerIpcHandlers(service: AppService): void {
event,
payload: {
agentRef: string
- sessions?: Array<{ chatId?: unknown; lastActivityAt?: unknown }>
+ sessions?: Array<{ chatId?: unknown; lastActivityAt?: unknown; title?: unknown }>
}
) => {
assertTrustedSender(event)
@@ -1868,6 +1868,8 @@ export function registerIpcHandlers(service: AppService): void {
chatId: sanitizeString(session?.chatId),
lastActivityAt:
typeof session?.lastActivityAt === 'string' ? session.lastActivityAt : undefined,
+ title:
+ typeof session?.title === 'string' ? session.title.trim() || undefined : undefined,
}))
.filter(session => Boolean(session.chatId))
: []
diff --git a/desktop-app/src/preload.ts b/desktop-app/src/preload.ts
index e877c72f5..4a9e10952 100644
--- a/desktop-app/src/preload.ts
+++ b/desktop-app/src/preload.ts
@@ -662,7 +662,7 @@ const clerum = Object.freeze({
getIndex: (agentRef: string) => ipcRenderer.invoke('chat:getIndex', { agentRef }),
reconcileServerSessions: (
agentRef: string,
- sessions: Array<{ chatId: string; lastActivityAt?: string }>
+ sessions: Array<{ chatId: string; lastActivityAt?: string; title?: string }>
) => ipcRenderer.invoke('chat:reconcileSessions', { agentRef, sessions }),
dismissOnboarding: (agentRef: string) =>
ipcRenderer.invoke('chat:dismissOnboarding', { agentRef }),
diff --git a/desktop-app/src/renderer.d.ts b/desktop-app/src/renderer.d.ts
index d7a00fa11..b2dd6c84d 100644
--- a/desktop-app/src/renderer.d.ts
+++ b/desktop-app/src/renderer.d.ts
@@ -750,7 +750,7 @@ declare global {
getIndex: (agentRef: string) => Promise<ChatIndex>
reconcileServerSessions: (
agentRef: string,
- sessions: Array<{ chatId: string; lastActivityAt?: string }>
+ sessions: Array<{ chatId: string; lastActivityAt?: string; title?: string }>
) => Promise<number>
dismissOnboarding: (agentRef: string) => Promise<void>
markUnreadTerminal: (agentRef: string, chatId: string) => Promise<void>
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/chatListTitleMerge.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/chatListTitleMerge.test.tsx
index e3b079bbb..4a7ab3396 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/chatListTitleMerge.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/chatListTitleMerge.test.tsx
@@ -673,3 +673,104 @@ describe('late global catalog response', () => {
}
)
})
+
+type ReconcileCall = [string, Array<{ chatId: string; lastActivityAt?: string; title?: string }>]
+
+describe('server titles persisted into the local index (spec 15 §5.3)', () => {
+ it('sends the server title of each session with the freshness write', async () => {
+ clerum.chat.getIndex.mockResolvedValue(localIndex([{ id: 'chat-a', title: 'New Chat' }]))
+ clerum.rpc.listSessions.mockResolvedValue(
+ serverSessions([
+ { agent: 'agent-x', chatId: 'chat-a', title: 'Deploy staging' },
+ { agent: 'agent-x', chatId: 'chat-b' },
+ ])
+ )
+
+ renderController({ selectedAgent: 'agent-x', agentNames: ['agent-x'] })
+
+ await waitFor(() =>
+ expect(clerum.chat.reconcileServerSessions).toHaveBeenCalledWith('agent-x', [
+ { chatId: 'chat-a', lastActivityAt: expect.any(String), title: 'Deploy staging' },
+ { chatId: 'chat-b', lastActivityAt: expect.any(String) },
+ ])
+ )
+ })
+
+ it('persists the titles of a Host the user never selects', async () => {
+ clerum.chat.getIndex.mockResolvedValue(localIndex([]))
+ clerum.rpc.listSessions.mockImplementation(async (agentRef: string) =>
+ agentRef === 'agent-y'
+ ? serverSessions([{ agent: 'agent-y', chatId: 'chat-y', title: 'Cross-device title' }])
+ : serverSessions([{ agent: 'agent-x', chatId: 'chat-x', title: 'Selected title' }])
+ )
+
+ renderController({ selectedAgent: 'agent-x', agentNames: ['agent-x', 'agent-y'] })
+
+ // Witness: the selected Host's write went out too.
+ await waitFor(() =>
+ expect(
+ (clerum.chat.reconcileServerSessions.mock.calls as ReconcileCall[]).some(
+ ([agentRef]) => agentRef === 'agent-x'
+ )
+ ).toBe(true)
+ )
+ await waitFor(() =>
+ expect(clerum.chat.reconcileServerSessions).toHaveBeenCalledWith('agent-y', [
+ { chatId: 'chat-y', lastActivityAt: expect.any(String), title: 'Cross-device title' },
+ ])
+ )
+ })
+
+ it('paints the stored server title on the next start before any catalog arrives', async () => {
+ // A stateful index: the freshness write applies titles the way ChatStore does.
+ const index = localIndex([{ id: 'chat-a', title: 'New Chat' }])
+ clerum.chat.getIndex.mockImplementation(async () => ({
+ ...index,
+ chats: index.chats.map(chat => ({ ...chat })),
+ }))
+ clerum.chat.reconcileServerSessions.mockImplementation(
+ async (_agentRef: string, sessions: ReconcileCall[1]) => {
+ let changed = 0
+ for (const session of sessions) {
+ const chat = index.chats.find(c => c.id === session.chatId)
+ if (chat && session.title && chat.title !== session.title) {
+ chat.title = session.title
+ changed += 1
+ }
+ }
+ return changed
+ }
+ )
+ clerum.rpc.listSessions.mockResolvedValue(
+ serverSessions([{ agent: 'agent-x', chatId: 'chat-a', title: 'Deploy staging' }])
+ )
+
+ const first = renderController({ selectedAgent: 'agent-x', agentNames: ['agent-x'] })
+ await waitFor(() =>
+ expect(index.chats.find(c => c.id === 'chat-a')?.title).toBe('Deploy staging')
+ )
+ first.unmount()
+
+ // Second start: the catalog never answers while the cache paints. A new
+ // source function also drops the module-level catalog cache, as a restart does.
+ const catalog = deferred<SessionsListResult>()
+ clerum.rpc.listSessions = vi.fn(() => catalog.promise)
+ const titles: Array<string | undefined> = []
+ const second = renderController({ selectedAgent: 'agent-x', agentNames: ['agent-x'] })
+ titles.push(second.result.current.chatList.find(c => c.id === 'chat-a')?.title)
+
+ await waitFor(() =>
+ expect(second.result.current.chatList.find(c => c.id === 'chat-a')?.title).toBe(
+ 'Deploy staging'
+ )
+ )
+ // Witness: the second start did ask for the catalog, which is still pending.
+ expect(clerum.rpc.listSessions).toHaveBeenCalled()
+ titles.push(second.result.current.chatList.find(c => c.id === 'chat-a')?.title)
+ expect(titles).not.toContain('New Chat')
+
+ await act(async () => {
+ catalog.resolve(serverSessions([]))
+ })
+ })
+})
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/renameSessionQueue.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/renameSessionQueue.test.tsx
index 3d2908e06..e2a096cba 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/renameSessionQueue.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/renameSessionQueue.test.tsx
@@ -425,6 +425,61 @@ describe('rename pending queue — concurrency (spec 15 §2.5)', () => {
})
})
+ // A poll during an in-flight rename must not write the server's (older) title
+ // into the local index over the optimistic one (§2.2 case E).
+ it('omits the server title of a chat with an in-flight rename from the index write', async () => {
+ const inFlight = deferred<{ title: string }>()
+ clerum.chat.getIndex.mockResolvedValue(
+ localIndex([
+ { id: 'c1', title: 'old' },
+ { id: 'c2', title: 'other' },
+ ])
+ )
+ clerum.rpc.listSessions.mockResolvedValue(
+ reportedSessions([
+ { chatId: 'c1', title: 'server c1' },
+ { chatId: 'c2', title: 'server c2' },
+ ])
+ )
+ clerum.rpc.renameSession.mockReturnValue(inFlight.promise)
+
+ const { result, rerender } = renderController({
+ selectedAgent: 'agent-x',
+ agentNames: ['agent-x'],
+ })
+ await waitFor(() => expect(titleInList(result.current, 'c1')).toBe('server c1'))
+
+ let pending: Promise<void> | undefined
+ await act(async () => {
+ pending = result.current.handleRenameChatForAgent('agent-x', 'c1', 'renamed')
+ await flushMicrotasks()
+ })
+ await waitFor(() => expect(clerum.rpc.renameSession).toHaveBeenCalledTimes(1))
+ const callsBeforePoll = clerum.chat.reconcileServerSessions.mock.calls.length
+
+ await act(async () => {
+ rerender({ agentNames: ['agent-x', 'agent-y'] })
+ await flushMicrotasks()
+ })
+
+ type Payload = Array<{ chatId: string; title?: string }>
+ const pollWrites = (clerum.chat.reconcileServerSessions.mock.calls as Array<[string, Payload]>)
+ .slice(callsBeforePoll)
+ .filter(([agentRef]) => agentRef === 'agent-x')
+ .map(([, sessions]) => sessions)
+ // Witness: the poll wrote this Host's index, with c2's server title.
+ expect(pollWrites.length).toBeGreaterThan(0)
+ for (const sessions of pollWrites) {
+ expect(sessions.find(s => s.chatId === 'c2')?.title).toBe('server c2')
+ expect(sessions.find(s => s.chatId === 'c1')).not.toHaveProperty('title')
+ }
+
+ await act(async () => {
+ inFlight.resolve({ title: 'renamed' })
+ await pending
+ })
+ })
+
// FIX 2: renaming the same chat twice before the first PATCH resolves. The
// second rename's pending marker must survive the first (stale) 200, so the
// newer optimistic title keeps its case-E protection and is not clobbered.
diff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.ts
index 71088566b..0331beb44 100644
--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts
+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts
@@ -378,6 +378,32 @@ export function useChatListController({
return pendingRenamesRef.current.get(pendingRenameKey(agentRef, chatId))?.state ?? 'none'
}, [])
+ // Persist server freshness and titles into the local index (spec §5.3), so a
+ // cold start paints the last server title instead of the hydration
+ // placeholder. A chat with a pending local rename keeps its local title (§2.2
+ // cases E/F). Best-effort: must never block or fail the visible render.
+ const persistServerSessions = useCallback(
+ (agentRef: string, sessions: SessionsListResult['items']) => {
+ try {
+ void chatStore
+ .reconcileServerSessions(
+ agentRef,
+ sessions.map(session => ({
+ chatId: session.chatId,
+ lastActivityAt: session.lastActivityAt,
+ ...(session.title && pendingRenameStateFor(agentRef, session.chatId) === 'none'
+ ? { title: session.title }
+ : {}),
+ }))
+ )
+ .catch(() => undefined)
+ } catch {
+ // ignore — reconciliation is best-effort freshness only
+ }
+ },
+ [chatStore.reconcileServerSessions, pendingRenameStateFor]
+ )
+
// Indirection so the catalog loaders (defined above the queue) can trigger a
// retry of pending renames after a listSessions poll without depending on the
// queue's identity. Wired in an effect once the queue is defined.
@@ -670,19 +696,7 @@ export function useChatListController({
.catch(() => undefined)
}
- // Persist server freshness into the local index (spec §5.3): keeps the
- // durable sidebar order aligned with the source of truth. Best-effort:
- // must never block or fail the visible local-cache render.
- try {
- void chatStore
- .reconcileServerSessions(
- agentRef,
- serverSessions.map(s => ({ chatId: s.chatId, lastActivityAt: s.lastActivityAt }))
- )
- .catch(() => undefined)
- } catch {
- // ignore — reconciliation is best-effort freshness only
- }
+ persistServerSessions(agentRef, serverSessions)
})
return { index, merged }
@@ -691,12 +705,12 @@ export function useChatListController({
authorityScope,
chatStore.getIndex,
chatStore.listSessions,
- chatStore.reconcileServerSessions,
fsm,
getHostAuthorityEpoch,
isHostAccessBlocked,
onHostAccessRevoked,
host,
+ persistServerSessions,
]
)
@@ -812,16 +826,7 @@ export function useChatListController({
)
})
- try {
- void chatStore
- .reconcileServerSessions(
- agentRef,
- serverSessions.map(s => ({ chatId: s.chatId, lastActivityAt: s.lastActivityAt }))
- )
- .catch(() => undefined)
- } catch {
- // ignore — reconciliation is best-effort freshness only
- }
+ persistServerSessions(agentRef, serverSessions)
} finally {
chatListLoadingMoreByAgentRef.current.delete(agentRef)
if (selectedAgentRef.current === agentRef) {
@@ -830,11 +835,11 @@ export function useChatListController({
}
}, [
chatStore.listSessions,
- chatStore.reconcileServerSessions,
fsm,
getHostAuthorityEpoch,
isHostAccessBlocked,
onHostAccessRevoked,
+ persistServerSessions,
])
const loadChatList = useCallback(
@@ -1737,6 +1742,9 @@ export function useChatListController({
)
})
+ // A Host the user never selects keeps its titles across restarts too.
+ if (requestStillCurrent()) persistServerSessions(agentRef, sessions)
+
// §2.5: retry only this Host's pending renames as its poll settles.
if (requestStillCurrent()) {
flushPendingRenamesRef.current(
@@ -1775,6 +1783,7 @@ export function useChatListController({
isHostAccessBlocked,
onHostAccessRevoked,
latestReloadRevision,
+ persistServerSessions,
])
// Stable across renders whose inputs did not change, so consumers keyed on the
diff --git a/desktop-app/ui/src/hooks/useChatStore.ts b/desktop-app/ui/src/hooks/useChatStore.ts
index f8e7c82b0..c1facd01e 100644
--- a/desktop-app/ui/src/hooks/useChatStore.ts
+++ b/desktop-app/ui/src/hooks/useChatStore.ts
@@ -137,8 +137,10 @@ export function useChatStore() {
)
const getIndex = useCallback((agentRef: string) => window.clerum.chat.getIndex(agentRef), [])
const reconcileServerSessions = useCallback(
- (agentRef: string, sessions: Array<{ chatId: string; lastActivityAt?: string }>) =>
- window.clerum.chat.reconcileServerSessions(agentRef, sessions),
+ (
+ agentRef: string,
+ sessions: Array<{ chatId: string; lastActivityAt?: string; title?: string }>
+ ) => window.clerum.chat.reconcileServerSessions(agentRef, sessions),
[]
)
const dismissOnboarding = useCallback(