mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-17 04:11:35 +00:00
62 lines
2.4 KiB
TypeScript
62 lines
2.4 KiB
TypeScript
import { execFileSync } from 'node:child_process'
|
|
|
|
import { hiddenWindowsChildOptions } from '../windows-child-options'
|
|
|
|
interface StateDbPreflight {
|
|
python: string | null
|
|
script: string
|
|
home: string
|
|
log: (message: string) => void
|
|
/**
|
|
* The installation launcher of a PM-managed checkout (`.hermes/bin/hermes`).
|
|
* A managed checkout carries no venv of its own — the launcher owns
|
|
* interpreter and generation selection there — so the snapshot runs through
|
|
* it exactly like the update check does (`readSourceUpdate`).
|
|
*/
|
|
launcher?: string | null
|
|
}
|
|
|
|
// Synchronous by design: the caller must not stop the backend before the snapshot.
|
|
export function preflightStateDb({ python, script, home, log, launcher = null }: StateDbPreflight): void {
|
|
try {
|
|
const command: string | null = launcher ?? python
|
|
|
|
if (!command) {
|
|
throw new Error('Python not found')
|
|
}
|
|
|
|
const args: string[] = launcher ? ['--run-module', 'hermes_cli.backup_sqlite', home] : ['-I', '-S', script, home]
|
|
|
|
// Node refuses direct .cmd execFile; an older published launcher can still
|
|
// be one. Same fail-closed guard as the update check: shell:true would
|
|
// interpolate untrusted paths, so keep cmd.exe's one unavoidable parse
|
|
// closed instead.
|
|
const viaCmd: boolean = process.platform === 'win32' && /\.cmd$/i.test(command)
|
|
|
|
if (viaCmd && [command, ...args].some((value: string): boolean => /["%&|<>^\r\n]/.test(value))) {
|
|
throw new Error('The pre-flight snapshot contains an unsafe Windows command argument.')
|
|
}
|
|
|
|
const result: string = execFileSync(
|
|
viaCmd ? (process.env.ComSpec ?? 'cmd.exe') : command,
|
|
viaCmd
|
|
? ['/d', '/v:off', '/s', '/c', `""${command}" ${args.map((arg: string): string => `"${arg}"`).join(' ')}"`]
|
|
: args,
|
|
hiddenWindowsChildOptions({
|
|
encoding: 'utf8',
|
|
timeout: 30_000,
|
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
windowsVerbatimArguments: viaCmd
|
|
})
|
|
)
|
|
|
|
log(`[updates] state.db pre-flight: ${result.trim()}`)
|
|
} catch (error: unknown) {
|
|
const message =
|
|
`state.db pre-flight failed: ${error instanceof Error ? error.message : String(error)}. ` +
|
|
'Update cancelled before backend shutdown. Update the selected installation with its hermes update command, then retry.'
|
|
|
|
log(`[updates] ${message}`)
|
|
throw new Error(message, { cause: error })
|
|
}
|
|
}
|