Skip to content

The agent that grows with you

fix(desktop): keep peer windows on shared gateway

Gillec25aa35e1aab
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 85bec536387..4eb6d039c78 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -256,6 +256,7 @@ import {
import { missingRendererAssets } from './renderer-bundle'
import { attachRendererConsoleCapture, formatRendererBoundaryReport } from './renderer-log'
import {
+ buildInstanceWindowUrl,
buildSessionWindowUrl,
chatWindowWebPreferences,
createSessionWindowRegistry,
@@ -10870,8 +10871,10 @@ function createSessionWindow(sessionId, { watch = false } = {}) {
// Additional full "instance" windows — peers of the primary that render the
// COMPLETE app (sidebar, routing, its own draft) against the shared backend, so
// a user can run multiple GUI windows at once (⌘⇧N / the "New Window" palette
-// command). Unlike the compact session windows they carry no `?win` flag. The
-// primary mainWindow stays the notification / deep-link / pet-overlay anchor and
+// command). Unlike the compact session windows they carry no `?win` flag; a
+// separate `peer=1` marker prevents them from replaying app-launch source
+// restoration after joining that shared backend. The primary mainWindow stays
+// the notification / deep-link / pet-overlay anchor and
// is NOT tracked here. The set holds a strong reference so an open peer isn't
// garbage-collected, and drops it on close.
const instanceWindows = new Set<any>()
@@ -10951,7 +10954,14 @@ function createInstanceWindow() {
})
attachRendererConsoleCapture(win, 'instance', rememberLog)
- loadWindowUrl(win, DEV_SERVER || pathToFileURL(resolveRendererIndex()).toString(), 'Instance window')
+ loadWindowUrl(
+ win,
+ buildInstanceWindowUrl({
+ devServer: DEV_SERVER,
+ rendererIndexPath: DEV_SERVER ? undefined : resolveRendererIndex()
+ }),
+ 'Instance window'
+ )
return win
}
diff --git a/apps/desktop/electron/session-windows.test.ts b/apps/desktop/electron/session-windows.test.ts
index 1959cc44583..c0a11784292 100644
--- a/apps/desktop/electron/session-windows.test.ts
+++ b/apps/desktop/electron/session-windows.test.ts
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict'
import { test } from 'vitest'
import {
+ buildInstanceWindowUrl,
buildSessionWindowUrl,
chatWindowWebPreferences,
createSessionWindowRegistry,
@@ -88,6 +89,19 @@ test('buildSessionWindowUrl adds the watch flag for spectator windows, before th
assert.equal(url, 'http://localhost:5173/?win=secondary&watch=1#/abc')
})
+test('buildInstanceWindowUrl marks a full peer without selecting a specialized renderer', () => {
+ const url = buildInstanceWindowUrl({ devServer: 'http://localhost:5173/' })
+
+ assert.equal(url, 'http://localhost:5173/?peer=1')
+ assert.ok(!url.includes('win='))
+})
+
+test('buildInstanceWindowUrl marks a packaged full peer', () => {
+ const url = buildInstanceWindowUrl({ rendererIndexPath: '/opt/app/index.html' })
+
+ assert.match(url, /^file:\/\/.*index\.html\?peer=1$/)
+})
+
test('instanceWindowBounds cascades a new window off its source bounds', () => {
const bounds = instanceWindowBounds({ x: 100, y: 120, width: 1400, height: 900 }, { width: 1, height: 1 })
diff --git a/apps/desktop/electron/session-windows.ts b/apps/desktop/electron/session-windows.ts
index 790f1c03a73..19be87ff1c3 100644
--- a/apps/desktop/electron/session-windows.ts
+++ b/apps/desktop/electron/session-windows.ts
@@ -77,6 +77,23 @@ function buildSessionWindowUrl(sessionId: string, { devServer, rendererIndexPath
return `${pathToFileURL(rendererIndexPath).toString()}${query}${route}`
}
+// Full peer windows render the ordinary app shell, so they deliberately do
+// not use the `win` query parameter that selects a specialized renderer. The
+// separate marker lets the renderer distinguish a peer from the one primary
+// app window: app-launch source restoration belongs to the primary only, while
+// a peer keeps the already-running backend it joined during boot.
+function buildInstanceWindowUrl({ devServer, rendererIndexPath }: any = {}) {
+ const query = '?peer=1'
+
+ if (devServer) {
+ const base = devServer.endsWith('/') ? devServer.slice(0, -1) : devServer
+
+ return `${base}/${query}`
+ }
+
+ return `${pathToFileURL(rendererIndexPath).toString()}${query}`
+}
+
// Full "instance" windows (⌘⇧N / the "New Window" command) open a complete app
// peer, not a compact chat. Cascade each one off its source window's bounds so a
// new window doesn't land exactly on top of the one it was spawned from. Pure so
@@ -160,6 +177,7 @@ function createSessionWindowRegistry() {
}
export {
+ buildInstanceWindowUrl,
buildSessionWindowUrl,
chatWindowWebPreferences,
createSessionWindowRegistry,
diff --git a/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx b/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx
index fe9ef7afd58..79acb22e862 100644
--- a/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx
+++ b/apps/desktop/src/app/chat/sidebar/connection-switcher.test.tsx
@@ -40,6 +40,10 @@ vi.mock('@/store/boot', () => ({
})
}))
+vi.mock('@/store/windows', () => ({
+ isPeerInstanceWindow: vi.fn(() => false)
+}))
+
vi.mock('@/i18n', () => ({
useI18n: () => ({
t: {
@@ -65,6 +69,7 @@ vi.mock('@/i18n', () => ({
const connectionStore = await import('@/store/connections')
const bootStore = await import('@/store/boot')
+const windowStore = await import('@/store/windows')
const $activeConnectionId = connectionStore.$activeConnectionId as ReturnType<typeof atom<null | string>>
const $connectionsRegistry = connectionStore.$connectionsRegistry
const $desktopBoot = bootStore.$desktopBoot
@@ -72,6 +77,7 @@ const $pendingConnectionId = connectionStore.$pendingConnectionId
const initializeConnectionsRegistry = vi.mocked(connectionStore.initializeConnectionsRegistry)
const refreshConnectionsRegistry = vi.mocked(connectionStore.refreshConnectionsRegistry)
const selectConnection = vi.mocked(connectionStore.selectConnection)
+const isPeerInstanceWindow = vi.mocked(windowStore.isPeerInstanceWindow)
const onConnect = vi.fn()
const connection = (id: string, label: string, kind: 'local' | 'remote' = 'remote') => ({
@@ -106,6 +112,7 @@ afterEach(() => {
})
$pendingConnectionId.set(null)
$findInPage.set({ active: false, query: '', matchOrdinal: 0, matchCount: 0 })
+ isPeerInstanceWindow.mockReturnValue(false)
})
describe('ConnectionSwitcher', () => {
@@ -127,6 +134,22 @@ describe('ConnectionSwitcher', () => {
await waitFor(() => expect(initializeConnectionsRegistry).toHaveBeenCalledTimes(1))
})
+ it('keeps a full peer on the shared backend instead of replaying app-launch source restoration', async () => {
+ isPeerInstanceWindow.mockReturnValue(true)
+ $desktopBoot.set({
+ ...$desktopBoot.get(),
+ phase: 'renderer.ready',
+ progress: 100,
+ running: false,
+ visible: false
+ })
+
+ render(<ConnectionSwitcher onConnect={onConnect} />)
+
+ await waitFor(() => expect(refreshConnectionsRegistry).toHaveBeenCalledTimes(1))
+ expect(initializeConnectionsRegistry).not.toHaveBeenCalled()
+ })
+
it('adds no source chrome for a local-only setup', () => {
$connectionsRegistry.set(registry([connection('local', 'This device', 'local')]))
render(<ConnectionSwitcher onConnect={onConnect} />)
diff --git a/apps/desktop/src/app/chat/sidebar/connection-switcher.tsx b/apps/desktop/src/app/chat/sidebar/connection-switcher.tsx
index f0396abfd2e..22f559e7e33 100644
--- a/apps/desktop/src/app/chat/sidebar/connection-switcher.tsx
+++ b/apps/desktop/src/app/chat/sidebar/connection-switcher.tsx
@@ -36,6 +36,7 @@ import {
} from '@/store/connections'
import { closeFindBar } from '@/store/find-in-page'
import { notifyError } from '@/store/notifications'
+import { isPeerInstanceWindow } from '@/store/windows'
export function ConnectionSwitcher({ compact = false, onConnect }: { compact?: boolean; onConnect: () => void }) {
const { t } = useI18n()
@@ -64,8 +65,10 @@ export function ConnectionSwitcher({ compact = false, onConnect }: { compact?: b
// The primary boot owns its initial config/session fetches. Restoring a
// different source before those settle lets a late primary response repaint
// the sidebar under the new source label. Switch only after boot completes,
- // then the normal source reset/refetch remains the final writer.
- if (!boot.running) {
+ // then the normal source reset/refetch remains the final writer. Full peer
+ // windows already joined the shared backend during their own boot; replaying
+ // this app-launch preference there would move them away from that backend.
+ if (!boot.running && !isPeerInstanceWindow()) {
void initializeConnectionsRegistry().catch(() => undefined)
}
}, [boot.running])
diff --git a/apps/desktop/src/store/windows.test.ts b/apps/desktop/src/store/windows.test.ts
index e6c3f4974a5..d6b745be858 100644
--- a/apps/desktop/src/store/windows.test.ts
+++ b/apps/desktop/src/store/windows.test.ts
@@ -1,6 +1,12 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
-import { canOpenNewWindow, canOpenSessionWindow, openNewWindow, openSessionInNewWindow } from './windows'
+import {
+ canOpenNewWindow,
+ canOpenSessionWindow,
+ isPeerInstanceWindow,
+ openNewWindow,
+ openSessionInNewWindow
+} from './windows'
const desktopWindow = window as unknown as { hermesDesktop?: Window['hermesDesktop'] }
const initialHermesDesktop = desktopWindow.hermesDesktop
@@ -50,6 +56,15 @@ describe('canOpenSessionWindow', () => {
})
})
+describe('isPeerInstanceWindow', () => {
+ it('recognizes only the full peer marker', () => {
+ expect(isPeerInstanceWindow('?peer=1')).toBe(true)
+ expect(isPeerInstanceWindow('?peer=0')).toBe(false)
+ expect(isPeerInstanceWindow('?win=secondary')).toBe(false)
+ expect(isPeerInstanceWindow('')).toBe(false)
+ })
+})
+
describe('openSessionInNewWindow', () => {
it('no-ops without a session id', async () => {
const open = vi.fn().mockResolvedValue({ ok: true })
diff --git a/apps/desktop/src/store/windows.ts b/apps/desktop/src/store/windows.ts
index 3549ce4186d..1403863d536 100644
--- a/apps/desktop/src/store/windows.ts
+++ b/apps/desktop/src/store/windows.ts
@@ -83,6 +83,18 @@ export function isWatchWindow(): boolean {
// keystroke into N prompts, and a HUD is the last place to paint onboarding.
export const isAuxiliaryWindow = (): boolean => isSecondaryWindow() || isHudWindow()
+// A full peer window renders the ordinary app shell against the backend that
+// Electron already has running. It is not an auxiliary/specialized renderer,
+// but it must not replay the primary window's app-launch source restoration
+// after boot and silently re-home itself to another registered gateway.
+export function isPeerInstanceWindow(search = typeof window === 'undefined' ? '' : window.location.search): boolean {
+ try {
+ return new URLSearchParams(search).get('peer') === '1'
+ } catch {
+ return false
+ }
+}
+
// The profile a helper window (the HUD) was asked to boot against, carried in
// the query string by the main process (see hudUrl). The HUD is a full app
// renderer that otherwise adopts the PRIMARY backend's profile — wrong the