The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(desktop): reuse ensureMainWindow for second-instance relaunch
kshitijk4poor5e8f97c827fc
diff --git a/apps/desktop/electron/main-window-lifecycle.test.ts b/apps/desktop/electron/main-window-lifecycle.test.tsindex 9e13c452206..1284f5a3039 100644--- a/apps/desktop/electron/main-window-lifecycle.test.ts+++ b/apps/desktop/electron/main-window-lifecycle.test.ts@@ -2,13 +2,7 @@ import assert from 'node:assert/strict'import { test } from 'vitest'-import {- activateWindow,- decideSecondInstanceAction,- ensureMainWindow,- shouldQuitOnAllClosed,- shouldQuitOnLastChatClosed-} from './main-window-lifecycle'+import { activateWindow, ensureMainWindow, shouldQuitOnLastChatClosed } from './main-window-lifecycle'test('recreates a destroyed primary window without focusing it', () => {const destroyedWindow = {@@ -77,29 +71,6 @@ test('leaves live-window focus to deep-link delivery', () => {})})-// Regression for #130810: closing the last window must quit on-// Windows/Linux (never strand a windowless single-instance lock holder),-// while macOS stays in the Dock unless handing off to a detached script.-test('window-all-closed quits off macOS and only on handoff for darwin', () => {- assert.equal(shouldQuitOnAllClosed('win32', false), true)- assert.equal(shouldQuitOnAllClosed('linux', false), true)- assert.equal(shouldQuitOnAllClosed('darwin', false), false)- assert.equal(shouldQuitOnAllClosed('darwin', true), true)-})--// Regression for #130810: a second launch re-creates a destroyed primary,-// defers pre-ready (whenReady boot owns the first window), and activates a-// live one instead of silently exiting.-test('second-instance routes destroyed/missing/live primaries', () => {- assert.equal(decideSecondInstanceAction({ isDestroyed: () => true }, true), 'create')- assert.equal(decideSecondInstanceAction(null, true), 'create')- assert.equal(decideSecondInstanceAction(undefined, true), 'create')- assert.equal(decideSecondInstanceAction({ isDestroyed: () => true }, false), 'defer')- assert.equal(decideSecondInstanceAction(null, false), 'defer')- assert.equal(decideSecondInstanceAction({ isDestroyed: () => false }, true), 'activate')- assert.equal(decideSecondInstanceAction({ isDestroyed: () => false }, false), 'activate')-})-// Regression for #130810: an explicit relaunch restores + shows + focuses// (activation), so a minimized or tray-hidden window comes back instead of// flashing the taskbar. This must use show(), not showInactive().diff --git a/apps/desktop/electron/main-window-lifecycle.ts b/apps/desktop/electron/main-window-lifecycle.tsindex 68b3c69d2f9..40051cf57c8 100644--- a/apps/desktop/electron/main-window-lifecycle.ts+++ b/apps/desktop/electron/main-window-lifecycle.ts@@ -3,12 +3,12 @@ type MainWindowLike = {}type ActivatableWindow = MainWindowLike & {- isMinimized?: () => boolean- isVisible?: () => boolean- isFocused?: () => boolean- restore?: () => void- show?: () => void- focus?: () => void+ isMinimized: () => boolean+ isVisible: () => boolean+ isFocused: () => boolean+ restore: () => void+ show: () => void+ focus: () => void}type EnsureMainWindowOptions<T extends MainWindowLike> = {@@ -36,18 +36,6 @@ export function ensureMainWindow<T extends MainWindowLike>(}}-/**- * Whether `window-all-closed` must quit the app (#130810).- *- * Non-macOS platforms never keep a windowless app alive: closing the last- * window IS quitting. macOS keeps the Dock process alive by convention,- * except while handing off to a detached updater/swap/uninstall script —- * there the process must die so the script's PID-wait can proceed.- */-export function shouldQuitOnAllClosed(platform: NodeJS.Platform | string, isQuittingForHandoff: boolean): boolean {- return platform !== 'darwin' || isQuittingForHandoff === true-}-/*** Whether the last-chat-window `closed` fallback must quit the app (#130810).*@@ -104,61 +92,17 @@ export function activateWindow(window: ActivatableWindow | null | undefined): voreturn}- try {- if (typeof window.isMinimized === 'function' && window.isMinimized()) {- window.restore?.()- }- } catch {- // A window dying mid-restore must not break the relaunch path.+ if (window.isMinimized()) {+ window.restore()}// Re-read visibility after the restore: a tray-hidden window reports// invisible until it is shown.- let visible = true-- try {- visible = typeof window.isVisible === 'function' ? window.isVisible() : true- } catch {- visible = false- }-- if (!visible) {- try {- window.show?.()- } catch {- // Fall through to the focus attempt below.- }+ if (!window.isVisible()) {+ window.show()}- try {- if (typeof window.isFocused === 'function') {- if (!window.isFocused()) {- window.focus?.()- }- } else {- window.focus?.()- }- } catch {- // Best effort: the window is at least visible again.- }-}--export type SecondInstanceDecision = 'create' | 'activate' | 'defer'--/**- * Pure second-instance routing (#130810): a destroyed/missing primary is- * re-created once the app is ready (a pre-ready relaunch defers to the- * pending `whenReady` boot, which creates the first window anyway); a live- * primary is activated (restore + show + focus). Callers log the decision so- * a silent second exit stays diagnosable in desktop.log.- */-export function decideSecondInstanceAction(- window: MainWindowLike | null | undefined,- isReady: boolean-): SecondInstanceDecision {- if (!window || window.isDestroyed()) {- return isReady ? 'create' : 'defer'+ if (!window.isFocused()) {+ window.focus()}-- return 'activate'}diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex 43d60075c50..f2e76606440 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -362,9 +362,7 @@ import { registerMachineProfile } from './machine-profile'import { createMainProcessLagWatchdog } from './main-process-lag-watchdog'import {activateWindow as activateRestoredWindow,- decideSecondInstanceAction,ensureMainWindow,- shouldQuitOnAllClosed,shouldQuitOnLastChatClosed} from './main-window-lifecycle'import {@@ -15344,7 +15342,7 @@ function createWindow() {// A prevented close (tray absorb, active-work "Keep Running") leaves the// window alive, so the app is not quitting: keep the latch clear so a- // later close still quites cleanly (#130810).+ // later close still quits cleanly (#130810).if (event.defaultPrevented) {return}@@ -19396,39 +19394,17 @@ if (!isPrimaryInstance) {}// #130810: a second Start-menu / shortcut / Hermes.exe launch must never- // silently exit. Restore a live (minimized or tray-hidden) primary with- // activation, or re-create it when it was destroyed — and log the- // decision so a future silent exit stays diagnosable in desktop.log.- const decision = decideSecondInstanceAction(mainWindow, app.isReady())- const destroyed = !mainWindow || mainWindow.isDestroyed()-- rememberLog(- `[second-instance] relaunch (deepLink=${url ? 'yes' : 'no'} decision=${decision} ` +- `mainWindow=${destroyed ? 'destroyed' : 'live'} ` +- `minimized=${!destroyed && typeof mainWindow.isMinimized === 'function' ? mainWindow.isMinimized() : 'n/a'} ` +- `visible=${!destroyed && typeof mainWindow.isVisible === 'function' ? mainWindow.isVisible() : 'n/a'})`- )-- if (decision === 'create') {- createWindow()-- return- }-- if (decision === 'defer') {- // Pre-ready: the pending whenReady boot creates the first window.- return- }-- // A live primary: deep-link delivery already restored/shown/focused it- // above; a plain relaunch must activate it (restore + show + focus) so a- // tray-hidden or minimized window comes back instead of flashing the- // taskbar. Unlike the ambient focusWindow (showInactive per #83998), an- // explicit relaunch owns the foreground. Re-asserting activation after a- // deep link is idempotent (a visible + focused window is a no-op) and- // covers the queued-delivery case, where handleDeepLink stored the payload- // without showing the window yet.- activateRestoredWindow(mainWindow)+ // silently exit. Log it so a future silent exit stays diagnosable, then+ // restore a live primary with activation (restore + show + focus), or+ // re-create it when it was destroyed.+ rememberLog(`[second-instance] relaunch (deepLink=${url ? 'yes' : 'no'})`)+ ensureMainWindow(mainWindow, {+ isReady: app.isReady(),+ createWindow,+ focusWindow: activateRestoredWindow,+ // deep-link delivery focuses a live window after its renderer is ready.+ focusExisting: !url+ })})}@@ -19906,9 +19882,7 @@ app.on('window-all-closed', () => {// the bundle and relaunch — without this the script's PID-wait spins to its// full timeout and the user is left with an invisible app (or an uninstall// that appears to do nothing).- // #130810: on Windows/Linux closing the last window IS quitting — the app- // must not linger windowless holding the single-instance lock.- if (shouldQuitOnAllClosed(process.platform, isQuittingForHandoff)) {+ if (process.platform !== 'darwin' || isQuittingForHandoff) {app.quit()}})