Skip to content

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

TypeScript

fix(desktop): sort session catalogs before auto-select

Josué963b31668460
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
index 3f6212248..476705d59 100644
--- a/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
+++ b/desktop-app/ui/src/hooks/domain/__tests__/useAgentChatController.test.tsx
@@ -1365,6 +1365,36 @@ describe('useAgentChatController — characterization (D.0)', () => {
expect(clerum.chat.setLastActive).not.toHaveBeenCalledWith('agent-x', 'older-server')
})
+ it('auto-selects the newest server session from an unsorted catalog response', async () => {
+ clerum.chat.getIndex.mockResolvedValue({
+ version: 3,
+ lastActiveChatId: null,
+ onboardingDismissed: false,
+ chats: [],
+ })
+ clerum.rpc.listSessions.mockResolvedValue({
+ items: [
+ {
+ agent: 'agent-x',
+ chatId: 'older-server',
+ turnCount: 1,
+ lastActivityAt: '2026-05-01T00:00:00Z',
+ },
+ {
+ agent: 'agent-x',
+ chatId: 'newest-server',
+ turnCount: 1,
+ lastActivityAt: '2026-05-03T00:00:00Z',
+ },
+ ],
+ })
+
+ const { result } = renderController({ navItem: 'chat' })
+
+ await waitFor(() => expect(result.current.activeChatId).toBe('newest-server'))
+ expect(clerum.chat.setLastActive).not.toHaveBeenCalledWith('agent-x', 'older-server')
+ })
+
it('loads the next remote session page only when requested', async () => {
clerum.rpc.listSessions.mockImplementation(
async (_hostRef: string, _teamId: string | undefined, query?: { cursor?: string }) => {
diff --git a/desktop-app/ui/src/hooks/domain/useChatListController.ts b/desktop-app/ui/src/hooks/domain/useChatListController.ts
index e0a0fc0a6..1794cd44b 100644
--- a/desktop-app/ui/src/hooks/domain/useChatListController.ts
+++ b/desktop-app/ui/src/hooks/domain/useChatListController.ts
@@ -73,8 +73,16 @@ interface UseChatListControllerParams {
host: MutableRefObject<ChatListControllerHost | null>
}
+function sortableTimestamp(value: string): number {
+ const timestamp = Date.parse(value)
+ return Number.isFinite(timestamp) ? timestamp : 0
+}
+
const byUpdatedDesc = (a: { updatedAt: string }, b: { updatedAt: string }) =>
- new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()
+ sortableTimestamp(b.updatedAt) - sortableTimestamp(a.updatedAt)
+
+const byLastActivityDesc = (a: { lastActivityAt: string }, b: { lastActivityAt: string }) =>
+ sortableTimestamp(b.lastActivityAt) - sortableTimestamp(a.lastActivityAt)
function isRecoverableCatalogCursorError(error: unknown): boolean {
const message = error instanceof Error ? error.message : String(error)
@@ -206,7 +214,9 @@ export function useChatListController({
// New hosts scope the page server-side. Keep this boundary check for
// older hosts and malformed proxy responses so another agent's catalog
// entry can never leak into the selected agent's sidebar.
- const serverSessions = serverResult.items.filter(s => s.agent === agentRef)
+ const serverSessions = serverResult.items
+ .filter(s => s.agent === agentRef)
+ .sort(byLastActivityDesc)
chatListNextCursorByAgentRef.current[agentRef] = serverResult.nextCursor ?? null
setChatListHasMoreRemoteSessions(Boolean(serverResult.nextCursor))