Skip to content

The agent that grows with you

fix(desktop): base last-chat-window quit fallback on quitInProgress state (#130810)

JerryLiu36941ffe047f4dc
diff --git a/apps/desktop/electron/main-window-lifecycle.test.ts b/apps/desktop/electron/main-window-lifecycle.test.ts
index 42cf69baa75..9e13c452206 100644
--- a/apps/desktop/electron/main-window-lifecycle.test.ts
+++ b/apps/desktop/electron/main-window-lifecycle.test.ts
@@ -6,7 +6,8 @@ import {
activateWindow,
decideSecondInstanceAction,
ensureMainWindow,
- shouldQuitOnAllClosed
+ shouldQuitOnAllClosed,
+ shouldQuitOnLastChatClosed
} from './main-window-lifecycle'
test('recreates a destroyed primary window without focusing it', () => {
@@ -152,3 +153,187 @@ test('explicit relaunch never touches a destroyed or missing window', () => {
activateWindow(null)
activateWindow(undefined)
})
+
+// Regression for #130810: the last-chat `closed` fallback quits on
+// Windows/Linux when no chat surface remains and no quit is already tearing
+// down. It must not consult the overlay-suppression latch, must stay quiet
+// on macOS, during handoff, with peers left, or mid-quit.
+test('last-chat fallback quits only for a final non-macOS close outside a quit', () => {
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'win32',
+ isQuittingForHandoff: false,
+ remainingChatWindows: 0,
+ quitInProgress: false
+ }),
+ true
+ )
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'linux',
+ isQuittingForHandoff: false,
+ remainingChatWindows: 0,
+ quitInProgress: false
+ }),
+ true
+ )
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'darwin',
+ isQuittingForHandoff: false,
+ remainingChatWindows: 0,
+ quitInProgress: false
+ }),
+ false
+ )
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'win32',
+ isQuittingForHandoff: true,
+ remainingChatWindows: 0,
+ quitInProgress: false
+ }),
+ false
+ )
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'win32',
+ isQuittingForHandoff: false,
+ remainingChatWindows: 1,
+ quitInProgress: false
+ }),
+ false
+ )
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'linux',
+ isQuittingForHandoff: false,
+ remainingChatWindows: 2,
+ quitInProgress: false
+ }),
+ false
+ )
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'win32',
+ isQuittingForHandoff: false,
+ remainingChatWindows: 0,
+ quitInProgress: true
+ }),
+ false
+ )
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'linux',
+ isQuittingForHandoff: false,
+ remainingChatWindows: 0,
+ quitInProgress: true
+ }),
+ false
+ )
+})
+
+// Regression for #130810: an ordinary primary-window close on Windows/Linux
+// sets the overlay-suppression latch (appQuitting = true for #55920) in its
+// `close` handler before `closed` fires. With a hidden helper (Quick Entry,
+// HUD, pet overlay) still alive, `window-all-closed` never fires, so the
+// last-chat fallback is the only quit path — the latch must not block it.
+test('primary close with hidden helper still quits despite overlay-suppression latch', () => {
+ // Mirror main.ts state: overlay latch vs real quit progress are separate.
+ let appQuitting = false
+ let quitInProgress = false
+ const isQuittingForHandoff = false
+
+ // `close`: ordinary close, no active-work guard, not prevented. The primary
+ // window's `close` handler latches overlay suppression on Windows/Linux.
+ const closePrevented = false
+
+ if (!closePrevented) {
+ appQuitting = true
+ }
+
+ // `closed`: the primary chat surface is gone (remaining === 0) but a hidden
+ // helper BrowserWindow keeps `window-all-closed` from firing. No before-quit
+ // has run yet, so no quit is in progress.
+ const remainingChatWindows = 0
+
+ assert.equal(appQuitting, true)
+ assert.equal(quitInProgress, false)
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'win32',
+ isQuittingForHandoff,
+ remainingChatWindows,
+ quitInProgress
+ }),
+ true
+ )
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'linux',
+ isQuittingForHandoff,
+ remainingChatWindows,
+ quitInProgress
+ }),
+ true
+ )
+})
+
+// Regression for #130810: a "Keep Running" answer to the active-work prompt
+// holds the quit (before-quit preventDefault / window-close preventDefault)
+// and must leave quitInProgress false with the overlay latch reset, so a
+// later ordinary close still quits via the fallback.
+test('keep-running leaves a later final close able to quit', () => {
+ let appQuitting = false
+ let quitInProgress = false
+ const isQuittingForHandoff = false
+
+ // First attempt: active-work guard holds the quit. Mirror the fixed
+ // before-quit handler — a held quit resets the overlay latch and never sets
+ // quitInProgress.
+ const heldForActiveWork = true
+
+ if (heldForActiveWork) {
+ appQuitting = false
+ } else {
+ appQuitting = true
+ quitInProgress = true
+ }
+
+ assert.equal(quitInProgress, false)
+ assert.equal(appQuitting, false)
+
+ // The dialog resolves with "Keep Running": no confirm, no re-quit.
+ const quitConfirmedWithActiveWork = false
+ assert.equal(quitConfirmedWithActiveWork, false)
+ assert.equal(quitInProgress, false)
+
+ // Later, work has finished and the user closes the last chat window for
+ // real: ordinary `close` latches overlay suppression, `closed` finds no
+ // chat surfaces left and no quit in flight, so the fallback quits.
+ appQuitting = true
+ const remainingChatWindows = 0
+
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'win32',
+ isQuittingForHandoff,
+ remainingChatWindows,
+ quitInProgress
+ }),
+ true
+ )
+
+ // And once before-quit really proceeds, the fallback stays quiet (the quit
+ // teardown already owns the exit).
+ quitInProgress = true
+ assert.equal(
+ shouldQuitOnLastChatClosed({
+ platform: 'win32',
+ isQuittingForHandoff,
+ remainingChatWindows,
+ quitInProgress
+ }),
+ false
+ )
+})
diff --git a/apps/desktop/electron/main-window-lifecycle.ts b/apps/desktop/electron/main-window-lifecycle.ts
index e1e76e52a1d..68b3c69d2f9 100644
--- a/apps/desktop/electron/main-window-lifecycle.ts
+++ b/apps/desktop/electron/main-window-lifecycle.ts
@@ -48,6 +48,45 @@ export function shouldQuitOnAllClosed(platform: NodeJS.Platform | string, isQuit
return platform !== 'darwin' || isQuittingForHandoff === true
}
+/**
+ * Whether the last-chat-window `closed` fallback must quit the app (#130810).
+ *
+ * Hidden helpers (Quick Entry, HUD, pet overlay) are still BrowserWindows,
+ * so `window-all-closed` never fires while one lingers. This fallback quits
+ * when no chat surface remains. It is keyed on whether a quit is actually in
+ * progress (`quitInProgress`), never on the overlay-suppression latch
+ * (`appQuitting`): on Windows/Linux an ordinary primary-window close sets
+ * `appQuitting = true` in its `close` handler (for #55920 pop-in
+ * suppression) before `closed` fires, so gating on that latch would suppress
+ * the very quit the fallback exists for and strand a windowless
+ * single-instance lock holder.
+ */
+export function shouldQuitOnLastChatClosed({
+ platform,
+ isQuittingForHandoff,
+ remainingChatWindows,
+ quitInProgress
+}: {
+ platform: NodeJS.Platform | string
+ isQuittingForHandoff: boolean
+ remainingChatWindows: number
+ quitInProgress: boolean
+}): boolean {
+ if (platform === 'darwin') {
+ return false
+ }
+
+ if (isQuittingForHandoff) {
+ return false
+ }
+
+ if (quitInProgress) {
+ return false
+ }
+
+ return remainingChatWindows === 0
+}
+
/**
* Explicit-activation restore for a user-invoked relaunch (#130810).
*
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 7740aa935f3..43d60075c50 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -364,7 +364,8 @@ import {
activateWindow as activateRestoredWindow,
decideSecondInstanceAction,
ensureMainWindow,
- shouldQuitOnAllClosed
+ shouldQuitOnAllClosed,
+ shouldQuitOnLastChatClosed
} from './main-window-lifecycle'
import {
assertManagedUpdatePreflightClear,
@@ -14137,6 +14138,15 @@ let petOverlayWindow = null
// persisted popped-out flag survives for the next boot's restorePetOverlay
// (#55920).
let appQuitting = false
+// Whether a quit is actually in progress (#130810). Set in before-quit only
+// once the active-work guard has passed (quit is confirmed/proceeding, not
+// held for confirmation), so the last-chat `closed` fallback can tell "the
+// user closed the last chat surface, quit now" apart from "windows are
+// closing because a quit is already tearing down". Deliberately distinct from
+// appQuitting above, which is also set by the primary window's `close`
+// handler for overlay pop-in suppression and would otherwise suppress the
+// fallback on every ordinary close.
+let quitInProgress = false
// Set while a close is in flight: Electron's close() is async and can be
// aborted on macOS, so the window may still be alive after closePetOverlay().
// openPetOverlay must never reuse (or leave) a closing window — otherwise two
@@ -15329,9 +15339,16 @@ function createWindow() {
bindGeometryPersistence(mainWindow, schedulePersistWindowState)
mainWindow.on('maximize', schedulePersistWindowState)
mainWindow.on('unmaximize', schedulePersistWindowState)
- mainWindow.on('close', () => {
+ mainWindow.on('close', (event: Electron.Event) => {
schedulePersistWindowState.flush()
+ // 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).
+ if (event.defaultPrevented) {
+ return
+ }
+
// On Windows/Linux, closing the primary window IS quitting (the
// window-all-closed handler calls app.quit()). Latch the quit flag here,
// before 'closed' fires closePetOverlay() — otherwise the overlay's
@@ -19725,25 +19742,38 @@ function registerChatWindow(window: BrowserWindow) {
// here (preventDefault), and a multi-window close still has peers left,
// so quitting only when no chat surface remains is safe: it forces the
// ordinary before-quit teardown (backends, SSH, PTYs, watchers) even when
- // window-all-closed is blocked.
- if (!IS_MAC && !isQuittingForHandoff && chatWindows.size === 0 && !appQuitting) {
+ // window-all-closed is blocked. Keyed on quitInProgress, not appQuitting:
+ // an ordinary primary-window close sets appQuitting for overlay pop-in
+ // suppression before `closed` fires, which must not suppress this quit.
+ if (
+ shouldQuitOnLastChatClosed({
+ platform: process.platform,
+ isQuittingForHandoff,
+ remainingChatWindows: chatWindows.size,
+ quitInProgress
+ })
+ ) {
app.quit()
}
})
}
app.on('before-quit', event => {
- // Latch first, before ANY teardown below closes the pet overlay: its
- // 'closed' handler must not echo pop-in during quit, or the persisted
- // popped-out state is wiped and the overlay never restores (#55920).
- appQuitting = true
-
// Runs ahead of every teardown below, so "Keep Running" leaves the app
- // exactly as it was.
+ // exactly as it was: a held quit is not a quit in progress, and the
+ // overlay-suppression latch must not leak into the next close (#130810).
if (heldQuitForActiveWork(event)) {
+ appQuitting = false
return
}
+ // Quit is really proceeding: latch both before ANY teardown below closes
+ // the pet overlay — its 'closed' handler must not echo pop-in during quit,
+ // or the persisted popped-out state is wiped and the overlay never restores
+ // (#55920).
+ appQuitting = true
+ quitInProgress = true
+
minimizeToTray.beginQuit()
mainProcessLagWatchdog.stop()