Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(codex): start the turn when Codex resolves a stricter sandbox

AdityaUmale6a8a4e32247e
diff --git a/server/drivers/codex-approval-config.ts b/server/drivers/codex-approval-config.ts
index fba57623cf..2d2a97114f 100644
--- a/server/drivers/codex-approval-config.ts
+++ b/server/drivers/codex-approval-config.ts
@@ -33,3 +33,12 @@ export function effectiveApprovalPolicy(value: unknown): unknown {
export function effectiveApprovalsReviewer(value: unknown): string {
return value === "auto_review" || value === "guardian_subagent" ? value : "user";
}
+
+const SANDBOX_ACCESS_ORDER: readonly unknown[] = ["readOnly", "workspaceWrite", "dangerFullAccess"];
+
+/** Codex may resolve a stricter sandbox than asked (read-only on Windows without its own sandbox), never a looser or unknown one. */
+export function resolvedSandboxAllowed(requested: unknown, resolved: unknown): boolean {
+ if (resolved === requested) return true;
+ const access = SANDBOX_ACCESS_ORDER.indexOf(resolved);
+ return access >= 0 && access < SANDBOX_ACCESS_ORDER.indexOf(requested);
+}
diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.ts
index 23ada54935..4e493c4310 100644
--- a/server/drivers/codex.test.ts
+++ b/server/drivers/codex.test.ts
@@ -951,6 +951,39 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)
expect(calls.find((call: { method: string }) => call.method === "turn/start").params.sandboxPolicy).toEqual(sandbox);
});
+ it.each([
+ { resumed: false, approvalMode: "ask", sandbox: { type: "readOnly" } },
+ { resumed: true, approvalMode: "ask", sandbox: { type: "readOnly" } },
+ { resumed: false, approvalMode: "full", sandbox: { type: "workspaceWrite" } },
+ { resumed: false, approvalMode: "full", sandbox: { type: "readOnly" } },
+ ] as const)("starts the turn in a stricter sandbox Codex chose itself, as on Windows: %j", async ({ resumed, approvalMode, sandbox }) => {
+ await create({ mode: "resume" });
+ const dump = join(scratch, "stricter-sandbox.json");
+ process.env.FAKE_CODEX_DUMP = dump;
+ process.env.FAKE_CODEX_RESOLVED_SANDBOX = JSON.stringify(sandbox);
+ await instance.adapter.sendTurn({ threadId: "t-stricter-sandbox", text: "continue", approvalMode,
+ ...(resumed ? { resumeCursor: "codex-thread-1" } : {}) });
+ await recorder.until((event) => event.type === "turn.completed");
+ expect(recorder.events.at(-1)).toMatchObject({ ok: true });
+ const calls = JSON.parse(readFileSync(dump, "utf8")).calls;
+ expect(calls.find((call: { method: string }) => call.method === "turn/start").params.sandboxPolicy).toEqual(sandbox);
+ });
+
+ it.each([
+ { approvalMode: "ask", sandbox: { type: "externalSandbox" } },
+ { approvalMode: "full", sandbox: { type: "externalSandbox" } },
+ ] as const)("refuses a resolved sandbox it cannot rank: %j", async ({ approvalMode, sandbox }) => {
+ await create({ mode: "resume" });
+ const dump = join(scratch, "unranked-sandbox.json");
+ process.env.FAKE_CODEX_DUMP = dump;
+ process.env.FAKE_CODEX_RESOLVED_SANDBOX = JSON.stringify(sandbox);
+ await instance.adapter.sendTurn({ threadId: "t-unranked-sandbox", text: "continue", approvalMode });
+ await recorder.until((event) => event.type === "turn.completed");
+ expect(recorder.events.at(-1)).toMatchObject({ ok: false });
+ const calls = JSON.parse(readFileSync(dump, "utf8")).calls;
+ expect(calls.some((call: { method: string }) => call.method === "turn/start")).toBe(false);
+ });
+
it.each([false, true].flatMap(resumed => [null, {}, { type: "dangerFullAccess" }].map(sandbox => ({ resumed, sandbox }))))("refuses an absent or mismatched resolved sandbox: %j", async ({ resumed, sandbox }) => {
await create({ mode: "resume" });
const dump = join(scratch, "invalid-sandbox.json");
diff --git a/server/drivers/codex.ts b/server/drivers/codex.ts
index a4c104680e..863bb962ac 100644
--- a/server/drivers/codex.ts
+++ b/server/drivers/codex.ts
@@ -65,7 +65,7 @@ import { parseProtocolAskQuestions, questionChoices } from "../../shared/ask-que
import { codexQuestionReply, openAsyncQuestion, type CodexAsk } from "./codex-questions.ts";
import { codexReleaseUpdate, codexUpdateCommand } from "./codex-update.ts";
export { codexUpdateCommand };
-import { effectiveApprovalPolicy, effectiveApprovalsReviewer } from "./codex-approval-config.ts"; import { AGENTS_TOOL_TIMEOUT_SECONDS } from "./agents-tool-limit.ts"; import { COMPUTER_PROXY_TIMEOUT_MS } from "../harness-budget.ts";
+import { effectiveApprovalPolicy, effectiveApprovalsReviewer, resolvedSandboxAllowed } from "./codex-approval-config.ts"; import { AGENTS_TOOL_TIMEOUT_SECONDS } from "./agents-tool-limit.ts"; import { COMPUTER_PROXY_TIMEOUT_MS } from "../harness-budget.ts";
import { canUseMcpServer } from "../../shared/tool-scope.ts";
import { assertToolScopeSupported } from "../../shared/tool-scope-support.ts";
import { appMountFor, gateServer, mcpStdioServer } from "../mcp-gate-config.ts"; import { OWN_APPS_SERVER } from "../../shared/own-apps.ts";
@@ -195,7 +195,7 @@ function withResolvedSandbox(params: CodexApprovalParams, session: unknown): Cod
if (!sandbox || typeof sandbox.type !== "string") {
throw new Error("Codex did not return its resolved sandbox policy. Update Codex, then retry; the turn was not started because its permissions could not be verified.");
}
- if (sandbox.type !== requested.type) {
+ if (!resolvedSandboxAllowed(requested.type, sandbox.type)) {
throw new Error("Codex did not apply the requested sandbox mode; cannot safely start the turn.");
}
return { ...params, turn: { ...params.turn, sandboxPolicy: sandbox } };