{"record":{"id":"970656a3d5c397fc","repo":"Hmbown/CodeWhale","slug":"powershell-exe-exited-r-code-messages-join-raw-slice-0-1600","errorCode":null,"errorMessage":"powershell.exe exited ${r.code}: ${(messages.join(\"\") || raw).slice(0, 1600)}","messagePattern":"powershell\\.exe exited (.+?): (.+?)","errorType":"exception","errorClass":"ExecError","httpStatus":null,"severity":"error","filePath":"crates/tui/plugins/computer-use/src/backends/win32.mjs","lineNumber":190,"sourceCode":"    return runner(\"powershell.exe\", [\"-NoProfile\", \"-NonInteractive\", \"-EncodedCommand\", encoded], {\n      timeoutMs: o.timeoutMs ?? 25_000,\n      maxBuffer: 32 * 1024 * 1024,\n    });\n  }\n\n  /** ps() but truthful: timeout, nonzero exit, and spawn failure all throw. */\n  async function psOk(script, o = {}) {\n    const r = await ps(script, o);\n    if (r.aborted) throw Object.assign(new ExecError(\"computer request cancelled\", r), { code: \"cancelled\" });\n    if (r.timedOut) throw new ExecError(`powershell timed out after ${o.timeoutMs ?? 25_000}ms`, r);\n    if (r.code !== 0) {\n      const raw = (r.stderr || r.stdout).trim();\n      // EncodedCommand serializes errors as CLIXML; surface the error strings,\n      // not a truncated XML/progress header that conceals the actual failure.\n      const messages = [...raw.matchAll(/<S S=\"Error\">([\\s\\S]*?)<\\/S>/g)].map(m => m[1]\n        .replace(/_x([0-9A-Fa-f]{4})_/g, (_, hex) => String.fromCharCode(parseInt(hex, 16)))\n        .replace(/&lt;/g, \"<\").replace(/&gt;/g, \">\").replace(/&quot;/g, '\"').replace(/&apos;/g, \"'\").replace(/&amp;/g, \"&\"));\n      throw new ExecError(`powershell.exe exited ${r.code}: ${(messages.join(\"\") || raw).slice(0, 1600)}`, r);\n    }\n    return r;\n  }\n\n  async function psJson(script, o = {}) {\n    const r = await psOk(script, o);\n    const out = r.stdout.trim();\n    const j = tryJson(out, null);\n    if (!j) throw new ExecError(`powershell did not return JSON: ${(r.stderr || out).trim().slice(0, 300)}`, r);\n    return j;\n  }\n\n  let lastRaster = null;\n  let activeDisplay = null;\n  const heldButtons = new Set();\n  const heldKeys = new Set();\n\n  async function releaseInput({ buttons = [...heldButtons], keys = [...heldKeys] } = {}) {","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/plugins/computer-use/src/backends/win32.mjs#L172-L208","documentation":"After a PowerShell invocation finishes with a nonzero exit code, psOk extracts the CLIXML <S S=\"Error\"> messages (decoding _xHHHH_ escapes and XML entities) from stderr/stdout and throws this ExecError containing the exit code plus the decoded error text (truncated to 1600 chars). It surfaces the real PowerShell error strings rather than raw CLIXML noise.","triggerScenarios":"Running any psOk-backed script that throws a terminating error, hits a missing cmdlet/module, fails a P/Invoke call, or exits with a nonzero code (e.g. script uses `exit 1`, or a UIA COM call fails).","commonSituations":"Typo'd cmdlet or missing module (CommandNotFoundException); access denied talking to UIA or privileged windows; encoding corruption making the message garbled; scripts that `exit 1` to signal business-logic failures.","solutions":["Read the decoded error text in the message — it contains the actual PowerShell exception","Run the same script manually with `-EncodedCommand` (or paste it) to reproduce outside the harness","Add try/catch inside your PowerShell script and emit structured error JSON instead of a bare throw","Verify required modules/permissions (run elevated if UIA targets elevated windows)"],"exampleFix":"// before\nconst r = await psOk(script);\n// after\ntry { const r = await psOk(script); }\ncatch (e) { if (/powershell\\.exe exited/.test(e.message)) console.error('PS failure detail:', e.message); throw e; }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { return await psOk(script); }\ncatch (e) {\n  const detail = /powershell\\.exe exited \\d+: ([\\s\\S]*)/.exec(e.message)?.[1] ?? '';\n  log.error('PowerShell failed', detail.slice(0, 300));\n  throw e;\n}","preventionTips":["Wrap risky PowerShell statements in try/catch and emit structured errors","Verify required modules and elevation before running the script","Avoid bare `exit N` in scripts unless the caller expects it","Keep CLIXML decoding in mind: rely on the backend's decoded message, not raw stderr"],"tags":["powershell","nonzero-exit","clixml","windows","process"],"backgroundTag":"process-exit-nonzero","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-22T16:17:23.217Z"}