Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

fix: improve Windows compatibility for CLI commands and sandbox (#261)

Alpha Diop3a71175236ca
diff --git a/codex-cli/src/utils/agent/handle-exec-command.ts b/codex-cli/src/utils/agent/handle-exec-command.ts
index 93e8ea52aa..48f6f95410 100644
--- a/codex-cli/src/utils/agent/handle-exec-command.ts
+++ b/codex-cli/src/utils/agent/handle-exec-command.ts
@@ -272,7 +272,15 @@ async function getSandbox(runInSandbox: boolean): Promise<SandboxType> {
return SandboxType.MACOS_SEATBELT;
} else if (await isInLinux()) {
return SandboxType.NONE;
+ } else if (process.platform === "win32") {
+ // On Windows, we don't have a sandbox implementation yet, so we fall back to NONE
+ // instead of throwing an error, which would crash the application
+ log(
+ "WARNING: Sandbox was requested but is not available on Windows. Continuing without sandbox.",
+ );
+ return SandboxType.NONE;
}
+ // For other platforms, still throw an error as before
throw new Error("Sandbox was mandated, but no sandbox is available!");
} else {
return SandboxType.NONE;
diff --git a/codex-cli/src/utils/agent/platform-commands.ts b/codex-cli/src/utils/agent/platform-commands.ts
new file mode 100644
index 0000000000..7be02c7ac5
--- /dev/null
+++ b/codex-cli/src/utils/agent/platform-commands.ts
@@ -0,0 +1,86 @@
+/**
+ * Utility functions for handling platform-specific commands
+ */
+
+import { log, isLoggingEnabled } from "./log.js";
+
+/**
+ * Map of Unix commands to their Windows equivalents
+ */
+const COMMAND_MAP: Record<string, string> = {
+ ls: "dir",
+ grep: "findstr",
+ cat: "type",
+ rm: "del",
+ cp: "copy",
+ mv: "move",
+ touch: "echo.>",
+ mkdir: "md",
+};
+
+/**
+ * Map of common Unix command options to their Windows equivalents
+ */
+const OPTION_MAP: Record<string, Record<string, string>> = {
+ ls: {
+ "-l": "/p",
+ "-a": "/a",
+ "-R": "/s",
+ },
+ grep: {
+ "-i": "/i",
+ "-r": "/s",
+ },
+};
+
+/**
+ * Adapts a command for the current platform.
+ * On Windows, this will translate Unix commands to their Windows equivalents.
+ * On Unix-like systems, this will return the original command.
+ *
+ * @param command The command array to adapt
+ * @returns The adapted command array
+ */
+export function adaptCommandForPlatform(command: Array<string>): Array<string> {
+ // If not on Windows, return the original command
+ if (process.platform !== "win32") {
+ return command;
+ }
+
+ // Nothing to adapt if the command is empty
+ if (command.length === 0) {
+ return command;
+ }
+
+ const cmd = command[0];
+
+ // If cmd is undefined or the command doesn't need adaptation, return it as is
+ if (!cmd || !COMMAND_MAP[cmd]) {
+ return command;
+ }
+
+ if (isLoggingEnabled()) {
+ log(`Adapting command '${cmd}' for Windows platform`);
+ }
+
+ // Create a new command array with the adapted command
+ const adaptedCommand = [...command];
+ adaptedCommand[0] = COMMAND_MAP[cmd];
+
+ // Adapt options if needed
+ const optionsForCmd = OPTION_MAP[cmd];
+ if (optionsForCmd) {
+ for (let i = 1; i < adaptedCommand.length; i++) {
+ const option = adaptedCommand[i];
+ if (option && optionsForCmd[option]) {
+ adaptedCommand[i] = optionsForCmd[option];
+ }
+ }
+ }
+
+ if (isLoggingEnabled()) {
+ log(`Adapted command: ${adaptedCommand.join(" ")}`);
+ }
+
+ return adaptedCommand;
+}
diff --git a/codex-cli/src/utils/agent/sandbox/raw-exec.ts b/codex-cli/src/utils/agent/sandbox/raw-exec.ts
index c0dd7c912e..6cfb304731 100644
--- a/codex-cli/src/utils/agent/sandbox/raw-exec.ts
+++ b/codex-cli/src/utils/agent/sandbox/raw-exec.ts
@@ -8,6 +8,7 @@ import type {
} from "child_process";
import { log, isLoggingEnabled } from "../log.js";
+import { adaptCommandForPlatform } from "../platform-commands.js";
import { spawn } from "child_process";
import * as os from "os";
@@ -23,7 +24,21 @@ export function exec(
_writableRoots: Array<string>,
abortSignal?: AbortSignal,
): Promise<ExecResult> {
- const prog = command[0];
+ // Adapt command for the current platform (e.g., convert 'ls' to 'dir' on Windows)
+ const adaptedCommand = adaptCommandForPlatform(command);
+
+ if (
+ isLoggingEnabled() &&
+ JSON.stringify(adaptedCommand) !== JSON.stringify(command)
+ ) {
+ log(
+ `Command adapted for platform: ${command.join(
+ " ",
+ )} -> ${adaptedCommand.join(" ")}`,
+ );
+ }
+
+ const prog = adaptedCommand[0];
if (typeof prog !== "string") {
return Promise.resolve({
stdout: "",
@@ -72,7 +87,7 @@ export function exec(
detached: true,
};
- const child: ChildProcess = spawn(prog, command.slice(1), fullOptions);
+ const child: ChildProcess = spawn(prog, adaptedCommand.slice(1), fullOptions);
// If an AbortSignal is provided, ensure the spawned process is terminated
// when the signal is triggered so that cancellations propagate down to any
// long‑running child processes. We default to SIGTERM to give the process a