{"record":{"id":"0bcd85b4134383b4","repo":"Hmbown/CodeWhale","slug":"powershell-exe-exited-r-code-r-stderr-r-stdout-trim-slice-0","errorCode":null,"errorMessage":"powershell.exe exited ${r.code}: ${(r.stderr || r.stdout).trim().slice(0, 300)}","messagePattern":"powershell\\.exe exited (.+?): (.+?)","errorType":"exception","errorClass":"ExecError","httpStatus":null,"severity":"error","filePath":"crates/tui/plugins/computer-use/src/backends/win32.mjs","lineNumber":134,"sourceCode":"      \"This held-input gesture requires a connected Codewhale Computer Use desktop helper so a disconnected client cannot leave keys or buttons pressed. Start the helper and reconnect before retrying.\"\n    ), { code: \"input_owner_required\" });\n  }\n\n  async function ps(script, o = {}) {\n    throwIfAborted();\n    const encoded = Buffer.from(script, \"utf16le\").toString(\"base64\");\n    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) throw new ExecError(`powershell.exe exited ${r.code}: ${(r.stderr || r.stdout).trim().slice(0, 300)}`, r);\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  const heldButtons = new Set();\n  const heldKeys = new Set();\n\n  async function releaseInput({ buttons = [...heldButtons], keys = [...heldKeys] } = {}) {\n    buttons = buttons.filter((button) => heldButtons.has(button));\n    keys = keys.filter((key) => heldKeys.has(key));","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/crates/tui/plugins/computer-use/src/backends/win32.mjs#L116-L152","documentation":"psOk throws this when powershell.exe terminates with a nonzero exit code. The message includes the exit code and up to 300 chars of stderr (or stdout if stderr is empty), surfacing whatever the script wrote. It exists so script failures (parse errors, thrown .NET exceptions, missing cmdlets) reach the caller truthfully instead of being swallowed.","triggerScenarios":"Any `ps()` run in the win32 backend whose ChildProcess exit code is not 0 — e.g. a PowerShell syntax error in a generated script, `Add-Type` failing, a cmdlet throwing a terminating error, or the process being terminated externally with a nonzero status.","commonSituations":"Windows PowerShell 5.1 vs PowerShell 7 syntax differences in generated scripts; execution policy or Constrained Language Mode blocking Add-Type; non-English locales writing errors to stderr; antivirus killing spawned PowerShell processes.","solutions":["Read the stderr/stdout excerpt in the message — it contains the actual PowerShell error","Re-run the failing script manually in powershell.exe to reproduce and see the full error","Check execution policy (Set-ExecutionPolicy) and that required modules/assemblies (.NET, System.Drawing, UIAutomation) load on the host","If a generated script broke, update the backend script template for the PowerShell version installed on the host"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  await psOk(script);\n} catch (e) {\n  if (e instanceof ExecError && /exited \\d+/.test(e.message)) {\n    console.error(\"powershell error detail:\", e.message);\n  }\n}","preventionTips":["Test generated scripts manually in powershell.exe on the target host","Keep scripts compatible with Windows PowerShell 5.1 and 7","Check execution policy and .NET assembly availability on new hosts"],"tags":["windows","powershell","process-execution","nonzero-exit"],"backgroundTag":"command-not-found","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-22T21:17:16.096Z"}