diff --git a/apps/desktop/src/main/lib/app-state/index.ts b/apps/desktop/src/main/lib/app-state/index.ts index 8c24b6d80e9..b1073216fd6 100644 --- a/apps/desktop/src/main/lib/app-state/index.ts +++ b/apps/desktop/src/main/lib/app-state/index.ts @@ -94,6 +94,10 @@ export async function initAppState(): Promise { console.log(`App state initialized at: ${APP_STATE_PATH}`); } +export function isAppStateInitialized(): boolean { + return _appState !== null; +} + export const appState = new Proxy({} as AppStateDB, { get(_target, prop) { if (!_appState) { diff --git a/apps/desktop/src/main/windows/main.ts b/apps/desktop/src/main/windows/main.ts index bf90d288e0d..ba138a70d2e 100644 --- a/apps/desktop/src/main/windows/main.ts +++ b/apps/desktop/src/main/windows/main.ts @@ -17,7 +17,11 @@ import { env } from "shared/env.shared"; import type { AgentLifecycleEvent } from "shared/notification-types"; import { createIPCHandler } from "trpc-electron/main"; import { productName } from "~/package.json"; -import { appState, pruneWindowScopedState } from "../lib/app-state"; +import { + appState, + isAppStateInitialized, + pruneWindowScopedState, +} from "../lib/app-state"; import { browserManager } from "../lib/browser/browser-manager"; import { attachEditContextMenu } from "../lib/edit-context-menu"; import { createApplicationMenu } from "../lib/menu"; @@ -298,6 +302,10 @@ function snapshotWindowState(window: BrowserWindow): WindowState { /** Persist every open window's bounds + org so they can be restored on relaunch. */ export function persistOpenWindows(): void { + // Quit before initAppState() completed: windows are only created after init, + // so there is nothing to snapshot — appState access below would throw, and + // writing the empty set would clobber the previous session's restore state. + if (!isAppStateInitialized()) return; const persisted: PersistedWindow[] = getAllWindows() .filter((w) => !w.isDestroyed()) .map((w) => ({