Skip to content

The agent that grows with you

fix(desktop): paginate archived sessions

Shawn Wang2cc27f99e0b3
diff --git a/apps/desktop/src/app/settings/sessions-settings.tsx b/apps/desktop/src/app/settings/sessions-settings.tsx
index b0d17816024..9a3a417ca71 100644
--- a/apps/desktop/src/app/settings/sessions-settings.tsx
+++ b/apps/desktop/src/app/settings/sessions-settings.tsx
@@ -7,7 +7,6 @@ import { Tip } from '@/components/ui/tooltip'
import {
deleteSession,
getHermesConfigRecord,
- listAllProfileSessions,
peekConfigReadOrigin,
retainConfigReadOrigin,
saveHermesConfig,
@@ -24,6 +23,7 @@ import { notify, notifyError } from '@/store/notifications'
import { applyConfiguredDefaultProjectDir, ensureDefaultWorkspaceCwd } from '@/store/session'
import { untombstoneSessions } from '@/store/session-removal'
import { forgetSessionUnread } from '@/store/session-unread'
+import { listEveryArchivedSession } from '@/store/sidebar-archive'
import type { HermesConfigRecord, SessionInfo } from '@/types/hermes'
import { EmptyState, ListRow, SectionHeading, SettingsContent, SettingsSkeleton, ToggleRow } from './primitives'
@@ -33,8 +33,6 @@ import { useSettingDeepLink } from './use-setting-deep-link'
const DEFAULT_AUTO_ARCHIVE_DAYS = 3
-const ARCHIVED_FETCH_LIMIT = 200
-
interface SessionsSettingsProps {
subpage?: string
}
@@ -64,8 +62,7 @@ function ArchivedSessionsSettings({ includeDefaultDirectory }: { includeDefaultD
setLoading(true)
try {
- const result = await listAllProfileSessions(ARCHIVED_FETCH_LIMIT, 0, 'only')
- setLocalSessions(result.sessions)
+ setLocalSessions(await listEveryArchivedSession())
} catch (err) {
notifyError(err, s.failedLoad)
} finally {
diff --git a/apps/desktop/src/store/sidebar-archive.test.ts b/apps/desktop/src/store/sidebar-archive.test.ts
index e7e14985ab4..5f139262e7b 100644
--- a/apps/desktop/src/store/sidebar-archive.test.ts
+++ b/apps/desktop/src/store/sidebar-archive.test.ts
@@ -2,18 +2,31 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import { listAllProfileSessions, type SessionInfo } from '@/hermes'
-import { $archivedSessions, loadArchivedSessions } from './sidebar-archive'
+import { $archivedSessions, listEveryArchivedSession, loadArchivedSessions } from './sidebar-archive'
vi.mock('@/hermes', () => ({
listAllProfileSessions: vi.fn()
}))
-describe('loadArchivedSessions', () => {
- beforeEach(() => {
- $archivedSessions.set([])
- vi.mocked(listAllProfileSessions).mockReset()
- })
+const row = (id: string): SessionInfo =>
+ ({
+ id,
+ archived: true,
+ input_tokens: 0,
+ output_tokens: 0,
+ message_count: 1,
+ started_at: 1,
+ last_active: 1,
+ source: 'desktop',
+ tool_call_count: 0
+ }) as SessionInfo
+
+beforeEach(() => {
+ $archivedSessions.set([])
+ vi.mocked(listAllProfileSessions).mockReset()
+})
+describe('loadArchivedSessions', () => {
it('keeps the last successful result when a refresh fails', async () => {
const existing = { id: 'archived-1', title: 'Keep me' } as SessionInfo
$archivedSessions.set([existing])
@@ -24,3 +37,24 @@ describe('loadArchivedSessions', () => {
expect($archivedSessions.get()).toEqual([existing])
})
})
+
+describe('listEveryArchivedSession', () => {
+ it('paginates until the backend total is loaded', async () => {
+ vi.mocked(listAllProfileSessions)
+ .mockResolvedValueOnce({ sessions: [row('a'), row('b')], total: 3, limit: 200, offset: 0 })
+ .mockResolvedValueOnce({ sessions: [row('c')], total: 3, limit: 200, offset: 2 })
+
+ await expect(listEveryArchivedSession()).resolves.toEqual([row('a'), row('b'), row('c')])
+ expect(listAllProfileSessions).toHaveBeenNthCalledWith(1, 200, 0, 'only', 'recent', 'all', {}, 0)
+ expect(listAllProfileSessions).toHaveBeenNthCalledWith(2, 200, 0, 'only', 'recent', 'all', {}, 2)
+ })
+
+ it('stops on an empty page when profiles changed during pagination', async () => {
+ vi.mocked(listAllProfileSessions)
+ .mockResolvedValueOnce({ sessions: [row('a')], total: 2, limit: 200, offset: 0 })
+ .mockResolvedValueOnce({ sessions: [], total: 2, limit: 200, offset: 1 })
+
+ await expect(listEveryArchivedSession()).resolves.toEqual([row('a')])
+ expect(listAllProfileSessions).toHaveBeenCalledTimes(2)
+ })
+})
diff --git a/apps/desktop/src/store/sidebar-archive.ts b/apps/desktop/src/store/sidebar-archive.ts
index 549a9f57b82..de84a441833 100644
--- a/apps/desktop/src/store/sidebar-archive.ts
+++ b/apps/desktop/src/store/sidebar-archive.ts
@@ -4,9 +4,29 @@ import { listAllProfileSessions, type SessionInfo } from '@/hermes'
import { $sessions } from './session'
-// Archived rows are excluded from the sessions query, so the Archived view has
-// to fetch its own set. Capped: it's a lookup surface, not a feed.
-const ARCHIVED_FETCH_LIMIT = 200
+const ARCHIVED_FETCH_PAGE_SIZE = 200
+
+export async function listEveryArchivedSession(): Promise<SessionInfo[]> {
+ const sessions: SessionInfo[] = []
+
+ while (true) {
+ const page = await listAllProfileSessions(
+ ARCHIVED_FETCH_PAGE_SIZE,
+ 0,
+ 'only',
+ 'recent',
+ 'all',
+ {},
+ sessions.length
+ )
+
+ sessions.push(...page.sessions)
+
+ if (page.sessions.length === 0 || sessions.length >= page.total) {
+ return sessions
+ }
+ }
+}
export const $archivedSessions = atom<SessionInfo[]>([])
export const $archivedSessionsLoading = atom(false)
@@ -19,9 +39,7 @@ export async function loadArchivedSessions(): Promise<void> {
$archivedSessionsLoading.set(true)
try {
- const result = await listAllProfileSessions(ARCHIVED_FETCH_LIMIT, 0, 'only')
-
- $archivedSessions.set(result.sessions)
+ $archivedSessions.set(await listEveryArchivedSession())
} catch {
// A background refresh must not turn a usable Archived view into an empty
// one when the backend is temporarily unavailable. Keep the last good set.