Skip to content

The agent that grows with you

fix(desktop): star map reset strands the previous profile's load

Maciej Lisiewski91429bee0566
diff --git a/apps/desktop/src/store/starmap.test.ts b/apps/desktop/src/store/starmap.test.ts
new file mode 100644
index 00000000000..75520668368
--- /dev/null
+++ b/apps/desktop/src/store/starmap.test.ts
@@ -0,0 +1,79 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+import type { StarmapGraph } from '@/types/hermes'
+
+const { fetches } = vi.hoisted(() => ({
+ fetches: [] as Array<{ reject: (error: unknown) => void; resolve: (graph: StarmapGraph) => void }>
+}))
+
+vi.mock('@/hermes', () => ({
+ getStarmapGraph: () =>
+ new Promise<StarmapGraph>((resolve, reject) => {
+ fetches.push({ reject, resolve })
+ })
+}))
+
+const { $starmapError, $starmapGraph, $starmapLoading, loadStarmapGraph, resetStarmapGraph } =
+ await import('./starmap')
+
+const graph = (id: string): StarmapGraph => ({ edges: [], nodes: [{ id }] }) as unknown as StarmapGraph
+
+beforeEach(async () => {
+ // Settle leftovers from a previous test so its flight cannot write here.
+ while (fetches.length) {
+ fetches.shift()?.resolve(graph('leftover'))
+ await new Promise(resolve => setTimeout(resolve, 0))
+ }
+
+ resetStarmapGraph()
+ $starmapLoading.set(false)
+})
+
+describe('loadStarmapGraph', () => {
+ it('drops the previous profile graph when a reset lands mid-load', async () => {
+ const previous = loadStarmapGraph()
+ resetStarmapGraph()
+ const current = loadStarmapGraph()
+
+ expect(fetches).toHaveLength(2)
+ fetches[1].resolve(graph('current-node'))
+ await current
+ fetches[0].resolve(graph('previous-node'))
+ await previous
+
+ expect($starmapGraph.get()?.nodes.map(node => node.id)).toEqual(['current-node'])
+ expect($starmapLoading.get()).toBe(false)
+ })
+
+ it('keeps the newer in-flight slot when the stranded load settles first', async () => {
+ const previous = loadStarmapGraph()
+ resetStarmapGraph()
+ const current = loadStarmapGraph()
+
+ fetches[0].reject(new Error('previous profile went away'))
+ await previous
+
+ expect($starmapError.get()).toBeNull()
+ expect($starmapLoading.get()).toBe(true)
+ // A caller arriving now joins the current profile's load, not a new fetch.
+ void loadStarmapGraph()
+ expect(fetches).toHaveLength(2)
+
+ fetches[1].resolve(graph('current-node'))
+ await current
+
+ expect($starmapGraph.get()?.nodes.map(node => node.id)).toEqual(['current-node'])
+ expect($starmapLoading.get()).toBe(false)
+ })
+
+ it('serves the cached graph without refetching', async () => {
+ const pending = loadStarmapGraph()
+ fetches[0].resolve(graph('cached-node'))
+ await pending
+
+ await loadStarmapGraph()
+
+ expect(fetches).toHaveLength(1)
+ expect($starmapGraph.get()?.nodes.map(node => node.id)).toEqual(['cached-node'])
+ })
+})
diff --git a/apps/desktop/src/store/starmap.ts b/apps/desktop/src/store/starmap.ts
index b43a52bad9b..a7c08179487 100644
--- a/apps/desktop/src/store/starmap.ts
+++ b/apps/desktop/src/store/starmap.ts
@@ -12,6 +12,10 @@ export const $starmapError = atom<null | string>(null)
let inflight: Promise<void> | null = null
+// Bumped by resetStarmapGraph on a profile switch: a load still awaiting the
+// previous profile's graph must not publish it, nor clear the newer slot.
+let starmapEpoch = 0
+
export async function loadStarmapGraph(force = false): Promise<void> {
if (inflight) {
return inflight
@@ -24,14 +28,26 @@ export async function loadStarmapGraph(force = false): Promise<void> {
$starmapLoading.set(true)
$starmapError.set(null)
+ const epoch = starmapEpoch
+
inflight = (async () => {
try {
- $starmapGraph.set(await getStarmapGraph())
+ const graph = await getStarmapGraph()
+
+ if (epoch === starmapEpoch) {
+ $starmapGraph.set(graph)
+ }
} catch (err) {
- $starmapError.set(err instanceof Error ? err.message : String(err))
+ if (epoch === starmapEpoch) {
+ $starmapError.set(err instanceof Error ? err.message : String(err))
+ }
} finally {
- $starmapLoading.set(false)
- inflight = null
+ // Same epoch means this is still the only flight; a stranded one leaves
+ // the newer load's spinner and slot alone.
+ if (epoch === starmapEpoch) {
+ $starmapLoading.set(false)
+ inflight = null
+ }
}
})()
@@ -59,7 +75,10 @@ export function evictStarmapNode(id: string): () => void {
/** Drop the cache so the next open refetches against the now-active profile. */
export function resetStarmapGraph(): void {
+ starmapEpoch += 1
inflight = null
$starmapGraph.set(null)
$starmapError.set(null)
+ // The stranded load no longer owns the spinner; a fresh load re-raises it.
+ $starmapLoading.set(false)
}