Skip to content

Commit 2611629

Browse files
committed
fix(cursor): steer Windows bridge shell away from PS 5.1 syntax loops
Native-shell rejection told the model to replay the same bash/CMD command through the Codex bridge, which on Windows PowerShell 5.1 fails and loops (#604). Adapt for the host shell and inject PS-safe guidance instead.
1 parent ca7b104 commit 2611629

4 files changed

Lines changed: 60 additions & 9 deletions

File tree

‎src/adapters/cursor/native-exec-shell.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,19 @@ import { errorText, execBytes, execStreamCloseBytes } from "./native-exec-common
2323
const backgroundShells = new Map<number, { child: ChildProcessWithoutNullStreams; outputLength: number }>();
2424
let nextShellId = 1;
2525

26-
const NATIVE_SHELL_DISABLED =
27-
"Route this through the Codex bridge shell tool from the current catalog (`shell_command` or `exec_command`, including the long `mcp_opencodex-responses_*` display name if listed). Do not tell the user shell access is blocked, disabled, or denied — silently call that bridge tool with the same command.";
26+
function nativeShellDisabledMessage(): string {
27+
// Do not insist on "the same command" — on Windows PowerShell 5.1 that steers the
28+
// model into replaying bash/CMD idioms (`cd /d`, `&&`, heredocs) through the bridge
29+
// and looping (issue #604).
30+
const adapt = process.platform === "win32"
31+
? "Adapt the command for Windows PowerShell 5.1 before calling the bridge (no CMD `cd /d`, no bash heredocs, no `&&` — use `;` / PowerShell cmdlets). After a shell failure, fix the syntax once; do not repeat the same failing command."
32+
: "Adapt the command for the host shell if needed before calling the bridge. After a shell failure, fix the command once; do not repeat the same failing command.";
33+
return (
34+
"Route this through the Codex bridge shell tool from the current catalog (`shell_command` or `exec_command`, including the long `mcp_opencodex-responses_*` display name if listed). "
35+
+ "Do not tell the user shell access is blocked, disabled, or denied — silently call that bridge tool. "
36+
+ adapt
37+
);
38+
}
2839

2940
function rejectedShellResult(command: string, cwd: string, started: number) {
3041
return create(ShellResultSchema, {
@@ -36,7 +47,7 @@ function rejectedShellResult(command: string, cwd: string, started: number) {
3647
exitCode: 1,
3748
signal: "",
3849
stdout: "",
39-
stderr: NATIVE_SHELL_DISABLED,
50+
stderr: nativeShellDisabledMessage(),
4051
executionTime: Date.now() - started,
4152
aborted: true,
4253
}),
@@ -95,7 +106,7 @@ export function rejectShellStreamExecForPolicy(execMsg: ExecServerMessage): Uint
95106
event: { case: "start", value: create(ShellStreamStartSchema, { sandboxPolicy: args.requestedSandboxPolicy }) },
96107
})),
97108
execBytes(execMsg, "shellStream", create(ShellStreamSchema, {
98-
event: { case: "stderr", value: create(ShellStreamStderrSchema, { data: NATIVE_SHELL_DISABLED }) },
109+
event: { case: "stderr", value: create(ShellStreamStderrSchema, { data: nativeShellDisabledMessage() }) },
99110
})),
100111
execBytes(execMsg, "shellStream", create(ShellStreamSchema, {
101112
event: { case: "exit", value: create(ShellStreamExitSchema, { code: 1, cwd, aborted: true }) },
@@ -196,7 +207,7 @@ export function rejectBackgroundShellSpawnExecForPolicy(execMsg: ExecServerMessa
196207
const args = execMsg.message.value;
197208
const cwd = resolve(args.workingDirectory || process.cwd());
198209
return execBytes(execMsg, "backgroundShellSpawnResult", create(BackgroundShellSpawnResultSchema, {
199-
result: { case: "error", value: create(BackgroundShellSpawnErrorSchema, { command: args.command, workingDirectory: cwd, error: NATIVE_SHELL_DISABLED }) },
210+
result: { case: "error", value: create(BackgroundShellSpawnErrorSchema, { command: args.command, workingDirectory: cwd, error: nativeShellDisabledMessage() }) },
200211
}));
201212
}
202213

@@ -233,7 +244,7 @@ export function backgroundShellSpawnExec(execMsg: ExecServerMessage): Uint8Array
233244
export function rejectWriteShellStdinExecForPolicy(execMsg: ExecServerMessage): Uint8Array {
234245
if (execMsg.message.case !== "writeShellStdinArgs") throw new Error("invalid shell stdin exec");
235246
return execBytes(execMsg, "writeShellStdinResult", create(WriteShellStdinResultSchema, {
236-
result: { case: "error", value: create(WriteShellStdinErrorSchema, { error: NATIVE_SHELL_DISABLED }) },
247+
result: { case: "error", value: create(WriteShellStdinErrorSchema, { error: nativeShellDisabledMessage() }) },
237248
}));
238249
}
239250

‎src/adapters/cursor/tool-definitions.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,10 @@ function discoveryToolLabel(wireNames: readonly string[]): string | undefined {
401401
export function buildCursorToolGuidanceSystemNote(
402402
tools: readonly Pick<OcxTool, "namespace" | "name" | "freeform">[] | undefined,
403403
toolChoice?: OcxRequestOptions["toolChoice"],
404+
options?: { platform?: NodeJS.Platform },
404405
): string | undefined {
405406
if (!tools?.length) return undefined;
407+
const platform = options?.platform ?? process.platform;
406408
const wireNames = [...new Set(
407409
tools
408410
.filter(tool => cursorToolAllowedByChoice(tool, toolChoice, tools))
@@ -417,6 +419,14 @@ export function buildCursorToolGuidanceSystemNote(
417419
const hasApplyPatch = cursorRequestAdvertisesApplyPatch(tools, toolChoice);
418420
const discoveryTools = discoveryToolLabel(wireNames);
419421
const unavailableNeighborNames = unavailableNeighborAgentToolNames(wireNames);
422+
const windowsShellNote = hasBareExec && platform === "win32"
423+
? "Host shell is Windows PowerShell 5.1-compatible: use PowerShell syntax only. Do not use CMD `cd /d`, bash heredocs (`<<EOF`), or `&&` chaining (unsupported on Windows PowerShell 5.1). Prefer `Set-Location`, `Get-Content`, `Select-String`, and `;` between statements. After a shell failure, fix the syntax once — do not repeat the same failing command through the bridge."
424+
: undefined;
425+
const bridgeFallbackNote = hasBareExec
426+
? (platform === "win32"
427+
? `If a Cursor-native file read, directory listing, grep, or shell operation is rejected by the runtime, silently use ${shellBridgeLabel} with an equivalent PowerShell-safe command (e.g. \`Get-Content\`, \`Get-ChildItem\`, \`Select-String\`). Do not tell the user access is blocked. For file edits, use \`apply_patch\` when available.`
428+
: `If a Cursor-native file read, directory listing, grep, or shell operation is rejected by the runtime, silently use ${shellBridgeLabel} with the equivalent shell command instead (e.g. \`cat\`, \`ls\`, \`rg\`, \`grep\`). Do not tell the user access is blocked. For file edits, use \`apply_patch\` when available.`)
429+
: undefined;
420430
const notes = [
421431
`Cursor tool calls: available tool names are exactly ${listedNames}.`,
422432
"Use the current tool catalog as ground truth and call only those exact names with their listed argument keys.",
@@ -432,6 +442,7 @@ export function buildCursorToolGuidanceSystemNote(
432442
hasBareExec
433443
? "Never tell the user that shell or read access is blocked, disabled, or denied unless the Codex shell bridge tool itself fails. Prefer the bridge over Cursor-native Shell/Read; do not narrate phrases like \"Native shell access is blocked\" — silently call `shell_command` / `exec_command`."
434444
: undefined,
445+
windowsShellNote,
435446
"Cursor product features (Chronicle, screen recording, Notes, Plans, background agents) are available only if this turn's catalog lists a matching tool; do not offer or promise them otherwise.",
436447
hasBareExec
437448
? `For file read/search/listing, use ${shellBridgeLabel} when no more specific listed tool is available.`
@@ -450,9 +461,7 @@ export function buildCursorToolGuidanceSystemNote(
450461
? `Use ${discoveryTools} only for explicit discovery/resource tasks, not generic tool-count demos.`
451462
: undefined,
452463
"Do not count or report a tool call unless a tool result was actually returned.",
453-
hasBareExec
454-
? `If a Cursor-native file read, directory listing, grep, or shell operation is rejected by the runtime, silently use ${shellBridgeLabel} with the equivalent shell command instead (e.g. \`cat\`, \`ls\`, \`rg\`, \`grep\`). Do not tell the user access is blocked. For file edits, use \`apply_patch\` when available.`
455-
: undefined,
464+
bridgeFallbackNote,
456465
].filter((note): note is string => typeof note === "string");
457466
return notes.join(" ");
458467
}

‎tests/cursor-native-exec-policy.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ describe("Cursor native exec sandbox policy", () => {
142142
expect(deniedShellText).toContain("exec_command");
143143
expect(deniedShellText).toContain("mcp_opencodex-responses_*");
144144
expect(deniedShellText).toContain("Do not tell the user");
145+
expect(deniedShellText).not.toContain("with the same command");
146+
expect(deniedShellText).toContain("do not repeat the same failing command");
145147
expect(deniedShellText).not.toContain("disabled by OpenCodex policy");
146148
expect(deniedShellText).not.toContain("sandbox denial");
147149
expect(deniedShell.message.case).toBe("shellResult");

‎tests/cursor-tool-definitions.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,35 @@ describe("Cursor tool definitions", () => {
324324
expect(note).toContain("Never tell the user that shell or read access is blocked");
325325
});
326326

327+
test("adds Windows PowerShell 5.1 host-shell guidance when platform is win32 (#604)", () => {
328+
const note = buildCursorToolGuidanceSystemNote(
329+
[{ name: "shell_command", description: "Run", parameters: {} }],
330+
undefined,
331+
{ platform: "win32" },
332+
);
333+
expect(note).toBeDefined();
334+
if (!note) throw new Error("Expected Cursor tool guidance note");
335+
336+
expect(note).toContain("Windows PowerShell 5.1");
337+
expect(note).toContain("cd /d");
338+
expect(note).toContain("<<EOF");
339+
expect(note).toContain("do not repeat the same failing command");
340+
expect(note).toContain("Get-Content");
341+
expect(note).not.toContain("`cat`, `ls`, `rg`, `grep`");
342+
});
343+
344+
test("keeps POSIX shell examples off Windows when platform is linux (#604)", () => {
345+
const note = buildCursorToolGuidanceSystemNote(
346+
[{ name: "exec_command", description: "Run", parameters: {} }],
347+
undefined,
348+
{ platform: "linux" },
349+
);
350+
expect(note).toBeDefined();
351+
if (!note) throw new Error("Expected Cursor tool guidance note");
352+
expect(note).not.toContain("Windows PowerShell 5.1");
353+
expect(note).toContain("`cat`, `ls`, `rg`, `grep`");
354+
});
355+
327356
test("adds codex-native edit guidance only when apply_patch is advertised", () => {
328357
const tools: OcxTool[] = [
329358
{ name: "exec_command", description: "Run", parameters: {} },

0 commit comments

Comments
 (0)