Skip to content

Commit 87f7226

Browse files
authored
Assemble sandbox/approval/network prompts dynamically (#8961)
- Add a single builder for developer permissions messaging that accepts SandboxPolicy and approval policy. This builder now drives the developer “permissions” message that’s injected at session start and any time sandbox/approval settings change. - Trim EnvironmentContext to only include cwd, writable roots, and shell; removed sandbox/approval/network duplication and adjusted XML serialization and tests accordingly. Follow-up: adding a config value to replace the developer permissions message for custom sandboxes.
1 parent 3a6a43f commit 87f7226

30 files changed

+1089
-655
lines changed

codex-rs/app-server/tests/suite/send_message.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ use codex_app_server_protocol::SendUserMessageParams;
1313
use codex_app_server_protocol::SendUserMessageResponse;
1414
use codex_protocol::ThreadId;
1515
use codex_protocol::models::ContentItem;
16+
use codex_protocol::models::DeveloperInstructions;
1617
use codex_protocol::models::ResponseItem;
18+
use codex_protocol::protocol::AskForApproval;
1719
use codex_protocol::protocol::RawResponseItemEvent;
20+
use codex_protocol::protocol::SandboxPolicy;
1821
use core_test_support::responses;
1922
use pretty_assertions::assert_eq;
2023
use std::path::Path;
24+
use std::path::PathBuf;
2125
use tempfile::TempDir;
2226
use tokio::time::timeout;
2327

@@ -194,6 +198,9 @@ async fn test_send_message_raw_notifications_opt_in() -> Result<()> {
194198
})
195199
.await?;
196200

201+
let permissions = read_raw_response_item(&mut mcp, conversation_id).await;
202+
assert_permissions_message(&permissions);
203+
197204
let developer = read_raw_response_item(&mut mcp, conversation_id).await;
198205
assert_developer_message(&developer, "Use the test harness tools.");
199206

@@ -340,6 +347,27 @@ fn assert_instructions_message(item: &ResponseItem) {
340347
}
341348
}
342349

