Skip to content

The agent that grows with you

fix(desktop): stop focusWindow() and ambient reveals from stealing the Windows foreground during streaming (#83998)

Brooklyn Nicholson8d30c4eaabd8
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 1b69e181185..405f6ba236f 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -605,6 +605,7 @@ import {
WindowConnectionRouteRegistry
} from './window-connection-route'
import { registerWindowControlIpc, windowControlState } from './window-controls'
+import { revealAction, shouldFocusToTakeKeyboard } from './window-focus-policy'
import { createWindowOpenHandler } from './window-open-policy'
import { installWindowRendererLifecycle } from './window-renderer-lifecycle'
import { wireWindowReveal } from './window-reveal'
@@ -6945,11 +6946,16 @@ function sendOpenUpdatesRequested() {
webContents.send('hermes:open-updates')
+ // #83998: never pump the Windows foreground from an ambient surface —
+ // showInactive + a guarded focus keep the raise from dismissing another
+ // app's native dialog.
if (!mainWindow.isVisible()) {
- mainWindow.show()
+ mainWindow.showInactive()
}
- mainWindow.focus()
+ if (shouldFocusToTakeKeyboard(mainWindow)) {
+ mainWindow.focus()
+ }
}
// Push titlebar/fullscreen chrome state to a window's renderer. Defaults to the
@@ -13801,11 +13807,18 @@ function focusWindow(win) {
win.restore()
}
- if (!win.isVisible()) {
- win.show()
+ // #83998: show() and focus() both seize the Windows OS foreground,
+ // dismissing other apps' native save/confirm dialogs while Hermes streams
+ // in the background. Reveal without activation, and only take the keyboard
+ // when the window doesn't already have focus — a redundant focus() still
+ // pumps SetForegroundWindow.
+ if (revealAction(win.isVisible()) === 'showInactive') {
+ win.showInactive()
}
- win.focus()
+ if (shouldFocusToTakeKeyboard(win)) {
+ win.focus()
+ }
}
function spawnSecondaryWindow({
@@ -19006,7 +19019,12 @@ function handleDeepLink(url) {
mainWindow.restore()
}
- mainWindow.focus()
+ // #83998: a deep link must deliver without re-pumping the Windows
+ // foreground when the window already has focus.
+ if (shouldFocusToTakeKeyboard(mainWindow)) {
+ mainWindow.focus()
+ }
+
mainWindow.webContents.send('hermes:deep-link', payload)
rememberLog(`[deeplink] delivered ${kind}/${name}`)
} catch (err) {
diff --git a/apps/desktop/electron/window-focus-policy.test.ts b/apps/desktop/electron/window-focus-policy.test.ts
new file mode 100644
index 00000000000..12e537d9dbf
--- /dev/null
+++ b/apps/desktop/electron/window-focus-policy.test.ts
@@ -0,0 +1,34 @@
+/**
+ * Invariants for the Windows foreground-steal fix (#83998).
+ *
+ * `focusWindow()` runs for every ambient window raise (tray restore,
+ * notification click, second-instance, dock/taskbar activate). On Windows,
+ * `show()` + unconditional `focus()` seizes the OS foreground and dismisses
+ * other apps' native dialogs while Hermes streams in the background. The
+ * main-process call sites now follow the policy in window-focus-policy.ts;
+ * these tests pin it.
+ */
+
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { revealAction, shouldFocusToTakeKeyboard } from './window-focus-policy'
+
+test('an invisible window is revealed without stealing foreground (showInactive)', () => {
+ assert.equal(revealAction(false), 'showInactive')
+})
+
+test('an already-visible window is left alone by the reveal path', () => {
+ assert.equal(revealAction(true), 'none')
+})
+
+test('focus is only taken when the window does not already have it', () => {
+ assert.equal(shouldFocusToTakeKeyboard({ isFocused: () => false }), true)
+})
+
+test('an already-focused window never pumps the OS foreground path', () => {
+ // The #83998 regression: a redundant .focus() on a focused window still
+ // runs SetForegroundWindow on Windows, dismissing another app's dialog.
+ assert.equal(shouldFocusToTakeKeyboard({ isFocused: () => true }), false)
+})
diff --git a/apps/desktop/electron/window-focus-policy.ts b/apps/desktop/electron/window-focus-policy.ts
new file mode 100644
index 00000000000..d1d2ff487dd
--- /dev/null
+++ b/apps/desktop/electron/window-focus-policy.ts
@@ -0,0 +1,45 @@
+/**
+ * Foreground-focus policy for the Windows focus-steal bug (#83998).
+ *
+ * On Windows, `BrowserWindow.show()` and `.focus()` both seize the OS
+ * foreground: any native dialog another app is showing (Notepad's save
+ * confirmation, a print dialog) is forcibly dismissed when Hermes calls
+ * either while streaming in the background. The report's repro is a
+ * `focusWindow()` call reaching the window mid-stream.
+ *
+ * The fix is at the call sites, not here — but the policy is pure so the
+ * invariants are unit-testable without booting Electron:
+ *
+ * - `revealWindow` (ambient reveal, e.g. `focusWindow`'s un-hide branch)
+ * uses `showInactive()`: raising Hermes must never steal foreground from
+ * the app the user is working in.
+ * - `focusWindow`'s keyboard hand-off only calls `.focus()` when the window
+ * is NOT already focused. On Windows a redundant `.focus()` on a focused
+ * window still pumps the OS SetForegroundWindow path, which is exactly
+ * the ambient steal. When the window already has focus, the call is a
+ * no-op in intent and must be a no-op in effect.
+ */
+
+export interface FocusPolicyWindow {
+ isFocused: () => boolean
+}
+
+/**
+ * The reveal half of `focusWindow`: a window that is hidden or minimized
+ * must come back, but never by stealing the OS foreground. Callers should
+ * do the restore/minimize-unminimize themselves, then call this to make the
+ * window visible; it returns the action to run on the window so the policy
+ * is testable without a real BrowserWindow.
+ */
+export function revealAction(windowVisible: boolean): 'showInactive' | 'none' {
+ return windowVisible ? 'none' : 'showInactive'
+}
+
+/**
+ * The keyboard half of `focusWindow`: focus only moves when the window
+ * doesn't already have it. A `.focus()` call on an already-focused window
+ * is a foreground pump on Windows and a no-op everywhere else — skip it.
+ */
+export function shouldFocusToTakeKeyboard({ isFocused }: FocusPolicyWindow): boolean {
+ return !isFocused()
+}