Skip to content

The agent that grows with you

fix(desktop): suppress the pet overlay pop-in echo on quit so the popped-out state survives restart (#55920)

Brooklyn Nicholsonb415d38c2535
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 5bc169afb10..a94264fb259 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -404,7 +404,7 @@ import { wireOauthSessionResponse } from './oauth-session-response'
import { listWindowsProcesses, reapPackageRootedProcesses } from './package-process-reap'
import { createParentStartMarkerResolver, parentWatchdogEnv } from './parent-process-identity'
import { bundledPayload, installIdForRoot, type PayloadInfo } from './payload-backend'
-import { petOverlayClickThrough } from './pet-overlay'
+import { petOverlayClickThrough, shouldPopInOnOverlayClosed } from './pet-overlay'
import { placePetOverlay, registerPetOverlayIpc } from './pet-overlay-ipc'
import {
buildRegistryProfileRoutes,
@@ -14264,6 +14264,14 @@ registerMachineProfile()
// pushes pet state over IPC (hermes:pet-overlay:state); the overlay just renders
// it. Control flows back (pop-in, composer submit) via hermes:pet-overlay:control.
let petOverlayWindow = null
+// Set once the app is really exiting (before-quit, or the primary window's
+// close on Windows/Linux where closing it IS quitting). Distinct from
+// isQuittingForHandoff, which is specific to the update/swap hand-off and is
+// reset when a hand-off is aborted: a quit latch must never reset. The pet
+// overlay's 'closed' handler reads it to suppress its pop-in echo so the
+// persisted popped-out flag survives for the next boot's restorePetOverlay
+// (#55920).
+let appQuitting = 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
@@ -14380,9 +14388,10 @@ function spawnPetOverlayWindow(bounds) {
petOverlayClosing = false
// If the overlay went away on its own (e.g. ⌘W), tell the main renderer to
- // pop the pet back in so it doesn't stay hidden. Harmless echo when we're
- // the ones who closed it (popInPet already cleared the active flag).
- if (mainWindow && !mainWindow.isDestroyed()) {
+ // pop the pet back in so it doesn't stay hidden. Never during a quit:
+ // popInPet() persists $petOverlayActive=false, which would wipe the
+ // popped-out state the next boot's restorePetOverlay() needs (#55920).
+ if (shouldPopInOnOverlayClosed({ appQuitting, mainWindowAlive: Boolean(mainWindow && !mainWindow.isDestroyed()) })) {
mainWindow.webContents.send('hermes:pet-overlay:control', { type: 'pop-in' })
}
})
@@ -15388,7 +15397,17 @@ function createWindow() {
bindGeometryPersistence(mainWindow, schedulePersistWindowState)
mainWindow.on('maximize', schedulePersistWindowState)
mainWindow.on('unmaximize', schedulePersistWindowState)
- mainWindow.on('close', () => schedulePersistWindowState.flush())
+ mainWindow.on('close', () => {
+ schedulePersistWindowState.flush()
+ // 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
+ // 'closed' handler echoes pop-in and wipes the persisted popped-out state
+ // the next boot needs (#55920).
+ if (!IS_MAC) {
+ appQuitting = true
+ }
+ })
// the closed wrapper remains truthy, so clear only the window this callback owns.
mainWindow.on('closed', () => {
@@ -19567,6 +19586,11 @@ function registerChatWindow(window: BrowserWindow) {
}
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.
if (heldQuitForActiveWork(event)) {
diff --git a/apps/desktop/electron/pet-overlay.test.ts b/apps/desktop/electron/pet-overlay.test.ts
index a1949af284e..255a3ec95db 100644
--- a/apps/desktop/electron/pet-overlay.test.ts
+++ b/apps/desktop/electron/pet-overlay.test.ts
@@ -9,7 +9,7 @@ import assert from 'node:assert/strict'
import { test } from 'vitest'
-import { clampRectToWorkArea, petOverlayClickThrough, resolvePetOverlayBounds } from './pet-overlay'
+import { clampRectToWorkArea, petOverlayClickThrough, resolvePetOverlayBounds, shouldPopInOnOverlayClosed } from './pet-overlay'
test('petOverlayClickThrough is off on Linux, where forward:true never re-arms the sprite', () => {
assert.equal(petOverlayClickThrough('darwin'), true)
@@ -17,6 +17,28 @@ test('petOverlayClickThrough is off on Linux, where forward:true never re-arms t
assert.equal(petOverlayClickThrough('linux'), false)
})
+// ─── quit-path pop-in suppression (#55920) ──────────────────────────────────
+//
+// The overlay's 'closed' handler echoes pop-in so a pet whose window went away
+// on its own (⌘W) comes back inside the app. During a quit that echo is a bug:
+// popInPet() persists $petOverlayActive=false, so restorePetOverlay() on the
+// next boot skips restoration and the pet silently reverts to in-window.
+
+test('the overlay close echo pops the pet back in while the app is alive', () => {
+ assert.equal(shouldPopInOnOverlayClosed({ appQuitting: false, mainWindowAlive: true }), true)
+})
+
+test('a quit suppresses the pop-in echo, so the popped-out state survives for the next boot', () => {
+ // before-quit teardown and the non-macOS primary-window close both latch the
+ // quit flag before closePetOverlay() fires the overlay's 'closed' handler.
+ assert.equal(shouldPopInOnOverlayClosed({ appQuitting: true, mainWindowAlive: true }), false)
+})
+
+test('no live main window means no echo, quit or not', () => {
+ assert.equal(shouldPopInOnOverlayClosed({ appQuitting: false, mainWindowAlive: false }), false)
+ assert.equal(shouldPopInOnOverlayClosed({ appQuitting: true, mainWindowAlive: false }), false)
+})
+
// A laptop panel left behind after a bigger external monitor is unplugged.
const LAPTOP = [{ workArea: { x: 0, y: 0, width: 1366, height: 728 } }]
diff --git a/apps/desktop/electron/pet-overlay.ts b/apps/desktop/electron/pet-overlay.ts
index abfe9c501db..c073d348573 100644
--- a/apps/desktop/electron/pet-overlay.ts
+++ b/apps/desktop/electron/pet-overlay.ts
@@ -31,6 +31,28 @@ const clamp = (v, lo, hi) => Math.max(lo, Math.min(v, hi))
*/
export const petOverlayClickThrough = (platform = process.platform) => platform !== 'linux'
+/**
+ * Whether the overlay window's 'closed' handler should tell the main renderer
+ * to pop the pet back in (#55920).
+ *
+ * The overlay going away on its own (⌘W, a display teardown) must echo back so
+ * the pet doesn't stay hidden. But when the app itself is quitting — the
+ * before-quit teardown closes the overlay, and on Windows/Linux so does the
+ * primary window's 'closed' handler — that echo is a bug: it runs popInPet(),
+ * which persists `$petOverlayActive=false`, so restorePetOverlay() on the next
+ * boot skips restoration and the pet silently reverts to in-window. The quit
+ * path must leave the persisted popped-out flag alone.
+ */
+export function shouldPopInOnOverlayClosed({
+ appQuitting,
+ mainWindowAlive
+}: {
+ appQuitting: boolean
+ mainWindowAlive: boolean
+}): boolean {
+ return !appQuitting && mainWindowAlive
+}
+
/**
* Keep the WHOLE rect inside `workArea`: size is capped to the work area, then
* the origin is clamped so no edge crosses it.