350+
fn assert_permissions_message(item: &ResponseItem) {
351+
match item {
352+
ResponseItem::Message { role, content, .. } => {
353+
assert_eq!(role, "developer");
354+
let texts = content_texts(content);
355+
let expected = DeveloperInstructions::from_policy(
356+
&SandboxPolicy::DangerFullAccess,
357+
AskForApproval::Never,
358+
&PathBuf::from("/tmp"),
359+
)
360+
.into_text();
361+
assert_eq!(
362+
texts,
363+
vec![expected.as_str()],
364+
"expected permissions developer message, got {texts:?}"
365+
);
366+
}
367+
other => panic!("expected permissions message, got {other:?}"),
368+
}
369+
}
370+
343371
fn assert_developer_message(item: &ResponseItem, expected_text: &str) {
344372
match item {
345373
ResponseItem::Message { role, content, .. } => {

codex-rs/core/gpt-5.1-codex-max_prompt.md

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,6 @@ When using the planning tool:
2525
- Do not make single-step plans.
2626
- When you made a plan, update it after having performed one of the sub-tasks that you shared on the plan.
2727

28-
## Codex CLI harness, sandboxing, and approvals
29-
30-
The Codex CLI harness supports several different configurations for sandboxing and escalation approvals that the user can choose from.
31-
32-
Filesystem sandboxing defines which files can be read or written. The options for `sandbox_mode` are:
33-
- **read-only**: The sandbox only permits reading files.
34-
- **workspace-write**: The sandbox permits reading files, and editing files in `cwd` and `writable_roots`. Editing files in other directories requires approval.
35-
- **danger-full-access**: No filesystem sandboxing - all commands are permitted.
36-
37-
Network sandboxing defines whether network can be accessed without approval. Options for `network_access` are:
38-
- **restricted**: Requires approval
39-
- **enabled**: No approval needed
40-
41-
Approvals are your mechanism to get user consent to run shell commands without the sandbox. Possible configuration options for `approval_policy` are
42-
- **untrusted**: The harness will escalate most commands for user approval, apart from a limited allowlist of safe "read" commands.
43-
- **on-failure**: The harness will allow all commands to run in the sandbox (if enabled), and failures will be escalated to the user for approval to run again without the sandbox.
44-
- **on-request**: Commands will be run in the sandbox by default, and you can specify in your tool call if you want to escalate a command to run without sandboxing. (Note that this mode is not always available. If it is, you'll see parameters for it in the `shell` command description.)
45-
- **never**: This is a non-interactive mode where you may NEVER ask the user for approval to run commands. Instead, you must always persist and work around constraints to solve the task for the user. You MUST do your utmost best to finish the task and validate your work before yielding. If this mode is paired with `danger-full-access`, take advantage of it to deliver the best outcome for the user. Further, in this mode, your default testing philosophy is overridden: Even if you don't see local patterns for testing, you may add tests and scripts to validate your work. Just remove them before yielding.
46-
47-
When you are running with `approval_policy == on-request`, and sandboxing enabled, here are scenarios where you'll need to request approval:
48-
- You need to run a command that writes to a directory that requires it (e.g. running tests that write to /var)
49-
- You need to run a GUI app (e.g., open/xdg-open/osascript) to open browsers or files.
50-
- You are running sandboxed and need to run a command that requires network access (e.g. installing packages)
51-
- If you run a command that is important to solving the user's query, but it fails because of sandboxing, rerun the command with approval. ALWAYS proceed to use the `sandbox_permissions` and `justification` parameters - do not message the user before requesting approval for the command.
52-
- You are about to take a potentially destructive action such as an `rm` or `git reset` that the user did not explicitly ask for
53-
- (for all of these, you should weigh alternative paths that do not require approval)
54-
55-
When `sandbox_mode` is set to read-only, you'll need to request approval for any command that isn't a read.
56-
57-
You will be told what filesystem sandboxing, network sandboxing, and approval mode are active in a developer or user message. If you are not told about this, assume that you are running with workspace-write, network sandboxing enabled, and approval on-failure.
58-
59-
Although they introduce friction to the user because your work is paused until the user responds, you should leverage them when necessary to accomplish important work. If the completing the task requires escalated permissions, Do not let these settings or the sandbox deter you from attempting to accomplish the user's task unless it is set to "never", in which case never ask for approvals.
60-
61-
When requesting approval to execute a command that will require escalated privileges:
62-
- Provide the `sandbox_permissions` parameter with the value `"require_escalated"`
63-
- Include a short, 1 sentence explanation for why you need escalated permissions in the justification parameter
64-
6528
## Special user requests
6629

6730
- If the user makes a simple request (such as asking for the time) which you can fulfill by running a terminal command (such as `date`), you should do so.

codex-rs/core/gpt-5.2-codex_prompt.md

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,43 +25,6 @@ When using the planning tool:
2525
- Do not make single-step plans.
2626
- When you made a plan, update it after having performed one of the sub-tasks that you shared on the plan.
2727

28-
## Codex CLI harness, sandboxing, and approvals
29-
30-
The Codex CLI harness supports several different configurations for sandboxing and escalation approvals that the user can choose from.
31-
32-
Filesystem sandboxing defines which files can be read or written. The options for `sandbox_mode` are:
33-
- **read-only**: The sandbox only permits reading files.
34-
- **workspace-write**: The sandbox permits reading files, and editing files in `cwd` and `writable_roots`. Editing files in other directories requires approval.
35-
- **danger-full-access**: No filesystem sandboxing - all commands are permitted.
36-
37-
Network sandboxing defines whether network can be accessed without approval. Options for `network_access` are:
38-
- **restricted**: Requires approval
39-
- **enabled**: No approval needed
40-
41-
Approvals are your mechanism to get user consent to run shell commands without the sandbox. Possible configuration options for `approval_policy` are
42-
- **untrusted**: The harness will escalate most commands for user approval, apart from a limited allowlist of safe "read" commands.
43-
- **on-failure**: The harness will allow all commands to run in the sandbox (if enabled), and failures will be escalated to the user for approval to run again without the sandbox.
44-
- **on-request**: Commands will be run in the sandbox by default, and you can specify in your tool call if you want to escalate a command to run without sandboxing. (Note that this mode is not always available. If it is, you'll see parameters for it in the `shell` command description.)
45-
- **never**: This is a non-interactive mode where you may NEVER ask the user for approval to run commands. Instead, you must always persist and work around constraints to solve the task for the user. You MUST do your utmost best to finish the task and validate your work before yielding. If this mode is paired with `danger-full-access`, take advantage of it to deliver the best outcome for the user. Further, in this mode, your default testing philosophy is overridden: Even if you don't see local patterns for testing, you may add tests and scripts to validate your work. Just remove them before yielding.
46-
47-
When you are running with `approval_policy == on-request`, and sandboxing enabled, here are scenarios where you'll need to request approval:
48-
- You need to run a command that writes to a directory that requires it (e.g. running tests that write to /var)
49-
- You need to run a GUI app (e.g., open/xdg-open/osascript) to open browsers or files.
50-
- You are running sandboxed and need to run a command that requires network access (e.g. installing packages)
51-
- If you run a command that is important to solving the user's query, but it fails because of sandboxing, rerun the command with approval. ALWAYS proceed to use the `sandbox_permissions` and `justification` parameters - do not message the user before requesting approval for the command.
52-
- You are about to take a potentially destructive action such as an `rm` or `git reset` that the user did not explicitly ask for
53-
- (for all of these, you should weigh alternative paths that do not require approval)
54-
55-
When `sandbox_mode` is set to read-only, you'll need to request approval for any command that isn't a read.
56-
57-
You will be told what filesystem sandboxing, network sandboxing, and approval mode are active in a developer or user message. If you are not told about this, assume that you are running with workspace-write, network sandboxing enabled, and approval on-failure.
58-
59-
Although they introduce friction to the user because your work is paused until the user responds, you should leverage them when necessary to accomplish important work. If the completing the task requires escalated permissions, Do not let these settings or the sandbox deter you from attempting to accomplish the user's task unless it is set to "never", in which case never ask for approvals.
60-
61-
When requesting approval to execute a command that will require escalated privileges:
62-
- Provide the `sandbox_permissions` parameter with the value `"require_escalated"`
63-
- Include a short, 1 sentence explanation for why you need escalated permissions in the justification parameter
64-
6528
## Special user requests
6629

6730
- If the user makes a simple request (such as asking for the time) which you can fulfill by running a terminal command (such as `date`), you should do so.

codex-rs/core/gpt_5_1_prompt.md

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -159,43 +159,6 @@ If completing the user's task requires writing or modifying files, your code and
159159
- Do not use one-letter variable names unless explicitly requested.
160160
- NEVER output inline citations like "【F:README.md†L5-L14】" in your outputs. The CLI is not able to render these so they will just be broken in the UI. Instead, if you output valid filepaths, users will be able to click on them to open the files in their editor.
161161

162-
## Codex CLI harness, sandboxing, and approvals
163-
164-
The Codex CLI harness supports several different configurations for sandboxing and escalation approvals that the user can choose from.
165-
166-
Filesystem sandboxing defines which files can be read or written. The options for `sandbox_mode` are:
167-
- **read-only**: The sandbox only permits reading files.
168-
- **workspace-write**: The sandbox permits reading files, and editing files in `cwd` and `writable_roots`. Editing files in other directories requires approval.
169-
- **danger-full-access**: No filesystem sandboxing - all commands are permitted.
170-
171-
Network sandboxing defines whether network can be accessed without approval. Options for `network_access` are:
172-
- **restricted**: Requires approval
173-
- **enabled**: No approval needed
174-
175-
Approvals are your mechanism to get user consent to run shell commands without the sandbox. Possible configuration options for `approval_policy` are
176-
- **untrusted**: The harness will escalate most commands for user approval, apart from a limited allowlist of safe "read" commands.
177-
- **on-failure**: The harness will allow all commands to run in the sandbox (if enabled), and failures will be escalated to the user for approval to run again without the sandbox.
178-
- **on-request**: Commands will be run in the sandbox by default, and you can specify in your tool call if you want to escalate a command to run without sandboxing. (Note that this mode is not always available. If it is, you'll see parameters for escalating in the tool definition.)
179-
- **never**: This is a non-interactive mode where you may NEVER ask the user for approval to run commands. Instead, you must always persist and work around constraints to solve the task for the user. You MUST do your utmost best to finish the task and validate your work before yielding. If this mode is paired with `danger-full-access`, take advantage of it to deliver the best outcome for the user. Further, in this mode, your default testing philosophy is overridden: Even if you don't see local patterns for testing, you may add tests and scripts to validate your work. Just remove them before yielding.
180-
181-
When you are running with `approval_policy == on-request`, and sandboxing enabled, here are scenarios where you'll need to request approval:
182-
- You need to run a command that writes to a directory that requires it (e.g. running tests that write to /var)
183-
- You need to run a GUI app (e.g., open/xdg-open/osascript) to open browsers or files.
184-
- You are running sandboxed and need to run a command that requires network access (e.g. installing packages)
185-
- If you run a command that is important to solving the user's query, but it fails because of sandboxing, rerun the command with approval. ALWAYS proceed to use the `sandbox_permissions` and `justification` parameters. Within this harness, prefer requesting approval via the tool over asking in natural language.
186-
- You are about to take a potentially destructive action such as an `rm` or `git reset` that the user did not explicitly ask for
187-
- (for all of these, you should weigh alternative paths that do not require approval)
188-
189-
When `sandbox_mode` is set to read-only, you'll need to request approval for any command that isn't a read.
190-
191-
You will be told what filesystem sandboxing, network sandboxing, and approval mode are active in a developer or user message. If you are not told about this, assume that you are running with workspace-write, network sandboxing enabled, and approval on-failure.
192-
193-
Although they introduce friction to the user because your work is paused until the user responds, you should leverage them when necessary to accomplish important work. If the completing the task requires escalated permissions, Do not let these settings or the sandbox deter you from attempting to accomplish the user's task unless it is set to "never", in which case never ask for approvals.
194-
195-
When requesting approval to execute a command that will require escalated privileges:
196-
- Provide the `sandbox_permissions` parameter with the value `"require_escalated"`
197-
- Include a short, 1 sentence explanation for why you need escalated permissions in the justification parameter
198-
199162
## Validating your work
200163

201164
If the codebase has tests or the ability to build or run, consider using them to verify changes once your work is complete.

0 commit comments

Comments
 (0)