{"record":{"id":"79a4abbd21b646c5","repo":"stablyai/orca","slug":"result-stderr-powershell-process-enumeration","errorCode":null,"errorMessage":"${result.stderr || 'PowerShell process enumeration failed'}","messagePattern":"\\$\\{result\\.stderr \\|\\| 'PowerShell process enumeration failed'\\}","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"config/scripts/idle-cpu-process-sampling.mjs","lineNumber":66,"sourceCode":"\nfunction readUnixProcesses() {\n  const stdout = execFileSync('ps', ['-axo', 'pid=,ppid=,pcpu=,rss=,cputime=,command='], {\n    encoding: 'utf8',\n    env: { ...process.env, LC_ALL: 'C', LANG: 'C' },\n    maxBuffer: 20 * 1024 * 1024\n  })\n  return parseUnixProcesses(stdout)\n}\n\nfunction readWindowsProcesses() {\n  const script =\n    'Get-CimInstance Win32_Process | Select-Object ProcessId,ParentProcessId,WorkingSetSize,CommandLine | ConvertTo-Json -Compress'\n  const result = spawnSync('powershell.exe', ['-NoProfile', '-Command', script], {\n    encoding: 'utf8',\n    maxBuffer: 20 * 1024 * 1024\n  })\n  if (result.status !== 0) {\n    throw new Error(result.stderr || 'PowerShell process enumeration failed')\n  }\n  const parsed = JSON.parse(result.stdout || '[]')\n  const entries = Array.isArray(parsed) ? parsed : [parsed]\n  return entries.map((entry) => ({\n    pid: Number(entry.ProcessId),\n    ppid: Number(entry.ParentProcessId),\n    percentCpu: 0,\n    cpuTimeSeconds: null,\n    rssBytes: Number(entry.WorkingSetSize) || 0,\n    command: String(entry.CommandLine || '')\n  }))\n}\n\nexport function readProcessRows() {\n  return process.platform === 'win32' ? readWindowsProcesses() : readUnixProcesses()\n}\n\nexport function descendantsOf(rows, rootPid) {","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/stablyai/orca/blob/1136503c6a231a16dce8f921f6fadb63d181e8db/config/scripts/idle-cpu-process-sampling.mjs#L48-L84","documentation":"readWindowsProcesses runs a PowerShell CIM query (Get-CimInstance Win32_Process) via spawnSync to enumerate processes on Windows. It throws when spawnSync returns a non-zero status, surfacing result.stderr (or a generic fallback). readProcessRows only selects this path when process.platform === 'win32', so the error is Windows-only. A non-zero status means PowerShell itself failed to run the query, distinct from a spawn error (which would appear on result.error and surface differently).","triggerScenarios":"powershell.exe is absent or not on PATH; PowerShell execution policy or profile blocks the command; the WinMgmt/CIM service is stopped or corrupted; the CIM provider returns an error to stderr.","commonSituations":"Minimal/hardened Windows CI image without PowerShell; WMI repository corruption; group policy restricting script execution; antivirus intercepting PowerShell.","solutions":["Confirm powershell.exe resolves on PATH (where powershell) on the host.","Inspect result.stderr in the thrown error for the PowerShell-level failure.","Verify the WinMgmt service is running (Get-Service WinMgmt) and the CIM repository is healthy.","On non-Windows hosts this code path never runs — confirm process.platform is not unexpectedly 'win32'."],"exampleFix":"// before\nconst result = spawnSync('powershell.exe', ['-NoProfile', '-Command', script], { encoding: 'utf8', maxBuffer: 20 * 1024 * 1024 })\nif (result.status !== 0) {\n  throw new Error(result.stderr || 'PowerShell process enumeration failed')\n}\n\n// after — surface spawn errors too, and keep a readable status\nif (result.error) {\n  throw result.error\n}\nif (result.status !== 0) {\n  throw new Error(`PowerShell process enumeration failed (status ${result.status}): ${result.stderr}`)\n}","handlingStrategy":"try-catch","validationCode":"const psOk = (() => {\n  try { spawnSync('powershell.exe', ['-NoProfile', '-Command', 'exit 0']); return true }\n  catch { return false }\n})()\nif (process.platform === 'win32' && !psOk) throw new Error('powershell.exe required on PATH')","typeGuard":null,"tryCatchPattern":"try {\n  rows = readProcessRows()\n} catch (err) {\n  if (process.platform !== 'win32') throw err\n  log.warn(`Windows process enumeration failed: ${err.message}; falling back to empty rows`)\n  rows = []\n}","preventionTips":["On Windows CI, confirm powershell.exe is on PATH and WinMgmt is running before sampling.","Treat a non-zero PowerShell status as a host/environment problem, not a sampling bug."],"tags":["windows","powershell","process-sampling"],"backgroundTag":null,"analyzedSha":"1136503c6a231a16dce8f921f6fadb63d181e8db","analyzedAt":"2026-08-12T23:15:58.167Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}