Files
orca/tools/win-update-e2e/powershell-runner.mjs
T
OrcaWinandBrennan Benson 0b71f3bfba test(e2e): prove the terminal daemon survives a main-process crash on Windows (#7742) (#9311)
* test(e2e): prove the terminal daemon survives a main-process crash on Windows (#7742)

Add a win-crash-survival e2e harness (sibling to win-update-e2e) that
force-kills ONLY the packaged app's real Electron main (resolved via
app.evaluate -> process.pid, /F no /T) and asserts the detached
orca-terminal-daemon.exe plus its ConPTY shell survive with no pwsh
0xE9 FailFast, then that a relaunch re-adopts the SAME daemon and the
reattached UI binds to the SAME survivor shell (proved via a per-shell
env sentinel read back through the restored terminal).

This guards the #7742 fix (standalone relocated daemon that outlives
main death) against regression. A directional `--expect orphaned`
profile fails on a fixed build, keeping the survival assertions honest.

Windows-only; reuses win-update-e2e app-driver/daemon-process modules.

* test(e2e): harden Windows crash-survival proof

* test(ci): keep crash survival gate durable

* test(e2e): tolerate restart hydration navigation

* test(e2e): prove exact shell input after crash

* perf(ci): avoid crash harness installer rebuilds

* test(ci): harden crash survival evidence and cost

* test(e2e): fail closed on authoritative crash target

* test(e2e): fail closed on crash liveness evidence

---------

Co-authored-by: Brennan Benson <79079362+brennanb2025@users.noreply.github.com>
2026-07-19 11:27:29 -07:00

93 lines
3.3 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']
// Cap sync probes so a wedged PowerShell call can't block the harness until the
// whole CI job times out. Callers can override via opts.timeout.
const DEFAULT_SYNC_TIMEOUT_MS = 60_000
/** Quote a value as a PowerShell single-quoted literal. */
export function quotePowerShellLiteral(value) {
return `'${String(value).replaceAll("'", "''")}'`
}
/**
* 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,
timeout: DEFAULT_SYNC_TIMEOUT_MS,
...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,
timeout: DEFAULT_SYNC_TIMEOUT_MS,
...opts
})
return {
code: result.status ?? (result.error ? -1 : 0),
stdout: result.stdout ?? '',
stderr: result.stderr ?? '',
error: result.error ?? null
}
}