Sub topic 10
Sandboxing what the agent can touch
This chapter assumes you know how approval prompts work from the previous chapter, and that you are comfortable with shell commands and file paths. Approvals ask you before an action runs. A sandbox is different. It is a boundary enforced by the operating system, so a command cannot go outside it even if you approve it or the agent makes a mistake.
What a sandbox limits
A sandbox restricts two things for every command the agent starts:
- Filesystem access. Which directories the command can read or write.
- Network access. Whether the command can open outside connections.
Approvals and sandboxes work together. The approval policy decides when the agent must ask you. The sandbox decides what a command can do after you say yes, or when you never get asked.
Sandbox modes in Codex CLI
Codex CLI exposes the sandbox directly through a setting. The configuration from the earlier chapter looked like this:
# ~/.codex/config.toml
approval_policy = "on-request"
sandbox_mode = "workspace-write"The common values of sandbox_mode are:
| Mode | Filesystem | Typical use |
|---|---|---|
| `read-only` | Read anywhere, write nowhere | Exploring an unfamiliar repository |
| `workspace-write` | Write inside the project directory | Everyday edits and test runs |
| `danger-full-access` | No filesystem restriction | Only inside a disposable VM or container |
In workspace-write, network access is off for commands by default. A command like npm install may therefore fail until you allow it. Check your tool's current documentation, because option names and defaults change between versions.
Other tools
Claude Code and Copilot CLI approach this differently. Claude Code relies mainly on its permission rules, and it offers an optional sandboxed mode for shell commands in recent versions. Copilot CLI asks you to trust a directory and then asks before running tools. Because the details vary, confirm what each tool enforces. A permission rule that denies reading .env limits the agent's own file tools. It does not necessarily stop a shell command from reading the file, unless an OS-level sandbox also applies.
Choosing a boundary
- Start with read-only for unknown code.
- Use workspace-write for tasks in a repository you trust, on a git branch.
- Run in a container or VM before you disable the sandbox or approvals.
- Keep credentials out of the workspace and the environment where possible.
Common errors
- A command fails with "permission denied" or a network error. The sandbox blocked it. Change the mode or run that step yourself.
- A tool needs to write to a cache outside the project, such as the package manager cache. Add that path as a writable directory if your tool supports it, instead of turning the sandbox off.
- You assume approvals protect you. They only protect you while you read each prompt carefully.
Exercise
You run Codex CLI with sandboxmode = "workspace-write" and approvalpolicy = "on-request". The agent proposes npm install, and you approve it, but it fails with a network error. Why did it fail, and what is a safer fix than switching to danger-full-access?
Show answer
Approval only lets the command start. The sandbox still blocks network access in workspace-write mode, so the download fails. A safer fix is to enable network access narrowly for that session, or to run the install yourself outside the agent, instead of removing the filesystem boundary. If you need full access, do it inside a container or VM.