Skip to content

The agent that grows with you

fix(desktop): spawn the Windows update hand-off wrapper with a hidden console it can share

teknium18ac3215ac672
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index dbcc386d0a3..1e901fef43e 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -4314,11 +4314,13 @@ async function applyUpdates(opts: { stopSafeBlockers?: boolean } = {}) {
// A bare detached+hidden powershell spawn silently dies before -File
// processing (console-subsystem init failure — see
- // wrapHandoffForDetachedConsole). Route through `cmd start` so the
- // script gets its own minimized console and survives our exit. The
- // wrapper cmd.exe exits immediately, so child.pid is NOT the script's
- // pid — the script claims the update marker itself with its own $PID
- // as its first action, and a relaunched Desktop parks on that.
+ // wrapHandoffForDetachedConsole). Route through a NON-detached, hidden
+ // `cmd start /b` wrapper: cmd.exe owns one hidden console, the script
+ // runs inside it (no window is ever created, #116161) and outlives
+ // both cmd.exe and this process. The wrapper cmd.exe exits
+ // immediately, so child.pid is NOT the script's pid — the script
+ // claims the update marker itself with its own $PID as its first
+ // action, and a relaunched Desktop parks on that.
const wrapped = wrapHandoffForDetachedConsole(scriptHandoff, [
'-InstallRoot',
updateRoot,
@@ -4338,7 +4340,7 @@ async function applyUpdates(opts: { stopSafeBlockers?: boolean } = {}) {
HERMES_UPDATE_STARTED_AT: String(updateStartedAt),
PATH: pathWithHermesManagedNode(venvBin)
},
- detached: true,
+ detached: wrapped.detached,
stdio: 'ignore'
})
diff --git a/apps/desktop/electron/updater-process.test.ts b/apps/desktop/electron/updater-process.test.ts
index a53acb437ee..76b61a33ce6 100644
--- a/apps/desktop/electron/updater-process.test.ts
+++ b/apps/desktop/electron/updater-process.test.ts
@@ -219,7 +219,10 @@ test('resolveUpdateScriptHandoff is Windows-only (POSIX updates in place)', () =
assert.equal(handoff, null)
})
-test('wrapHandoffForDetachedConsole uses start /b without creating a console', () => {
+test('wrapHandoffForDetachedConsole runs the script inside a non-detached hidden wrapper console', () => {
+ // #116161: `start /min` allocated a NEW (minimized, visible) console for
+ // powershell on every hand-off; `detached: true` (DETACHED_PROCESS) would
+ // leave the wrapper console-less, forcing the same allocation under `/b`.
const root = String.raw`C:\Users\hermes\AppData\Local\hermes\hermes-agent`
const expected = path.join(root, 'scripts', 'desktop-update', 'windows.ps1')
@@ -232,6 +235,7 @@ test('wrapHandoffForDetachedConsole uses start /b without creating a console', (
const wrapped = wrapHandoffForDetachedConsole(handoff, ['-InstallRoot', root, '-Branch', 'main'])
assert.equal(wrapped.command, 'cmd.exe')
+ assert.equal(wrapped.detached, false)
assert.deepEqual(wrapped.args, [
'/d',
'/s',
diff --git a/apps/desktop/electron/updater-process.ts b/apps/desktop/electron/updater-process.ts
index 4b9e4583117..e0d8dd6cb6e 100644
--- a/apps/desktop/electron/updater-process.ts
+++ b/apps/desktop/electron/updater-process.ts
@@ -127,24 +127,34 @@ export function resolvePosixScriptHandoff(
}
/**
- * Wrap a PowerShell hand-off invocation so it survives a detached, hidden
- * spawn from Electron.
+ * Wrap a PowerShell hand-off invocation so it survives a hidden spawn from
+ * Electron without ever showing a console window (#116161).
*
* Verified empirically (2026-08-09, Windows 11): `spawn('powershell', [...,
* '-File', script], { detached: true, stdio: 'ignore', windowsHide: true })`
* exits 0 WITHOUT executing a single line of the script. powershell.exe is a
- * console-subsystem binary; detached+windowsHide gives it no console to
- * attach to, and Windows PowerShell 5.1 dies during console init before
- * -File processing (the same class of failure as #54220's conhost work, on
- * the launch side). The same spawn with a visible console, or non-detached,
- * runs fine — so unit tests and foreground use hide the bug.
+ * console-subsystem binary; libuv maps `detached: true` to DETACHED_PROCESS,
+ * which gives the child NO console (and makes the OS ignore CREATE_NO_WINDOW),
+ * and Windows PowerShell 5.1 dies during console init before -File processing
+ * (the same class of failure as #54220's conhost work, on the launch side).
*
- * `cmd /c start "" /b powershell ...` is the production shape: `start /b`
- * keeps the child in the wrapper's hidden console while still returning
- * immediately, so the script outlives cmd.exe without creating a visible
- * console window. The spawned pid is therefore the WRAPPER's — callers must
- * not use it as a marker owner (the script claims the marker itself with its
- * own $PID).
+ * The parent-console model that follows from that (and that every other
+ * hidden spawn in this app relies on): a console child inherits its parent's
+ * console; only a console-LESS parent forces the OS to allocate a new,
+ * visible one. So the wrapper cmd.exe is spawned NON-detached — libuv then
+ * honours `windowsHide` (CREATE_NO_WINDOW) and cmd.exe owns one hidden
+ * console — and `start "" /b powershell ...` runs the script inside that
+ * hidden console (`/min` would tell `start` to allocate a NEW console for the
+ * child, which is what flashed a minimized PowerShell window on every
+ * hand-off; `/b` shares the wrapper's). The console outlives cmd.exe for as
+ * long as powershell is attached to it.
+ *
+ * Survival past our own exit does not need `detached`: libuv's per-process
+ * job object has JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK, so grandchildren
+ * (`start`'s powershell) are never members and Windows does not tie a
+ * process's lifetime to its parent. `start` returns immediately, so the
+ * spawned pid is the WRAPPER's — callers must not use it as a marker owner
+ * (the script claims the marker itself with its own $PID).
*/
export function wrapHandoffForDetachedConsole(
handoff: UpdateScriptHandoff,
@@ -152,10 +162,13 @@ export function wrapHandoffForDetachedConsole(
): {
command: string
args: string[]
+ /** Spawn NON-detached so the wrapper gets a hidden console the script inherits. */
+ detached: false
} {
return {
command: 'cmd.exe',
- args: ['/d', '/s', '/c', 'start', '', '/b', handoff.command, ...handoff.args, ...extraArgs]
+ args: ['/d', '/s', '/c', 'start', '', '/b', handoff.command, ...handoff.args, ...extraArgs],
+ detached: false
}
}