hermes-agent/apps/desktop/electron/backend-serve-support.ts
Brooklyn Nicholson b9cb268def fix(desktop): quote spaced Windows shell commands before cmd.exe truncates them
With shell: true, spawn hands the command line to cmd.exe, which cuts it at
the first unquoted space, so installs under spaced paths (C:\Users\John Doe\…)
fail every probe and backend launch. Adds windowsShellCommand() and applies it
at the four shell-delegating sites: the --version probe, the serve --help
probe, and both spawnOwnedBackend call sites.

Adapted from #74073 (thanks @dk3yyyy) to current site locations.
2026-09-29 18:42:10 -05:00

106 lines
4.1 KiB
TypeScript

import fs from 'node:fs'
import path from 'node:path'
import { sourceDeclaresServe } from './backend-command'
import { execProbe, isTimeoutError, PROBE_TIMEOUT_MS } from './backend-probes'
import { windowsShellCommand } from './windows-child-options'
interface ServeCandidate {
command?: string | null
root?: string
args?: string[]
env?: Record<string, string>
shell?: boolean
label?: string
}
// Does the resolved runtime understand the `serve` subcommand? The desktop
// spawns `hermes serve`; runtimes older than serve only have `dashboard`, so
// main.ts routes those through the legacy `dashboard --no-open` form instead
// of crashing on an unknown subcommand.
//
// Fast path: read the runtime's own dashboard.py (instant, covers managed
// installs, dev checkouts, and the Windows venv). Fallback: probe the CLI once
// (covers a bare `hermes` resolved from PATH with no known source root). Result
// is cached per resolved runtime so we probe at most once per backend — except
// a probe that failed by timeout, which is evicted so the next start re-probes
// rather than pinning a cold-AV false negative for the process lifetime.
//
// One cache per desktop runtime context; source inspection precedes a CLI probe.
export function createBackendServeSupportResolver(hermesHome: string, rememberLog: (message: string) => void) {
const cache = new Map<string, Promise<boolean>>()
return async function backendSupportsServe(backend: ServeCandidate): Promise<boolean> {
if (!backend || !backend.command) {
return true
}
const key = `${backend.command}::${backend.root || ''}`
if (cache.has(key)) {
return cache.get(key)!
}
const pending = (async () => {
let supported: boolean | null = null
if (backend.root) {
try {
const src = await fs.promises.readFile(
path.join(backend.root, 'hermes_cli', 'subcommands', 'dashboard.py'),
'utf8'
)
supported = sourceDeclaresServe(src)
} catch {
supported = null // source unreadable — fall through to the probe
}
}
if (supported === null) {
try {
const prefix = backend.args && backend.args[0] === '-m' ? backend.args.slice(0, 2) : []
// Same cold-Windows Python-startup class as the runtime probes
// (#61764/#72632/#72707): `serve --help` imports at least as much as
// `hermes --version` (~10.5s measured cold), and a false negative here
// is cached for the process lifetime, silently routing a modern
// runtime through the legacy `dashboard` form. Share the probe budget
// and its timeout-only retry instead of a thinner local bound.
await execProbe(windowsShellCommand(backend.command, Boolean(backend.shell)), [...prefix, 'serve', '--help'], {
cwd: backend.root || undefined,
env: { ...process.env, HERMES_HOME: hermesHome, ...(backend.env || {}) },
timeout: PROBE_TIMEOUT_MS,
stdio: 'ignore',
// `.cmd`/`.bat` shim backends carry shell: true in their descriptor
// (see resolveHermesBackend step 4); execFileSync of a .cmd without
// shell throws EINVAL on modern Node, which the catch below would
// mis-cache as "serve unsupported" for the process lifetime.
shell: Boolean(backend.shell),
windowsHide: true
})
supported = true
} catch (err) {
// A timeout says nothing about the runtime, only about this machine
// right now (cold AV scan, slow disk). Evict so the next call
// re-probes; a genuine "unknown subcommand" exit stays cached.
if (isTimeoutError(err) && cache.get(key) === pending) {
cache.delete(key)
}
supported = false
}
}
rememberLog(
`[backend] \`serve\` ${supported ? 'supported' : 'unsupported → routing via legacy `dashboard`'} for ${backend.label || key}`
)
return supported
})()
// Publish the promise before yielding; late results never overwrite a newer entry.
cache.set(key, pending)
return pending
}
}