Skip to content

The agent that grows with you

fix(desktop): gate stale-fullscreen recovery on save-time provenance

Brooklyn Nicholson7cd77b4f1464
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 00e2cbfe0fa..f4209999562 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -3649,7 +3649,10 @@ function readWindowState() {
// Persist the window's restored (non-maximized) bounds plus its maximized flag.
// getNormalBounds() keeps the pre-maximize size, so un-maximizing next session
-// lands back where the user actually sized the window.
+// lands back where the user actually sized the window. While fullscreen,
+// getNormalBounds() reports the fullscreen bounds with isMaximized=false — the
+// broken transition behind #94319 — so record that provenance and let recovery
+// on the next launch recognize the snapshot instead of guessing from geometry.
function persistWindowState() {
if (!mainWindow || mainWindow.isDestroyed() || mainWindow.isMinimized()) {
return
@@ -3660,7 +3663,18 @@ function persistWindowState() {
fs.mkdirSync(path.dirname(DESKTOP_WINDOW_STATE_PATH), { recursive: true })
writeFileAtomic(
DESKTOP_WINDOW_STATE_PATH,
- JSON.stringify({ x, y, width, height, isMaximized: mainWindow.isMaximized() }, null, 2)
+ JSON.stringify(
+ {
+ x,
+ y,
+ width,
+ height,
+ isMaximized: mainWindow.isMaximized(),
+ boundsCapturedFullScreen: mainWindow.isFullScreen()
+ },
+ null,
+ 2
+ )
)
} catch (err) {
rememberLog(`[window-state] persist failed: ${err?.message || err}`)
diff --git a/apps/desktop/electron/window-state.test.ts b/apps/desktop/electron/window-state.test.ts
index 89f7d12be91..a2efa4e0b94 100644
--- a/apps/desktop/electron/window-state.test.ts
+++ b/apps/desktop/electron/window-state.test.ts
@@ -69,6 +69,15 @@ test('sanitizeWindowState treats isMaximized strictly', () => {
assert.equal(sanitizeWindowState({ width: 1400, height: 900, isMaximized: 'yes' }).isMaximized, false)
})
+test('sanitizeWindowState treats boundsCapturedFullScreen strictly and stays optional', () => {
+ assert.equal(sanitizeWindowState({ width: 1400, height: 900, boundsCapturedFullScreen: true }).boundsCapturedFullScreen, true)
+ assert.equal(
+ sanitizeWindowState({ width: 1400, height: 900, boundsCapturedFullScreen: 'yes' }).boundsCapturedFullScreen,
+ undefined
+ )
+ assert.equal(sanitizeWindowState({ width: 1400, height: 900 }).boundsCapturedFullScreen, undefined)
+})
+
// ─── matchingWorkArea ──────────────────────────────────────────────────────────────
test('matchingWorkArea accepts a window on the primary or a secondary display', () => {
@@ -129,10 +138,45 @@ test('computeWindowOptions does not clamp when displays are unknown', () => {
})
test('computeWindowOptions recovers stale fullscreen normal bounds to a centered windowed size on Windows', () => {
+ const saved = sanitizeWindowState({
+ x: 0,
+ y: 0,
+ width: 1920,
+ height: 1040,
+ isMaximized: false,
+ boundsCapturedFullScreen: true
+ })
+ assert.deepEqual(computeWindowOptions(saved, PRIMARY, 'win32'), { width: 1536, height: 832 })
+})
+
+test('computeWindowOptions recovers a legacy exact-work-area snapshot written before the provenance flag', () => {
+ // Pre-boundsCapturedFullScreen snapshot: fullscreen bounds persisted as
+ // normal, matching the work area to the pixel. Unambiguous, so recovered.
const saved = sanitizeWindowState({ x: 0, y: 0, width: 1920, height: 1040, isMaximized: false })
assert.deepEqual(computeWindowOptions(saved, PRIMARY, 'win32'), { width: 1536, height: 832 })
})
+test('computeWindowOptions recovers a legacy snapshot matching the full display bounds', () => {
+ // A fullscreen window over a hidden taskbar reports the display's full
+ // bounds, not the work area. Still an exact match, so still recovered.
+ const displays = [{ workArea: { x: 0, y: 0, width: 1920, height: 1040 }, bounds: { x: 0, y: 0, width: 1920, height: 1080 } }]
+ const saved = sanitizeWindowState({ x: 0, y: 0, width: 1920, height: 1080, isMaximized: false })
+ assert.deepEqual(computeWindowOptions(saved, displays, 'win32'), { width: 1536, height: 832 })
+})
+
+test('computeWindowOptions preserves a deliberate near-fullscreen normal window near the origin on Windows', () => {
+ // The reviewer's case: a valid normal window the user sized on purpose,
+ // satisfying the old ≥90% + near-origin predicates. It must survive
+ // restore untouched (position clamped inside the work area).
+ const saved = sanitizeWindowState({ x: 0, y: 0, width: 1824, height: 988, isMaximized: false })
+ assert.deepEqual(computeWindowOptions(saved, PRIMARY, 'win32'), {
+ width: 1824,
+ height: 988,
+ x: 0,
+ y: 0
+ })
+})
+
test('computeWindowOptions preserves deliberate near-fullscreen normal bounds outside Windows', () => {
const saved = sanitizeWindowState({ x: 0, y: 0, width: 1920, height: 1040, isMaximized: false })
assert.deepEqual(computeWindowOptions(saved, PRIMARY, 'darwin'), {
diff --git a/apps/desktop/electron/window-state.ts b/apps/desktop/electron/window-state.ts
index 364982929bc..3ede99773ed 100644
--- a/apps/desktop/electron/window-state.ts
+++ b/apps/desktop/electron/window-state.ts
@@ -13,15 +13,18 @@ const MIN_HEIGHT = 620
// a saved position, so the title bar stays grabbable after a monitor unplugs.
const MIN_VISIBLE = 48
-// A stale normal-bounds snapshot can be almost identical to the work area while
-// isMaximized=false (for example after a DPI/display transition). Restoring that
-// verbatim leaves Windows with no meaningful restore-down size. Treat a window
-// that covers at least 90% of both work-area dimensions and starts near that
-// work area's origin as the stuck-fullscreen shape, then recover to a centered
-// 80% windowed size.
-const STALE_FULLSCREEN_RATIO = 0.9
+// A stale normal-bounds snapshot written while the window was fullscreen has
+// no meaningful restore-down size, so recovery (see staleFullscreenWorkArea)
+// gives the window back a centered, deliberately windowed shape instead.
const RECOVERED_WINDOW_RATIO = 0.8
+// Legacy snapshots (written before boundsCapturedFullScreen existed) have no
+// provenance flag, so stale fullscreen-as-normal bounds can only be told apart
+// from a deliberate near-fullscreen window by an exact geometry match: the
+// bounds equal a display's work area (or its full bounds, where the taskbar is
+// hidden) within this many pixels on every edge.
+const LEGACY_EXACT_MATCH_TOLERANCE = 2
+
const finite = v => typeof v === 'number' && Number.isFinite(v)
const clamp = (v, lo, hi) => Math.max(lo, Math.min(v, hi))
@@ -31,6 +34,10 @@ interface SanitizedWindowState {
isMaximized: boolean
x?: number
y?: number
+ // Provenance recorded at save time: the persisted bounds were captured while
+ // the window was fullscreen, the transition known to write fullscreen bounds
+ // as normal ones. Absent on legacy snapshots; strict like isMaximized.
+ boundsCapturedFullScreen?: boolean
}
// Parse raw JSON → clean state, or null if garbage. width/height are required
@@ -46,6 +53,10 @@ function sanitizeWindowState(raw?: any): SanitizedWindowState | null {
isMaximized: raw.isMaximized === true
}
+ if (raw.boundsCapturedFullScreen === true) {
+ state.boundsCapturedFullScreen = true
+ }
+
if (finite(raw.x) && finite(raw.y)) {
state.x = Math.round(raw.x)
state.y = Math.round(raw.y)
@@ -108,6 +119,15 @@ function firstLaunchSize(workArea: WorkArea): WindowOptions {
}
}
+// A stale normal-bounds snapshot written while the window was fullscreen: the
+// broken transition persists fullscreen bounds as normal ones, leaving Windows
+// with no meaningful restore-down size. Provenance is recorded at save time
+// (boundsCapturedFullScreen, see persistWindowState in main.ts), so recovery
+// here never guesses from geometry. Legacy snapshots written before that flag
+// existed recover only on an unambiguous match: bounds that equal a display's
+// work area (or its full bounds, when the taskbar hides) within
+// LEGACY_EXACT_MATCH_TOLERANCE on every edge — a window the user deliberately
+// sized to 90-something percent never matches.
function staleFullscreenWorkArea(state, displays) {
if (
!state ||
@@ -121,22 +141,54 @@ function staleFullscreenWorkArea(state, displays) {
return null
}
+ if (state.boundsCapturedFullScreen !== true) {
+ return legacyStaleFullscreenWorkArea(state, displays)
+ }
+
+ // Flagged at save time: recover to a centered windowed size on the work
+ // area the fullscreen bounds actually covered.
return (
displays.find(({ workArea: a } = {}) => {
if (!a || !finite(a.x) || !finite(a.y) || !finite(a.width) || !finite(a.height)) {
return false
}
- const nearOriginX = Math.abs(state.x - a.x) <= a.width * (1 - STALE_FULLSCREEN_RATIO)
- const nearOriginY = Math.abs(state.y - a.y) <= a.height * (1 - STALE_FULLSCREEN_RATIO)
- const fillsWidth = state.width >= a.width * STALE_FULLSCREEN_RATIO
- const fillsHeight = state.height >= a.height * STALE_FULLSCREEN_RATIO
+ const x = Math.min(state.x + state.width, a.x + a.width) - Math.max(state.x, a.x)
+ const y = Math.min(state.y + state.height, a.y + a.height) - Math.max(state.y, a.y)
- return nearOriginX && nearOriginY && fillsWidth && fillsHeight
+ return x > 0 && y > 0
})?.workArea ?? null
)
}
+// Exact-match recovery for legacy snapshots with no provenance flag.
+function legacyStaleFullscreenWorkArea(state, displays) {
+ const t = LEGACY_EXACT_MATCH_TOLERANCE
+
+ const exact = ({ x, y, width, height }) =>
+ Math.abs(state.x - x) <= t &&
+ Math.abs(state.y - y) <= t &&
+ Math.abs(state.width - width) <= t &&
+ Math.abs(state.height - height) <= t
+
+ for (const { workArea: a, bounds: b } of displays) {
+ for (const candidate of [a, b]) {
+ if (
+ candidate &&
+ finite(candidate.x) &&
+ finite(candidate.y) &&
+ finite(candidate.width) &&
+ finite(candidate.height) &&
+ exact(candidate)
+ ) {
+ return a
+ }
+ }
+ }
+
+ return null
+}
+
function computeWindowOptions(state: WindowOptions, displays, platform = process.platform): WindowOptions {
const opts: WindowOptions = { width: state.width, height: state.height }