Delegate to Codex (CLI)
Run codex exec non-interactively for a second opinion, review, or a delegated
edit worker. Model default: gpt-5.6-sol at -c model_reasoning_effort='"high"'
(medium/low only for small or mechanical work).
Happy path
Isolate. Read-only work runs in the current checkout with
-s read-only:slug="codex-$(date +%Y%m%d-%H%M%S)" run_dir="/tmp/codex-$slug"; mkdir -p "$run_dir"Edit work gets its own worktree instead, so the diff is easy to inspect or discard:
slug="codex-$(date +%Y%m%d-%H%M%S)" worktree="../$(basename "$PWD")-$slug" git worktree add -b "agent/codex/$slug" "$worktree" HEAD run_dir="$worktree/.agent-runs/$slug"; mkdir -p "$run_dir"Write the brief to
$run_dir/prompt.md. Include context, exact task, constraints, verification commands, output contract — and a hard completion criterion: GPT-5.6 Sol is exploratory and keeps widening scope without an unambiguous definition of "done".Launch. Always feed the prompt from the file with
- < prompt.md; never pass it as a bare argument under a harness (see Gotchas: stdin wedge).Read-only reviewer / second opinion:
codex exec -C "$PWD" \ -m gpt-5.6-sol -c model_reasoning_effort='"high"' \ -s read-only --json -o "$run_dir/final.md" \ - < "$run_dir/prompt.md" > "$run_dir/events.jsonl"Edit worker — same command with
-C "$worktree"and--sandbox workspace-writeinstead of-s read-only.Research briefs that need current information: add
-c tools.web_search=true.Harvest. Final answer in
$run_dir/final.md; session id in thethread.startedevent'sthread_idfield in$run_dir/events.jsonl— keep it, it is the only safe handle for a follow-up turn (see Gotchas). For edit work also: diff viagit -C "$worktree" diff— harvest from the working tree, not branch history, since the worker's commits may be missing (see Gotchas) — and run a fresh read-only reviewer over the diff before merging.
Gotchas
- A missing
codexbinary or stale auth surfaces mid-run as an abort — or a hang indistinguishable from the stdin wedge. Preflightcodex --versionand auth (codex login, ChatGPT auth, or a scopedCODEX_API_KEY/OPENAI_API_KEY) before long runs. - Stdin wedge.
codex execreads piped stdin whenever you pass-, no prompt, or a prompt argument — and under a harness stdin never closes, so it wedges at startup (0% CPU, no output). Use- < prompt.md, or add< /dev/nullto any argument form. Nevercodex exec "$(cat prompt.md)"bare. - Workers cannot commit in a linked worktree. The sandbox can't write the
parent repo's
.git/worktrees/<name>, sogit commit/git mergefail even withworkspace-write. The orchestrator runs all git commands; workers only edit and resolve content. Commit-shaped deliverable: brief the worker "commit; if commit fails, produce agit bundle" and fetch from the bundle. codex exec review --base <ref>recurses on 0.144.1 — re-execs itself endlessly, emits no findings, leaves stray processes. Review with plaincodex exec -s read-onlyand a "review the diff between and HEAD" prompt instead.codex exec resumesilently drops the original sandbox. It accepts-m,-c,--json,-obut rejects-Cand-s(exit 2), so the resumed turn takessandbox_modefrom~/.codex/config.toml— a run launched-s read-onlyresumes with whatever the config says, up todanger-full-access. Re-assert it through-c, which is accepted:codex exec resume "$thread_id" -m gpt-5.6-sol -c sandbox_mode='"read-only"' --json -o resume.md "..." < /dev/null. Re-pass-mtoo, or the resume runs on the config's model.- Resume by id, not
--last.--lastpicks the newest recorded session in the cwd, which is the wrong thread as soon as any other codex run has started since. Use thethread_idfrom thethread.startedevent; it stays stable across resumes of the same thread. - Resume cannot change the working directory (
-Crejected), so a thread is pinned to the tree it started in. Cross-tree follow-ups need a fresh run. - A crashing MCP server in
~/.codex/config.tomlaborts the whole run. Pass--ignore-user-config(auth still resolves viaCODEX_HOME) and re-specify-m/-con the CLI. - ChatGPT-plan usage limits abort runs mid-flight with a reset time. Fall back to another vendor until then.
-s read-onlyis a hard filesystem boundary — commands that write caches or build artifacts fail under it.-pselects a config profile, not an agent persona; custom subagents are TOML files under.codex/agents/or~/.codex/agents/.codex applyapplies the latest agent diff to the current tree — checkpwdand branch first.- Worktrees omit ignored files. Copy only explicit prerequisites (e.g.
.env.local), never secret directories. If the worker needs uncommitted local changes, apply an explicit patch in the worktree — never checkpoint unrelated user WIP withgit add -A.
Not possible
- No approval prompts in exec mode:
-a/--ask-for-approvalis rejected. - No
--searchflag oncodex exec— use-c tools.web_search=true. - No worktree creation or cleanup — manage them yourself.
- No resuming
--ephemeralruns. - The sandbox is not security isolation:
--dangerously-bypass-approvals-and-sandboxonly inside a bounded container/VM/CI runner — a worktree is not a security sandbox.
Evidence and full mechanics behind each gotcha: reference/gotchas.md.