Skip to content

The agent that grows with you

fix(desktop): never resolve a source root's interpreter off PATH

ethernet268e1055e806
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 311a39ed033..56219dd67b2 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -463,6 +463,7 @@ import {
} from './session-windows'
import { ensureLoginShellPath } from './shell-path'
import { createSourcePythonBackend, resolveSourceInstallationBackend, type SourceBackend } from './source-backend'
+import { resolveSourcePython } from './source-python'
import { createBootstrapCoordinator, sshConfigFingerprint } from './ssh-bootstrap-coordinator'
import { collectSshConfigHosts, parseSshGOutput } from './ssh-config'
import { createSshProbeConnection, pickLocalPort, redactSecrets, SshConnection } from './ssh-connection'
@@ -2687,26 +2688,12 @@ function isHermesSourceRoot(root) {
return directoryExists(root) && fileExists(path.join(root, 'hermes_cli', 'main.py'))
}
-async function findPythonForRoot(root) {
- const override = process.env.HERMES_DESKTOP_PYTHON
-
- if (override && fileExists(override)) {
- return override
- }
-
- const relativePaths = IS_WINDOWS
- ? [path.join('.venv', 'Scripts', 'python.exe'), path.join('venv', 'Scripts', 'python.exe')]
- : [path.join('.venv', 'bin', 'python'), path.join('venv', 'bin', 'python')]
-
- for (const relativePath of relativePaths) {
- const candidate = path.join(root, relativePath)
-
- if (fileExists(candidate)) {
- return candidate
- }
- }
-
- return findSystemPython()
+async function findPythonForRoot(root: string): Promise<string | null> {
+ return resolveSourcePython(root, {
+ override: process.env.HERMES_DESKTOP_PYTHON,
+ isWindows: IS_WINDOWS,
+ fileExists
+ })
}
async function findSystemPython() {
diff --git a/apps/desktop/electron/source-python.test.ts b/apps/desktop/electron/source-python.test.ts
new file mode 100644
index 00000000000..1c7079acdbe
--- /dev/null
+++ b/apps/desktop/electron/source-python.test.ts
@@ -0,0 +1,69 @@
+import assert from 'node:assert/strict'
+import path from 'node:path'
+
+import { test } from 'vitest'
+
+import { createSourcePythonBackend } from './source-backend'
+import { resolveSourcePython } from './source-python'
+
+const ROOT: string = path.join(path.sep, 'checkout')
+
+/** A checkout reporting exactly `present` (root-relative) as existing files. */
+function checkout(present: string[] = [], exists: boolean = true) {
+ const files = new Set(present.map(entry => path.join(ROOT, entry)))
+
+ return { root: ROOT, fileExists: (candidate: string): boolean => (exists && files.has(candidate)) }
+}
+
+test('a checkout with no virtualenv resolves to no interpreter, never a PATH Python', (): void => {
+ const { root, fileExists } = checkout()
+
+ // The invariant: "this root has no runtime" must reach callers as null.
+ // Answering with a PATH Python runs an install's update probe and its backend
+ // under an interpreter nothing selected — it can import the checkout while
+ // lacking its selected dependencies (updater/checkout-source.ts).
+ assert.equal(resolveSourcePython(root, { fileExists, isWindows: false }), null)
+ assert.equal(resolveSourcePython(root, { fileExists, isWindows: true }), null)
+})
+
+test('a virtualenv-less checkout yields no source backend, so the ladder moves on', (): void => {
+ const { root, fileExists } = checkout()
+ const python: string | null = resolveSourcePython(root, { fileExists, isWindows: false })
+
+ assert.equal(createSourcePythonBackend(root, python, ['serve']), null)
+})
+
+test('the checkout virtualenv is preferred over the flat venv, on both layouts', (): void => {
+ const venv: string = path.join('venv', 'bin', 'python')
+ const dotVenv: string = path.join('.venv', 'bin', 'python')
+ const windows: string = path.join('.venv', 'Scripts', 'python.exe')
+
+ assert.equal(
+ resolveSourcePython(ROOT, { fileExists: checkout([venv, dotVenv]).fileExists, isWindows: false }),
+ path.join(ROOT, dotVenv)
+ )
+ assert.equal(
+ resolveSourcePython(ROOT, { fileExists: checkout([venv]).fileExists, isWindows: false }),
+ path.join(ROOT, venv)
+ )
+ assert.equal(
+ resolveSourcePython(ROOT, { fileExists: checkout([windows]).fileExists, isWindows: true }),
+ path.join(ROOT, windows)
+ )
+})
+
+test('an explicit developer interpreter wins, and is only honoured when it exists', (): void => {
+ const preferred: string = path.join(path.sep, 'opt', 'hermes', 'bin', 'python')
+ const aged: string = path.join(ROOT, 'venv', 'bin', 'python')
+ const { root, fileExists } = checkout(['venv/bin/python'], true)
+ const withPreferred = (candidate: string): boolean => candidate === preferred || fileExists(candidate)
+
+ assert.equal(resolveSourcePython(root, { override: preferred, fileExists: withPreferred, isWindows: false }), preferred)
+ // An override naming a file that is gone does not resurrect a PATH Python;
+ // it falls through to the checkout, then to null.
+ assert.equal(
+ resolveSourcePython(root, { override: preferred, fileExists, isWindows: false }),
+ aged
+ )
+ assert.equal(resolveSourcePython(ROOT, { override: preferred, fileExists: checkout().fileExists, isWindows: false }), null)
+})
\ No newline at end of file
diff --git a/apps/desktop/electron/source-python.ts b/apps/desktop/electron/source-python.ts
new file mode 100644
index 00000000000..14e3cd1fcf6
--- /dev/null
+++ b/apps/desktop/electron/source-python.ts
@@ -0,0 +1,57 @@
+import { statSync } from 'node:fs'
+import path from 'node:path'
+
+export interface SourcePythonOptions {
+ /** Explicit developer interpreter (`HERMES_DESKTOP_PYTHON`), honoured as-is. */
+ override?: string
+ isWindows?: boolean
+ fileExists?: (candidate: string) => boolean
+}
+
+/** A checkout's own interpreter paths, in preference order. */
+function interpreterPaths(isWindows: boolean): string[] {
+ return isWindows
+ ? [path.join('.venv', 'Scripts', 'python.exe'), path.join('venv', 'Scripts', 'python.exe')]
+ : [path.join('.venv', 'bin', 'python'), path.join('venv', 'bin', 'python')]
+}
+
+function defaultFileExists(candidate: string): boolean {
+ try {
+ return statSync(candidate).isFile()
+ } catch {
+ return false
+ }
+}
+
+/**
+ * The interpreter that belongs to a source checkout: an explicit override, else
+ * the checkout's own virtualenv, else null.
+ *
+ * Never a PATH Python. A system interpreter can import a checkout while lacking
+ * its selected dependencies (`updater/checkout-source.ts`), so falling back
+ * silently turns "this root has no runtime" into an update probe or a backend
+ * running under an interpreter nothing selected — and a failed read becomes a
+ * wrong answer instead of a refusal. PM deletes the in-tree `venv`/`.venv` once
+ * a generation is committed, which makes null the ordinary answer for a managed
+ * install: its callers resolve the installation launcher instead
+ * (`resolveSourceInstallationBackend`, `readSourceUpdate`) or refuse
+ * (`preflightStateDb`), and the backend ladder moves on to its next rung.
+ */
+export function resolveSourcePython(
+ root: string,
+ { override, isWindows = process.platform === 'win32', fileExists = defaultFileExists }: SourcePythonOptions = {}
+): string | null {
+ if (override && fileExists(override)) {
+ return override
+ }
+
+ for (const relative of interpreterPaths(isWindows)) {
+ const candidate: string = path.join(root, relative)
+
+ if (fileExists(candidate)) {
+ return candidate
+ }
+ }
+
+ return null
+}
\ No newline at end of file