Skip to content

The agent that grows with you

fix(desktop): reap detached hindsight venv daemon before update handoff (Windows)

xy9526666806b18224df526
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 5661ab23daa..0d4fb95a415 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -393,6 +393,7 @@ import {
scanVenvBlockers,
stopSafeVenvBlockers
} from './venv-blocker-scan'
+import { isHermesOwnedVenvDaemon } from './venv-holder-select'
import { fetchMarketplaceThemes, searchMarketplaceThemes } from './vscode-marketplace'
import { createWakeIndicatorWindowController } from './wake-indicator-window'
import { enumerateWindowsFrontToBack, enumerationFailed, readWindowBelow } from './window-below'
@@ -3224,6 +3225,55 @@ function isShimLocked(shimPath) {
}
}
+// Kill only Hermes-OWNED venv daemons (the memory plugin's hindsight daemon:
+// exe under venv\Scripts AND cmdline referencing hindsight_api.main). The
+// daemon is spawned DETACHED, so it outlives the backend tree-kill and keeps
+// venv files mapped. External holders (a user terminal running `hermes`,
+// unrelated scripts) are NOT killed — scanVenvBlockers reports them and the
+// hand-off aborts, per existing design. Selection lives in the pure
+// venv-holder-select module (ordinal path-prefix, no PowerShell -like
+// wildcard hazards) so it's testable without Electron.
+function killHermesOwnedVenvDaemons(updateRoot) {
+ if (!IS_WINDOWS) {
+ return
+ }
+
+ const scriptsDir = path.join(updateRoot, 'venv', 'Scripts')
+
+ 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) =>
+ isHermesOwnedVenvDaemon(p?.ExecutablePath, p?.CommandLine, scriptsDir)
+ )
+ } catch {
+ // Best-effort: the venv-blocker scan downstream is the real backstop.
+ return
+ }
+
+ for (const holder of holders) {
+ const pid = Number(holder?.ProcessId)
+
+ if (Number.isInteger(pid) && pid > 0) {
+ rememberLog(`[updates] stopping Hermes-owned venv daemon (hindsight) PID ${pid} before hand-off`)
+ forceKillProcessTree(pid)
+ }
+ }
+}
+
+
// 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
@@ -3574,6 +3624,13 @@ async function releaseBackendLock(updateRoot, tag) {
stopAllPoolBackends
})
+ // Reap Hermes-OWNED venv daemons the tree-kill above cannot reach: the
+ // memory plugin's hindsight daemon is spawned DETACHED (it outlives the
+ // backend) yet runs off venv\Scripts\pythonw.exe, keeping venv files
+ // mapped past the backend teardown (#75477/#75478). Narrowly scoped
+ // (venv-holder-select) — external holders are never killed here.
+ killHermesOwnedVenvDaemons(updateRoot)
+
const shim = venvHermesShimPath(updateRoot)
const gate = await waitForBackendRelease(
diff --git a/apps/desktop/electron/venv-holder-select.test.ts b/apps/desktop/electron/venv-holder-select.test.ts
new file mode 100644
index 00000000000..9067159f6e1
--- /dev/null
+++ b/apps/desktop/electron/venv-holder-select.test.ts
@@ -0,0 +1,63 @@
+import assert from 'node:assert/strict'
+
+import { test } from 'vitest'
+
+import { hasWindowsPathPrefix, isHermesOwnedVenvDaemon } from './venv-holder-select'
+
+const SCRIPTS = 'C:\\Hermes\\venv\\Scripts'
+
+test('matches the hindsight daemon shim (exe under venv Scripts + hindsight cmdline)', () => {
+ assert.equal(
+ isHermesOwnedVenvDaemon(
+ 'C:\\Hermes\\venv\\Scripts\\pythonw.exe',
+ 'C:\\Hermes\\venv\\Scripts\\pythonw.exe -m hindsight_api.main --daemon --idle-timeout 300 --port 9177',
+ SCRIPTS
+ ),
+ true
+ )
+})
+
+test('Windows path prefix match is ordinal case-insensitive', () => {
+ assert.equal(
+ isHermesOwnedVenvDaemon(
+ 'c:\\hermes\\venv\\scripts\\python.exe',
+ 'python.exe -m hindsight_api.main --daemon',
+ 'C:\\Hermes\\venv\\Scripts'
+ ),
+ true
+ )
+})
+
+test('excludes external venv holders that are not the hindsight daemon', () => {
+ // a user terminal running the hermes CLI from the venv — must NOT be killed
+ assert.equal(
+ isHermesOwnedVenvDaemon('C:\\Hermes\\venv\\Scripts\\hermes.exe', 'hermes chat -q "hi"', SCRIPTS),
+ false
+ )
+ // an unrelated python script using the venv interpreter
+ assert.equal(
+ isHermesOwnedVenvDaemon('C:\\Hermes\\venv\\Scripts\\python.exe', 'python C:\\tools\\import.py', SCRIPTS),
+ false
+ )
+})
+
+test('excludes exes outside the venv even when the cmdline mentions hindsight', () => {
+ assert.equal(
+ isHermesOwnedVenvDaemon('C:\\Other\\pythonw.exe', 'pythonw -m hindsight_api.main --daemon', SCRIPTS),
+ false
+ )
+})
+
+test('prefix boundary: sibling dirs (ScriptsX) do not match', () => {
+ assert.equal(
+ hasWindowsPathPrefix('C:\\Hermes\\venv\\ScriptsX\\python.exe', SCRIPTS),
+ false
+ )
+ assert.equal(hasWindowsPathPrefix('C:\\Hermes\\venv\\Scripts\\python.exe', SCRIPTS), true)
+})
+
+test('null/undefined fields never match', () => {
+ assert.equal(isHermesOwnedVenvDaemon(null, 'x', SCRIPTS), false)
+ assert.equal(isHermesOwnedVenvDaemon('C:\\Hermes\\venv\\Scripts\\pythonw.exe', null, SCRIPTS), false)
+ assert.equal(isHermesOwnedVenvDaemon(undefined, undefined, SCRIPTS), false)
+})
diff --git a/apps/desktop/electron/venv-holder-select.ts b/apps/desktop/electron/venv-holder-select.ts
new file mode 100644
index 00000000000..12128efe18f
--- /dev/null
+++ b/apps/desktop/electron/venv-holder-select.ts
@@ -0,0 +1,42 @@
+/**
+ * venv-holder-select.ts
+ *
+ * Pure Windows venv-holder selection logic (testable without Electron).
+ *
+ * The pre-update handoff kills Hermes-OWNED venv daemons (the memory plugin's
+ * hindsight daemon) so the updater never races a mapped shim. External
+ * holders (a user terminal running `hermes`, unrelated scripts) must NOT be
+ * killed — current design reports them via scanVenvBlockers and ABORTS the
+ * handoff instead (main.ts releaseBackendLock / applyUpdates).
+ */
+
+/** Ordinal case-insensitive prefix check for Windows paths. */
+export function hasWindowsPathPrefix(
+ exePath: string,
+ venvScriptsDir: string
+): boolean {
+ const prefix = `${venvScriptsDir}\\`
+
+ return (
+ exePath.length >= prefix.length &&
+ exePath.slice(0, prefix.length).toLowerCase() === prefix.toLowerCase()
+ )
+}
+
+/**
+ * True when a process is a Hermes-owned venv daemon: its exe lives under
+ * `<venv>\Scripts\` (ordinal case-insensitive prefix) AND its cmdline
+ * references `hindsight_api.main` (the memory daemon the memory plugin
+ * spawns DETACHED — it outlives Hermes and holds venv shims mapped).
+ */
+export function isHermesOwnedVenvDaemon(
+ exePath: string | null | undefined,
+ cmdline: string | null | undefined,
+ venvScriptsDir: string
+): boolean {
+ if (!exePath || !cmdline) {
+ return false
+ }
+
+ return hasWindowsPathPrefix(exePath, venvScriptsDir) && /hindsight_api\.main/i.test(cmdline)
+}
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..97dc2f3148d
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1 @@
+xy952666680