Files
orca/tools/win-update-e2e/powershell-runner.mjs
T
Jinwoo Hong e9b9836da5 feat(daemon): Phase 0 update-survival groundwork — daemon file log + packaged-update proof harness (#7532)
Two independent pieces, no behavior change to terminal handling:

1. Daemon lifecycle file log. The detached daemon runs with stdio
ignored, so field failures have zero daemon-side evidence. The daemon
now writes rotated NDJSON lifecycle events (startup/ready/hello
accept+reject/session create/attach/exit/kill/shutdown/uncaught
exceptions) to logs/daemon.log via a new optional --log-file fork arg.
Fail-open (any fs error disables logging), adoption-neutral (old
daemons without the arg keep working, protocol untouched), and the
diagnostic bundle collector now includes the file, bounded by the same
lookback window as trace spans.

2. tools/win-update-e2e: a packaged NSIS update proof harness. Installs
version N, drives the installed app (isolated userData), plants a
canary marker session, silently updates to N+1, relaunches, and
asserts an explicit expectations profile: --expect cold-restore
(today's behavior) or --expect survival (the Phase 1 target). Window
flashes are detected by baseline-diffed window enumeration with
canary-title attribution; daemons are identified by command-line
marker, never exe name. Refuses to run when a pre-existing Orca app is
running or (without --allow-existing-install) installed, and only
uninstalls an install it fully owns.
2026-07-06 01:09:54 -07:00

83 lines
2.9 KiB
JavaScript

// Thin wrapper around Windows PowerShell 5.1 for the harness's Win32 probes.
//
// Everything that inspects windows or processes goes through Windows PowerShell
// (powershell.exe) rather than pwsh, because 5.1 is guaranteed present on every
// Windows box and the .ps1 probes are written for its quirks (notably the
// single-item .Count pitfall — see window-enum.ps1 / daemon-processes.mjs).
import { spawn, spawnSync } from 'node:child_process'
const POWERSHELL = 'powershell.exe'
const BASE_ARGS = ['-NoProfile', '-NonInteractive', '-ExecutionPolicy', 'Bypass']
/**
* Run a .ps1 file synchronously and return { code, stdout, stderr }.
* scriptArgs is an array of string arguments passed after -File.
*/
export function runScriptFileSync(scriptPath, scriptArgs = [], opts = {}) {
const result = spawnSync(POWERSHELL, [...BASE_ARGS, '-File', scriptPath, ...scriptArgs], {
encoding: 'utf8',
maxBuffer: 64 * 1024 * 1024,
...opts
})
return {
code: result.status ?? (result.error ? -1 : 0),
stdout: result.stdout ?? '',
stderr: result.stderr ?? '',
error: result.error ?? null
}
}
/**
* Run a .ps1 file whose stdout is a single JSON document and return the parsed
* value. Throws with the raw stderr/stdout attached when parsing fails, so a
* malformed probe surfaces its actual PowerShell error instead of a bare
* SyntaxError.
*/
export function runScriptFileJson(scriptPath, scriptArgs = [], opts = {}) {
const { code, stdout, stderr, error } = runScriptFileSync(scriptPath, scriptArgs, opts)
if (error) {
throw new Error(`Failed to spawn PowerShell for ${scriptPath}: ${error.message}`)
}
const trimmed = stdout.trim()
if (!trimmed) {
throw new Error(
`PowerShell script ${scriptPath} produced no stdout (exit ${code}). stderr:\n${stderr}`
)
}
try {
return JSON.parse(trimmed)
} catch (parseError) {
throw new Error(
`PowerShell script ${scriptPath} did not emit valid JSON (exit ${code}): ` +
`${parseError.message}\n--- stdout ---\n${trimmed}\n--- stderr ---\n${stderr}`
)
}
}
/**
* Spawn a .ps1 file as a long-running background child. Returns the ChildProcess
* so the caller can track/stop it. Used for the window watch loop.
*/
export function spawnScriptFile(scriptPath, scriptArgs = [], opts = {}) {
return spawn(POWERSHELL, [...BASE_ARGS, '-File', scriptPath, ...scriptArgs], {
stdio: ['ignore', 'pipe', 'pipe'],
...opts
})
}
/** Run an inline command string synchronously and return { code, stdout, stderr }. */
export function runCommandSync(command, opts = {}) {
const result = spawnSync(POWERSHELL, [...BASE_ARGS, '-Command', command], {
encoding: 'utf8',
maxBuffer: 64 * 1024 * 1024,
...opts
})
return {
code: result.status ?? (result.error ? -1 : 0),
stdout: result.stdout ?? '',
stderr: result.stderr ?? '',
error: result.error ?? null
}
}