can1357/oh-my-pi · error · Error

Command exited with code ${result.exitCode}

Error message

Command exited with code ${result.exitCode}

What it means

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.

Source

Thrown at packages/coding-agent/src/extensibility/legacy-pi-coding-agent-shim.ts:384

	let output = "";
	const onData = (data: Buffer) => {
		output += data.toString("utf8");
		if (onUpdate) {
			const snapshot = legacyBashSnapshot(output);
			onUpdate({ content: [{ type: "text", text: snapshot.text }], details: snapshot.details });
		}
	};
	try {
		const result = await operations.exec(spawn.command, spawn.cwd, {
			onData,
			signal,
			timeout,
			env: spawn.env,
		});
		const snapshot = legacyBashSnapshot(output);
		const text = snapshot.text || "(no output)";
		if (result.exitCode !== 0 && result.exitCode !== null) {
			throw new Error(appendStatus(text, `Command exited with code ${result.exitCode}`));
		}
		return { content: [{ type: "text", text }], details: snapshot.details };
	} catch (err) {
		const snapshot = legacyBashSnapshot(output);
		const text = snapshot.text;
		if (err instanceof Error && err.message === "aborted") {
			throw new Error(appendStatus(text, "Command aborted"));
		}
		if (err instanceof Error && err.message.startsWith("timeout:")) {
			throw new Error(appendStatus(text, `Command timed out after ${err.message.slice("timeout:".length)} seconds`));
		}
		throw err;
	}
}

/**
 * Convert an image attachment to PNG using the legacy package-root contract.
 *

View on GitHub (pinned to 9690622007)

Solutions

  1. Read the captured output appended to the error to diagnose the command failure
  2. Fix the underlying command, its arguments, cwd, or env
  3. Treat non-zero exit as expected signal-handling in the agent loop rather than a hard failure
  4. If the command legitimately uses non-zero exits, wrap it (e.g. 'cmd || true')

Example fix

// before
run("grep pattern file")
// after (tolerate no-match exit code 1)
run("grep pattern file || true")
Defensive patterns

Strategy: try-catch

Type guard

function isNonZeroExit(e: unknown): boolean { return e instanceof Error && /Command exited with code \d+/.test(e.message); }

Try / catch

try { await bashTool.execute({ command }); } catch (err) {
  if (err instanceof Error && err.message.includes('Command exited with code')) {
    // treat as tool-result failure: feed output back to the model, don't crash
    return { failed: true, output: err.message };
  }
  throw err;
}

Prevention

When it happens

Trigger: executeLegacyBashOperations spawns a shell command that terminates with a failing exit status; result.exitCode !== 0 && !== null (null means signal-killed, handled elsewhere).

Common situations: 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.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/eb933a273f798b20. Report an issue: GitHub.