{"record":{"id":"eb933a273f798b20","repo":"can1357/oh-my-pi","slug":"command-exited-with-code-result-exitcode","errorCode":null,"errorMessage":"Command exited with code ${result.exitCode}","messagePattern":"Command exited with code (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/extensibility/legacy-pi-coding-agent-shim.ts","lineNumber":384,"sourceCode":"\tlet output = \"\";\n\tconst onData = (data: Buffer) => {\n\t\toutput += data.toString(\"utf8\");\n\t\tif (onUpdate) {\n\t\t\tconst snapshot = legacyBashSnapshot(output);\n\t\t\tonUpdate({ content: [{ type: \"text\", text: snapshot.text }], details: snapshot.details });\n\t\t}\n\t};\n\ttry {\n\t\tconst result = await operations.exec(spawn.command, spawn.cwd, {\n\t\t\tonData,\n\t\t\tsignal,\n\t\t\ttimeout,\n\t\t\tenv: spawn.env,\n\t\t});\n\t\tconst snapshot = legacyBashSnapshot(output);\n\t\tconst text = snapshot.text || \"(no output)\";\n\t\tif (result.exitCode !== 0 && result.exitCode !== null) {\n\t\t\tthrow new Error(appendStatus(text, `Command exited with code ${result.exitCode}`));\n\t\t}\n\t\treturn { content: [{ type: \"text\", text }], details: snapshot.details };\n\t} catch (err) {\n\t\tconst snapshot = legacyBashSnapshot(output);\n\t\tconst text = snapshot.text;\n\t\tif (err instanceof Error && err.message === \"aborted\") {\n\t\t\tthrow new Error(appendStatus(text, \"Command aborted\"));\n\t\t}\n\t\tif (err instanceof Error && err.message.startsWith(\"timeout:\")) {\n\t\t\tthrow new Error(appendStatus(text, `Command timed out after ${err.message.slice(\"timeout:\".length)} seconds`));\n\t\t}\n\t\tthrow err;\n\t}\n}\n\n/**\n * Convert an image attachment to PNG using the legacy package-root contract.\n *","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/extensibility/legacy-pi-coding-agent-shim.ts#L366-L402","documentation":"The legacy bash tool ran a command via the legacy shim and the process exited with a non-zero, non-null exit code. The captured stdout/stderr snapshot is preserved in the message with 'Command exited with code N' appended, so partial output survives into the error.","triggerScenarios":"executeLegacyBashOperations spawns a shell command that terminates with a failing exit status; result.exitCode !== 0 && !== null (null means signal-killed, handled elsewhere).","commonSituations":"Agent runs a build/test command that fails; command syntax errors; grep/find returning 1 on no matches; scripts using exit codes as control flow.","solutions":["Read the captured output appended to the error to diagnose the command failure","Fix the underlying command, its arguments, cwd, or env","Treat non-zero exit as expected signal-handling in the agent loop rather than a hard failure","If the command legitimately uses non-zero exits, wrap it (e.g. 'cmd || true')"],"exampleFix":"// before\nrun(\"grep pattern file\")\n// after (tolerate no-match exit code 1)\nrun(\"grep pattern file || true\")","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"function isNonZeroExit(e: unknown): boolean { return e instanceof Error && /Command exited with code \\d+/.test(e.message); }","tryCatchPattern":"try { await bashTool.execute({ command }); } catch (err) {\n  if (err instanceof Error && err.message.includes('Command exited with code')) {\n    // treat as tool-result failure: feed output back to the model, don't crash\n    return { failed: true, output: err.message };\n  }\n  throw err;\n}","preventionTips":["Prefer commands that signal failure only through output, not exit codes, when non-zero is benign","Append '|| true' for commands with semantic non-zero exits (grep, test)","Validate cwd/env/arguments before spawning","Handle exit-code errors as normal tool results in agent loops"],"tags":["bash","exit-code","process","legacy-shim"],"backgroundTag":"nonzero-exit-code","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}