Skip to content

an open-source alternative to the dots, bots and muses of the world, run an office of claude code/codex like agents on your laptop, sandboxes or anywhere, uses your existing subscriptions

fix(windows): spawn the direct-executable npm shim instead of cmd.exe

Chaitanya Giri14cb1e4c60b3
diff --git a/src/main/pty.ts b/src/main/pty.ts
index 75d5f1b5..55d90214 100644
--- a/src/main/pty.ts
+++ b/src/main/pty.ts
@@ -127,9 +127,11 @@ export function buildCmdCommandLine(resolved: string, args: string[]): string {
/** What an npm-style Windows `.cmd` shim actually runs, decoded from its text. */
export interface NpmShimTarget {
/** BARE interpreter name — `node`, `bun` or `deno` — to be resolved off PATH by
- * the caller (kept bare deliberately: see parseNpmCmdShim). */
- interpreter: string;
- /** Absolute, normalized win32 path to the script that interpreter should run. */
+ * the caller (kept bare deliberately: see parseNpmCmdShim), or NULL when the
+ * shim runs a native executable directly and there is no interpreter at all. */
+ interpreter: string | null;
+ /** Absolute, normalized win32 path to the script that interpreter should run —
+ * or to the executable itself when `interpreter` is null. */
scriptPath: string;
}
@@ -257,8 +259,29 @@ export function parseNpmCmdShim(shimPath: string, content: string): NpmShimTarge
if (head.slice(progTok.end, scriptTok.start).trim() !== '') return null;
progRaw = progTok.text;
} else {
+ // Two one-token shapes share this branch, told apart by what precedes the quote.
+ const before = head.slice(0, scriptTok.start).trim().replace(/^@/, '').trim();
+ if (before === '') {
+ // DIRECT-EXECUTABLE shim: nothing before the quoted target, so there is no
+ // interpreter — the shim runs a native binary. npm writes this whenever the
+ // package's bin has no shebang, which is the norm for a compiled CLI:
+ //
+ // "%dp0%\..\opencode-ai\bin\opencode.exe" %*
+ //
+ // opencode-ai is exactly that (its bin is ./bin/opencode.exe, a real
+ // binary), so EVERY Windows OpenCode install hit this shape, fell through to
+ // null, and got the cmd.exe fallback that truncates the hive protocol at its
+ // first newline. The agent then booted looking healthy with no idea it had
+ // an inbox. Handing the binary straight to CreateProcess with an argv array
+ // is strictly better than routing it through cmd.exe — same program, no
+ // shell parser in between.
+ const exe = expand(scriptTok.text);
+ if (!exe) return null;
+ // Only a real executable. Anything else here is a shape we have not modelled.
+ if (!/\.(exe|com)$/i.test(exe)) return null;
+ return { interpreter: null, scriptPath: exe };
+ }
// Classic ELSE arm: `node "<script>" %*` — the program is an unquoted word.
- const before = head.slice(0, scriptTok.start).trim();
const word = before.split(/\s+/).filter(Boolean).pop();
if (!word || word !== before) return null; // anything else before it → unknown shape
progRaw = word;
@@ -457,7 +480,7 @@ export class PtyManager {
* not installed or itself not a real .exe) degrades to exactly today's cmd.exe
* behaviour. Never throws.
*/
- private resolveWindowsShimSpawn(resolved: string): { file: string; script: string } | null {
+ private resolveWindowsShimSpawn(resolved: string): { file: string; script: string | null } | null {
if (process.platform !== 'win32') return null;
try {
const lower = resolved.toLowerCase();
@@ -478,6 +501,13 @@ export class PtyManager {
// install). Falling back to cmd.exe then at least reproduces today's error.
if (!existsSync(target.scriptPath)) return null;
+ // Direct-executable shim: the target IS the program. No interpreter to
+ // resolve, and no script argument to prepend — spawning it with an argv
+ // array is exactly what the cmd.exe route was standing in the way of.
+ if (target.interpreter === null) {
+ return { file: target.scriptPath, script: null };
+ }
+
const interp = this.resolveCommand(target.interpreter);
if (!interp.found) return null;
// Must be a REAL executable: if `node` itself only resolves to a `.cmd`
@@ -563,7 +593,11 @@ export class PtyManager {
// truncated it at the first newline, which is why Windows agents never
// learned they had an inbox/outbox and could not message each other.
file = shimSpawn.file;
- spawnArgs = [shimSpawn.script, ...(opts.args ?? [])];
+ // `script` is null for a direct-executable shim — there is nothing to
+ // prepend, the args go straight to the binary.
+ spawnArgs = shimSpawn.script === null
+ ? [...(opts.args ?? [])]
+ : [shimSpawn.script, ...(opts.args ?? [])];
} else {
file = needsCmd ? (process.env.ComSpec || 'cmd.exe') : resolved;
// #55: when routing through cmd.exe we must NOT pass `resolved` as a bare,
@@ -580,6 +614,25 @@ export class PtyManager {
spawnArgs = needsCmd
? buildCmdCommandLine(resolved, opts.args ?? [])
: (opts.args ?? []);
+ // The cmd.exe fallback is LOSSY and was previously SILENT, which is how a
+ // Windows agent could look perfectly healthy while never having received
+ // the hive protocol: cmd.exe cuts a multi-line argument at its first
+ // newline, and the whole protocol block rides on one such argument.
+ // resolveWindowsShimSpawn returns null for any shim shape it does not
+ // fully understand, so this path is reachable on a real machine while
+ // every unit test passes. Say so, loudly, with the two facts needed to
+ // diagnose it: which target refused to decode, and whether a multi-line
+ // argument is actually at risk in THIS spawn.
+ if (needsCmd) {
+ const multiline = (opts.args ?? []).some((a) => a.includes('\n'));
+ console.warn(
+ `[pty] Windows: "${resolved}" could not be decoded as an npm shim — falling back to cmd.exe.` +
+ (multiline
+ ? ' A MULTI-LINE ARGUMENT IS PRESENT AND WILL BE TRUNCATED AT ITS FIRST NEWLINE.' +
+ ' The agent will start and look healthy without ever receiving the hive protocol.'
+ : ' No multi-line argument in this spawn, so nothing is lost here.')
+ );
+ }
}
const proc = pty.spawn(file, spawnArgs, {
name: 'xterm-256color',
diff --git a/test/win-cmd-shim.test.cjs b/test/win-cmd-shim.test.cjs
index c0f54bd0..5fbfc139 100644
--- a/test/win-cmd-shim.test.cjs
+++ b/test/win-cmd-shim.test.cjs
@@ -273,3 +273,58 @@ test('single-line args still go through cmd.exe unchanged (fallback preserved)',
'/d /s /c ""C:\\Program Files\\x\\tool.cmd" --model gpt-5-codex"'
);
});
+
+/**
+ * DIRECT-EXECUTABLE shims — the shape that broke OpenCode on Windows.
+ *
+ * npm writes an interpreter-less shim whenever a package's `bin` target has no
+ * shebang, which is the norm for a compiled CLI. `opencode-ai` is exactly that:
+ * its bin is `./bin/opencode.exe`, a real binary. The resolver only modelled
+ * "interpreter + script", so it returned null for every Windows OpenCode install
+ * and the spawn fell back to cmd.exe — which truncates the multi-line hive
+ * protocol at its first newline. Agents booted looking healthy and had no idea
+ * they had an inbox.
+ *
+ * The shim body below is not hand-written: it is the verbatim output of npm's own
+ * `cmd-shim` package for a binary target.
+ */
+const DIRECT_EXE_SHIM = [
+ '@ECHO off',
+ 'GOTO start',
+ ':find_dp0',
+ 'SET dp0=%~dp0',
+ 'EXIT /b',
+ ':start',
+ 'SETLOCAL',
+ 'CALL :find_dp0',
+ '"%dp0%\\..\\opencode-ai\\bin\\opencode.exe" %*'
+].join('\r\n');
+
+test('a direct-executable shim resolves to the binary, with no interpreter', () => {
+ const t = parseNpmCmdShim('C:\\Users\\r\\AppData\\Roaming\\npm\\opencode.cmd', DIRECT_EXE_SHIM);
+ assert.ok(t, 'the shape npm generates for a binary bin must be understood');
+ assert.equal(t.interpreter, null, 'null interpreter marks "run this directly"');
+ assert.equal(
+ t.scriptPath,
+ 'C:\\Users\\r\\AppData\\Roaming\\npm\\..\\opencode-ai\\bin\\opencode.exe'
+ .replace(/\\[^\\]+\\\.\./, ''),
+ 'the target is expanded and normalized to an absolute path'
+ );
+});
+
+test('the hive prompt survives a direct-executable shim, where cmd.exe destroys it', () => {
+ const prompt = 'You are "Michael" (god-1), an autonomous agent.\n\nHIVE PROTOCOL\n- inbox\n- outbox';
+ // What the fixed path does: spawn the binary with an ARRAY, escaped by node-pty.
+ const line = argsToCommandLine('C:\\x\\opencode-ai\\bin\\opencode.exe', ['--prompt', prompt]);
+ assert.ok(line.includes('HIVE PROTOCOL'), 'the protocol block reaches the child intact');
+ assert.ok(line.includes('outbox'), 'including everything after the first newline');
+ // What the old fallback did: one pre-escaped string handed to cmd.exe, which
+ // treats the first newline as a statement separator.
+ const viaCmd = buildCmdCommandLine('C:\\x\\opencode.cmd', ['--prompt', prompt]);
+ assert.ok(viaCmd.split(/\r?\n/)[0].length < viaCmd.length, 'cmd.exe form is cut at a newline');
+});
+
+test('an interpreter-less shim pointing at a NON-executable is still refused', () => {
+ const odd = DIRECT_EXE_SHIM.replace('opencode.exe', 'opencode.txt');
+ assert.equal(parseNpmCmdShim('C:\\npm\\opencode.cmd', odd), null);
+});