{"record":{"id":"cfa87aa20ee30502","repo":"Hmbown/CodeWhale","slug":"cmd-exited-r-code-trim-r-stderr-r-stdout","errorCode":null,"errorMessage":"${cmd} exited ${r.code}: ${trim(r.stderr || r.stdout)}","messagePattern":"(.+?) exited (.+?): (.+?)","errorType":"exception","errorClass":"ExecError","httpStatus":null,"severity":"error","filePath":"crates/tui/plugins/computer-use/src/exec.mjs","lineNumber":157,"sourceCode":"        });\n        child.stdin.write(JSON.stringify(message) + \"\\n\");\n        return await Promise.race([next, cancelled, new Promise((_, reject) => { commandTimer = setTimeout(() => reject(new ExecError(\"Native input owner did not acknowledge pointer motion\")), opts.timeoutMs ?? 20_000); })]);\n      }\n      catch (error) { await release().catch(() => {}); throw error; }\n      finally { clearTimeout(commandTimer); signal?.removeEventListener(\"abort\", abort); }\n    } };\n  } catch (error) {\n    await release().catch(() => {});\n    throw error;\n  } finally { clearTimeout(timer); }\n}\n\n/** run() and throw a typed error on non-zero exit / timeout. */\nexport async function runOk(cmd, args = [], opts = {}) {\n  const r = await run(cmd, args, opts);\n  if (r.aborted) throw Object.assign(new ExecError(\"computer request cancelled\", r), { code: \"cancelled\" });\n  if (r.timedOut) throw new ExecError(`timeout after ${opts.timeoutMs ?? 20_000}ms: ${cmd}`, r);\n  if (r.code !== 0) throw new ExecError(`${cmd} exited ${r.code}: ${trim(r.stderr || r.stdout)}`, r);\n  return r;\n}\n\nexport class ExecError extends Error {\n  constructor(message, result) {\n    super(message);\n    this.name = \"ExecError\";\n    this.result = result;\n  }\n}\n\n/** True when the executable exists on PATH (or opts.fullPath exists). */\nexport async function have(cmd) {\n  const probe = process.platform === \"win32\" ? \"where\" : \"which\";\n  const r = await run(probe, [cmd], { timeoutMs: 5000 });\n  return r.code === 0 && r.stdout.trim().length > 0;\n}\n","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/plugins/computer-use/src/exec.mjs#L139-L175","documentation":"runOk converts a non-zero child exit code into this ExecError containing the command, exit code, and trimmed stderr (or stdout as fallback). Unlike the timeout case, the process ran and finished but reported failure. The full run result is attached to the error for diagnosis.","triggerScenarios":"Any runOk-invoked command exiting non-zero: PowerShell script exceptions (thrown strings like 'element_read_only'), missing executables or modules, access-denied on files, or scripts that Write-Output an error JSON and exit 1.","commonSituations":"PowerShell execution policy blocking scripts; a UIA action failing inside the script; a required Windows feature/assembly missing; invalid arguments causing the command itself to fail fast.","solutions":["Read e.message / e.result.stderr for the root cause string emitted by the command.","Fix the underlying failure it reports (read-only element, missing file, permission) before retrying.","Run the same command manually in a shell to reproduce and inspect the full stderr.","Check PowerShell execution policy and module availability if the failure is a policy or cmdlet-not-found error."],"exampleFix":"// before\nawait runOk('powershell', ['-Command', script]);\n// after\ntry {\n  await runOk('powershell', ['-Command', script], { timeoutMs: 30000 });\n} catch (e) {\n  const detail = e.result?.stderr || e.result?.stdout || '';\n  throw new Error(`automation step failed: ${detail}`); // surface real cause\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"const isExitCodeError = (e) =>\n  e instanceof Error && / exited \\d+: /.test(e.message) && e.result != null;","tryCatchPattern":"try {\n  return await runOk(cmd, args, opts);\n} catch (e) {\n  if (isExitCodeError(e)) {\n    const cause = (e.result.stderr || e.result.stdout || '').trim();\n    throw new Error(`${cmd} failed (${e.message.match(/ exited (\\d+)/)?.[1]}): ${cause}`);\n  }\n  throw e;\n}","preventionTips":["Always inspect e.result.stderr/stdout before retrying a failed command.","Check PowerShell execution policy and module availability in target environments.","Reproduce failing commands manually to see full diagnostics.","Validate arguments and file paths before spawning the process."],"tags":["process","exit-code","powershell","command"],"backgroundTag":"command-not-found","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"}