Give an agent your shell without giving it your machine

3 months ago

Codex CLI is OpenAI's coding agent, and it runs on your own computer, in your own terminal, with your own shell. It is one Rust workspace, and it is big. A hundred and thirty five crates, about one and a half million lines. Most of what I want to show you is about the boundary between the model and your machine.

Ask about this presentation

Chapters

Show transcript
  1. Codex CLI is OpenAI's coding agent, and it runs on your own computer, in your own terminal, with your own shell. It is one Rust workspace, and it is big. A hundred and thirty five crates, about one and a half million lines. Most of what I want to show you is about the boundary between the model and your machine.

    Slide text
    >_ openai / codex 01

    Give an agent your shell
    without giving it your machine

    Inside the architecture of OpenAI’s Codex CLI.

    135 crates 1.46M lines of Rust 3 OS sandboxes
  2. You install one executable and start it inside a repository. This is the screen you get. The model it is using, the directory it is pointed at, and underneath, a plan it keeps rewriting as it works. Everything from here is what sits behind that window.

    Slide text
    >_ the program 02

    One binary, launched inside a repository. It reads your files, runs commands in your shell, and edits code in place.

    Codex CLI running in a terminal, showing the model, working directory and a live plan
  3. The thing you install is a single executable that can act as five different programs. It checks the name it was launched under and its first argument. Started one way it applies a patch. Started another way it is the Linux sandbox that will wrap the next command, or the helper that runs processes, or the one that touches files. That keeps the helpers exactly in step with the agent that spawns them.

    Slide text
    >_ argv dispatch 03

    One executable,
    five programs

    On startup the binary looks at the name it was launched under and its first argument, then becomes whichever helper was asked for.

    One artifact to sign, one to install, and a helper that is always the same version as the agent that spawned it.

    // tokens the same binary answers to
    
    apply_patch         --codex-run-as-apply-patch
    linux sandbox       codex-linux-sandbox
    windows sandbox     --run-as-windows-sandbox
    exec helper         --codex-run-as-arg0-exec-helper
    filesystem helper   --codex-run-as-fs-helper
    codex-rs / arg0
  4. The core talks to the outside world through two queues, and that is all it does. A client sends a submission, which is an identifier and an operation. The core streams back events, each tagged with the identifier of the submission that caused it. The agent loop doesn't know whether a terminal, an editor, or another agent is on the other end of that pipe.

    Slide text
    >_ the protocol 04

    A client sends a submission: an id and an operation. The core sends back events tagged with the id that caused them. That is the entire contract.

       ┌────────────────┐                                  ┌────────────────┐
       │                │   Submission { id, op }          │                │
       │   front end    │ ───────────────────────────────▶ │   codex core   │
       │                │                                  │                │
       │  tui · exec    │ ◀─────────────────────────────── │ session · turn │
       │  mcp · app     │        Event { id, msg }         │     tools      │
       └────────────────┘                                  └────────────────┘
    codex-rs / protocol
  5. So the front ends stay thin. There is the terminal interface, built on ratatui, and at two hundred and seventy thousand lines it is the largest crate in the whole repository. There is a one shot mode that doesn't draw an interface, which is what scripts and the TypeScript and Python libraries drive. And there are two server modes. In one, Codex registers itself as a tool that another agent can call. In the other it answers remote procedure calls for the editor extensions and the desktop app.

    Slide text
    >_ front ends 05

    Because the contract is that thin, every surface is a renderer of the same event stream.

    codex

    The terminal interface, built on ratatui and crossterm. At 270 thousand lines it is the largest crate in the repository.

    codex exec

    One shot, no interface. This is what scripts, continuous integration, and the TypeScript and Python libraries drive.

    codex mcp-server

    Codex registers itself as a tool over stdio, so another agent can call it the way it calls anything else.

    codex app-server

    A JSON remote procedure call server. The editor extensions and the desktop app both speak it.

  6. Here is what the model is actually handed. Notice how much of it is about running things and coordinating, rather than editing code. It runs a command in a shell session that stays alive between calls, and types into it. It edits files through one patch format. And it can start other agents, wait on them, and pass them messages. Have a look for yourself, sort it any way you like. The registry is built fresh at the start of every session, so this list moves with your config.

    Slide text
    >_ the tool registry 06

    What the model
    can reach for

    The registry is assembled when a session starts, so the list depends on your config and on the servers you have connected.

    Sort it, or filter it, whichever way is useful to you.

  7. There is a second way to call tools, and it is my favorite part of this codebase. The model writes a JavaScript program, and Codex evaluates it in a fresh V8 isolate. Every tool shows up as a method on a global object called tools, so one program can do ten calls. The isolate is deliberately bare. It doesn't get Node, a file system, network access, or a console. The only way out of that sandbox is through the tools you allowed.

    Slide text
    >_ code mode 07

    Instead of one tool call per turn, it sends a program. Codex runs it in a fresh V8 isolate where every tool is a method on a global tools object.

    So it can loop over files, branch on a result, and compose ten calls into one round trip.

    Run JavaScript code to orchestrate/compose tool calls
    
    - Evaluates the provided JavaScript code in a fresh V8
      isolate as an async module.
    - All nested tools are available on the global `tools`
      object, for example `await tools.exec_command(...)`.
    - Runs raw JavaScript -- no Node, no file system,
      no network access, no console.
    - `store(key, value)` / `load(key)`: keep values for
      later exec calls in the same session.
    - `yield_control()`: yields the accumulated output to
      the model immediately while the script keeps running.
    the tool description the model is given
  8. Commands run inside the operating system's own sandbox, and there is a separate implementation for each one. On macOS it is Seatbelt. The profile is compiled into the binary, it opens by denying everything, and it is adapted from Chromium's renderer policy. On Linux it is Landlock and seccomp, with a vendored copy of bubblewrap as the second path. On Windows it is a restricted token, access control lists on the workspace, and firewall rules for the network.

    Slide text
    >_ sandboxing 08

    Three operating systems, three real sandboxes

    (version 1)
    
    ; start with closed-by-default
    (deny default)
    
    ; child processes inherit the
    ; policy of their parent
    (allow process-exec)
    (allow process-fork)
    (allow signal (target same-sandbox))
    seatbelt_base_policy.sbpl

    macOS

    A Seatbelt profile compiled into the binary, adapted from Chromium’s renderer policy.

    Linux

    Landlock plus seccomp, with a vendored copy of bubblewrap as the second path.

    Windows

    A restricted token, access control lists on the workspace, and filtering platform rules for the network.

  9. There is a second layer above the sandbox that reads the command itself and decides between allow, prompt, and forbidden. The rules are written in Starlark. Every rule carries the example invocations it must match and the ones it must not, and those get checked when the policy file loads. So a rule that quietly stops matching fails at startup, rather than letting a command slip through months later.

    Slide text
    >_ execpolicy 09

    Above the sandbox, a policy file reads the command itself and returns one of three answers.

    allow prompt forbidden

    Rules are Starlark, and each one ships the invocations it must match and the ones it must not.

    prefix_rule(
        pattern = ["cmd", ["alt1", "alt2"]],
        decision = "prompt",
        justification = "explain why this rule exists",
        match = [["cmd", "alt1"], "cmd alt2"],
        not_match = [["cmd", "oops"], "cmd alt3"],
    )
    codex-rs / execpolicy
  10. Every conversation is written to disk while it happens, one JSON object per line. A dated folder in your Codex home, with a SQLite index over the top so the picker stays quick. That file is what resume, fork, and archive all read. Fork is the one I would point at. It branches a session you already had, so you can try a second approach and keep the first one intact.

    Slide text
    >_ history 10

    Every session is a file you can pick back up

    Each conversation is appended to disk as it happens, one JSON object per line, with a SQLite index over the top so the picker stays fast.

    ~/.codex/sessions/YYYY/MM/DD/rollout-<timestamp>-<uuid>.jsonl

    codex resume

    Picks a past session back up where it stopped.

    codex fork

    Branches one, so a second approach costs you nothing.

    codex archive

    Moves it out of the picker without deleting it.

  11. So that is the shape. A small protocol in the middle, thin front ends around it, one binary that becomes its own helpers, and layer after layer of restriction between the model and your machine. It is Apache licensed and all of it is on GitHub, so you can go and read every one of those layers for yourself.

    Slide text
    >_ openai / codex 11

    A small protocol in the middle. Thin front ends around it. One binary that becomes its own helpers. And layer after layer of restriction between the model and your machine.

    github.com/openai/codex · apache 2.0

What happened in AI today, in your inbox

One short email each evening. Read it and you’re caught up.

Built with ❤️ in New York