Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(codex): in the bot box, run Ask/Auto commands in the box's sandbox instead of Codex's own bubblewrap
AdityaUmale512d24d38dc9
diff --git a/server/drivers/codex-approval-config.ts b/server/drivers/codex-approval-config.tsindex 199ae63bbd..68580a64be 100644--- a/server/drivers/codex-approval-config.ts+++ b/server/drivers/codex-approval-config.ts@@ -54,8 +54,11 @@ export interface CodexApprovalParams {}/** Codex persists these on its native thread, so they are explicit on start, resume and every turn.- * Codex has no narrower "edits only" sandbox, so a stray Edits value asks. */-export function namedApprovalParams(mode: Exclude<ApprovalMode, "custom">): CodexApprovalParams {+ * Codex has no narrower "edits only" sandbox, so a stray Edits value asks.+ * `boxed`: the engine runs in the bot box, whose seccomp filter refuses the mount calls Codex's own bubblewrap+ * needs, so the box is the sandbox there: Codex runs commands as they are (externalSandbox), and Ask asks before+ * every command that is not read-only. */+export function namedApprovalParams(mode: Exclude<ApprovalMode, "custom">, boxed = false): CodexApprovalParams {if (mode === "full") {return {thread: {@@ -70,17 +73,11 @@ export function namedApprovalParams(mode: Exclude<ApprovalMode, "custom">): Code},};}+ const approvalPolicy = boxed && mode !== "auto" ? "untrusted" : "on-request";+ const approvalsReviewer = mode === "auto" ? "auto_review" : "user";return {- thread: {- approvalPolicy: "on-request",- approvalsReviewer: mode === "auto" ? "auto_review" : "user",- sandbox: "workspace-write",- },- turn: {- approvalPolicy: "on-request",- approvalsReviewer: mode === "auto" ? "auto_review" : "user",- sandboxPolicy: { type: "workspaceWrite" },- },+ thread: { approvalPolicy, approvalsReviewer, sandbox: "workspace-write" },+ turn: { approvalPolicy, approvalsReviewer, sandboxPolicy: { type: boxed ? "externalSandbox" : "workspaceWrite" } },};}@@ -91,10 +88,12 @@ export function withResolvedSandbox(params: CodexApprovalParams, session: unknowif (!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 (!resolvedSandboxAllowed(requested.type, sandbox.type)) {+ // A thread cannot start in externalSandbox, only a turn can: the thread starts in workspace-write.+ const external = requested.type === "externalSandbox";+ if (!resolvedSandboxAllowed(external ? "workspaceWrite" : 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 } };+ return external ? params : { ...params, turn: { ...params.turn, sandboxPolicy: sandbox } };}function legacyCustomApprovalParams(config: Record<string, unknown>): CodexApprovalParams {diff --git a/server/drivers/codex-instructions.test.ts b/server/drivers/codex-instructions.test.tsindex 546ebf52c5..7eb1215150 100644--- a/server/drivers/codex-instructions.test.ts+++ b/server/drivers/codex-instructions.test.ts@@ -40,6 +40,8 @@ describe("Codex effective developer instructions", () => {expect(note).toContain("nobody");}expect(codexSandboxNote(namedApprovalParams("full"), "linux")).toBe("");+ // In the bot box Codex runs no sandbox of its own, so its uid mapping never applies.+ expect(codexSandboxNote(namedApprovalParams("ask", true), "linux")).toBe("");expect(codexSandboxNote(namedApprovalParams("ask"), "darwin")).toBe("");expect(codexSandboxNote(namedApprovalParams("auto"), "win32")).toBe("");diff --git a/server/drivers/codex-instructions.ts b/server/drivers/codex-instructions.tsindex 83fbb42c6f..be306228fb 100644--- a/server/drivers/codex-instructions.ts+++ b/server/drivers/codex-instructions.ts@@ -18,7 +18,7 @@ export function codexSandboxNote(if (platform !== "linux") return "";const sandboxPolicy = (params.turn.sandboxPolicy as { type?: unknown } | undefined)?.type;if (typeof sandboxPolicy === "string") {- return sandboxPolicy === "dangerFullAccess" ? "" : CODEX_LINUX_SANDBOX_NOTE;+ return sandboxPolicy === "dangerFullAccess" || sandboxPolicy === "externalSandbox" ? "" : CODEX_LINUX_SANDBOX_NOTE;}if (typeof params.thread.permissions === "string") return "";const threadSandbox = params.thread.sandbox;diff --git a/server/drivers/codex-mcp-mount.ts b/server/drivers/codex-mcp-mount.tsindex 74e2eb0d4e..456b307036 100644--- a/server/drivers/codex-mcp-mount.ts+++ b/server/drivers/codex-mcp-mount.ts@@ -41,6 +41,11 @@ export function boxProxyEnvNames(boxed: boolean = botBox.enabled()): string[] {return boxed ? [...BOX_PROXY_ENV, ...BOX_NO_PROXY_ENV] : [];}+/** Codex's own bubblewrap needs mount(), which the bot box's seccomp filter refuses: there the box is the sandbox. */+export function codexInSeccompBox(): boolean {+ return botBox.enabled() && botBox.seccomp() !== null;+}+/** A person's own stdio server mounted as configured gets its env only through Codex's own environment (env_vars),* which the bot box would otherwise cut down to its allow-list: the app-server spawn lets those names through. */export function customServerEnvBox(diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.tsindex 2f4e8434be..3624fc0d5b 100644--- a/server/drivers/codex.test.ts+++ b/server/drivers/codex.test.ts@@ -1046,6 +1046,39 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)expect(calls.some((call: { method: string }) => call.method === "turn/start")).toBe(false);});+ it.each([+ { approvalMode: "ask", approvalPolicy: "untrusted", approvalsReviewer: "user" },+ { approvalMode: "auto", approvalPolicy: "on-request", approvalsReviewer: "auto_review" },+ ] as const)("in the bot box with its seccomp filter, runs $approvalMode turns in the box's sandbox instead of Codex's own", async ({ approvalMode, approvalPolicy, approvalsReviewer }) => {+ vi.spyOn(botBox, "enabled").mockReturnValue(true);+ vi.spyOn(botBox, "seccomp").mockReturnValue("/data/box/seccomp.bpf");+ await create({ mode: "resume" });+ const dump = join(scratch, `box-sandbox-${approvalMode}.json`);+ process.env.FAKE_CODEX_DUMP = dump;+ await instance.adapter.sendTurn({ threadId: `t-box-sandbox-${approvalMode}`, text: "ls /", approvalMode });+ await recorder.until((event) => event.type === "turn.completed");+ expect(recorder.events.at(-1)).toMatchObject({ ok: true });+ const calls = JSON.parse(readFileSync(dump, "utf8")).calls as Array<{ method: string; params: Record<string, unknown> }>;+ expect(calls.find((call) => call.method === "thread/start")?.params).toMatchObject({ approvalPolicy, approvalsReviewer, sandbox: "workspace-write" });+ expect(calls.find((call) => call.method === "turn/start")?.params).toMatchObject({+ approvalPolicy, approvalsReviewer, sandboxPolicy: { type: "externalSandbox" },+ });+ });++ it("keeps Codex's own sandbox when the box runs without a seccomp filter", async () => {+ vi.spyOn(botBox, "enabled").mockReturnValue(true);+ vi.spyOn(botBox, "seccomp").mockReturnValue(null);+ await create({ mode: "resume" });+ const dump = join(scratch, "box-no-seccomp.json");+ process.env.FAKE_CODEX_DUMP = dump;+ await instance.adapter.sendTurn({ threadId: "t-box-no-seccomp", text: "ls /", approvalMode: "ask" });+ await recorder.until((event) => event.type === "turn.completed");+ const calls = JSON.parse(readFileSync(dump, "utf8")).calls as Array<{ method: string; params: Record<string, unknown> }>;+ expect(calls.find((call) => call.method === "turn/start")?.params).toMatchObject({+ approvalPolicy: "on-request", sandboxPolicy: { type: "workspaceWrite" },+ });+ });+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.tsindex 14975332e9..1f9a9f3954 100644--- a/server/drivers/codex.ts+++ b/server/drivers/codex.ts@@ -5,7 +5,7 @@ import { codexToolSurfaceArgs } from "./codex-tool-surface.ts";import { codexShellDisabled, codexTurnConfigArgs } from "./codex-guest.ts"; import { shellOff } from "./shell-off.ts";import { TEAM_TOOLS_FAILED, teamToolsFailed } from "./team-tools.ts";import { enabledMcpMounts, mcpStartupTracker, supportsMcpStartup } from "./codex-mcp-startup.ts";-import { boxAdmittedEnv, boxProxyEnvNames, customServerEnvBox, type McpApprovalMode, mountMcpServer } from "./codex-mcp-mount.ts";+import { boxAdmittedEnv, boxProxyEnvNames, codexInSeccompBox, customServerEnvBox, type McpApprovalMode, mountMcpServer } from "./codex-mcp-mount.ts";import { reportUnsupportedCalls, rolloutMark, type RolloutMark } from "./codex-unsupported-calls.ts";// The `codex` CLI headless over its app-server JSON-RPC (NDJSON on stdio): completion is// `turn/completed`, approvals arrive as server→client requests, resumeCursor is the codex thread id.@@ -1237,7 +1237,7 @@ export const CodexDriver: ProviderDriver<CodexConfig> = {: null;const promptSplit = stableInstructions !== null;// Omitting these on a resumed thread would keep the previous named mode.- let approvalParams: CodexApprovalParams = approvalMode === "custom" ? customApprovalParams(effectiveConfig) : namedApprovalParams(approvalMode);+ let approvalParams: CodexApprovalParams = approvalMode === "custom" ? customApprovalParams(effectiveConfig) : namedApprovalParams(approvalMode, codexInSeccompBox());const instructionsFor = (params: CodexApprovalParams) => codexDeveloperInstructions(effectiveConfig, stableInstructions ?? turn.system ?? "", shellOff(turn) ? "" : codexSandboxNote(params));// Validate effectiveConfig upfront so invalid native rules fail before thread creation/reuse.instructionsFor(approvalParams);