Skip to content

The agent that grows with you

fix(desktop): archived view drops loads from a previous connection

Maciej Lisiewski15c489a7a921
diff --git a/apps/desktop/src/store/sidebar-archive.test.ts b/apps/desktop/src/store/sidebar-archive.test.ts
index 4017e65053c..ed81768396f 100644
--- a/apps/desktop/src/store/sidebar-archive.test.ts
+++ b/apps/desktop/src/store/sidebar-archive.test.ts
@@ -1,8 +1,15 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
+import { $apiRequestScope } from '@/api/client'
import type { SessionInfo } from '@/types/hermes'
-import { $archivedSessions, listEveryArchivedSession, loadArchivedSessions } from './sidebar-archive'
+import { $sidebarShowArchived } from './layout'
+import {
+ $archivedSessions,
+ $archivedSessionsLoading,
+ listEveryArchivedSession,
+ loadArchivedSessions
+} from './sidebar-archive'
const listAllProfileSessions = vi.hoisted(() => vi.fn())
@@ -35,6 +42,8 @@ const page = (from: number, count: number): SessionInfo[] =>
Array.from({ length: count }, (_, i) => row(`a-${from + i}`))
beforeEach(() => {
+ $sidebarShowArchived.set(false)
+ $apiRequestScope.set({ connectionId: null, profile: null })
$archivedSessions.set([])
listAllProfileSessions.mockReset()
})
@@ -49,6 +58,100 @@ describe('loadArchivedSessions', () => {
expect($archivedSessions.get()).toEqual([existing])
})
+
+ it('does not publish the previous connection rows after a switch mid-load', async () => {
+ let resolvePrevious!: (page: unknown) => void
+ let resolveCurrent!: (page: unknown) => void
+
+ listAllProfileSessions
+ .mockImplementationOnce(() => new Promise(resolve => (resolvePrevious = resolve)))
+ .mockImplementationOnce(() => new Promise(resolve => (resolveCurrent = resolve)))
+
+ $apiRequestScope.setKey('connectionId', 'conn-a')
+ const previous = loadArchivedSessions()
+ $apiRequestScope.setKey('connectionId', 'conn-b')
+ const current = loadArchivedSessions()
+
+ // The new connection's reload must fetch, not be swallowed by A's flag.
+ expect(listAllProfileSessions).toHaveBeenCalledTimes(2)
+
+ resolveCurrent({ sessions: [row('b-archived')], total: 1 })
+ await current
+ resolvePrevious({ sessions: [row('a-archived')], total: 1 })
+ await previous
+
+ expect($archivedSessions.get().map(session => session.id)).toEqual(['b-archived'])
+ expect($archivedSessionsLoading.get()).toBe(false)
+ })
+
+ it('clears the previous connection rows when a load starts on a new one', async () => {
+ let resolveCurrent!: (page: unknown) => void
+
+ listAllProfileSessions
+ .mockResolvedValueOnce({ sessions: [row('a-archived')], total: 1 })
+ .mockImplementationOnce(() => new Promise(resolve => (resolveCurrent = resolve)))
+
+ $apiRequestScope.setKey('connectionId', 'conn-a')
+ await loadArchivedSessions()
+ $apiRequestScope.setKey('connectionId', 'conn-b')
+ const current = loadArchivedSessions()
+
+ expect($archivedSessions.get()).toEqual([])
+
+ resolveCurrent({ sessions: [row('b-archived')], total: 1 })
+ await current
+
+ expect($archivedSessions.get().map(session => session.id)).toEqual(['b-archived'])
+ })
+
+ // Nothing else reloads on a scope switch: the sidebar effect watches only the Archived toggle
+ // and background sync waits for a backend sessions.changed event.
+ it('clears and reloads an open Archived view on a scope switch with no other trigger', async () => {
+ $sidebarShowArchived.set(true)
+ let resolveCurrent!: (page: unknown) => void
+
+ listAllProfileSessions
+ .mockResolvedValueOnce({ sessions: [row('a-archived')], total: 1 })
+ .mockImplementationOnce(() => new Promise(resolve => (resolveCurrent = resolve)))
+
+ await loadArchivedSessions()
+ $apiRequestScope.setKey('profile', 'work')
+
+ expect($archivedSessions.get()).toEqual([])
+ expect(listAllProfileSessions).toHaveBeenCalledTimes(2)
+
+ resolveCurrent({ sessions: [row('work-archived')], total: 1 })
+ await vi.waitFor(() => expect($archivedSessions.get().map(session => session.id)).toEqual(['work-archived']))
+ expect($archivedSessionsLoading.get()).toBe(false)
+ })
+
+ it('replaces a first load stranded by a scope switch while Archived is open', async () => {
+ $sidebarShowArchived.set(true)
+ let resolvePrevious!: (page: unknown) => void
+
+ listAllProfileSessions
+ .mockImplementationOnce(() => new Promise(resolve => (resolvePrevious = resolve)))
+ .mockResolvedValueOnce({ sessions: [row('b-archived')], total: 1 })
+
+ const previous = loadArchivedSessions()
+ $apiRequestScope.setKey('connectionId', 'conn-b')
+
+ await vi.waitFor(() => expect($archivedSessions.get().map(session => session.id)).toEqual(['b-archived']))
+ resolvePrevious({ sessions: [row('a-archived')], total: 1 })
+ await previous
+
+ expect($archivedSessions.get().map(session => session.id)).toEqual(['b-archived'])
+ expect($archivedSessionsLoading.get()).toBe(false)
+ })
+
+ it('clears a closed Archived view on a scope switch without fetching', async () => {
+ $archivedSessions.set([row('a-archived')])
+
+ $apiRequestScope.setKey('profile', 'work')
+
+ expect($archivedSessions.get()).toEqual([])
+ expect(listAllProfileSessions).not.toHaveBeenCalled()
+ })
})
describe('listEveryArchivedSession', () => {
diff --git a/apps/desktop/src/store/sidebar-archive.ts b/apps/desktop/src/store/sidebar-archive.ts
index 8f65c1111f1..4c54cba9f1a 100644
--- a/apps/desktop/src/store/sidebar-archive.ts
+++ b/apps/desktop/src/store/sidebar-archive.ts
@@ -1,8 +1,11 @@
+import { backendScopeKey } from '@hermes/shared'
import { atom, computed } from 'nanostores'
+import { $apiRequestScope } from '@/api/client'
import { listAllProfileSessions } from '@/api/sessions'
import type { SessionInfo } from '@/types/hermes'
+import { $sidebarShowArchived } from './layout'
import { $sessions } from './session'
const ARCHIVED_FETCH_PAGE_SIZE = 200
@@ -53,23 +56,69 @@ export async function listEveryArchivedSession(): Promise<SessionInfo[]> {
export const $archivedSessions = atom<SessionInfo[]>([])
export const $archivedSessionsLoading = atom(false)
+// Each load takes a generation; only the newest may publish or clear the
+// flag. The scope key pins it to the connection/profile it was issued on, so
+// a switch mid-load starts a fresh fetch and strands the previous one.
+let archiveGeneration = 0
+let loadingScope: null | string = null
+
+function archiveScopeKey(): string {
+ const { connectionId, profile } = $apiRequestScope.get()
+
+ return backendScopeKey(connectionId, profile)
+}
+
export async function loadArchivedSessions(): Promise<void> {
- if ($archivedSessionsLoading.get()) {
+ const scope = archiveScopeKey()
+
+ if ($archivedSessionsLoading.get() && loadingScope === scope) {
return
}
+ const generation = ++archiveGeneration
+ loadingScope = scope
+
$archivedSessionsLoading.set(true)
try {
- $archivedSessions.set(await listEveryArchivedSession())
+ const sessions = await listEveryArchivedSession()
+
+ if (generation === archiveGeneration && archiveScopeKey() === scope) {
+ $archivedSessions.set(sessions)
+ }
} 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.
} finally {
- $archivedSessionsLoading.set(false)
+ if (generation === archiveGeneration) {
+ $archivedSessionsLoading.set(false)
+ }
}
}
+// Rows from another connection/profile must not linger (or be acted on), and
+// nothing else reloads on a switch: the sidebar effect watches only the
+// Archived toggle and background sync waits for a backend sessions.changed.
+// Clear on every real scope change; reload (superseding a load stranded on
+// the old scope) while Archived is showing. The keep-last-good fallback in
+// loadArchivedSessions is therefore per scope.
+let observedScope = archiveScopeKey()
+
+$apiRequestScope.listen(() => {
+ const scope = archiveScopeKey()
+
+ if (scope === observedScope) {
+ return
+ }
+
+ observedScope = scope
+ $archivedSessions.set([])
+
+ if ($sidebarShowArchived.get()) {
+ void loadArchivedSessions()
+ }
+})
+
/** Spend on a session — provider-reported price when we have one, our own
* estimate otherwise. */
export const sessionCostUsd = (session: SessionInfo): number =>