Skip to content

The agent that grows with you

fix(desktop): refresh window state on connection republish

Brooklyn Nicholson88622842094c
diff --git a/apps/desktop/electron/connection-window-state.test.ts b/apps/desktop/electron/connection-window-state.test.ts
new file mode 100644
index 00000000000..751c86f791f
--- /dev/null
+++ b/apps/desktop/electron/connection-window-state.test.ts
@@ -0,0 +1,51 @@
+import { describe, expect, it, vi } from 'vitest'
+
+import { liveWindowState, overlayWindowState } from './connection-window-state'
+
+describe('liveWindowState', () => {
+ const makeWin = (destroyed = false) => ({ isDestroyed: () => destroyed }) as never
+
+ const deps = (win: unknown) => ({
+ fromWebContents: vi.fn().mockReturnValue(win),
+ getWindowState: vi.fn().mockReturnValue({ isFullscreen: true }),
+ fallback: null as never
+ })
+
+ it('reads the SENDER window state, not a cached snapshot (#102451)', () => {
+ const senderWin = makeWin()
+ const d = deps(senderWin)
+ const state = liveWindowState({} as never, d)
+ expect(state).toEqual({ isFullscreen: true })
+ expect(d.getWindowState).toHaveBeenCalledWith(senderWin)
+ })
+
+ it('falls back to the primary window when the reply carries no sender', () => {
+ const primary = makeWin()
+ const d = {
+ fromWebContents: vi.fn(),
+ getWindowState: vi.fn().mockReturnValue({ isFullscreen: false }),
+ fallback: primary as never
+ }
+ expect(liveWindowState(undefined, d)).toEqual({ isFullscreen: false })
+ expect(d.getWindowState).toHaveBeenCalledWith(primary)
+ })
+
+ it('returns undefined when no usable window exists', () => {
+ expect(liveWindowState(undefined, deps(null))).toBeUndefined()
+ expect(liveWindowState({} as never, deps(makeWin(true)))).toBeUndefined()
+ })
+})
+
+describe('overlayWindowState', () => {
+ it('spreads live state over the connection reply', () => {
+ expect(overlayWindowState({ model: 'x', isFullscreen: false }, { isFullscreen: true })).toEqual({
+ model: 'x',
+ isFullscreen: true
+ })
+ })
+
+ it('returns the connection untouched when no live state was computable', () => {
+ const connection = { model: 'x', isFullscreen: false }
+ expect(overlayWindowState(connection, undefined)).toEqual(connection)
+ })
+})
diff --git a/apps/desktop/electron/connection-window-state.ts b/apps/desktop/electron/connection-window-state.ts
new file mode 100644
index 00000000000..12b1dfd57b6
--- /dev/null
+++ b/apps/desktop/electron/connection-window-state.ts
@@ -0,0 +1,47 @@
+import type { BrowserWindow, WebContents } from 'electron'
+
+/**
+ * Live window state to attach to a connection reply (#102451).
+ *
+ * A republished connection must carry the calling window's CURRENT chrome state
+ * (fullscreen, maximized, …), not whatever `startHermes()` baked into the cached
+ * backend descriptor at mint time. The backend pool entry outlives renderer
+ * reloads, reconnects and sleep/wake, so a mint-time `...getWindowState()`
+ * spread goes stale the moment the user toggles fullscreen — and a stale
+ * `isFullscreen: false` overwrites the live flag in the renderer, losing the
+ * fullscreen titlebar inset until the next toggle. Reading the state at IPC
+ * reply time keeps every republish consistent with the
+ * `hermes:window-state-changed` live-push path.
+ *
+ * `undefined` when no usable window exists (sender gone, fallback destroyed) —
+ * callers then leave the connection's own (possibly stale) fields untouched
+ * rather than fabricating state.
+ */
+
+export interface LiveWindowStateDeps<TWindowState> {
+ /** Electron's `BrowserWindow.fromWebContents` (injectable for tests). */
+ fromWebContents: (contents: WebContents) => BrowserWindow | null
+ /** The `getWindowState(win)` snapshot used elsewhere in main. */
+ getWindowState: (win: BrowserWindow) => TWindowState
+ /** Primary window — the fallback when the reply has no live sender. */
+ fallback: BrowserWindow | null
+}
+
+export function liveWindowState<TWindowState>(
+ sender: WebContents | undefined,
+ deps: LiveWindowStateDeps<TWindowState>
+): TWindowState | undefined {
+ const win = (sender && deps.fromWebContents(sender)) || deps.fallback
+ if (!win || win.isDestroyed()) {
+ return undefined
+ }
+ return deps.getWindowState(win)
+}
+
+/** Overlay the (already computed) window state onto a connection reply. */
+export function overlayWindowState<TConnection extends object, TWindowState extends object>(
+ connection: TConnection,
+ windowState: TWindowState | undefined
+): TConnection & TWindowState {
+ return (windowState ? { ...connection, ...windowState } : { ...connection }) as TConnection & TWindowState
+}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 3a3fe6e2bc1..7e4ffe5ca16 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -192,6 +192,7 @@ import {
upsertConnection
} from './connection-registry'
import type { RosterProfileMetadata } from './connection-registry'
+import { liveWindowState, overlayWindowState } from './connection-window-state'
import { describeCrashReason, installCrashForensics } from './crash-forensics'
import { adoptServedDashboardToken, resolveServedDashboardToken } from './dashboard-token'
import { resolveDesktopHermesHome, resolveDesktopUserData } from './data-paths'
@@ -546,6 +547,7 @@ import { decodeWebText } from './web-text-decoder'
import { windowAcceleratorAction } from './window-accelerator'
import { enumerateWindowsFrontToBack, enumerationFailed, readWindowBelow } from './window-below'
import { bindWindowChromeEvents } from './window-chrome-events'
+import { liveWindowState, overlayWindowState } from './connection-window-state'
import {
registrySshPoolScopeByConnectionId,
registrySshScopeForWindowRoute,
@@ -15028,12 +15030,13 @@ ipcMain.handle('hermes:connection', async (event, profile, extra) => {
primaryProfileKey()
)
- return connectDesktopProfileRoute(route, spawnPriorityFrom(extra?.priority))
+ return connectDesktopProfileRoute(route, spawnPriorityFrom(extra?.priority), event.sender)
})
async function connectDesktopProfileRoute(
route: DesktopProfileRoute,
- spawnPriority: LocalBackendSpawnPriority = 'foreground'
+ spawnPriority: LocalBackendSpawnPriority = 'foreground',
+ sender?: Electron.WebContents
) {
// Coalesce concurrent renderer dials for one profile scope (#90812): the
// renderer-side reconnect lock is per-window, so two windows waking at once
@@ -15054,13 +15057,28 @@ async function connectDesktopProfileRoute(
clearSpawnPriority()
}
+ // Every republish carries LIVE window state (#102451): the backend pool entry
+ // (and the getWindowState() snapshot startHermes baked into it) outlives
+ // reloads, reconnects and sleep/wake, so a reply built only from the cached
+ // descriptor overwrites the renderer's live fullscreen flag with the
+ // mint-time snapshot. Reading the caller's state HERE keeps registry-scoped,
+ // primary-resolved and bare replies consistent with the
+ // hermes:window-state-changed live-push path.
+ const windowState = liveWindowState(sender, {
+ fromWebContents: BrowserWindow.fromWebContents,
+ getWindowState,
+ fallback: mainWindow
+ })
+
if (route.connectionId) {
- return { ...connection, connectionId: route.connectionId, registryScoped: true }
+ return overlayWindowState({ ...connection, connectionId: route.connectionId, registryScoped: true }, windowState)
}
const connectionId = resolvedConnectionId(readDesktopConnectionsRegistry(), connection)
- return connectionId ? { ...connection, connectionId } : connection
+ return connectionId
+ ? overlayWindowState({ ...connection, connectionId }, windowState)
+ : overlayWindowState(connection, windowState)
}
// Registry-scoped variant: resolve a backend for (connectionId, profile).
@@ -15070,7 +15088,7 @@ async function connectDesktopProfileRoute(
// ensureBackend when the v1 route is local, and forces a genuinely-local
// child when the v1 global mode is remote (the registry 'local' entry always
// means this machine) unless the profile is remote-only.
-ipcMain.handle('hermes:connection:for', async (_event, payload) => {
+ipcMain.handle('hermes:connection:for', async (event, payload) => {
const { connectionId, profile, priority } = payload && typeof payload === 'object' ? (payload as any) : ({} as any)
const registry = readDesktopConnectionsRegistry()
const id = registryDialConnectionId(connectionId, registry.primary)
@@ -15078,7 +15096,8 @@ ipcMain.handle('hermes:connection:for', async (_event, payload) => {
return connectDesktopProfileRoute(
{ connectionId: id, profile: String(profile ?? '').trim() || 'default' },
- spawnPriority
+ spawnPriority,
+ event.sender
)
})