Skip to content

The agent that grows with you

fix(desktop): kill external autostart venv holders before update hand-off

Austin Pickett3aa0ac5e0d7f
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index f041020ca5f..d07d4541be6 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -542,7 +542,7 @@ import {
import { startRelaunchWaiter } from './updater/relaunch-waiter'
import { preflightStateDb } from './updater/state-db-preflight'
import { createStoreStrategy } from './updater/store-client'
-import { isHermesOwnedVenvDaemon } from './venv-holder-select'
+import { isExternalVenvHolder, isHermesOwnedVenvDaemon } from './venv-holder-select'
import { fetchMarketplaceThemes, searchMarketplaceThemes } from './vscode-marketplace'
import { createWakeIndicatorWindowController } from './wake-indicator-window'
import { decodeWebText } from './web-text-decoder'
@@ -3695,6 +3695,22 @@ function isShimLocked(shimPath) {
// held installation. Selection lives in the pure
// venv-holder-select module (ordinal path-prefix, no PowerShell -like
// wildcard hazards) so it's testable without Electron.
+function scanWindowsProcesses(): Array<{ ProcessId?: unknown; ExecutablePath?: unknown; CommandLine?: unknown }> {
+ const out = execFileSync(
+ 'powershell',
+ [
+ '-NoProfile',
+ '-Command',
+ 'Get-CimInstance Win32_Process | Where-Object { $_.ExecutablePath -and $_.CommandLine } | Select-Object ProcessId, ExecutablePath, CommandLine | ConvertTo-Json -Compress'
+ ],
+ hiddenWindowsChildOptions({ encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 15_000 })
+ )
+
+ const parsed = JSON.parse(String(out || '[]'))
+
+ return Array.isArray(parsed) ? parsed : [parsed]
+}
+
function killHermesOwnedVenvDaemons(updateRoot) {
if (!IS_WINDOWS) {
return
@@ -3705,19 +3721,7 @@ function killHermesOwnedVenvDaemons(updateRoot) {
let holders = []
try {
- const out = execFileSync(
- 'powershell',
- [
- '-NoProfile',
- '-Command',
- 'Get-CimInstance Win32_Process | Where-Object { $_.ExecutablePath -and $_.CommandLine } | Select-Object ProcessId, ExecutablePath, CommandLine | ConvertTo-Json -Compress'
- ],
- hiddenWindowsChildOptions({ encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 15_000 })
- )
-
- const parsed = JSON.parse(String(out || '[]'))
-
- holders = (Array.isArray(parsed) ? parsed : [parsed]).filter(p =>
+ holders = scanWindowsProcesses().filter(p =>
isHermesOwnedVenvDaemon(p?.ExecutablePath, p?.CommandLine, scriptsDir)
)
} catch {
@@ -3742,6 +3746,50 @@ function killHermesOwnedVenvDaemons(updateRoot) {
}
}
+// Kill EXTERNAL Hermes processes that hold this install's venv shim (#62311):
+// the gateway Startup item and dashboard Scheduled Task are launched outside
+// this app (Task Scheduler / autostart), so the backend teardown above never
+// sees them — yet they map venv files and made every update hand-off abort
+// with "venv shim still locked". Selection is deliberately narrow
+// (isExternalVenvHolder: exe under venv\Scripts AND unambiguously a Hermes
+// program) — unrelated processes that merely mention the install root or use
+// the venv interpreter for their own scripts are never killed; the shim-lock
+// probe still aborts the hand-off for those. Called before the release gate
+// AND inside each gate pass, so a respawning autostart holder is re-killed
+// instead of winning the 15s race.
+function killExternalVenvHolders(updateRoot) {
+ if (!IS_WINDOWS) {
+ return
+ }
+
+ const scriptsDir = path.join(resolveVenvDir(updateRoot), 'Scripts')
+
+ let holders = []
+
+ try {
+ holders = scanWindowsProcesses().filter(p =>
+ isExternalVenvHolder(p?.ExecutablePath as string | undefined, p?.CommandLine as string | undefined, scriptsDir)
+ )
+ } catch {
+ // Best-effort: the shim-lock probe remains the backstop.
+ return
+ }
+
+ for (const holder of holders) {
+ const pid = Number(holder?.ProcessId)
+
+ if (Number.isInteger(pid) && pid > 0) {
+ rememberLog(`[updates] stopping external Hermes venv holder (autostart gateway/dashboard) PID ${pid} before hand-off`)
+
+ try {
+ forceKillProcessTree(pid)
+ } catch (error) {
+ rememberLog(`[updates] taskkill PID ${pid} failed: ${error instanceof Error ? error.message : String(error)}`)
+ }
+ }
+ }
+}
+
// Force-kill the entire process TREE rooted at each PID. Node's child.kill()
// only signals the direct child, so on Windows a backend `hermes.exe` that
// spawned its own grandchildren (a `hermes` REPL, a pty terminal session, the
@@ -4214,6 +4262,12 @@ async function releaseBackendLock(updateRoot: string, tag: string): Promise<{ un
// (venv-holder-select) — external holders are never killed here.
killHermesOwnedVenvDaemons(updateRoot)
+ // External autostart Hermes processes (gateway Startup item, dashboard
+ // Scheduled Task) also hold the venv shim and are invisible to the backend
+ // teardown (#62311). Kill them before the gate AND re-scan inside each gate
+ // pass, so a respawning holder loses the race instead of the update.
+ killExternalVenvHolders(updateRoot)
+
const shim = venvHermesShimPath(updateRoot)
const gate = await waitForBackendRelease(
@@ -4222,6 +4276,9 @@ async function releaseBackendLock(updateRoot: string, tag: string): Promise<{ un
isShimLocked: () => Boolean(isShimLocked(shim)),
isPidAlive: isPidAliveWindows,
collectStragglerPids: () => {
+ // Re-kill resurgent external holders (autostart gateway/dashboard) on
+ // every pass — #62311 — before collecting the desktop-owned stragglers.
+ killExternalVenvHolders(updateRoot)
const stragglers = []
const currentHermesProcess = backendConnectionState.getProcess()
diff --git a/apps/desktop/electron/venv-holder-select.test.ts b/apps/desktop/electron/venv-holder-select.test.ts
index d38e567b356..9d6fd3636fb 100644
--- a/apps/desktop/electron/venv-holder-select.test.ts
+++ b/apps/desktop/electron/venv-holder-select.test.ts
@@ -2,7 +2,7 @@ import assert from 'node:assert/strict'
import { test } from 'vitest'
-import { hasWindowsPathPrefix, isHermesOwnedVenvDaemon } from './venv-holder-select'
+import { hasWindowsPathPrefix, isExternalVenvHolder, isHermesOwnedVenvDaemon } from './venv-holder-select'
const SCRIPTS = 'C:\\Hermes\\venv\\Scripts'
@@ -55,3 +55,62 @@ test('null/undefined fields never match', () => {
assert.equal(isHermesOwnedVenvDaemon('C:\\Hermes\\venv\\Scripts\\pythonw.exe', null, SCRIPTS), false)
assert.equal(isHermesOwnedVenvDaemon(undefined, undefined, SCRIPTS), false)
})
+
+// --- isExternalVenvHolder (#62311) ------------------------------------------
+
+test('matches the autostart gateway shim (hermes.exe under venv Scripts)', () => {
+ assert.equal(
+ isExternalVenvHolder(
+ 'C:\\Hermes\\venv\\Scripts\\hermes.exe',
+ '"C:\\Hermes\\venv\\Scripts\\hermes.exe" gateway run --external-supervisor',
+ SCRIPTS
+ ),
+ true
+ )
+})
+
+test('matches the dashboard scheduled task (python -m hermes_cli / -m hermes)', () => {
+ assert.equal(
+ isExternalVenvHolder(
+ 'C:\\Hermes\\venv\\Scripts\\python.exe',
+ '"C:\\Hermes\\venv\\Scripts\\python.exe" -m hermes_cli.main dashboard',
+ SCRIPTS
+ ),
+ true
+ )
+ assert.equal(
+ isExternalVenvHolder('C:\\Hermes\\venv\\Scripts\\pythonw.exe', 'pythonw.exe -m hermes serve', SCRIPTS),
+ true
+ )
+})
+
+test('never matches an unrelated process that merely borrows the venv interpreter', () => {
+ // a user's own script running on the venv python — NOT Hermes, must NOT be killed
+ assert.equal(
+ isExternalVenvHolder('C:\\Hermes\\venv\\Scripts\\python.exe', 'python C:\\tools\\import.py', SCRIPTS),
+ false
+ )
+ // hindsight daemon is selected by isHermesOwnedVenvDaemon, not here
+ assert.equal(
+ isExternalVenvHolder('C:\\Hermes\\venv\\Scripts\\pythonw.exe', 'pythonw -m hindsight_api.main --daemon', SCRIPTS),
+ false
+ )
+})
+
+test('never matches a process outside the venv, even with hermes in the cmdline', () => {
+ // an editor / shell whose command line mentions the install root (#62445 regression guard)
+ assert.equal(
+ isExternalVenvHolder('C:\\Windows\\System32\\cmd.exe', 'cmd /c cd C:\\Hermes\\venv\\Scripts && dir', SCRIPTS),
+ false
+ )
+ assert.equal(isExternalVenvHolder('C:\\Other\\hermes.exe', 'hermes gateway run', SCRIPTS), false)
+})
+
+test('sibling-dir and boundary safety for the external selector', () => {
+ assert.equal(
+ isExternalVenvHolder('C:\\Hermes\\venv\\ScriptsX\\hermes.exe', 'hermes gateway run', SCRIPTS),
+ false
+ )
+ assert.equal(isExternalVenvHolder(null, 'hermes gateway run', SCRIPTS), false)
+ assert.equal(isExternalVenvHolder('C:\\Hermes\\venv\\Scripts\\hermes.exe', null, SCRIPTS), false)
+})
diff --git a/apps/desktop/electron/venv-holder-select.ts b/apps/desktop/electron/venv-holder-select.ts
index d79d2e0a5f3..ed3ed679844 100644
--- a/apps/desktop/electron/venv-holder-select.ts
+++ b/apps/desktop/electron/venv-holder-select.ts
@@ -34,3 +34,40 @@ export function isHermesOwnedVenvDaemon(
return hasWindowsPathPrefix(exePath, venvScriptsDir) && /hindsight_api\.main/i.test(cmdline)
}
+
+/**
+ * True when a process is an external Hermes process holding this install's venv
+ * (#62311): its exe lives under `<venv>\Scripts\` AND it is unambiguously a
+ * Hermes program — the `hermes.exe` shim, `python -m hermes_cli...`, or
+ * `python -m hermes ...`. These are the autostart holders (the gateway Startup
+ * item, the dashboard Scheduled Task) that neither the desktop's backend
+ * teardown nor the hindsight-daemon sweep reach, and that keep the venv shim
+ * locked so the update hand-off aborts every time.
+ *
+ * Deliberately NARROWER than a bare path/cmdline substring against the install
+ * root (the approach that sank #62445): an unrelated process that merely
+ * mentions the install root or borrows the venv interpreter for its own script
+ * must NOT be tree-killed. Non-Hermes venv users still abort the hand-off via
+ * the shim-lock probe instead.
+ */
+export function isExternalVenvHolder(
+ exePath: string | null | undefined,
+ cmdline: string | null | undefined,
+ venvScriptsDir: string
+): boolean {
+ if (!exePath || !cmdline) {
+ return false
+ }
+
+ if (!hasWindowsPathPrefix(exePath, venvScriptsDir)) {
+ return false
+ }
+
+ const exeName = exePath.slice(exePath.lastIndexOf('\\') + 1).toLowerCase()
+
+ if (exeName === 'hermes.exe') {
+ return true
+ }
+
+ return /hermes_cli/i.test(cmdline) || /(^|\s|")-m\s+hermes([.\s"']|$)/i.test(cmdline)
+}