Skip to content

The agent that grows with you

fix(desktop): Windows ticket helper uses an installed launcher's own interpreter

unsupportedpastels51b1d3cbd163
diff --git a/apps/desktop/electron/local-gateway-python-runtime.test.ts b/apps/desktop/electron/local-gateway-python-runtime.test.ts
new file mode 100644
index 00000000000..85091b42140
--- /dev/null
+++ b/apps/desktop/electron/local-gateway-python-runtime.test.ts
@@ -0,0 +1,61 @@
+import fs from 'node:fs/promises'
+import os from 'node:os'
+import path from 'node:path'
+
+import { afterEach, beforeEach, expect, test } from 'vitest'
+
+import { ticketRuntimeFor } from './local-gateway-python'
+
+let dir: string
+
+beforeEach(async () => { dir = await fs.realpath(await fs.mkdtemp(path.join(os.tmpdir(), 'ticket-runtime-'))) })
+afterEach(async () => { await fs.rm(dir, { recursive: true, force: true }) })
+
+// A POSIX script stands in for the installation launcher's --print-runtime-command boundary.
+async function launcher(body: string): Promise<string> {
+ const file = path.join(dir, 'hermes')
+ await fs.writeFile(file, `#!/bin/sh\nprintf '%s\\n' "$@" > "${dir}/argv"\n${body}\n`, { mode: 0o755 })
+
+ return file
+}
+
+test('a Python backend is the helper interpreter as-is', async () => {
+ const backend = { command: '/venv/bin/python', kind: 'python', shell: false, env: { A: '1' } }
+ await expect(ticketRuntimeFor(backend)).resolves.toBe(backend)
+})
+
+test.skipIf(process.platform === 'win32')('an installed launcher backend mints with the interpreter it reports', async () => {
+ // The normal Windows install resolves to kind 'command' (the installation launcher).
+ const python = process.execPath
+ const command = await launcher(`printf '%s' '${JSON.stringify([python, '-I', '-c', 'bootstrap'])}'`)
+ const root = path.join(dir, 'hermes-agent')
+ await fs.mkdir(root)
+
+ await expect(ticketRuntimeFor({ command, kind: 'command', shell: false, root, env: { PYTHONPATH: '/scrubbed', B: '2' } }))
+ .resolves.toEqual({ command: python, env: { PYTHONPATH: root, B: '2' } })
+ expect((await fs.readFile(path.join(dir, 'argv'), 'utf8')).trim()).toBe('--print-runtime-command')
+})
+
+test.skipIf(process.platform === 'win32')('a launcher without a usable runtime command is refused with its reason', async () => {
+ const root = dir
+
+ for (const [body, reason] of [
+ ['exit 3', 'launcher exited 3'],
+ ["printf 'not json'", 'did not report a runtime command'],
+ [`printf '%s' '["relative/python"]'`, 'invalid runtime command'],
+ [`printf '%s' '["${dir}/missing-python"]'`, 'invalid runtime command'],
+ [`printf '%s' '{}'`, 'invalid runtime command']
+ ]) {
+ const command = await launcher(body)
+ await expect(ticketRuntimeFor({ command, kind: 'command', shell: false, root })).rejects.toThrow(reason)
+ }
+})
+
+test('backends with no interpreter to name stay refused', async () => {
+ for (const backend of [
+ { command: 'hermes', kind: 'command', shell: false },
+ { command: '"python.cmd"', kind: 'python', shell: true }
+ ]) {
+ await expect(ticketRuntimeFor(backend)).rejects.toThrow('requires the installed Hermes Python runtime')
+ }
+})
diff --git a/apps/desktop/electron/local-gateway-python.ts b/apps/desktop/electron/local-gateway-python.ts
index 4fb94092ae8..e4ac3d3f748 100644
--- a/apps/desktop/electron/local-gateway-python.ts
+++ b/apps/desktop/electron/local-gateway-python.ts
@@ -1,4 +1,6 @@
import { type ChildProcessWithoutNullStreams, spawn } from 'node:child_process'
+import { statSync } from 'node:fs'
+import path from 'node:path'
import { hiddenWindowsChildOptions } from './windows-child-options'
@@ -160,10 +162,80 @@ interface ResolvedTicketBackend {
kind: string
shell?: boolean
env?: NodeJS.ProcessEnv
+ root?: string
+}
+
+const RUNTIME_COMMAND_TIMEOUT_MS = 30_000
+
+/** Ask an installation launcher for its interpreter; it owns ABI and store selection. */
+function launcherRuntimePython(backend: ResolvedTicketBackend & { root: string }): Promise<string> {
+ return new Promise((resolve, reject) => {
+ const fail = (detail: string) => reject(new Error(`Gateway ticket bootstrap could not resolve the installed Hermes Python runtime: ${detail}`))
+
+ const child = spawn(backend.command, ['--print-runtime-command'], hiddenWindowsChildOptions({
+ cwd: backend.root, env: { ...process.env, ...backend.env }, shell: Boolean(backend.shell), stdio: ['ignore', 'pipe', 'ignore']
+ }))
+
+ let output = ''
+ const timer = setTimeout(() => child.kill('SIGKILL'), RUNTIME_COMMAND_TIMEOUT_MS)
+ child.stdout!.setEncoding('utf8')
+ child.stdout!.on('data', data => {
+ output += String(data)
+
+ if (output.length > 65536) { child.kill('SIGKILL') }
+ })
+ child.on('error', error => { clearTimeout(timer); fail(error.message) })
+ child.on('close', code => {
+ clearTimeout(timer)
+
+ if (code !== 0) { fail(`launcher exited ${code ?? 'without a code'}; refresh this installation's launcher`);
+
+ return }
+
+ let command: unknown
+
+ try { command = JSON.parse(output) } catch { fail('launcher did not report a runtime command');
+
+ return }
+
+ const python = Array.isArray(command) ? command[0] : undefined
+
+ if (typeof python !== 'string' || !path.isAbsolute(python) || python.includes('\0') || !isFile(python)) {
+ fail('launcher reported an invalid runtime command');
+
+ return
+ }
+
+ resolve(python)
+ })
+ })
+}
+
+function isFile(candidate: string): boolean {
+ try { return statSync(candidate).isFile() } catch { return false }
+}
+
+/**
+ * The helper needs a bare interpreter. A Python backend is one; an installed
+ * launcher backend names its own. The helper is stdlib-only, so the checkout on
+ * PYTHONPATH is its whole import closure (-P still keeps the cwd out).
+ */
+export async function ticketRuntimeFor(backend: ResolvedTicketBackend): Promise<{ command: string; env?: NodeJS.ProcessEnv }> {
+ if (backend.kind === 'python' && !backend.shell) {
+ return backend
+ }
+
+ if (backend.kind === 'command' && backend.root) {
+ const python = await launcherRuntimePython({ ...backend, root: backend.root })
+
+ return { command: python, env: { ...backend.env, PYTHONPATH: backend.root } }
+ }
+
+ throw new Error('Gateway ticket bootstrap requires the installed Hermes Python runtime')
}
export function createGatewayTicketResolver(resolveBackend: () => Promise<ResolvedTicketBackend>, cwd: () => string) {
- let cached: Promise<ResolvedTicketBackend> | undefined
+ let cached: Promise<{ command: string; env?: NodeJS.ProcessEnv }> | undefined
let cachedGeneration = -1
return async (endpoint: TicketEndpoint, purpose: 'interactive' | 'native-http'): Promise<string> => {
@@ -172,13 +244,7 @@ export function createGatewayTicketResolver(resolveBackend: () => Promise<Resolv
if (!cached || cachedGeneration !== generation) {
cachedGeneration = generation
- const flight = resolveBackend().then(backend => {
- if (backend.kind !== 'python' || backend.shell) {
- throw new Error('Gateway ticket bootstrap requires the installed Hermes Python runtime')
- }
-
- return backend
- }).catch(error => {
+ const flight = resolveBackend().then(ticketRuntimeFor).catch(error => {
if (cached === flight) { cached = undefined }
throw error
})