Skip to content

The agent that grows with you

fix(desktop): remembered plugin page survives boot when the session list beats disk plugins

teknium1212f6610480a
diff --git a/apps/desktop/src/app/contrib/hooks/restore-late-plugin-route.test.tsx b/apps/desktop/src/app/contrib/hooks/restore-late-plugin-route.test.tsx
new file mode 100644
index 00000000000..848981a7e19
--- /dev/null
+++ b/apps/desktop/src/app/contrib/hooks/restore-late-plugin-route.test.tsx
@@ -0,0 +1,99 @@
+/**
+ * Remembered-route restore vs late disk plugins. A remembered plugin page is
+ * session-shaped until its route registers; if the session list arrives first
+ * (an already-running backend), the restore used to classify it as a stale
+ * session id, navigate to the last chat, and erase the remembered route for
+ * every later boot. The latch now also waits for the disk door's first scan.
+ */
+import { act, renderHook } from '@testing-library/react'
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+
+import { registry } from '@/contrib/registry'
+import { $diskPluginsScanPending } from '@/contrib/runtime-loader'
+import { _resetLegacyDiscardForTests, getRememberedRoute, setRememberedRoute, setRememberedSessionId } from '@/store/session'
+
+import { makeSessionInfo } from '../../../test/session-info'
+
+import { useDesktopIntegrations } from './use-desktop-integrations'
+
+vi.mock('@/store/windows', async importOriginal => ({ ...(await importOriginal<object>()), isHudWindow: () => false }))
+
+const desktopWindow = window as unknown as { hermesDesktop: unknown }
+const originalBridge = desktopWindow.hermesDesktop
+
+beforeEach(() => {
+ window.localStorage.clear()
+ _resetLegacyDiscardForTests()
+ desktopWindow.hermesDesktop = {
+ setPreviewShortcutActive: vi.fn(),
+ onOpenUpdatesRequested: vi.fn(),
+ onFocusSession: vi.fn(),
+ onNotificationAction: vi.fn(),
+ onNotificationActivate: vi.fn(),
+ onDeepLink: vi.fn(),
+ signalDeepLinkReady: vi.fn(),
+ onClosePreviewRequested: vi.fn(),
+ onOpenFolderRequested: vi.fn()
+ }
+})
+
+afterEach(() => {
+ $diskPluginsScanPending.set(false)
+ desktopWindow.hermesDesktop = originalBridge
+})
+
+function mountRestore() {
+ const navigate = vi.fn()
+ const sessions = [makeSessionInfo({ id: 'sess-1' })]
+ renderHook(() =>
+ useDesktopIntegrations({
+ activeProfile: 'default',
+ chatOpen: false,
+ hasPreview: false,
+ locationPathname: '/',
+ navigate,
+ profileReady: true,
+ refreshSessions: vi.fn(),
+ resumeExhaustedSessionId: null,
+ resumeLastSession: true,
+ routedSessionId: null,
+ runtimeIdByStoredSessionId: { current: new Map() },
+ sessions
+ })
+ )
+
+ return navigate
+}
+
+it('restores a remembered plugin page whose route registers after the session list arrived', () => {
+ setRememberedRoute('/html-gallery', 'default')
+ setRememberedSessionId('sess-1', 'default')
+ $diskPluginsScanPending.set(true)
+
+ const navigate = mountRestore()
+
+ // Latch held: neither the last chat nor an erased route.
+ expect(navigate).not.toHaveBeenCalled()
+ expect(getRememberedRoute('default')).toBe('/html-gallery')
+
+ const dispose = registry.register({ area: 'routes', id: 'gallery:page', data: { path: '/html-gallery' }, render: () => null })
+ act(() => $diskPluginsScanPending.set(false))
+
+ expect(navigate).toHaveBeenCalledWith('/html-gallery', { replace: true })
+ expect(getRememberedRoute('default')).toBe('/html-gallery')
+ dispose()
+})
+
+it('still drops a remembered session route nobody owns once the disk scan has settled', () => {
+ setRememberedRoute('/gone-session', 'default')
+ setRememberedSessionId('sess-1', 'default')
+ $diskPluginsScanPending.set(true)
+
+ const navigate = mountRestore()
+ expect(navigate).not.toHaveBeenCalled()
+
+ act(() => $diskPluginsScanPending.set(false))
+
+ expect(navigate).toHaveBeenCalledWith('/sess-1', { replace: true })
+ expect(getRememberedRoute('default')).toBeNull()
+})
diff --git a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
index 8c0ba9b47a3..ebd479f368c 100644
--- a/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
+++ b/apps/desktop/src/app/contrib/hooks/use-desktop-integrations.ts
@@ -1,8 +1,10 @@
+import { useStore } from '@nanostores/react'
import { useEffect, useRef } from 'react'
import { closeActiveTab } from '@/app/chat/close-tab'
import { commandFocusedPreview } from '@/app/chat/right-rail/preview-nav'
import { openSession } from '@/app/open-session'
+import { $diskPluginsScanPending } from '@/contrib/runtime-loader'
import { resolveDeepLinkAction } from '@/lib/deeplink-routes'
import { pathFromHermesDeepLink, resolveHermesOpenPath } from '@/lib/hermes-open-target'
import { storedSessionIdForNotification } from '@/lib/session-ids'
@@ -100,6 +102,7 @@ export function useDesktopIntegrations({
}, [])
const restoredRef = useRef(false)
+ const diskPluginsScanPending = useStore($diskPluginsScanPending)
// Wait until boot has adopted the primary profile, then restore that profile's
// navigation exactly once. The same effect owns subsequent writes so the
@@ -144,6 +147,14 @@ export function useDesktopIntegrations({
return
}
+ // A remembered plugin page looks session-shaped until its route
+ // registers, and disk plugins load async. Hold the latch through the
+ // first disk scan so a page that is merely late is not erased as stale
+ // (an already-running backend can hand us the session list first).
+ if (routeSession && diskPluginsScanPending) {
+ return
+ }
+
restoredRef.current = true
if (
@@ -187,7 +198,7 @@ export function useDesktopIntegrations({
} else if (!routedSessionId && !isOverlayView(appViewForPath(locationPathname))) {
setRememberedRoute(locationPathname, activeProfile)
}
- }, [activeProfile, locationPathname, navigate, profileReady, resumeLastSession, routedSessionId, sessions])
+ }, [activeProfile, diskPluginsScanPending, locationPathname, navigate, profileReady, resumeLastSession, routedSessionId, sessions])
useEffect(() => {
if (!profileReady || !resumeExhaustedSessionId) {
diff --git a/apps/desktop/src/contrib/runtime-loader.ts b/apps/desktop/src/contrib/runtime-loader.ts
index 12238f3e676..80acab45160 100644
--- a/apps/desktop/src/contrib/runtime-loader.ts
+++ b/apps/desktop/src/contrib/runtime-loader.ts
@@ -28,6 +28,8 @@
* trust seam.
*/
+import { atom } from 'nanostores'
+
import { installPluginSdk, sdkImportMap } from '@/sdk/runtime'
import { notifyError } from '@/store/notifications'
@@ -547,6 +549,11 @@ async function scanDiskPlugins(): Promise<void> {
/** Manual rescan (the ⌘K "Reload desktop plugins" fallback). */
export const discoverRuntimePlugins = scanDiskPlugins
+/** True while the disk door's FIRST scan is in flight. Boot code that must
+ * not mistake a not-yet-registered plugin route for a stale one
+ * (remembered-route restore) waits on this instead of on timing. */
+export const $diskPluginsScanPending = atom(false)
+
/** Start the self-maintaining disk door: initial scan, per-file hot reload,
* fs-watched folder reconciliation (poll fallback on older shells). Idempotent. */
export function watchRuntimePlugins(): void {
@@ -557,6 +564,7 @@ export function watchRuntimePlugins(): void {
}
watching = true
+ $diskPluginsScanPending.set(true)
const dirWatchIds = new Set<string>()
const watchedDirs = new Set<string>()
@@ -614,7 +622,7 @@ export function watchRuntimePlugins(): void {
return all
}
- void scanDiskPlugins()
+ void scanDiskPlugins().then(() => $diskPluginsScanPending.set(false))
void startDirWatches().then(watched => {
if (watched) {
return