The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): keep the popped-out pet wholly inside a display work area
brooklyn!412d0b7bb034
diff --git a/apps/desktop/electron/pet-overlay.test.ts b/apps/desktop/electron/pet-overlay.test.tsindex 3a7be2a541a..e012a48a9c2 100644--- a/apps/desktop/electron/pet-overlay.test.ts+++ b/apps/desktop/electron/pet-overlay.test.ts@@ -9,10 +9,11 @@ import assert from 'node:assert/strict'import { test } from 'vitest'-import { resolvePetOverlayBounds } from './pet-overlay'+import { clampRectToWorkArea, 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 } },@@ -56,16 +57,65 @@ test('a spot on any connected display is used as-is, even if not the anchor dispassert.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 }+// ─── partly off-screen: clamp the whole rect in (#85092) ─────────────────────++const within = (r, a) => r.x >= a.x && r.y >= a.y && r.x + r.width <= a.x + a.width && r.y + r.height <= a.y + a.height++test('a first pop-out that is mostly off-screen is pulled wholly onto the display', () => {+ // The #85092 repro: 1707x1067 logical work area (2560x1600 @ 150%), overlay+ // spawned from a pet near the bottom-right corner with only a 49px sliver+ // visible. That passes a 48px-overlap rule, so it must be clamped, not trusted.+ const area = { x: 0, y: 0, width: 1707, height: 1067 }+ const resolved = resolvePetOverlayBounds({ x: 1658, y: 806, width: 292, height: 408 }, [{ workArea: area }], null)++ assert.deepEqual(resolved, { x: 1707 - 292, y: 1067 - 408, width: 292, height: 408 })+})++test('a sliver of overlap on any side is clamped to the nearest edge, not re-centered', () => {+ for (const sliver of [+ { x: -270, y: 150, width: 300, height: 400 },+ { x: 200, y: -390, width: 300, height: 400 },+ { x: 1360, y: 700, width: 300, height: 400 }+ ]) {+ const resolved = resolvePetOverlayBounds(sliver, LAPTOP, ANCHOR_ON_LAPTOP)++ assert.ok(within(resolved, LAPTOP[0].workArea), JSON.stringify(resolved))+ assert.equal(resolved.width, 300)+ assert.equal(resolved.height, 400)+ }+})++test('a rect straddling two displays is clamped into the one it overlaps most', () => {+ // 250px on the laptop, 50px on the external → stays on the laptop.+ const straddle = { x: 1116, y: 200, width: 300, height: 400 }+ const resolved = resolvePetOverlayBounds(straddle, LAPTOP_PLUS_EXTERNAL, ANCHOR_ON_EXTERNAL)- const resolved = resolvePetOverlayBounds(sliver, LAPTOP, ANCHOR_ON_LAPTOP)+ assert.deepEqual(resolved, { x: 1366 - 300, y: 200, width: 300, height: 400 })+})- assert.notDeepEqual(resolved, sliver)- assert.equal(resolved.x, Math.round((1366 - 300) / 2))- assert.equal(resolved.y, Math.round((728 - 400) / 2))+test('clampRectToWorkArea keeps any rect wholly inside the work area', () => {+ const area = { x: -1920, y: 25, width: 1920, height: 1055 }++ for (const rect of [+ { x: -5000, y: -5000, width: 300, height: 400 },+ { x: 5000, y: 5000, width: 300, height: 400 },+ { x: -1000, y: 500, width: 300, height: 400 },+ { x: -1920, y: 25, width: 4000, height: 3000 },+ { x: -100.6, y: 900.4, width: 299.5, height: 400.2 }+ ]) {+ const clamped = clampRectToWorkArea(rect, area)++ assert.ok(within(clamped, area), JSON.stringify(clamped))+ assert.ok([clamped.x, clamped.y, clamped.width, clamped.height].every(Number.isInteger))+ }++ // Already inside: unchanged.+ assert.deepEqual(clampRectToWorkArea({ x: -1000, y: 500, width: 300, height: 400 }, area), {+ x: -1000,+ y: 500,+ width: 300,+ height: 400+ })})// ─── off-screen re-home ────────────────────────────────────────────────────diff --git a/apps/desktop/electron/pet-overlay.ts b/apps/desktop/electron/pet-overlay.tsindex d9b0901741a..bfa227dfebf 100644--- a/apps/desktop/electron/pet-overlay.ts+++ b/apps/desktop/electron/pet-overlay.ts@@ -11,9 +11,9 @@* 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.+ * completely unfindable: the pet "vanishes." A window that only *mostly* left+ * the screen is just as lost, so the overlay is kept wholly inside a work area+ * rather than trusting a sliver of overlap (#85092).*/import { matchingWorkArea } from './window-state'@@ -22,18 +22,33 @@ import { matchingWorkArea } from './window-state'// enforced in main.ts's spawnPetOverlayWindow.const MIN_SIZE = 80+const clamp = (v, lo, hi) => Math.max(lo, Math.min(v, hi))+/**- * 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.+ * Keep the WHOLE rect inside `workArea`: size is capped to the work area, then+ * the origin is clamped so no edge crosses it.+ */+export function clampRectToWorkArea(rect, workArea) {+ const width = clamp(Math.round(rect.width), MIN_SIZE, Math.round(workArea.width))+ const height = clamp(Math.round(rect.height), MIN_SIZE, Math.round(workArea.height))++ return {+ x: clamp(Math.round(rect.x), workArea.x, workArea.x + workArea.width - width),+ y: clamp(Math.round(rect.y), workArea.y, workArea.y + workArea.height - height),+ width,+ height+ }+}++/**+ * Resolve where the pet-overlay window may sit. A `requested` screen rect that+ * overlaps any connected display is clamped wholly into the work area it+ * overlaps most (the display Electron's `screen.getDisplayMatching` picks).*- * 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.+ * A rect on no display at all (saved on a since-unplugged monitor) 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.** Returns null for missing/garbage input (main falls back to its defaults);* returns `requested` unchanged when there is nothing to validate against.@@ -55,8 +70,10 @@ export function resolvePetOverlayBounds(requested, displays, anchor) {return requested}- if (matchingWorkArea({ x, y, width, height }, list)) {- return requested+ const overlapping = matchingWorkArea({ x, y, width, height }, list, 1)++ if (overlapping) {+ return clampRectToWorkArea(requested, overlapping)}const area = workAreaForAnchor(list, anchor)@@ -65,14 +82,13 @@ export function resolvePetOverlayBounds(requested, displays, anchor) {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)))+ const { width: w, height: h } = clampRectToWorkArea(requested, area)return {- x: Math.round(area.x + (area.width - cappedWidth) / 2),- y: Math.round(area.y + (area.height - cappedHeight) / 2),- width: cappedWidth,- height: cappedHeight+ x: Math.round(area.x + (area.width - w) / 2),+ y: Math.round(area.y + (area.height - h) / 2),+ width: w,+ height: h}}@@ -89,6 +105,7 @@ function workAreaForAnchor(displays, anchor) {) {const cx = anchor.x + anchor.width / 2const cy = anchor.y + anchor.height / 2+const containing = displays.find(({ workArea: a }) => {if (!a) {return falsediff --git a/apps/desktop/electron/window-state.ts b/apps/desktop/electron/window-state.tsindex afd991b37f5..ba780156abd 100644--- a/apps/desktop/electron/window-state.ts+++ b/apps/desktop/electron/window-state.ts@@ -52,9 +52,9 @@ function sanitizeWindowState(raw?: any): SanitizedWindowState | null {// Return the work area with the largest meaningful overlap with `bounds`.// `displays` is Electron's screen.getAllDisplays() shape. A small sliver does-// not count: the saved position is only trusted when at least MIN_VISIBLE is+// not count: the saved position is only trusted when at least `minVisible` is// reachable on both axes.-function matchingWorkArea(bounds, displays) {+function matchingWorkArea(bounds, displays, minVisible = MIN_VISIBLE) {if (!Array.isArray(displays)) {return null}@@ -70,7 +70,7 @@ function matchingWorkArea(bounds, displays) {const x = Math.min(bounds.x + bounds.width, a.x + a.width) - Math.max(bounds.x, a.x)const y = Math.min(bounds.y + bounds.height, a.y + a.height) - Math.max(bounds.y, a.y)- if (x < MIN_VISIBLE || y < MIN_VISIBLE) {+ if (x < minVisible || y < minVisible) {continue}