Skip to content

Commit 1212d3e

Browse files
committed
feat(sandbox): add needed activation mode
1 parent aee4670 commit 1212d3e

49 files changed

Lines changed: 645 additions & 97 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/cli/policy.md‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ agent id instead of skipping it.
236236
},
237237
},
238238
"sandbox": {
239-
"requireMode": ["all", "non-main"],
239+
"requireMode": ["all", "non-main", "needed"],
240240
},
241241
"scopes": {
242242
"release-workspace": {
@@ -392,16 +392,16 @@ node command should update `policy.jsonc` after review instead of relying on
392392

393393
#### Sandbox posture
394394

395-
| Policy field | Observed state | Use when |
396-
| ----------------------------------------------------- | ------------------------------------------------------- | -------------------------------------------------------------- |
397-
| `sandbox.requireMode` | `agents.defaults.sandbox.mode` and per-agent mode | Allow only reviewed sandbox modes such as `all` or `non-main`. |
398-
| `sandbox.allowBackends` | `agents.defaults.sandbox.backend` and per-agent backend | Allow only reviewed sandbox backends such as `docker`. |
399-
| `sandbox.containers.denyHostNetwork` | Container-backed sandbox/browser network mode | Deny host network mode. |
400-
| `sandbox.containers.denyContainerNamespaceJoin` | Container-backed sandbox/browser network mode | Deny joining another container network namespace. |
401-
| `sandbox.containers.requireReadOnlyMounts` | Container-backed sandbox/browser mount mode | Require mounts to be read-only. |
402-
| `sandbox.containers.denyContainerRuntimeSocketMounts` | Container-backed sandbox/browser mount targets | Deny container runtime socket mounts. |
403-
| `sandbox.containers.denyUnconfinedProfiles` | Container security profile posture | Deny unconfined container security profiles. |
404-
| `sandbox.browser.requireCdpSourceRange` | Sandbox browser CDP source range | Require browser CDP exposure to declare a source range. |
395+
| Policy field | Observed state | Use when |
396+
| ----------------------------------------------------- | ------------------------------------------------------- | ------------------------------------------------------------------------- |
397+
| `sandbox.requireMode` | `agents.defaults.sandbox.mode` and per-agent mode | Allow only reviewed sandbox modes such as `all`, `needed`, or `non-main`. |
398+
| `sandbox.allowBackends` | `agents.defaults.sandbox.backend` and per-agent backend | Allow only reviewed sandbox backends such as `docker`. |
399+
| `sandbox.containers.denyHostNetwork` | Container-backed sandbox/browser network mode | Deny host network mode. |
400+
| `sandbox.containers.denyContainerNamespaceJoin` | Container-backed sandbox/browser network mode | Deny joining another container network namespace. |
401+
| `sandbox.containers.requireReadOnlyMounts` | Container-backed sandbox/browser mount mode | Require mounts to be read-only. |
402+
| `sandbox.containers.denyContainerRuntimeSocketMounts` | Container-backed sandbox/browser mount targets | Deny container runtime socket mounts. |
403+
| `sandbox.containers.denyUnconfinedProfiles` | Container security profile posture | Deny unconfined container security profiles. |
404+
| `sandbox.browser.requireCdpSourceRange` | Sandbox browser CDP source range | Require browser CDP exposure to declare a source range. |
405405

406406
Policy treats missing `sandbox.mode` as its implicit default `off`, so
407407
`sandbox.requireMode` reports a fresh or unconfigured sandbox as outside an

‎docs/cli/sandbox.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ For `ssh` and OpenShell `remote`, recreate matters more than with Docker: the re
4747

4848
Inspect the effective sandbox mode/scope/workspace access, sandbox tool policy, and elevated-tool gates (with fix-it config key paths).
4949

50+
For `mode: "needed"`, the report distinguishes the direct chat runtime from the sandbox that activates only when a sandbox-bound tool runs.
51+
5052
The report keeps `workspaceRoot` as the configured sandbox root and separately shows the effective host workspace, backend runtime workdir, and Docker mount table. For `workspaceAccess: "rw"`, the effective host workspace is the agent workspace rather than a directory below `workspaceRoot`.
5153

5254
```bash
@@ -99,7 +101,7 @@ Sandbox settings live in `~/.openclaw/openclaw.json` under `agents.defaults.sand
99101
"agents": {
100102
"defaults": {
101103
"sandbox": {
102-
"mode": "all", // off, non-main, all
104+
"mode": "all", // off, non-main, needed, all
103105
"backend": "docker", // docker, ssh, openshell (plugin-provided)
104106
"scope": "agent", // session, agent, shared
105107
"docker": {

‎docs/gateway/config-agents.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ Optional sandboxing for the embedded agent. See [Sandboxing](/gateway/sandboxing
720720
agents: {
721721
defaults: {
722722
sandbox: {
723-
mode: "non-main", // off (default) | non-main | all
723+
mode: "non-main", // off (default) | non-main | needed | all
724724
backend: "docker", // docker (default) | ssh | openshell
725725
scope: "agent", // session | agent (default) | shared
726726
workspaceAccess: "none", // none (default) | ro | rw

‎docs/gateway/configuration.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ candidate contains a redacted secret placeholder such as `***` or `[redacted]`.
309309
agents: {
310310
defaults: {
311311
sandbox: {
312-
mode: "non-main", // off | non-main | all
312+
mode: "non-main", // off | non-main | needed | all
313313
scope: "agent", // session | agent | shared
314314
},
315315
},

‎docs/gateway/sandbox-vs-tool-policy-vs-elevated.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ openclaw sandbox explain --json
2525
It prints:
2626

2727
- effective sandbox mode/scope/workspace access
28-
- whether the session is currently sandboxed (main vs non-main)
28+
- whether the session is currently sandboxed, or will sandbox only when sandbox-bound tools run
2929
- effective sandbox tool allow/deny (and whether it came from agent/global/default)
3030
- elevated gates and fix-it key paths
3131

@@ -35,6 +35,7 @@ Sandboxing is controlled by `agents.defaults.sandbox.mode`:
3535

3636
- `"off"`: everything runs on the host.
3737
- `"non-main"`: only non-main sessions are sandboxed (common "surprise" for groups/channels).
38+
- `"needed"`: chat-only turns run direct; sandbox-bound tools such as `exec` start the sandbox when called and fail closed if the backend is unavailable.
3839
- `"all"`: everything is sandboxed.
3940

4041
`agents.defaults.sandbox.workspaceAccess` controls what the sandbox can see: `"none"`, `"ro"`, or `"rw"`.

‎docs/gateway/sandboxing.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ Not sandboxed:
2626

2727
Three independent settings control sandbox behavior:
2828

29-
| Setting | Key | Values | Default |
30-
| ------- | --------------------------------- | ---------------------------- | -------- |
31-
| Mode | `agents.defaults.sandbox.mode` | `off`, `non-main`, `all` | `off` |
32-
| Scope | `agents.defaults.sandbox.scope` | `agent`, `session`, `shared` | `agent` |
33-
| Backend | `agents.defaults.sandbox.backend` | `docker`, `ssh`, `openshell` | `docker` |
29+
| Setting | Key | Values | Default |
30+
| ------- | --------------------------------- | ---------------------------------- | -------- |
31+
| Mode | `agents.defaults.sandbox.mode` | `off`, `non-main`, `needed`, `all` | `off` |
32+
| Scope | `agents.defaults.sandbox.scope` | `agent`, `session`, `shared` | `agent` |
33+
| Backend | `agents.defaults.sandbox.backend` | `docker`, `ssh`, `openshell` | `docker` |
3434

3535
**Mode** controls when sandboxing applies:
3636

3737
- `off`: no sandboxing.
3838
- `non-main`: sandbox every session except the agent's main session. The main session key is always `agent:<agentId>:main` (or `global` when `session.scope` is `"global"`); it is not configurable. Group/channel sessions use their own keys, so they always count as non-main and get sandboxed.
39+
- `needed`: chat-only turns run direct. Sandbox-bound OpenClaw tools such as `exec` start the configured backend when called and fail closed if it is unavailable. Codex native execution stays disabled until an OpenClaw sandbox environment exists, so it cannot fall back to host execution.
3940
- `all`: every session runs in a sandbox.
4041

4142
**Scope** controls how many containers/environments are created:

‎docs/install/docker.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ For full configuration, images, security notes, and multi-agent profiles:
549549
agents: {
550550
defaults: {
551551
sandbox: {
552-
mode: "non-main", // off | non-main | all
552+
mode: "non-main", // off | non-main | needed | all
553553
scope: "agent", // session | agent | shared
554554
},
555555
},

‎extensions/codex/src/app-server/dynamic-tool-build.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,40 @@ describe("Codex app-server dynamic tool build", () => {
15431543
expect(shouldEnableCodexAppServerNativeToolSurface(params)).toBe(false);
15441544
});
15451545

1546+
it("disables Codex native tools while needed mode waits for sandbox-bound tools", () => {
1547+
const workspaceDir = path.join(tempDir, "workspace");
1548+
const params = createParams(path.join(tempDir, "needed-session.jsonl"), workspaceDir);
1549+
params.disableTools = false;
1550+
params.sessionKey = "agent:main:main";
1551+
params.config = {
1552+
agents: { defaults: { sandbox: { mode: "needed" } } },
1553+
tools: { exec: { host: "auto" } },
1554+
} as never;
1555+
1556+
expect(shouldEnableCodexAppServerNativeToolSurface(params)).toBe(false);
1557+
expect(
1558+
shouldEnableCodexAppServerNativeToolSurface(params, {
1559+
enabled: true,
1560+
backendId: "docker",
1561+
} as never),
1562+
).toBe(false);
1563+
expect(
1564+
shouldEnableCodexAppServerNativeToolSurface(
1565+
params,
1566+
{
1567+
enabled: true,
1568+
backendId: "docker",
1569+
backend: {},
1570+
tools: {
1571+
allow: ["exec", "process", "read", "write", "edit", "apply_patch"],
1572+
deny: [],
1573+
},
1574+
} as never,
1575+
{ sandboxExecServerEnabled: true },
1576+
),
1577+
).toBe(true);
1578+
});
1579+
15461580
it("keeps Codex native tool surfaces when the effective exec target is node", () => {
15471581
const workspaceDir = path.join(tempDir, "workspace");
15481582
const sessionParams = createParams(path.join(tempDir, "session.jsonl"), workspaceDir);

‎extensions/codex/src/app-server/dynamic-tool-build.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,18 @@ export function shouldEnableCodexAppServerNativeToolSurface(
513513
if (params.disableTools) {
514514
return false;
515515
}
516+
const nativeExecutionPolicy = resolveCodexNativeExecutionPolicy({
517+
config: params.config,
518+
sessionKey: resolveCodexRuntimePolicySessionKey(params, options.runtimeSessionKey),
519+
sessionId: params.sessionId,
520+
agentId: options.agentId,
521+
execOverrides: params.execOverrides,
522+
sandboxAvailable: sandbox?.enabled,
523+
readRuntimeSessionEntry: true,
524+
});
525+
if (nativeExecutionPolicy.blockKind === "sandbox-activation-required") {
526+
return false;
527+
}
516528
const toolsAllow = includeForcedCodexDynamicToolAllow(params.toolsAllow, params);
517529
if (toolsAllow === undefined) {
518530
return canCodexAppServerNativeToolSurfaceHonorSandbox(sandbox, options);

‎extensions/codex/src/app-server/native-execution-policy.test.ts‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,77 @@ describe("resolveCodexNativeExecutionPolicy", () => {
4747
});
4848
});
4949

50+
it("blocks native execution while needed mode is waiting for tool activation", () => {
51+
expect(
52+
resolveCodexNativeExecutionPolicy({
53+
config: {
54+
agents: { defaults: { sandbox: { mode: "needed" } } },
55+
tools: { exec: { host: "auto" } },
56+
},
57+
sessionKey: "agent:main:main",
58+
sandboxAvailable: false,
59+
}),
60+
).toMatchObject({
61+
nativeToolSurfaceAllowed: false,
62+
requestedExecHost: "auto",
63+
effectiveExecHost: "gateway",
64+
blockKind: "sandbox-activation-required",
65+
});
66+
});
67+
68+
it("allows native execution after needed mode has an active sandbox", () => {
69+
expect(
70+
resolveCodexNativeExecutionPolicy({
71+
config: {
72+
agents: { defaults: { sandbox: { mode: "needed" } } },
73+
tools: { exec: { host: "auto" } },
74+
},
75+
sessionKey: "agent:main:main",
76+
sandboxAvailable: true,
77+
}),
78+
).toMatchObject({
79+
nativeToolSurfaceAllowed: true,
80+
requestedExecHost: "auto",
81+
effectiveExecHost: "sandbox",
82+
});
83+
});
84+
85+
it("keeps an explicit gateway host available in needed mode", () => {
86+
expect(
87+
resolveCodexNativeExecutionPolicy({
88+
config: {
89+
agents: { defaults: { sandbox: { mode: "needed" } } },
90+
tools: { exec: { host: "gateway" } },
91+
},
92+
sessionKey: "agent:main:main",
93+
sandboxAvailable: false,
94+
}),
95+
).toMatchObject({
96+
nativeToolSurfaceAllowed: true,
97+
requestedExecHost: "gateway",
98+
effectiveExecHost: "gateway",
99+
});
100+
});
101+
102+
it("keeps node routing blocked as node execution in needed mode", () => {
103+
expect(
104+
resolveCodexNativeExecutionPolicy({
105+
config: {
106+
agents: { defaults: { sandbox: { mode: "needed" } } },
107+
tools: { exec: { host: "node", node: "worker-needed" } },
108+
},
109+
sessionKey: "agent:main:main",
110+
sandboxAvailable: false,
111+
}),
112+
).toMatchObject({
113+
nativeToolSurfaceAllowed: false,
114+
requestedExecHost: "node",
115+
effectiveExecHost: "node",
116+
blockKind: "node-exec",
117+
node: "worker-needed",
118+
});
119+
});
120+
50121
it("resolves auto to sandbox when a sandbox is active", () => {
51122
expect(
52123
resolveCodexNativeExecutionPolicy({

0 commit comments

Comments
 (0)