skip to content

A CI job runs Codex's codex exec to fix failing tests unattended, and every command that downloads a dependency fails. Why, and how should the job be configured?

level: seniorimportance: should knowfreq 7%

answer

  1. headless means no prompts
  2. which sandbox the job chose
  3. network off by default
  4. install before Codex runs
  5. API key for one step

basics

~20 s

By default codex exec never asks for approval, and its workspace-write sandbox keeps network access off, so downloads fail back to the model. Install dependencies before Codex runs, enable sandbox_workspace_write.network_access only if needed, and reserve --yolo for an isolated runner.

solid answer

~40 s

In Codex 0.159.3, `codex exec` defaults to approval policy `never` and a read-only sandbox. A fixing job passes `--sandbox workspace-write`, which allows edits in the checkout but keeps command **network access off**; because nothing can be approved, `npm install` or `pip install` simply fails and the error goes back to the model. The cleanest fix is to install dependencies in an earlier step, outside Codex. If Codex's own commands must download, add `-c 'sandbox_workspace_write.network_access=true'`, optionally constrained by the `network_proxy` feature's domain rules. `--dangerously-bypass-approvals-and-sandbox` (alias `--yolo`) removes the sandbox entirely and belongs only on a runner or container that is itself the isolation boundary. Give `CODEX_API_KEY` to the Codex invocation alone, never as a job-level variable, and capture `--json` events and `-o` for the pipeline.

code

bash · 13 lines
bash
# Step 1: dependencies, with network, before Codex and without its key
npm ci

# Step 2: Codex edits the checkout; its commands stay offline
CODEX_API_KEY="$CODEX_KEY" codex exec \
  --sandbox workspace-write \
  --json \
  -o codex-summary.md \
  "Run npm test, find the cause of the failures and make the smallest fix" \
  > codex-events.jsonl

# Only if Codex's own commands must download:
#   add -c 'sandbox_workspace_write.network_access=true'

go deeper

for a junior

Recall that codex exec never asks for approval and that workspace-write keeps the network off unless configured.

for a middle

Explain how the headless never policy and the workspace-write network default combine, and which setting turns network on.

for a senior

Configure the job: dependencies installed first, network enabled only if needed, --yolo only inside isolation, and the API key scoped to the Codex step.

for a principal

Set the organisation's rule for agent jobs: which sandbox and network policy is standard, who may grant more, and how keys and write permissions are split across jobs.

## The symptom and its cause A pipeline calls Codex's non-interactive mode, `codex exec`, after the test suite fails, asking it to find the cause and make the smallest fix. Codex edits files, but every attempt to run `npm install`, `pip install` or a Gradle download fails, and the run ends without a working fix. Two defaults combine to produce this: - **Approval policy `never`.** The 0.159.3 source sets `never` as the headless default, so `codex exec` never waits for a person. Anything the sandbox blocks is not escalated; the failure is handed back to the model. - **Network off in `workspace-write`.** The job had to pass `--sandbox workspace-write` so Codex could edit (the default sandbox for `codex exec` is `read-only`). That mode allows writes in the workspace but keeps network access for commands **off** unless `[sandbox_workspace_write] network_access = true`. The key's default in the source is `false`. In the logs the pattern is recognisable: with `--json`, the command items for the install attempts complete with network errors, the model may try a workaround or report that it cannot install, and the turn can end without the fix the job wanted. No prompt ever appears, because there is nobody to show it to and the policy forbids asking. Nothing is broken: the job is doing exactly what a safe unattended agent should do. The question is how much to open. ## Options, from narrowest to widest | Approach | Network for Codex's commands | When it fits | |---|---|---| | Install dependencies in an earlier CI step | still off | the usual case: tests need packages, not the internet | | `-c 'sandbox_workspace_write.network_access=true'` | on, unrestricted | Codex's own commands must fetch something | | the same, plus `features.network_proxy` with `domains` rules | on, limited to allowed hosts | downloads from a known registry only | | `--sandbox danger-full-access` or `--yolo` | no sandbox at all | the runner or container is itself the isolation boundary | 1. **Prepare before Codex runs.** The documentation's CI auto-fix pattern runs setup commands first, without the API key, then runs Codex. The packages are on disk when the agent starts. 2. **Enable network deliberately.** Turning on `network_access` gives unrestricted outbound access; enabling the `network_proxy` feature makes the configured domain rules apply. Domain rules on their own do not turn network access on. 3. **Bypass only inside isolation.** `--dangerously-bypass-approvals-and-sandbox` (alias `--yolo`) means no sandbox and no approvals, and in `codex exec` it also skips the Git repository check. The documentation warns that inside such a container a malicious project can read anything available there, including Codex credentials. ## Credentials scoped to one step The non-interactive guide is explicit about keys: - do **not** set `OPENAI_API_KEY` or `CODEX_API_KEY` as a job-level environment variable in workflows that check out or run repository code, because build scripts, tests and dependency lifecycle hooks in the same job can read them; - set `CODEX_API_KEY` only for the Codex invocation, for example inline: `CODEX_API_KEY=... codex exec ...`; - on GitHub Actions, the documentation recommends the Codex GitHub Action, which starts a proxy for the key instead of exposing it to a shell step. ## Reading the result - `--json` turns stdout into JSON Lines events: `thread.started`, `turn.started`, `item.*`, `turn.completed` (with token usage), `turn.failed` and `error`. - `-o <file>` / `--output-last-message <file>` writes the final message to a file for a PR body or job summary. - In 0.159.3, `codex exec` exits with status 1 when the run reported an error, so the step fails visibly. - Saving the diff as an artifact and opening the pull request in a later job, which holds write permissions but not the key, keeps the agent's step read-only toward the repository host. ## What not to reach for - `web_search = "live"` changes the search tool's results; it does not give commands network access. - `--approve-for-me` routes approval requests to an automatic reviewer under `workspace-write`; it does not open the network on its own. - `--skip-git-repo-check` only lets the run start outside a repository.

  • When is `--yolo` acceptable in this job?
    `--yolo` is an alias of `--dangerously-bypass-approvals-and-sandbox`: no sandbox, no approvals, and no Git check in `codex exec`. Use it only when the runner or container is already the isolation boundary, for example where Codex's Linux sandbox cannot start inside Docker, and remember that anything in that environment, Codex credentials included, is exposed to the code it runs.
  • How do you let Codex download from one registry without opening the whole internet?
    Turn on `sandbox_workspace_write.network_access`, then enable the `network_proxy` feature with `domains` rules such as `"registry.npmjs.org" = "allow"`. The proxy constrains traffic only when network access is already on; domain rules without the feature do not restrict anything, and the feature without network access does nothing.
  • How does the pipeline learn that the Codex step failed?
    In 0.159.3 `codex exec` exits with status 1 when the run reported an error. With `--json`, a script can also watch for `turn.failed` and `error` events, and `-o` leaves the final message in a file for the job summary.

  • --sandbox workspace-write gives codex exec network access; it only limits file writes
  • codex exec will pause and ask for network approval, so the job just hangs
  • --yolo is the normal CI setting because nobody is there to approve
  • Set the Codex API key at job level so every step can use it
  • Setting web_search to live lets test commands download packages

Assumes Codex 0.159.3: codex exec defaults to approval never and a read-only sandbox, and sandbox_workspace_write.network_access defaults to false.