Skip to content

Commit 3ee0342

Browse files
authored
fix(codex): honor Docker bind write policy (#83849)
* fix(codex): honor docker bind write policy * docs: note docker bind sandbox fix * fix(codex): expose docker sandbox fallback tools
1 parent 10b313d commit 3ee0342

6 files changed

Lines changed: 246 additions & 11 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ Docs: https://docs.openclaw.ai
115115
- Messages/Codex: keep Codex direct/source chats on message-tool visible delivery by default while documenting and testing `messages.visibleReplies: "automatic"` as the old-mode opt-out; channel wildcard model overrides now apply to direct chats before harness delivery defaults.
116116
- Memory/QMD: keep archived session transcript hits visible after QMD export while preserving normal `.md` session ids that only resemble archive names. (#83518; fixes #83506) Thanks @tanshanshan.
117117
- Codex app-server: preserve network access for sandboxed Codex code-mode turns when the OpenClaw sandbox allows outbound egress. Fixes #83347. Thanks @YusukeIt0.
118+
- Codex app-server: honor writable Docker bind mounts for sandboxed workspace-write turns while disabling native Code Mode when container-path aliases or read-only bind shadows cannot be represented safely host-side. Fixes #83737. (#83849) Thanks @joshavant.
118119
- QA-Lab: keep the OTLP smoke decoder independent of removed OpenTelemetry generated-root internals.
119120
- Messages: default group/channel visible replies to automatic final delivery again, keeping `message_tool` opt-in for ambient/shared rooms and tool-reliable models.
120121
- CLI/TUI: force standalone `/exit` runs to terminate after `runTui` returns so onboarding-launched TUI children do not stay alive invisibly. (#83501) Thanks @fuller-stack-dev.

‎extensions/codex/src/app-server/run-attempt.test.ts‎

Lines changed: 102 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ describe("runCodexAppServerAttempt", () => {
748748
);
749749
});
750750

751-
it("does not expose sandbox shell tools unless non-Docker sandbox routing is available", async () => {
751+
it("keeps Docker sandbox shell tools hidden when native Code Mode can honor sandbox paths", async () => {
752752
testing.setOpenClawCodingToolsFactoryForTests(() => [
753753
createRuntimeDynamicTool("exec"),
754754
createRuntimeDynamicTool("process"),
@@ -769,11 +769,69 @@ describe("runCodexAppServerAttempt", () => {
769769
effectiveWorkspace: workspaceDir,
770770
sandboxSessionKey,
771771
sandbox: { enabled: true, backendId: "docker" } as never,
772+
nativeToolSurfaceEnabled: true,
772773
runAbortController: new AbortController(),
773774
sessionAgentId: "main",
774775
pluginConfig: {},
775776
onYieldDetected: () => undefined,
776777
});
778+
779+
expect(dockerTools.map((tool) => tool.name)).toEqual(["message"]);
780+
});
781+
782+
it("exposes Docker sandbox shell tools when native Code Mode cannot honor sandbox paths", async () => {
783+
testing.setOpenClawCodingToolsFactoryForTests(() => [
784+
createRuntimeDynamicTool("exec"),
785+
createRuntimeDynamicTool("process"),
786+
createRuntimeDynamicTool("message"),
787+
]);
788+
const sessionFile = path.join(tempDir, "session.jsonl");
789+
const workspaceDir = path.join(tempDir, "workspace");
790+
const params = createParams(sessionFile, workspaceDir);
791+
params.disableTools = false;
792+
const sandboxSessionKey = params.sessionKey;
793+
if (!sandboxSessionKey) {
794+
throw new Error("createParams must provide a sessionKey for Codex dynamic tool tests.");
795+
}
796+
797+
const tools = await testing.buildDynamicTools({
798+
params,
799+
resolvedWorkspace: workspaceDir,
800+
effectiveWorkspace: workspaceDir,
801+
sandboxSessionKey,
802+
sandbox: {
803+
enabled: true,
804+
backendId: "docker",
805+
docker: { binds: ["/tmp/openclaw-data:/data:rw"] },
806+
} as never,
807+
nativeToolSurfaceEnabled: false,
808+
runAbortController: new AbortController(),
809+
sessionAgentId: "main",
810+
pluginConfig: {},
811+
onYieldDetected: () => undefined,
812+
});
813+
814+
expect(tools.map((tool) => tool.name)).toEqual(["message", "sandbox_exec", "sandbox_process"]);
815+
expect(tools.find((tool) => tool.name === "sandbox_exec")?.description).toContain(
816+
"Docker container-path bind layout",
817+
);
818+
});
819+
820+
it("does not expose sandbox shell tools when sandbox routing is disabled", async () => {
821+
testing.setOpenClawCodingToolsFactoryForTests(() => [
822+
createRuntimeDynamicTool("exec"),
823+
createRuntimeDynamicTool("process"),
824+
createRuntimeDynamicTool("message"),
825+
]);
826+
const sessionFile = path.join(tempDir, "session.jsonl");
827+
const workspaceDir = path.join(tempDir, "workspace");
828+
const params = createParams(sessionFile, workspaceDir);
829+
params.disableTools = false;
830+
const sandboxSessionKey = params.sessionKey;
831+
if (!sandboxSessionKey) {
832+
throw new Error("createParams must provide a sessionKey for Codex dynamic tool tests.");
833+
}
834+
777835
const disabledSandboxTools = await testing.buildDynamicTools({
778836
params,
779837
resolvedWorkspace: workspaceDir,
@@ -786,7 +844,6 @@ describe("runCodexAppServerAttempt", () => {
786844
onYieldDetected: () => undefined,
787845
});
788846

789-
expect(dockerTools.map((tool) => tool.name)).toEqual(["message"]);
790847
expect(disabledSandboxTools.map((tool) => tool.name)).toEqual(["message"]);
791848
});
792849

@@ -1203,6 +1260,41 @@ describe("runCodexAppServerAttempt", () => {
12031260
expect(testing.shouldEnableCodexAppServerNativeToolSurface(params)).toBe(false);
12041261
});
12051262

1263+
it("disables Codex native tool surfaces when Docker bind targets need container paths", () => {
1264+
const workspaceDir = path.join(tempDir, "workspace");
1265+
const params = createParams(path.join(tempDir, "session.jsonl"), workspaceDir);
1266+
params.disableTools = false;
1267+
1268+
expect(
1269+
testing.shouldEnableCodexAppServerNativeToolSurface(params, {
1270+
enabled: true,
1271+
backendId: "docker",
1272+
docker: { binds: ["/tmp/openclaw-data:/data:rw"] },
1273+
} as never),
1274+
).toBe(false);
1275+
1276+
expect(
1277+
testing.shouldEnableCodexAppServerNativeToolSurface(params, {
1278+
enabled: true,
1279+
backendId: "docker",
1280+
docker: { binds: ["/tmp/openclaw-data:/tmp/openclaw-data:rw"] },
1281+
} as never),
1282+
).toBe(true);
1283+
1284+
expect(
1285+
testing.shouldEnableCodexAppServerNativeToolSurface(params, {
1286+
enabled: true,
1287+
backendId: "docker",
1288+
docker: {
1289+
binds: [
1290+
"/tmp/openclaw-data:/tmp/openclaw-data:rw",
1291+
"/tmp/openclaw-data/secrets:/tmp/openclaw-data/secrets:ro",
1292+
],
1293+
},
1294+
} as never),
1295+
).toBe(false);
1296+
});
1297+
12061298
it("forces the message dynamic tool for message-tool-only source replies", () => {
12071299
const workspaceDir = path.join(tempDir, "workspace");
12081300
const params = createParams(path.join(tempDir, "session.jsonl"), workspaceDir);
@@ -8962,13 +9054,19 @@ describe("runCodexAppServerAttempt", () => {
89629054
{
89639055
enabled: true,
89649056
backendId: "docker",
8965-
docker: { network: "bridge" },
9057+
docker: {
9058+
network: "bridge",
9059+
binds: [
9060+
"/tmp/openclaw-writable-data:/data:rw",
9061+
"/tmp/openclaw-readonly-data:/readonly:ro",
9062+
],
9063+
},
89669064
} as never,
89679065
"/tmp/workspace",
89689066
),
89699067
).toEqual({
89709068
type: "workspaceWrite",
8971-
writableRoots: ["/tmp/workspace"],
9069+
writableRoots: ["/tmp/workspace", path.resolve("/tmp/openclaw-writable-data")],
89729070
networkAccess: true,
89739071
excludeTmpdirEnvVar: false,
89749072
excludeSlashTmp: false,

‎extensions/codex/src/app-server/run-attempt.ts‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ import {
3434
resolveBootstrapContextForRun,
3535
setActiveEmbeddedRun,
3636
supportsModelTools,
37+
hasSandboxBindContainerPathAliases,
38+
hasSandboxBindReadonlyHostShadows,
39+
resolveWritableSandboxBindHostRoots,
3740
runAgentCleanupStep,
3841
type AgentMessage,
3942
type EmbeddedRunAttemptParams,
@@ -452,11 +455,17 @@ function resolveCodexAppServerSandboxPolicyForOpenClawSandbox(
452455
return undefined;
453456
}
454457
const networkAccess = codexNetworkAccessForOpenClawSandbox(sandbox);
458+
const writableRoots = new Set([cwd]);
459+
if (sandbox.backendId === "docker") {
460+
for (const root of resolveWritableSandboxBindHostRoots(sandbox.docker.binds)) {
461+
writableRoots.add(root);
462+
}
463+
}
455464
// Codex app-server still runs on the Gateway host, so keep Codex's
456465
// filesystem sandbox while mirroring the OpenClaw sandbox egress policy.
457466
return {
458467
type: "workspaceWrite",
459-
writableRoots: [cwd],
468+
writableRoots: [...writableRoots],
460469
networkAccess,
461470
excludeTmpdirEnvVar: false,
462471
excludeSlashTmp: false,
@@ -891,7 +900,7 @@ export async function runCodexAppServerAttempt(
891900
disableTools: params.disableTools,
892901
toolsAllow: params.toolsAllow,
893902
});
894-
const nativeToolSurfaceEnabled = shouldEnableCodexAppServerNativeToolSurface(params);
903+
const nativeToolSurfaceEnabled = shouldEnableCodexAppServerNativeToolSurface(params, sandbox);
895904
for (const diagnostic of bundleMcpThreadConfig.diagnostics) {
896905
embeddedAgentLog.warn(`bundle-mcp: ${diagnostic.pluginId}: ${diagnostic.message}`);
897906
}
@@ -906,6 +915,7 @@ export async function runCodexAppServerAttempt(
906915
effectiveWorkspace,
907916
sandboxSessionKey,
908917
sandbox,
918+
nativeToolSurfaceEnabled,
909919
runAbortController,
910920
sessionAgentId,
911921
pluginConfig,
@@ -3200,6 +3210,7 @@ type DynamicToolBuildParams = {
32003210
effectiveWorkspace: string;
32013211
sandboxSessionKey: string;
32023212
sandbox: Awaited<ReturnType<typeof resolveSandboxContext>>;
3213+
nativeToolSurfaceEnabled?: boolean;
32033214
runAbortController: AbortController;
32043215
sessionAgentId: string;
32053216
pluginConfig: CodexPluginConfig;
@@ -3354,15 +3365,33 @@ function includeForcedMessageToolAllow(
33543365
return normalized.has("message") ? toolsAllow : [...toolsAllow, "message"];
33553366
}
33563367

3357-
function shouldEnableCodexAppServerNativeToolSurface(params: EmbeddedRunAttemptParams): boolean {
3368+
function shouldEnableCodexAppServerNativeToolSurface(
3369+
params: EmbeddedRunAttemptParams,
3370+
sandbox?: OpenClawSandboxContext,
3371+
): boolean {
33583372
const toolsAllow = includeForcedMessageToolAllow(params.toolsAllow, params);
33593373
if (toolsAllow === undefined) {
3360-
return true;
3374+
return canCodexAppServerNativeToolSurfaceHonorSandbox(sandbox);
33613375
}
33623376
// Codex native code mode exposes its shell/file surface as one app-server
33633377
// capability, so narrow OpenClaw allowlists must fail closed rather than
33643378
// widening `message` or `web_search` into shell access.
3365-
return hasWildcardCodexToolsAllow(toolsAllow);
3379+
return (
3380+
hasWildcardCodexToolsAllow(toolsAllow) &&
3381+
canCodexAppServerNativeToolSurfaceHonorSandbox(sandbox)
3382+
);
3383+
}
3384+
3385+
function canCodexAppServerNativeToolSurfaceHonorSandbox(
3386+
sandbox: OpenClawSandboxContext | undefined,
3387+
): boolean {
3388+
if (!sandbox?.enabled || sandbox.backendId !== "docker") {
3389+
return true;
3390+
}
3391+
return (
3392+
!hasSandboxBindContainerPathAliases(sandbox.docker.binds) &&
3393+
!hasSandboxBindReadonlyHostShadows(sandbox.docker.binds)
3394+
);
33663395
}
33673396

33683397
function disableCodexPluginThreadConfig(pluginConfig?: unknown): CodexPluginConfig {
@@ -3398,7 +3427,7 @@ function addSandboxShellDynamicToolsIfAvailable(
33983427
...execTool,
33993428
name: "sandbox_exec",
34003429
description:
3401-
"Run a shell command through OpenClaw's configured sandbox backend for this session. Use only when the command must execute in the OpenClaw sandbox backend, such as an SSH-backed sandbox. Use Codex's native shell for normal local workspace commands.",
3430+
"Run a shell command through OpenClaw's configured sandbox backend for this session. Use only when the command must execute in the OpenClaw sandbox backend, such as an SSH-backed sandbox or Docker container-path bind layout that Codex's native shell cannot represent. Use Codex's native shell for normal local workspace commands.",
34023431
execute: async (toolCallId, args, signal, onUpdate) => {
34033432
const result = await execTool.execute(toolCallId, args, signal, onUpdate);
34043433
return {
@@ -3427,7 +3456,7 @@ function addSandboxShellDynamicToolsIfAvailable(
34273456

34283457
function shouldExposeSandboxExecDynamicTool(input: DynamicToolBuildParams): boolean {
34293458
const backendId = input.sandbox?.enabled ? input.sandbox.backendId.trim().toLowerCase() : "";
3430-
return Boolean(backendId && backendId !== "docker");
3459+
return Boolean(backendId && (backendId !== "docker" || input.nativeToolSurfaceEnabled === false));
34313460
}
34323461

34333462
function isSandboxShellDynamicToolExcluded(config: CodexPluginConfig): boolean {

‎src/agents/sandbox/fs-paths.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import path from "node:path";
33
import { describe, expect, it } from "vitest";
44
import {
55
buildSandboxFsMounts,
6+
hasSandboxBindContainerPathAliases,
7+
hasSandboxBindReadonlyHostShadows,
68
parseSandboxBindMount,
79
resolveSandboxFsPathWithMounts,
10+
resolveWritableSandboxBindHostRoots,
811
} from "./fs-paths.js";
912
import { createSandboxTestContext } from "./test-fixtures.js";
1013
import type { SandboxContext } from "./types.js";
@@ -47,6 +50,50 @@ describe("parseSandboxBindMount", () => {
4750
writable: false,
4851
});
4952
});
53+
54+
it("returns only unique writable bind host roots", () => {
55+
expect(
56+
resolveWritableSandboxBindHostRoots([
57+
"/tmp/data:/data:rw",
58+
"/tmp/read-only:/read-only:ro",
59+
"/tmp/default-write:/default-write",
60+
"/tmp/data:/data-two:rw",
61+
"invalid-bind",
62+
]),
63+
).toEqual([path.resolve("/tmp/data"), path.resolve("/tmp/default-write")]);
64+
});
65+
66+
it("omits writable bind roots that contain read-only host shadows", () => {
67+
expect(
68+
resolveWritableSandboxBindHostRoots([
69+
"/tmp/data:/tmp/data:rw",
70+
"/tmp/data/secrets:/tmp/data/secrets:ro",
71+
"/tmp/readonly-parent:/tmp/readonly-parent:ro",
72+
"/tmp/readonly-parent/work:/tmp/readonly-parent/work:rw",
73+
]),
74+
).toEqual([path.resolve("/tmp/readonly-parent/work")]);
75+
});
76+
77+
it("detects bind mounts whose container path differs from the host path", () => {
78+
expect(hasSandboxBindContainerPathAliases(["/tmp/data:/tmp/data:rw"])).toBe(false);
79+
expect(hasSandboxBindContainerPathAliases(["/tmp/data:/data:rw"])).toBe(true);
80+
expect(hasSandboxBindContainerPathAliases(["invalid-bind"])).toBe(false);
81+
});
82+
83+
it("detects read-only bind shadows inside writable host roots", () => {
84+
expect(
85+
hasSandboxBindReadonlyHostShadows([
86+
"/tmp/data:/tmp/data:rw",
87+
"/tmp/data/secrets:/tmp/data/secrets:ro",
88+
]),
89+
).toBe(true);
90+
expect(
91+
hasSandboxBindReadonlyHostShadows([
92+
"/tmp/data:/tmp/data:ro",
93+
"/tmp/data/work:/tmp/data/work:rw",
94+
]),
95+
).toBe(false);
96+
});
5097
});
5198

5299
describe("resolveSandboxFsPathWithMounts", () => {

‎src/agents/sandbox/fs-paths.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,56 @@ export function buildSandboxFsMounts(sandbox: SandboxFsBridgeContext): SandboxFs
102102
return dedupeMounts(mounts);
103103
}
104104

105+
export function resolveWritableSandboxBindHostRoots(
106+
binds: readonly string[] | undefined,
107+
): string[] {
108+
const parsedBinds = parseSandboxBindMounts(binds);
109+
const readonlyRoots = parsedBinds.filter((bind) => !bind.writable).map((bind) => bind.hostRoot);
110+
const roots: string[] = [];
111+
const seen = new Set<string>();
112+
for (const parsed of parsedBinds) {
113+
if (
114+
!parsed.writable ||
115+
seen.has(parsed.hostRoot) ||
116+
readonlyRoots.some((root) => isHostPathWithinOrEqual(parsed.hostRoot, root))
117+
) {
118+
continue;
119+
}
120+
seen.add(parsed.hostRoot);
121+
roots.push(parsed.hostRoot);
122+
}
123+
return roots;
124+
}
125+
126+
export function hasSandboxBindContainerPathAliases(binds: readonly string[] | undefined): boolean {
127+
for (const parsed of parseSandboxBindMounts(binds)) {
128+
if (parsed.hostRoot !== parsed.containerRoot) {
129+
return true;
130+
}
131+
}
132+
return false;
133+
}
134+
135+
export function hasSandboxBindReadonlyHostShadows(binds: readonly string[] | undefined): boolean {
136+
const parsedBinds = parseSandboxBindMounts(binds);
137+
const writableRoots = parsedBinds.filter((bind) => bind.writable).map((bind) => bind.hostRoot);
138+
const readonlyRoots = parsedBinds.filter((bind) => !bind.writable).map((bind) => bind.hostRoot);
139+
return writableRoots.some((writableRoot) =>
140+
readonlyRoots.some((readonlyRoot) => isHostPathWithinOrEqual(writableRoot, readonlyRoot)),
141+
);
142+
}
143+
144+
function parseSandboxBindMounts(binds: readonly string[] | undefined): ParsedBindMount[] {
145+
const parsed: ParsedBindMount[] = [];
146+
for (const bind of binds ?? []) {
147+
const mount = parseSandboxBindMount(bind);
148+
if (mount) {
149+
parsed.push(mount);
150+
}
151+
}
152+
return parsed;
153+
}
154+
105155
export function resolveSandboxFsPathWithMounts(params: {
106156
filePath: string;
107157
cwd: string;
@@ -263,6 +313,11 @@ function isPathInsideHost(root: string, target: string): boolean {
263313
return isPathInside(canonicalRoot, canonicalTarget);
264314
}
265315

316+
function isHostPathWithinOrEqual(root: string, target: string): boolean {
317+
const relative = path.relative(path.resolve(root), path.resolve(target));
318+
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
319+
}
320+
266321
function toHostSegments(relativePosix: string): string[] {
267322
return relativePosix.split("/").filter(Boolean);
268323
}

0 commit comments

Comments
 (0)