Skip to content

The agent that grows with you

fix(desktop): ticket runtime survives Store bundles and spaced .cmd launchers

Teknium59417b261302
diff --git a/apps/desktop/electron/local-gateway-python-bundled.test.ts b/apps/desktop/electron/local-gateway-python-bundled.test.ts
new file mode 100644
index 00000000000..7a10189c2ae
--- /dev/null
+++ b/apps/desktop/electron/local-gateway-python-bundled.test.ts
@@ -0,0 +1,66 @@
+import fs from 'node:fs/promises'
+import net from 'node:net'
+import path from 'node:path'
+
+import { expect, test } from 'vitest'
+
+import type { InstallStamp } from './install-stamp'
+import { closeGatewayTicketBridges, createGatewayTicketResolver } from './local-gateway-python'
+import { shortSocketTmpDir } from './local-gateway.test-helpers'
+import { bundledPayload, payloadTicketRuntime } from './payload-backend'
+
+// #138906 (RT-21): the Store/MSIX bundled backend is `kind: 'python'` but its command is the
+// payload's CLI shim (bin/hermes.exe), which rejects the ticket helper's `-P -u -c`. This runs
+// the real resolver and helper process against a payload laid out the way the builder stamps it.
+test.skipIf(process.platform === 'win32')('a bundled payload mints tickets with its interpreter, not the CLI shim', async () => {
+ const home = await shortSocketTmpDir('gw-bundled-')
+ const resources = await shortSocketTmpDir('gw-bundled-res-')
+ const repo = path.resolve('../..')
+ const python = process.env.HERMES_TEST_PYTHON || 'python3'
+ const root = path.join(resources, 'agent-payload')
+ await fs.mkdir(path.join(root, 'bin'), { recursive: true })
+ await fs.mkdir(path.join(root, 'tools', 'python', 'bin'), { recursive: true })
+ await fs.mkdir(path.join(root, 'venv', 'site-packages'), { recursive: true })
+ await fs.symlink(repo, path.join(root, 'app'))
+ // The CLI shim: like hermes.exe, it is not an interpreter and refuses interpreter flags.
+ await fs.writeFile(path.join(root, 'bin', 'hermes'), '#!/bin/sh\necho "hermes: unrecognized arguments: $1" >&2\nexit 2\n', { mode: 0o755 })
+ const { execFileSync } = await import('node:child_process')
+ const realPython = execFileSync(python, ['-c', 'import sys; print(sys.executable)'], { encoding: 'utf8' }).trim()
+ await fs.symlink(realPython, path.join(root, 'tools', 'python', 'bin', 'python3'))
+
+ const stamp = {
+ schemaVersion: 1, commit: 'a'.repeat(40), commitDate: null, branch: null, builtAt: null, dirty: false,
+ source: 'ci', distribution: 'desktop-app', updateMechanism: 'microsoft-store', baseVersion: null,
+ displayVersion: null, distance: null, payload: 'bundled', tag: 'v1.0.0',
+ runtime: { repoDir: 'app', toolsDir: 'tools', storePython: 'tools/python/bin/python3',
+ sitePackages: 'venv/site-packages', commands: { hermes: 'bin/hermes' } }
+ } as unknown as InstallStamp
+
+ const payload = bundledPayload(resources, stamp)!
+ const endpoint = { profile_id: home, instance_id: 'owner', runtime_protocol: 1, control_home: null }
+
+ // The shape resolveHermesBackend() returns for a bundled payload (main.ts).
+ const backend = {
+ kind: 'python', command: payload.shim, shell: false, root: payload.repoDir,
+ env: { PYTHONPATH: '', PYTHONHOME: '', HERMES_RUNTIME_DIR: payload.toolsDir }, ...payloadTicketRuntime(payload)
+ }
+
+ const server = net.createServer(socket => socket.once('data', () => {
+ socket.end(JSON.stringify({ protocol: 1, id: 1, ok: true, result: { ...endpoint, ticket: 'bundled-grant' } }) + '\n')
+ }))
+
+ await new Promise<void>(resolve => server.listen(path.join(home, 'gateway.sock'), resolve))
+ await fs.chmod(path.join(home, 'gateway.sock'), 0o600)
+ await fs.chmod(home, 0o700)
+
+ try {
+ const mint = createGatewayTicketResolver(async () => backend, () => home)
+ const ticket = await mint(endpoint, 'native-http')
+ expect(ticket).toBe('bundled-grant')
+ } finally {
+ closeGatewayTicketBridges()
+ await new Promise<void>(resolve => server.close(() => resolve()))
+ await fs.rm(home, { recursive: true, force: true })
+ await fs.rm(resources, { recursive: true, force: true })
+ }
+}, 30_000)
diff --git a/apps/desktop/electron/local-gateway-python-launcher-argv.test.ts b/apps/desktop/electron/local-gateway-python-launcher-argv.test.ts
new file mode 100644
index 00000000000..b03f70fabfc
--- /dev/null
+++ b/apps/desktop/electron/local-gateway-python-launcher-argv.test.ts
@@ -0,0 +1,57 @@
+import type * as ChildProcessAPI from 'node:child_process'
+import { EventEmitter } from 'node:events'
+import { PassThrough } from 'node:stream'
+
+import { afterEach, expect, test, vi } from 'vitest'
+
+const spawned = vi.hoisted(() => [] as { command: string; args: string[]; options: any }[])
+
+vi.mock('node:child_process', async importOriginal => {
+ const actual = await importOriginal<typeof ChildProcessAPI>()
+
+ return { ...actual, spawn: (command: string, args: string[], options: any) => {
+ spawned.push({ command, args, options })
+ const child = Object.assign(new EventEmitter(), { stdout: new PassThrough(), kill: () => true })
+ setImmediate(() => {
+ child.stdout.end(JSON.stringify([process.execPath, '-I', '-c', 'bootstrap']))
+ child.emit('close', 0)
+ })
+
+ return child
+ } }
+})
+
+const { ticketRuntimeFor } = await import('./local-gateway-python')
+
+const realPlatform = process.platform
+
+afterEach(() => {
+ Object.defineProperty(process, 'platform', { value: realPlatform })
+ spawned.length = 0
+})
+
+// RT-10 (#138774): a HERMES_DESKTOP_HERMES=...\hermes.cmd override is a shell backend. With
+// shell:true Node hands cmd.exe one unquoted line, which a profile path with a space splits at
+// `C:\Users\John` (#74064). The runtime-command probe must quote the launcher like every other
+// shell spawn (windowsShellCommand), and keep the probe flag a separate argument.
+test('a .cmd override in a path with spaces asks its launcher for the runtime as one quoted token', async () => {
+ Object.defineProperty(process, 'platform', { value: 'win32' })
+ const command = 'C:\\Users\\John Doe\\AppData\\Local\\hermes\\bin\\hermes.cmd'
+ const root = 'C:\\Users\\John Doe\\AppData\\Local\\hermes\\hermes-agent'
+
+ await expect(ticketRuntimeFor({ command, kind: 'command', shell: true, root }))
+ .resolves.toEqual({ command: process.execPath, env: { PYTHONPATH: root } })
+ expect(spawned).toHaveLength(1)
+ expect(spawned[0].command).toBe(`"${command}"`)
+ expect(spawned[0].args).toEqual(['--print-runtime-command'])
+ expect(spawned[0].options).toMatchObject({ shell: true, cwd: root, windowsHide: true })
+})
+
+test('a launcher spawned without a shell keeps its bare path', async () => {
+ Object.defineProperty(process, 'platform', { value: 'win32' })
+ const command = 'C:\\Users\\John Doe\\AppData\\Local\\hermes\\bin\\hermes.exe'
+
+ await ticketRuntimeFor({ command, kind: 'command', shell: false, root: 'C:\\r' })
+ expect(spawned[0].command).toBe(command)
+ expect(spawned[0].options).toMatchObject({ shell: false })
+})
diff --git a/apps/desktop/electron/local-gateway-python.ts b/apps/desktop/electron/local-gateway-python.ts
index eb1c26650e3..25f6be5afdd 100644
--- a/apps/desktop/electron/local-gateway-python.ts
+++ b/apps/desktop/electron/local-gateway-python.ts
@@ -3,7 +3,7 @@ import { statSync } from 'node:fs'
import path from 'node:path'
import type { GatewayTicketPurpose } from './local-gateway'
-import { hiddenWindowsChildOptions } from './windows-child-options'
+import { hiddenWindowsChildOptions, windowsShellCommand } from './windows-child-options'
interface TicketEndpoint {
profile_id: string
@@ -176,7 +176,9 @@ function launcherRuntimePython(backend: ResolvedTicketBackend & { root: 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({
+ // A .cmd override is a shell backend: quote it as the backend spawn does, or cmd.exe
+ // splits a launcher path with a space at the first one (#74064).
+ const child = spawn(windowsShellCommand(backend.command, Boolean(backend.shell)), ['--print-runtime-command'], hiddenWindowsChildOptions({
cwd: backend.root, env: { ...process.env, ...backend.env }, shell: Boolean(backend.shell), stdio: ['ignore', 'pipe', 'ignore']
}))