# Codex CLI Provider ## Overview The Codex CLI provider enables CLI Agent Orchestrator (CAO) to work with **Codex CLI** (OpenAI's coding agent) through your OpenAI API key, allowing you to orchestrate multiple Codex-based agents. ## Quick Start ### Prerequisites 1. **OpenAI API Key** or **ChatGPT Subscription**: Authentication for Codex CLI 2. **Codex CLI**: Install the CLI tool via npm 3. **tmux**: Required for terminal management ```bash # Install Codex CLI npm install -g @openai/codex # Authenticate (set API key) export OPENAI_API_KEY=your-key-here # Or use interactive login codex login ``` > **Security note.** If Codex is launched under CAO without credentials, the > startup handler leaves the first-run sign-in menu on screen for an operator to > complete (it cannot be answered automatically). That live sign-in pane — OAuth > device code or API-key paste — is attachable through the Web UI and the PTY > WebSocket like any other CAO terminal, so enable control-plane authentication > before launching Codex workers that will need to sign in interactively. ### Using Codex Provider with CAO Create a terminal using the Codex provider: ```bash # Start the CAO server in one terminal cao-server # In another terminal, launch a Codex-backed CAO session cao launch --agents codex_developer --provider codex ``` You can also create a session via HTTP API (query parameters): ```bash curl -X POST "http://localhost:9889/sessions?provider=codex&agent_profile=codex_developer" ``` ## Features ### Status Detection The Codex provider automatically detects terminal states: - **IDLE**: Terminal is ready for input - **PROCESSING**: Codex is thinking or working - **WAITING_USER_ANSWER**: Waiting for user approval/confirmation - **COMPLETED**: Task finished with assistant response - **ERROR**: Error occurred during execution The provider supports two output formats for status detection: - **Label style**: `You ...` / `assistant: ...` (synthetic/test format) - **Bullet style**: `› user message` / `• response` (real Codex interactive mode) The `USER_PREFIX_PATTERN` uses `[^\S\n]` (horizontal whitespace only) to prevent matching across newline boundaries, correctly distinguishing `› ` (idle prompt) from `› text` (user input). **Completion footer (Codex 0.155 and later).** Codex streams the final answer with no spinner, so a frame of a streaming answer looks like a finished turn. From 0.155, Codex ends each successful turn with a dim footer under the final message: `Worked for 32s • 4:19 PM` (0.159 and later), `Worked for 1m 5s · done 4:19 PM` or `done 4:19 PM` (0.155), and a bare `4:19 PM` for short turns on 0.156 to 0.158. The footer always has the time. When `initialize()` reads such a release in the startup banner, and the bottom TUI rows match `TUI_FOOTER_PATTERN`, the rendered screen follows these rules: - A turn is COMPLETED when the last relevant row above the composer is the completion footer, under a reply or tool row. A complete `N background terminals running · /ps to view · /stop to close` notice may appear between that footer and the composer; CAO excludes it only when a recognized completion footer precedes it (allowing blank rows). An empty turn is also COMPLETED when CAO recorded the exact dispatched message, its full rendered echo is visible after a pre-dispatch screen capture, and a valid footer follows it with no response rows in between. Without that attribution, an empty-looking frame remains UNKNOWN. An error cell (`■ ...`) also ends a turn. - A user message with rows under it and no footer is PROCESSING. A user message with nothing under it is UNKNOWN, which keeps the last status. - A frame without recognizable bottom TUI rows may be a redraw: UNKNOWN, unless a spinner or the completion footer decides. The configurable status line can be absent; shortcut hints can still identify the TUI area. - A spinner above the footer of a finished turn is history (for example, a spinner line that a tool result quotes). - Extraction removes the footer row from the reply. For an attributed empty turn it returns `""`; manual input, incomplete echoes, stale screens, and ambiguous captures do not imply an empty answer. The raw stream (`CAO_PYTE_STATUS=false`) keeps the older rules, because it keeps every redraw. A provider rebuilt after a cao-server restart does not run `initialize()` and also keeps the older rules. ### Message Extraction The provider automatically extracts the last assistant response from terminal output using a two-phase approach: 1. **Primary**: Finds the last user message (`You ...` or `› text`) and extracts everything between it and the next empty idle prompt 2. **Fallback**: Uses assistant markers (`assistant:` or `•`) when no user message is found This works for both the label format (`assistant: response`) and Codex's native bullet format (`• response with multiple bullets`). ## Configuration CAO's Codex provider launches `codex` with tmux-compatible flags and relies on your existing Codex CLI configuration/authentication. - `--provider codex` selects the provider. - `--agents ` specifies the agent profile. When an agent profile with a `system_prompt` is provided, it is injected into Codex as `developer_instructions` via the `-c` config override flag. - Model/timeout/approval settings are configured in Codex CLI itself (outside of CAO). ### Agent Profile Integration When you launch with an agent profile (e.g., `--agents code_supervisor`), CAO: 1. Loads the agent profile from the agent store (built-in or `~/.aws/cli-agent-orchestrator/agent_store/`) 2. Extracts the `system_prompt` from the profile's Markdown content 3. Passes it to Codex via `-c developer_instructions=""`, which Codex injects as a developer role message This enables Codex to operate with role-specific instructions (e.g., supervisor, developer, reviewer) just like other providers. ### MCP Server Integration If the agent profile includes `mcpServers`, CAO injects each MCP server into Codex via `-c mcp_servers..=` config overrides. This is per-session and does not modify the user's global `~/.codex/config.toml`. For example, the `code_supervisor` profile includes the `cao-mcp-server` which provides `handoff` and `send_message` tools. This allows the supervisor agent to delegate work to Developer and Reviewer agents through CAO's multi-agent orchestration. CAO passes each server's environment as **one TOML inline table with quoted keys** (`-c mcp_servers..env={ "LOG.LEVEL" = "info" }`) rather than one override per key. Codex parses the value side of `-c key=value` as TOML, so any environment key an [agent plugin](agent-plugins.md) may legally declare is expressible there. The server *name* is different: it becomes a segment of the override's dotted **path**, so a name that is not a bare TOML key (`^[A-Za-z0-9_-]+$`) would nest the entry under the wrong table. An agent-plugin server with such a name is not delivered to Codex and is reported as `mcp.server_name_unsupported`; that agent plugin's other servers are unaffected. For the CAO server, CAO also sets `omit_tools_from=["deferred"]`. Codex defers MCP tools behind its `tool_search` tool, and a search for `assign handoff` can rank other CAO tools first; a supervisor then reports that `cao-mcp-server` failed to start. With the setting, the CAO tools are in the initial tool list. The key exists from Codex 0.150. CAO also sets `tool_timeout_sec=600.0` (10 minutes) for each MCP server to allow long-running operations like handoff. **Important**: The value must be a TOML float (`600.0`, not `600`) because Codex deserializes this field via `Option`. A TOML integer is silently rejected, falling back to the 60-second default. ### Memory Injection When CAO's memory system is enabled, interactive Codex terminals receive relevant memories through the provider start prompt. Orchestrated terminals receive them with their first CAO-delivered message. Neither route changes the project tree. The legacy project-file route is available only when `memory.injection.write_project_files=true`. In that mode, the built-in `codex_memory` event plugin writes a delimited block into `/AGENTS.md`: ```markdown ## Context from CAO Memory - [project] testing-framework: Always use pytest for this project ... ``` Because `AGENTS.md` is a user-authored, repo-root file, the event plugin owns **only** the delimited block and replaces it in place on each run — any hand-written content around it is preserved (the same approach as the Claude Code `CLAUDE.md` event plugin, not Kiro's whole-file ownership). The event plugin is observer-only: it runs after the terminal is created, logs-and-skips on any error, and never crashes `cao-server`. With memory disabled, it does not inspect, create, rewrite, or clean up memory files. See [memory.md](memory.md) for the full memory system. ### Launch Flags The Codex provider automatically adds these flags for tmux compatibility: - `--no-alt-screen`: Runs Codex in inline mode so output stays in normal scrollback, making `tmux capture-pane` reliable - `--disable shell_snapshot`: Prevents TTY input conflicts (SIGTTIN) caused by the shell_snapshot subprocess inheriting stdin in tmux By default, CAO also passes `--yolo` (alias for `--dangerously-bypass-approvals-and-sandbox`) because CAO agents run in non-interactive tmux sessions where approval prompts block handoff/assign flows. Profiles can opt out via `codexProfile`; see [Custom Codex Profile](#custom-codex-profile). Any unrestricted allowed-tools configuration (`allowedTools: ["*"]`, `--allowed-tools '*'`, or `cao launch --yolo`) forces `--yolo` regardless of the profile setting. > **`--yolo` makes your Codex sandbox settings inert — do not rely on them as containment.** > Because `--yolo` bypasses the sandbox entirely, nothing in your `~/.codex/config.toml` > `[sandbox_*]` tables applies to a default CAO worker. That includes > `[sandbox_workspace_write].network_access = false`: setting it does not stop a worker reaching > the network, and it does not restrict Codex's built-in backend web tool, which runs server-side > rather than as a sandboxed shell command (reported in > [#707](https://github.com/awslabs/cli-agent-orchestrator/issues/707)). A worker that reads > untrusted web content can carry prompt-injected instructions into the working tree with no > human in the loop. > > To actually contain a Codex worker: > > - **`codexProfile`** drops `--yolo`, so your named `$CODEX_HOME/.config.toml` governs > sandbox and approvals. Note that `allowedTools: ["*"]` overrides it and forces `--yolo`; CAO > logs a warning when that happens, so the discard is not silent. > - **`use_worktree=true`** puts writes in a throwaway git worktree behind review instead of the > live tree. That bounds the damage; it does not stop the network access. ### Custom Codex Profile The `codexProfile` field on an agent profile names a profile config file, `$CODEX_HOME/.config.toml` (`~/.codex/.config.toml` by default). When set, CAO drops `--yolo` and passes `--profile ` instead; codex layers that file on top of your base `~/.codex/config.toml`, letting the named profile govern sandbox and approval behavior. Unrestricted allowed tools (`allowedTools: ["*"]`, `--allowed-tools '*'`, or `cao launch --yolo`) override this field and always force `--yolo`. **Do not use a `[profiles.]` table in `~/.codex/config.toml`.** That was the older layout and codex now rejects it outright: with such a table present, `codex --profile ` exits immediately with ``` Error: --profile `` cannot be used while ~/.codex/config.toml contains legacy `profile = ""` or `[profiles.]` config; move those settings into ~/.codex/.config.toml and remove the legacy profile selector/table. ``` Verified on codex-cli 0.162.1. Each profile gets its own file; the keys go at the top level of that file, not under a `[profiles.*]` header. Note also that if `.config.toml` does not exist, codex starts anyway and layers nothing — so a typo in `codexProfile` yields a worker running under your *base* config rather than the sandbox you intended. Check the file name matches. **Important — non-interactive only**: CAO's status detector cannot interact with Codex's current boxed approval UI (`Command Approval Required / [a] Accept / [d] Decline`). Any `codexProfile` you reference MUST resolve to a non-interactive permission tier, or CAO sessions will time out waiting for input that nothing can deliver. Safe shapes: - **Read-only / audit agents**: `approval_policy = "never"` + `sandbox_mode = "read-only"` — write/network/escape attempts fail closed and return errors to the model. - **Write-permitted agents**: `approval_policy = "never"` + `sandbox_mode = "workspace-write"` — writes inside the workspace proceed; sandbox escapes fail closed. - **Smart Approvals (classifier-gated)**: `approval_policy = "on-request"` + `sandbox_mode = "workspace-write"` + `approvals_reviewer = "auto_review"` — the auto-review classifier decides escalations; denials fail closed without prompting. Avoid `approval_policy = "untrusted"` or `approval_policy = "on-request"` without `approvals_reviewer = "auto_review"` — those tiers prompt the user, which CAO cannot answer. Example — a reviewer that runs under Codex's read-only sandbox: ```markdown --- name: reviewer description: Code Reviewer provider: codex role: reviewer codexProfile: cao_reviewer --- You review code for quality and correctness. ``` Matching `~/.codex/cao_reviewer.config.toml` (a file of its own, named after the profile — not a section inside `~/.codex/config.toml`): ```toml sandbox_mode = "read-only" approval_policy = "never" ``` ### Inline Codex Config Overrides The `codexConfig` field on an agent profile is a map of Codex config overrides that CAO passes as `-c key=value` flags at launch — the same mechanism used for `developer_instructions` and `mcpServers`. It lets a profile set per-agent Codex knobs (reasoning effort, service tier, fast mode, model, …) **without editing the global `~/.codex/config.toml` or maintaining named profile files**. - **Keys** may be dotted paths into Codex's config schema (e.g. `model_reasoning_effort`, `service_tier`, `features.fast_mode`). - **Values** are serialized to TOML scalars: strings are quoted, booleans and numbers are emitted bare. So `model_reasoning_effort: "xhigh"` becomes `-c model_reasoning_effort="xhigh"` and `features.fast_mode: true` becomes `-c features.fast_mode=true`. - Overrides are applied in **both** the default `--yolo` path and the `--profile ` path, so effort/fast-mode knobs work whether or not a named profile governs sandbox/approvals. - `codexConfig` **composes** with `codexProfile`. Because Codex applies CLI `-c` overrides last, a key set in both wins from `codexConfig`. - Scope is per-session: nothing is written to the user's global `~/.codex/config.toml`. Example — a developer agent pinned to high reasoning effort and fast mode: ```markdown --- name: backend-developer description: Backend developer agent provider: codex role: developer codexConfig: model_reasoning_effort: "xhigh" service_tier: "fast" features.fast_mode: true --- You implement backend changes from a task spec. ``` This launches Codex as `codex --yolo … -c model_reasoning_effort="xhigh" -c service_tier="fast" -c features.fast_mode=true`, applying the effort and fast-mode settings to that agent only. ## Workflows ### 1. Interactive single-agent task ```bash cao launch --agents codex_developer --provider codex ``` In the tmux window, type your prompt at the Codex prompt. To get the CAO terminal id (useful for API automation / MCP), run: ```bash echo "$CAO_TERMINAL_ID" ``` ### 2. Automate send/get-output via HTTP API ```bash python3 - <<'PY' import time import requests terminal_id = "" requests.post( f"http://localhost:9889/terminals/{terminal_id}/input", params={"message": "Please review this Python code for security issues"}, ).raise_for_status() # Poll status until completion while True: status = requests.get(f"http://localhost:9889/terminals/{terminal_id}").json()["status"] if status in {"completed", "error", "waiting_user_answer"}: break time.sleep(1) resp = requests.get( f"http://localhost:9889/terminals/{terminal_id}/output", params={"mode": "last"}, ) resp.raise_for_status() print(resp.json()["output"]) PY ``` ## Authentication ### OpenAI API Key Setup 1. **Install Codex CLI**: ```bash npm install -g @openai/codex ``` 2. **Authenticate** (choose one): ```bash # Option 1: Set environment variable export OPENAI_API_KEY=your-key-here # Option 2: Interactive login codex login ``` 3. **Verify Installation**: ```bash codex --version ``` ## Troubleshooting ### Common Issues 1. **Authentication Failed**: ```bash # Re-authenticate codex logout codex login # Or set API key directly export OPENAI_API_KEY=your-key-here ``` 2. **Timeout / Hanging Tasks**: - Confirm `codex` works in a regular shell (`codex`, then exit) - Attach to the tmux session and check whether Codex is waiting for input/approval - Verify your OpenAI API key or ChatGPT subscription and network connectivity 3. **Status Detection Problems**: - Check terminal history for unexpected prompts - Verify Codex CLI version compatibility - Review custom prompt patterns ## Implementation Notes - Command building is handled by `CodexProvider._build_codex_command()` which constructs the launch command with flags and optional `developer_instructions`. - A warm-up `echo ready` command is sent before launching Codex to prevent immediate exit in fresh tmux sessions. - Workspace trust prompts are auto-accepted by `CodexProvider._handle_trust_prompt()` during initialization. - Status detection uses a bottom-N-lines approach (`IDLE_PROMPT_TAIL_LINES = 5`) to check the last few lines for the idle prompt, since `--no-alt-screen` mode keeps history in scrollback. - `ASSISTANT_PREFIX_PATTERN` matches both `assistant:` (label style) and `•` (Codex bullet style) for detecting assistant responses after user messages. - `USER_PREFIX_PATTERN` matches both `You` (label style) and `› text` (Codex interactive prompt), using `[^\S\n]` to prevent crossing newline boundaries. - `IDLE_PROMPT_STRICT_PATTERN` matches only empty prompt lines (`› ` or `❯ ` without trailing text) for extraction boundary detection. - Output mode `last` uses `CodexProvider.extract_last_message_from_script()`, which extracts text between the last user message and the next idle prompt. - Exiting a Codex terminal uses `/exit` (`POST /terminals/{terminal_id}/exit`). - **Exit check**: when the pane's command is again the shell that ran before Codex, the status is ERROR, so that the inbox does not type into the shell. The lookup costs about 0.1 s of tmux calls, so a ready verdict (IDLE or COMPLETED) reads it every time, and other verdicts read it at most every 2 s (`EXIT_CHECK_INTERVAL_S`). - **Handoff message context**: `_handoff_impl()` prepends a `[CAO Handoff]` prefix to the task message so the worker agent knows this is a blocking handoff. Without this, Codex agents proactively try to use `send_message` to notify the supervisor, which fails because the worker doesn't have the supervisor's terminal ID. The prefix tells the agent to simply output results and finish — the orchestrator captures the response automatically. - **TUI footer handling** (`--no-alt-screen` mode): The composer and bottom TUI rows are distinct from the completion footer above the composer. Older formats include `› [suggestion hint]` + `? for shortcuts` + `N% context left` (v0.110 and earlier) and `› [suggestion hint]` + `model · N% left · path` (v0.111+, PR #13202). In 0.169 the composer is followed by a status line and then shortcut hints; the configurable status line can be absent. `TUI_FOOTER_PATTERN` recognizes known bottom rows, and `_compute_tui_footer_cutoff()` uses them to exclude TUI chrome from user-message matching in `get_status()` and `extract_last_message_from_script()`. This is a visual heuristic: reply text imitating a completion footer and background-terminal notice can be mistaken for chrome. A truncated notice is not recognized and can leave a completed turn appearing PROCESSING; narrow or resized views can also hide identifying rows. - **TUI progress spinner**: During processing, Codex shows `• [text] (Ns • esc to interrupt)` inline. The `•` would falsely match `ASSISTANT_PREFIX_PATTERN`, and the TUI `›` hint would match idle prompt — triggering false COMPLETED. `TUI_PROGRESS_PATTERN` detects the spinner and returns PROCESSING before the COMPLETED check. codex-cli 0.153.x alternates the leading glyph between `•` and `◦` as the spinner animates, so the pattern accepts either; a frame that lands on the hollow glyph used to read IDLE. ### Status Values - `TerminalStatus.IDLE`: Ready for input - `TerminalStatus.PROCESSING`: Working on task - `TerminalStatus.WAITING_USER_ANSWER`: Waiting for user input - `TerminalStatus.COMPLETED`: Task finished - `TerminalStatus.ERROR`: Error occurred ## Best Practices ### 1. Agent Naming Use descriptive names for Codex agents: - `codex-frontend-dev` - Frontend development - `codex-security-reviewer` - Security code review - `codex-api-designer` - API design and documentation ### 2. Task Breakdown Break complex tasks into smaller, focused prompts: ```python # Instead of: "Build a complete web application" # Use: "Design the database schema for user authentication" "Implement the authentication API endpoints" "Create the login form component" "Write tests for the authentication flow" ``` ## End-to-End Testing The E2E test suite validates handoff, assign, and send_message flows against real CLI providers. ### Test Structure ``` test/e2e/ ├── conftest.py # Shared fixtures (server health, CLI checks, helpers) ├── test_handoff.py # Worker lifecycle tests (handoff) — 10 tests (2 per provider) ├── test_assign.py # Worker lifecycle tests (assign) — 10 tests (2 per provider) ├── test_send_message.py # Inbox delivery tests — 5 tests (1 per provider) └── test_supervisor_orchestration.py # Supervisor→worker delegation tests — 10 tests (2 per provider) ``` ### Prerequisites - Running CAO server: `uv run cao-server` - Authenticated CLI tools: `codex`, `claude`, `kiro-cli` - tmux installed - Agent profiles installed: `analysis_supervisor`, `data_analyst`, `report_generator` ```bash cao install examples/assign/analysis_supervisor.md cao install examples/assign/data_analyst.md cao install examples/assign/report_generator.md ``` ### Running E2E Tests ```bash # Run all E2E tests (all providers) uv run pytest -m e2e test/e2e/ -v # Run for a specific provider uv run pytest -m e2e test/e2e/ -v -k codex uv run pytest -m e2e test/e2e/ -v -k claude_code uv run pytest -m e2e test/e2e/ -v -k kiro_cli # Run a specific test type uv run pytest -m e2e test/e2e/test_handoff.py -v uv run pytest -m e2e test/e2e/test_assign.py -v uv run pytest -m e2e test/e2e/test_send_message.py -v uv run pytest -m e2e test/e2e/test_supervisor_orchestration.py -v -o "addopts=" ``` E2E tests are excluded from default `pytest` runs via the `-m 'not e2e'` addopts in `pyproject.toml`. ## Examples See the `examples/` directory for a step-by-step walkthrough: - `examples/codex-basic/` - Basic Codex usage (includes three agent profiles) - `examples/assign/` - Assign (async parallel) workflow with data analysts and report generator ## Contributing To contribute to the Codex provider: 1. Fork the repository 2. Create a feature branch 3. Add tests for new functionality 4. Update documentation 5. Submit a pull request ## Support For issues and questions: - GitHub Issues: [cli-agent-orchestrator](https://github.com/awslabs/cli-agent-orchestrator/issues) - Documentation: [Codex CLI Provider Docs](https://github.com/awslabs/cli-agent-orchestrator/blob/main/docs/codex-cli.md)