Skip to content

The agent that grows with you

fix(desktop): keep popped-out pet on-screen after a monitor unplug

Scott Lorien174ea05f5440
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 5cb41ab1ebb..49850cfe52b 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -321,6 +321,7 @@ import { LEGACY_OAUTH_PARTITION, resolveOauthPartition } from './oauth-partition
import { mintGatewayWsTicket as mintOauthGatewayWsTicket, requestWithOauthFallback } from './oauth-rest-request'
import { wireOauthSessionResponse } from './oauth-session-response'
import { createParentStartMarkerResolver, parentWatchdogEnv } from './parent-process-identity'
+import { resolvePetOverlayBounds } from './pet-overlay'
import { registerPetOverlayIpc } from './pet-overlay-ipc'
import {
pendingNotice as pendingPluginCompatNotice,
@@ -14407,6 +14408,49 @@ function closePetOverlay() {
petOverlayWindow = null
}
+// Re-home the popped-out pet after a display change: if the overlay still sits
+// on a connected display it stays put; otherwise it is re-centered on the main
+// window's display (see resolvePetOverlayBounds). The corrected spot is pushed
+// back to the renderer via the existing 'bounds' control channel, which it
+// persists for the next pop-out/restart.
+function rehomePetOverlay() {
+ if (!petOverlayWindow || petOverlayWindow.isDestroyed()) {
+ return
+ }
+
+ let anchor = null
+
+ try {
+ if (mainWindow && !mainWindow.isDestroyed()) {
+ anchor = mainWindow.getContentBounds()
+ }
+ } catch {
+ // Resolve falls back to the primary display when the anchor is unknown.
+ }
+
+ const current = petOverlayWindow.getBounds()
+ const resolved = resolvePetOverlayBounds(current, screen.getAllDisplays(), anchor)
+
+ if (!resolved) {
+ return
+ }
+
+ if (
+ resolved.x === current.x &&
+ resolved.y === current.y &&
+ resolved.width === current.width &&
+ resolved.height === current.height
+ ) {
+ return
+ }
+
+ petOverlayWindow.setBounds(resolved)
+
+ if (mainWindow && !mainWindow.isDestroyed()) {
+ mainWindow.webContents.send('hermes:pet-overlay:control', { type: 'bounds', bounds: resolved })
+ }
+}
+
// ── HUD mode ────────────────────────────────────────────────────────────────
//
// The chrome-free floating chat: a transparent, frameless, always-on-top
@@ -18798,6 +18842,18 @@ app.whenReady().then(() => {
screen.on('display-removed', reposition)
}
+ // The popped-out pet must never be stranded on a disconnected display: when
+ // the topology changes, pull an off-screen overlay back onto the display
+ // that holds the main window (and persist the corrected spot). Unlike the
+ // wake indicator this applies on every platform — the pet overlay exists
+ // everywhere, and rehomePetOverlay is a cheap no-op while the pet is in the
+ // window or still on-screen.
+ screen.on('display-added', rehomePetOverlay)
+
+ screen.on('display-metrics-changed', rehomePetOverlay)
+
+ screen.on('display-removed', rehomePetOverlay)
+
// A hard crash can interrupt the in-memory restore loop after exact remote
// serves were drained. The owner-only recovery journal survives that crash;
// its worker waits for the install marker to clear, then reopens every scope
diff --git a/apps/desktop/electron/pet-overlay-ipc.ts b/apps/desktop/electron/pet-overlay-ipc.ts
index 9738f4e8e04..e2ed8546edf 100644
--- a/apps/desktop/electron/pet-overlay-ipc.ts
+++ b/apps/desktop/electron/pet-overlay-ipc.ts
@@ -1,6 +1,8 @@
// IPC surface for the pop-out pet overlay (mascot window). Extracted from
// main.ts; window handles stay injected because main.ts owns their lifecycle.
-import { type BrowserWindow, ipcMain } from 'electron'
+import { type BrowserWindow, ipcMain, screen } from 'electron'
+
+import { resolvePetOverlayBounds } from './pet-overlay'
export interface PetOverlayIpcDeps {
getMainWindow: () => BrowserWindow | null
@@ -40,6 +42,26 @@ export function registerPetOverlayIpc({
// Fall back to raw bounds if the window geometry is unavailable.
}
+ // A remembered/dragged spot is only trusted while it still lands on a
+ // connected display — otherwise the transparent, non-activating overlay
+ // would open off-screen (e.g. saved on an external monitor that has since
+ // been unplugged) and the pet would be unfindable. Re-center on the main
+ // window's display instead, and echo the corrected bounds so the renderer
+ // persists the on-screen spot (self-healing).
+ if (screenBounds) {
+ let anchor = null
+
+ try {
+ if (mainWindow && !mainWindow.isDestroyed()) {
+ anchor = mainWindow.getContentBounds()
+ }
+ } catch {
+ // Resolve falls back to the primary display when the anchor is unknown.
+ }
+
+ screenBounds = resolvePetOverlayBounds(screenBounds, screen.getAllDisplays(), anchor) ?? screenBounds
+ }
+
openPetOverlay(screenBounds)
return { ok: true, bounds: screenBounds }
diff --git a/apps/desktop/electron/pet-overlay.test.ts b/apps/desktop/electron/pet-overlay.test.ts
new file mode 100644
index 00000000000..3a7be2a541a
--- /dev/null
+++ b/apps/desktop/electron/pet-overlay.test.ts
@@ -0,0 +1,143 @@
+/**
+ * Unit tests for the pet-overlay geometry helpers. These cover the logic that
+ * protects the popped-out pet: a remembered spot on a since-unplugged monitor
+ * must never strand the overlay off-screen (the "pet vanished" bug), and a
+ * live re-home must leave an on-screen pet alone.
+ */
+
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { resolvePetOverlayBounds } from './pet-overlay'
+
+// A laptop panel left behind after a bigger external monitor is unplugged.
+const LAPTOP = [{ workArea: { x: 0, y: 0, width: 1366, height: 728 } }]
+// External monitor to the right of the laptop panel, now disconnected.
+const LAPTOP_PLUS_EXTERNAL = [
+ { workArea: { x: 0, y: 0, width: 1366, height: 728 } },
+ { workArea: { x: 1366, y: 0, width: 1920, height: 1040 } }
+]
+
+const ANCHOR_ON_LAPTOP = { x: 100, y: 80, width: 1200, height: 700 }
+const ANCHOR_ON_EXTERNAL = { x: 1400, y: 100, width: 1200, height: 700 }
+
+const PET_BOUNDS = { x: 200, y: 150, width: 300, height: 400 }
+
+// ─── sanity / garbage ──────────────────────────────────────────────────────
+
+test('resolvePetOverlayBounds returns null for missing or garbage input', () => {
+ assert.equal(resolvePetOverlayBounds(null, LAPTOP, ANCHOR_ON_LAPTOP), null)
+ assert.equal(resolvePetOverlayBounds(undefined, LAPTOP, ANCHOR_ON_LAPTOP), null)
+ assert.equal(
+ resolvePetOverlayBounds({ x: NaN, y: 0, width: 100, height: 100 }, LAPTOP, ANCHOR_ON_LAPTOP),
+ null
+ )
+ assert.equal(
+ resolvePetOverlayBounds({ x: 0, y: 0, width: 'wide', height: 100 }, LAPTOP, ANCHOR_ON_LAPTOP),
+ null
+ )
+})
+
+test('resolvePetOverlayBounds returns requested unchanged with no displays to validate against', () => {
+ assert.equal(resolvePetOverlayBounds(PET_BOUNDS, null, ANCHOR_ON_LAPTOP), PET_BOUNDS)
+ assert.equal(resolvePetOverlayBounds(PET_BOUNDS, [], ANCHOR_ON_LAPTOP), PET_BOUNDS)
+})
+
+// ─── on-screen trust ───────────────────────────────────────────────────────
+
+test('an on-screen saved spot is used as-is', () => {
+ assert.deepEqual(resolvePetOverlayBounds(PET_BOUNDS, LAPTOP, ANCHOR_ON_LAPTOP), PET_BOUNDS)
+})
+
+test('a spot on any connected display is used as-is, even if not the anchor display', () => {
+ const onExternal = { x: 1400, y: 200, width: 300, height: 400 }
+
+ assert.deepEqual(resolvePetOverlayBounds(onExternal, LAPTOP_PLUS_EXTERNAL, ANCHOR_ON_LAPTOP), onExternal)
+})
+
+test('a spot with only a sliver of overlap (below MIN_VISIBLE) is treated as off-screen', () => {
+ // 300px-wide window parked so only 30px of it is inside the laptop panel —
+ // less than the 48px MIN_VISIBLE the main window's restore path trusts.
+ const sliver = { x: -270, y: 150, width: 300, height: 400 }
+
+ const resolved = resolvePetOverlayBounds(sliver, LAPTOP, ANCHOR_ON_LAPTOP)
+
+ assert.notDeepEqual(resolved, sliver)
+ assert.equal(resolved.x, Math.round((1366 - 300) / 2))
+ assert.equal(resolved.y, Math.round((728 - 400) / 2))
+})
+
+// ─── off-screen re-home ────────────────────────────────────────────────────
+
+test('a spot remembered on a since-unplugged external monitor is re-centered on the anchor display', () => {
+ // Saved while the external was connected (x 1366+); only the laptop remains.
+ const stale = { x: 1500, y: 400, width: 300, height: 400 }
+
+ const resolved = resolvePetOverlayBounds(stale, LAPTOP, ANCHOR_ON_LAPTOP)
+
+ assert.deepEqual(resolved, {
+ x: Math.round((1366 - 300) / 2),
+ y: Math.round((728 - 400) / 2),
+ width: 300,
+ height: 400
+ })
+})
+
+test('re-home centers on the display holding the anchor (main window), not the primary', () => {
+ const stale = { x: -800, y: 400, width: 300, height: 400 }
+
+ const resolved = resolvePetOverlayBounds(stale, LAPTOP_PLUS_EXTERNAL, ANCHOR_ON_EXTERNAL)
+
+ assert.deepEqual(resolved, {
+ x: Math.round(1366 + (1920 - 300) / 2),
+ y: Math.round((1040 - 400) / 2),
+ width: 300,
+ height: 400
+ })
+})
+
+test('re-home falls back to the primary display when the anchor is missing', () => {
+ const stale = { x: 1500, y: 400, width: 300, height: 400 }
+
+ const resolved = resolvePetOverlayBounds(stale, LAPTOP, null)
+
+ assert.deepEqual(resolved, {
+ x: Math.round((1366 - 300) / 2),
+ y: Math.round((728 - 400) / 2),
+ width: 300,
+ height: 400
+ })
+})
+
+test('re-homed size is capped to the target work area', () => {
+ const huge = { x: 1500, y: 400, width: 2000, height: 1500 }
+
+ const resolved = resolvePetOverlayBounds(huge, LAPTOP, ANCHOR_ON_LAPTOP)
+
+ assert.deepEqual(resolved, {
+ x: 0,
+ y: 0,
+ width: 1366,
+ height: 728
+ })
+})
+
+// ─── live re-home (display unplugged while the pet is popped out) ──────────
+
+test('a still-on-screen pet is left exactly where it is', () => {
+ assert.deepEqual(resolvePetOverlayBounds(PET_BOUNDS, LAPTOP, ANCHOR_ON_LAPTOP), PET_BOUNDS)
+})
+
+test('a pet stranded by an unplugged display is pulled back onto the anchor display', () => {
+ const stranded = { x: 1500, y: 400, width: 300, height: 400 }
+
+ const resolved = resolvePetOverlayBounds(stranded, LAPTOP, ANCHOR_ON_LAPTOP)
+
+ assert.deepEqual(resolved, {
+ x: Math.round((1366 - 300) / 2),
+ y: Math.round((728 - 400) / 2),
+ width: 300,
+ height: 400
+ })
+})
diff --git a/apps/desktop/electron/pet-overlay.ts b/apps/desktop/electron/pet-overlay.ts
new file mode 100644
index 00000000000..d9b0901741a
--- /dev/null
+++ b/apps/desktop/electron/pet-overlay.ts
@@ -0,0 +1,106 @@
+/**
+ * Pure geometry helpers for the popped-out pet overlay — deciding where its
+ * window may open and where it must be re-homed when the display topology
+ * changes. Side-effect-free so the on-screen validation is unit-testable
+ * without booting Electron; main.ts owns the live `screen` displays, the
+ * anchor window, and the overlay window itself.
+ *
+ * The bug this exists for: the renderer remembers the overlay's absolute
+ * screen position (localStorage hermes.desktop.pet-overlay-bounds.v1) and
+ * reuses it verbatim on the next pop-out / app restart. If that spot was on an
+ * external monitor that has since been unplugged, the overlay is created
+ * off-screen — and because it is a transparent, frameless, non-activating
+ * always-on-top panel hidden from Mission Control, an off-screen overlay is
+ * completely unfindable: the pet "vanishes." The main window's restore path
+ * already solves this class of problem (see window-state.ts `matchingWorkArea`); the
+ * overlay must apply the same rule, plus re-home itself while open.
+ */
+
+import { matchingWorkArea } from './window-state'
+
+// Below this, a pet window is too small to be useful — mirrors the floor
+// enforced in main.ts's spawnPetOverlayWindow.
+const MIN_SIZE = 80
+
+/**
+ * Resolve where the pet-overlay window should open. Trusts `requested` screen
+ * bounds when they still intersect some connected display (the same
+ * ≥ MIN_VISIBLE overlap rule the main window's restore path uses), so a spot
+ * remembered on a since-unplugged monitor can never strand the pet off-screen.
+ *
+ * When the requested spot is off-screen the window is re-centered on the
+ * display holding `anchor` (the main window's content bounds — where the user
+ * actually is), falling back to the primary display when the anchor is
+ * missing. Size is capped to the target display's work area so a spot saved on
+ * a since-disconnected bigger monitor can't exceed any screen the user now
+ * has.
+ *
+ * Returns null for missing/garbage input (main falls back to its defaults);
+ * returns `requested` unchanged when there is nothing to validate against.
+ */
+export function resolvePetOverlayBounds(requested, displays, anchor) {
+ if (!requested) {
+ return null
+ }
+
+ const { x, y, width, height } = requested
+
+ if (![x, y, width, height].every(Number.isFinite)) {
+ return null
+ }
+
+ const list = Array.isArray(displays) ? displays : []
+
+ if (!list.length) {
+ return requested
+ }
+
+ if (matchingWorkArea({ x, y, width, height }, list)) {
+ return requested
+ }
+
+ const area = workAreaForAnchor(list, anchor)
+
+ if (!area) {
+ return requested
+ }
+
+ const cappedWidth = Math.max(MIN_SIZE, Math.min(Math.round(width), Math.round(area.width)))
+ const cappedHeight = Math.max(MIN_SIZE, Math.min(Math.round(height), Math.round(area.height)))
+
+ return {
+ x: Math.round(area.x + (area.width - cappedWidth) / 2),
+ y: Math.round(area.y + (area.height - cappedHeight) / 2),
+ width: cappedWidth,
+ height: cappedHeight
+ }
+}
+
+// The work area of the display whose bounds contain the anchor's center (the
+// display the main window sits on), or the first display when the anchor is
+// missing/unknown. Null when there are no usable displays at all.
+function workAreaForAnchor(displays, anchor) {
+ if (
+ anchor &&
+ Number.isFinite(anchor.x) &&
+ Number.isFinite(anchor.y) &&
+ Number.isFinite(anchor.width) &&
+ Number.isFinite(anchor.height)
+ ) {
+ const cx = anchor.x + anchor.width / 2
+ const cy = anchor.y + anchor.height / 2
+ const containing = displays.find(({ workArea: a }) => {
+ if (!a) {
+ return false
+ }
+
+ return cx >= a.x && cx < a.x + a.width && cy >= a.y && cy < a.y + a.height
+ })
+
+ if (containing?.workArea) {
+ return containing.workArea
+ }
+ }
+
+ return displays.find(({ workArea: a }) => a)?.workArea ?? null
+}