Skip to content

Commit 5e9f0dd

Browse files
feat: add sandbox_mode, approval_policy, and network_access variables to Codex module
- Introduced new variables for sandbox configuration: sandbox_mode, approval_policy, and network_access. - Updated README to reflect the new variables and their usage. - Modified install and start scripts to utilize the new variables for better configuration management.
1 parent 2b47e77 commit 5e9f0dd

File tree

4 files changed

+58
-44
lines changed

4 files changed

+58
-44
lines changed

registry/coder-labs/modules/codex/README.md

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ module "coder-login" {
6565
}
6666
6767
module "codex" {
68-
source = "registry.coder.com/coder-labs/codex/coder"
69-
agent_id = coder_agent.example.id
70-
openai_api_key = "..."
71-
ai_prompt = data.coder_parameter.ai_prompt.value
72-
folder = "/home/coder/project"
73-
full_auto = true
68+
source = "registry.coder.com/coder-labs/codex/coder"
69+
agent_id = coder_agent.example.id
70+
openai_api_key = "..."
71+
ai_prompt = data.coder_parameter.ai_prompt.value
72+
folder = "/home/coder/project"
73+
approval_policy = "never" # Full auto mode
7474
}
7575
```
7676

@@ -94,52 +94,45 @@ The module automatically configures Codex with a secure sandbox that allows AI t
9494

9595
### Customizing Sandbox Behavior
9696

97-
You can override the default sandbox configuration using the `extra_codex_settings_toml` variable:
97+
You can customize the sandbox behavior using dedicated variables:
9898

99-
#### **For Containerized Environments (Recommended)**
99+
#### **Using Dedicated Variables (Recommended)**
100100

101-
If you encounter Landlock sandbox errors in containerized environments like Coder workspaces:
101+
For most use cases, use the dedicated sandbox variables:
102102

103103
```tf
104104
module "codex" {
105105
source = "registry.coder.com/coder-labs/codex/coder"
106106
# ... other variables ...
107107
108-
extra_codex_settings_toml = <<-EOT
109-
# Disable sandbox for containerized environments (per Codex docs)
110-
sandbox_mode = "danger-full-access"
111-
EOT
112-
}
113-
```
108+
# Containerized environments (fixes Landlock errors)
109+
sandbox_mode = "danger-full-access"
114110
115-
#### **For Read-Only Mode**
111+
# Or for read-only mode
112+
# sandbox_mode = "read-only"
116113
117-
```tf
118-
extra_codex_settings_toml = <<-EOT
119-
sandbox_mode = "read-only"
120-
EOT
121-
```
114+
# Or for full auto mode
115+
# approval_policy = "never"
122116
123-
#### **For Full Auto Mode**
124-
125-
```tf
126-
extra_codex_settings_toml = <<-EOT
127-
approval_policy = "never"
128-
EOT
117+
# Or disable network access
118+
# network_access = false
119+
}
129120
```
130121

131-
#### **For Restricted Network Access**
122+
#### **Using extra_codex_settings_toml (Advanced)**
132123

133-
If you want to disable network access for security reasons:
124+
For advanced configuration or when you need to override multiple settings:
134125

135126
```tf
136127
extra_codex_settings_toml = <<-EOT
137-
network_access = false
128+
# Any custom Codex configuration
129+
model = "gpt-4"
130+
disable_response_storage = true
138131
EOT
139132
```
140133

141134
> [!NOTE]
142-
> Custom settings completely override the base configuration, so you can change any sandbox behavior as needed.
135+
> The dedicated variables (`sandbox_mode`, `approval_policy`, `network_access`) are the recommended way to configure sandbox behavior. Use `extra_codex_settings_toml` only for advanced configuration that isn't covered by the dedicated variables.
143136
144137
## Troubleshooting
145138

registry/coder-labs/modules/codex/main.tf

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@ variable "extra_codex_settings_toml" {
5959
default = ""
6060
}
6161

62+
variable "sandbox_mode" {
63+
type = string
64+
description = "The sandbox mode for Codex. Options: workspace-write, read-only, danger-full-access."
65+
default = "workspace-write"
66+
validation {
67+
condition = contains(["workspace-write", "read-only", "danger-full-access"], var.sandbox_mode)
68+
error_message = "sandbox_mode must be one of: workspace-write, read-only, danger-full-access."
69+
}
70+
}
71+
72+
variable "approval_policy" {
73+
type = string
74+
description = "The approval policy for Codex. Options: on-request, never, untrusted."
75+
default = "on-request"
76+
validation {
77+
condition = contains(["on-request", "never", "untrusted"], var.approval_policy)
78+
error_message = "approval_policy must be one of: on-request, never, untrusted."
79+
}
80+
}
81+
82+
variable "network_access" {
83+
type = bool
84+
description = "Whether to allow network access in workspace-write mode."
85+
default = true
86+
}
87+
6288
variable "openai_api_key" {
6389
type = string
6490
description = "Codex API Key"
@@ -113,11 +139,7 @@ variable "codex_system_prompt" {
113139
default = ""
114140
}
115141

116-
variable "full_auto" {
117-
type = bool
118-
description = "Whether to run Codex in full-auto mode for automated task execution."
119-
default = false
120-
}
142+
121143

122144
resource "coder_env" "openai_api_key" {
123145
agent_id = var.agent_id
@@ -160,7 +182,6 @@ module "agentapi" {
160182
ARG_CODEX_MODEL='${var.codex_model}' \
161183
ARG_CODEX_START_DIRECTORY='${var.folder}' \
162184
ARG_CODEX_TASK_PROMPT='${base64encode(var.ai_prompt)}' \
163-
ARG_CODEX_FULL_AUTO='${var.full_auto}' \
164185
/tmp/start.sh
165186
EOT
166187

@@ -178,6 +199,9 @@ module "agentapi" {
178199
ARG_ADDITIONAL_EXTENSIONS='${base64encode(var.additional_extensions)}' \
179200
ARG_CODEX_START_DIRECTORY='${var.folder}' \
180201
ARG_CODEX_INSTRUCTION_PROMPT='${base64encode(var.codex_system_prompt)}' \
202+
ARG_SANDBOX_MODE='${var.sandbox_mode}' \
203+
ARG_APPROVAL_POLICY='${var.approval_policy}' \
204+
ARG_NETWORK_ACCESS='${var.network_access}' \
181205
/tmp/install.sh
182206
EOT
183207
}

registry/coder-labs/modules/codex/scripts/install.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ function populate_config_toml() {
100100
cat << EOF
101101
# Base sandbox configuration for Codex workspace access
102102
# This ensures Codex can read/write files in the specified folder for AI tasks
103-
sandbox_mode = "workspace-write"
104-
approval_policy = "on-request"
103+
sandbox_mode = "${ARG_SANDBOX_MODE}"
104+
approval_policy = "${ARG_APPROVAL_POLICY}"
105105
106106
# Allow network access in workspace-write mode for package installation, API calls, etc.
107107
[sandbox_workspace_write]
108-
network_access = true
108+
network_access = ${ARG_NETWORK_ACCESS}
109109
EOF
110110
)
111111

registry/coder-labs/modules/codex/scripts/start.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ printf "openai_api_key: %s\n" "$ARG_OPENAI_API_KEY"
2323
printf "codex_model: %s\n" "$ARG_CODEX_MODEL"
2424
printf "start_directory: %s\n" "$ARG_CODEX_START_DIRECTORY"
2525
printf "task_prompt: %s\n" "$ARG_CODEX_TASK_PROMPT"
26-
printf "full_auto: %s\n" "$ARG_CODEX_FULL_AUTO"
2726
echo "--------------------------------"
2827
set +o nounset
2928
CODEX_ARGS=()
@@ -57,9 +56,7 @@ if [ -n "$ARG_CODEX_MODEL" ]; then
5756
CODEX_ARGS+=("--model" "$ARG_CODEX_MODEL")
5857
fi
5958

60-
if [ "$ARG_CODEX_FULL_AUTO" = "true" ]; then
61-
CODEX_ARGS+=("--full-auto")
62-
fi
59+
6360

6461
if [ -n "$ARG_CODEX_TASK_PROMPT" ]; then
6562
printf "Running the task prompt %s\n" "$ARG_CODEX_TASK_PROMPT"

0 commit comments

Comments
 (0)