<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Apply-Patch on RockB</title><link>https://baeseokjae.github.io/tags/apply-patch/</link><description>Recent content in Apply-Patch on RockB</description><image><title>RockB</title><url>https://baeseokjae.github.io/images/og-default.png</url><link>https://baeseokjae.github.io/images/og-default.png</link></image><generator>Hugo</generator><language>en-us</language><lastBuildDate>Wed, 18 Nov 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://baeseokjae.github.io/tags/apply-patch/index.xml" rel="self" type="application/rss+xml"/><item><title>OpenAI Codex CLI 2026: What apply_patch Actually Does, and Why the Tool Description Matters</title><link>https://baeseokjae.github.io/posts/openai-codex-cli-guide-2026/</link><pubDate>Fri, 24 Apr 2026 18:04:05 +0000</pubDate><guid>https://baeseokjae.github.io/posts/openai-codex-cli-guide-2026/</guid><description>How OpenAI Codex CLI edits files: the *** Begin Patch envelope, the parser&amp;#39;s grammar and its two boundary errors, the sandbox check behind auto-approve, and the apply_patch instruction the model actually reads.</description><content:encoded><![CDATA[<p>OpenAI Codex CLI is a terminal-based AI coding agent that reads your codebase, writes and edits files, runs tests, and commits changes — all from your command line. Unlike web-based AI tools, Codex CLI runs locally against your actual repository, understanding real project context rather than a pasted snippet.</p>
<p><em>Revised 18 November 2026. This revision adds a mechanics section on the <code>apply_patch</code> tool — the envelope it emits, the grammar the parser enforces and the sandbox check that decides whether you are asked — and withdraws several usage figures that earlier revisions repeated without a primary source: weekly active users, npm download totals, GitHub stars, the share of the codebase written in Rust, and the claimed start-up and memory deltas. Those numbers could not be re-verified from source during this revision, so they are removed rather than restated. A correction pass the same day narrowed two statements to what the pinned source actually says: the slash-command table is a selection from a 66-variant enum rather than the enum itself, and patch auto-approval covers the profiles that apply no outer sandbox as well as the sandboxed branch.</em></p>
<h2 id="what-is-openai-codex-cli-the-rust-built-terminal-ai-agent">What Is OpenAI Codex CLI? (The Rust-Built Terminal AI Agent)</h2>
<p>OpenAI Codex CLI is an open-source, terminal-native AI coding agent that plans, writes, edits, and tests code within your local development environment. Unlike browser-based AI assistants, it reads your codebase, executes shell commands, and manages file changes — a repository-scoped engineering agent rather than a text-completion tool. The source lives in the <a href="https://github.com/openai/codex/tree/cc7a62e19cb4703495e3a6d01e1af076e8223833">openai/codex repository</a>; the CLI is the Rust workspace under <code>codex-rs/</code> in that tree, and this revision reads it at commit <code>cc7a62e19cb4703495e3a6d01e1af076e8223833</code> (18 November 2026).</p>
<p>The differentiator against chat-based assistants is the approval machinery, not the model: every file edit and every shell command passes through a policy check, so autonomy is a configuration decision rather than a default. That is also why the mechanics matter. The agent does not write files the way a human writes a diff, and the check that decides whether you are prompted has specific, readable conditions. Both are in the mechanics section below.</p>
<h3 id="why-the-rust-rewrite-matters">Why the Rust Rewrite Matters</h3>
<p>Codex CLI ships as a compiled binary that runs without a Node.js runtime, which matters for CI runners, remote servers and container images where the npm path would add a toolchain to maintain. The Rust core is also what makes OS-level sandbox enforcement practical: patch application and command execution route through process isolation, so a policy decision has teeth instead of being advisory. This guide does not restate vendor performance figures for that rewrite; the observable consequence for an operator is the approval and sandbox path described below.</p>
<hr>
<h2 id="installing-codex-cli-on-macos-windows-and-linux">Installing Codex CLI on macOS, Windows, and Linux</h2>
<p>Codex CLI installs from npm (universal), Homebrew (macOS), the standalone installer, or a release binary (Linux/CI); the channels and archive names below are the ones the repository&rsquo;s <a href="https://raw.githubusercontent.com/openai/codex/cc7a62e19cb4703495e3a6d01e1af076e8223833/README.md">README</a> lists at the pinned commit. Choose npm if you want the latest release immediately; choose Homebrew if you prefer managed updates integrated with your existing workflow. For CI pipelines or Docker images, the binary approach avoids runtime dependencies entirely. Before you install, confirm you have Node.js 16 or newer for the npm path — that is the <code>engines</code> field the published package declares in its <a href="https://registry.npmjs.org/@openai/codex/latest">registry metadata</a>. Installation is the same whether you&rsquo;re targeting a personal MacBook, a Linux VPS, or a Windows machine via WSL2.</p>
<h3 id="macos-homebrew-or-npm">macOS (Homebrew or npm)</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># Option A: Homebrew (recommended for macOS)</span>
</span></span><span style="display:flex;"><span>brew install --cask codex
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Option B: npm global install</span>
</span></span><span style="display:flex;"><span>npm install -g @openai/codex
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Verify installation</span>
</span></span><span style="display:flex;"><span>codex --version
</span></span></code></pre></div><h3 id="windows-powershell--wsl2">Windows (PowerShell + WSL2)</h3>
<p>Native Windows support runs through WSL2. PowerShell is supported with sandbox restrictions applied via Windows sandbox API.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-powershell" data-lang="powershell"><span style="display:flex;"><span><span style="color:#75715e"># Install WSL2 first if not present</span>
</span></span><span style="display:flex;"><span>wsl --install
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Then inside WSL2 terminal</span>
</span></span><span style="display:flex;"><span>npm install -g @openai/codex
</span></span></code></pre></div><p>For native PowerShell without WSL2:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-powershell" data-lang="powershell"><span style="display:flex;"><span>npm install -g @openai/codex
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Windows sandbox mode activates automatically — network calls are restricted</span>
</span></span></code></pre></div><h3 id="linux-npm-or-binary">Linux (npm or binary)</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># npm path (requires Node.js 16+ per the published engines field)</span>
</span></span><span style="display:flex;"><span>npm install -g @openai/codex
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Binary path (no Node.js needed, ideal for CI)</span>
</span></span><span style="display:flex;"><span>curl -fsSL https://chatgpt.com/codex/install.sh | sh
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># The installer pulls from releases.openai.com and falls back to GitHub Releases.</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># To take an archive by hand, use e.g. codex-x86_64-unknown-linux-musl.tar.gz</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># from github.com/openai/codex/releases/latest; the archive holds one binary.</span>
</span></span></code></pre></div><hr>
<h2 id="first-run-authentication-and-quick-start">First Run: Authentication and Quick Start</h2>
<p>Setting up Codex CLI authentication takes one command. Codex supports two auth paths: ChatGPT account login (browser OAuth flow) and direct API key injection. The ChatGPT account path is easier for individuals — it reuses your existing subscription and grants access to the latest Codex-specific model. The API key path is mandatory for CI/CD pipelines, team environments, and any context where a browser flow is unavailable. Once authenticated, Codex stores credentials in <code>~/.codex/auth.json</code> and reuses them across sessions. You do not need to re-authenticate per project; the credential is global. For teams, the recommended approach is injecting <code>OPENAI_API_KEY</code> as an environment variable to avoid credential files in shared environments. Codex is included in the Plus, Pro, Business, Edu and Enterprise ChatGPT plans; API-key users are billed per token at standard rates. After authentication, the recommended first action is running <code>codex &quot;describe the architecture of this project&quot;</code> on an existing codebase — this confirms connectivity and gives you a quick mental model of how Codex interprets your code before you ask it to change anything.</p>
<h3 id="authenticating-via-chatgpt-account">Authenticating via ChatGPT Account</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>codex login
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Opens browser — approve in ChatGPT, token saved automatically</span>
</span></span></code></pre></div><h3 id="authenticating-via-api-key">Authenticating via API Key</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>export OPENAI_API_KEY<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;sk-...&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Or add to your shell profile (~/.zshrc or ~/.bashrc)</span>
</span></span><span style="display:flex;"><span>echo <span style="color:#e6db74">&#39;export OPENAI_API_KEY=&#34;sk-...&#34;&#39;</span> &gt;&gt; ~/.zshrc
</span></span></code></pre></div><h3 id="your-first-task">Your First Task</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>cd my-project
</span></span><span style="display:flex;"><span>codex <span style="color:#e6db74">&#34;add a JSON serialization function to utils.ts and write unit tests&#34;</span>
</span></span></code></pre></div><p>Codex reads the repository, proposes a plan, and (depending on your approval mode) either shows a diff for review or executes directly. The output includes changed files, test results, and a summary of what was done.</p>
<hr>
<h2 id="the-two-knobs-approval-policy-and-sandbox-mode">The Two Knobs: Approval Policy and Sandbox Mode</h2>
<p>Codex CLI does not expose one &ldquo;autonomy level&rdquo; setting. It exposes two independent flags, and the pair is what determines how much the agent can do without you. From the installed <code>codex-cli 0.170.0</code> help output on 18 November 2026:</p>
<ul>
<li><code>-a, --ask-for-approval &lt;APPROVAL_POLICY&gt;</code> — when the model must ask before executing a command. The flag accepts exactly two values: <code>on-request</code> (&ldquo;the model decides when to ask the user for approval&rdquo;) and <code>never</code> (&ldquo;never ask for user approval; execution failures are immediately returned to the model&rdquo;). <code>codex -a untrusted</code> fails argument parsing with <code>possible values: on-request, never</code>, and the upstream configuration reference states that <code>untrusted</code> is unsupported. The Rust enum behind the setting still has an <code>UnlessTrusted</code> variant, but the CLI flag does not expose it.</li>
<li><code>-s, --sandbox &lt;SANDBOX_MODE&gt;</code> — the filesystem policy for model-generated shell commands, with the values <code>read-only</code>, <code>workspace-write</code> and <code>danger-full-access</code>.</li>
</ul>
<p>This is the correction most guides still get wrong, including earlier revisions of this page: there is no <code>--approval-mode suggest|auto-edit|full-auto</code> flag in the current CLI. If you are following a tutorial that uses one, it predates the current flag set. The vocabulary that survives is the <em>combination</em>: autonomous runs are <code>--sandbox workspace-write</code> with <code>--approve-for-me</code>, which routes approval requests through automatic review inside that sandbox rather than prompting you.</p>
<h3 id="setting-the-policy">Setting the Policy</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># Conservative: read-only sandbox, ask when the model requests it</span>
</span></span><span style="display:flex;"><span>codex -a on-request -s read-only <span style="color:#e6db74">&#34;refactor auth module&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Day-to-day: writable workspace, model decides when to ask</span>
</span></span><span style="display:flex;"><span>codex -a on-request -s workspace-write <span style="color:#e6db74">&#34;migrate all useState hooks to useReducer&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Unattended: writable workspace, approvals routed to automatic review</span>
</span></span><span style="display:flex;"><span>codex -s workspace-write --approve-for-me <span style="color:#e6db74">&#34;run all tests, fix failures, commit&#34;</span>
</span></span></code></pre></div><p>The two flags are also settable through <code>-c key=value</code> overrides, which is how you pin them in CI without editing a config file:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>codex -c approval_policy<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;never&#34;</span> -s workspace-write <span style="color:#e6db74">&#34;run the test suite and fix failures&#34;</span>
</span></span></code></pre></div><p>Do not reach for <code>--dangerously-bypass-approvals-and-sandbox</code> to make an unattended run work. Its own help text calls it &ldquo;EXTREMELY DANGEROUS&rdquo; and says it is intended solely for environments that are already sandboxed from the outside — and, as the mechanics section below shows, bypassing the sandbox changes the meaning of the approval decision rather than removing it.</p>
<h3 id="when-to-use-which-pair">When to Use Which Pair</h3>
<table>
	<thead>
			<tr>
					<th>Combination</th>
					<th>File Writes</th>
					<th>Shell Commands</th>
					<th>Recommended For</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>-a on-request -s read-only</code></td>
					<td>Needs approval</td>
					<td>Read-only, asks on request</td>
					<td>Unfamiliar repos, production hotfixes</td>
			</tr>
			<tr>
					<td><code>-a on-request -s workspace-write</code></td>
					<td>Inside workspace</td>
					<td>Asks when the model requests</td>
					<td>Active feature branches, refactors</td>
			</tr>
			<tr>
					<td><code>-s workspace-write --approve-for-me</code></td>
					<td>Inside workspace</td>
					<td>Auto-reviewed in sandbox</td>
					<td>CI/CD pipelines, isolated sandboxes</td>
			</tr>
	</tbody>
</table>
<hr>
<h2 id="what-codex-cli-actually-sends-when-it-edits-a-file">What Codex CLI Actually Sends When It Edits a File</h2>
<p>Codex CLI does not send a unified diff. When the model edits a file it emits a line-delimited text envelope, and a dedicated crate parses that envelope into hunks before anything touches the filesystem. The grammar is published as a doc comment at the top of <a href="https://raw.githubusercontent.com/openai/codex/cc7a62e19cb4703495e3a6d01e1af076e8223833/codex-rs/apply-patch/src/parser.rs"><code>codex-rs/apply-patch/src/parser.rs</code></a> at the pinned commit, quoted here verbatim:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>start: begin_patch environment_id? hunk+ end_patch
</span></span><span style="display:flex;"><span>begin_patch: &#34;*** Begin Patch&#34; LF
</span></span><span style="display:flex;"><span>environment_id: &#34;*** Environment ID: &#34; filename LF
</span></span><span style="display:flex;"><span>end_patch: &#34;*** End Patch&#34; LF?
</span></span><span style="display:flex;"><span>hunk: add_hunk | delete_hunk | update_hunk
</span></span><span style="display:flex;"><span>add_hunk: &#34;*** Add File: &#34; filename LF add_line+
</span></span><span style="display:flex;"><span>delete_hunk: &#34;*** Delete File: &#34; filename LF
</span></span><span style="display:flex;"><span>update_hunk: &#34;*** Update File: &#34; filename LF change_move? change?
</span></span><span style="display:flex;"><span>filename: /(.+)/
</span></span><span style="display:flex;"><span>add_line: &#34;+&#34; /(.+)/ LF -&gt; line
</span></span><span style="display:flex;"><span>change_move: &#34;*** Move to: &#34; filename LF
</span></span><span style="display:flex;"><span>change: (change_context | change_line)+ eof_line?
</span></span><span style="display:flex;"><span>change_context: (&#34;@@&#34; | &#34;@@ &#34; /(.+)/) LF
</span></span><span style="display:flex;"><span>change_line: (&#34;+&#34; | &#34;-&#34; | &#34; &#34;) /(.+)/ LF
</span></span><span style="display:flex;"><span>eof_line: &#34;*** End of File&#34; LF
</span></span></code></pre></div><p>Three things this settles that a reader usually has to guess at:</p>
<ol>
<li><strong>It is not a diff.</strong> There are no <code>---</code>/<code>+++</code> file headers, no <code>diff --git</code>, no line numbers. The target file is named once, by the <code>*** Update File: </code> marker, and located by the context lines inside the hunk.</li>
<li><strong>The grammar requires a hunk; the parser does not enforce that.</strong> <code>hunk+</code> in the <code>start</code> rule means a conforming payload carries at least one file operation. The Rust parser is laxer than the grammar it documents: boundary checking compares only the first and last line, and <a href="https://raw.githubusercontent.com/openai/codex/cc7a62e19cb4703495e3a6d01e1af076e8223833/codex-rs/apply-patch/src/streaming_parser.rs"><code>StreamingPatchParser::finish</code></a> returns whatever hunks it collected, so a bare <code>*** Begin Patch</code> / <code>*** End Patch</code> pair parses to zero hunks. The empty action is stopped one layer down — <code>assess_patch_safety()</code> rejects it with the reason <code>empty patch</code> before any path check runs.</li>
<li><strong>The markers are literal strings with a trailing space.</strong> <code>*** Add File: </code>, <code>*** Update File: </code> and <code>*** Move to: </code> are string constants in the same file; drop the space after the colon and the line stops matching the marker.</li>
</ol>
<p>Two markers are easy to miss because third-party guides rarely quote them. <code>*** Environment ID: &lt;id&gt;</code> attaches an optional identifier to the parsed argument struct, and <code>*** End of File</code> marks a change anchored at the end of the file — the <code>eof_line?</code> slot at the tail of the <code>change</code> rule. The parser stores those values; it does not translate them into a diff on disk.</p>
<h3 id="the-two-errors-the-parser-returns-and-why-they-are-exact-strings">The two errors the parser returns, and why they are exact strings</h3>
<p>Parsing does not check applicability. The module comment is explicit that the parser only validates shape — &ldquo;It does not attempt to actually check that the patch can be applied to the filesystem&rdquo; — so a syntactically valid payload can still fail when the change is applied. Application lives in the sibling modules (<code>file_update</code>, <code>seek_sequence</code>) re-exported from <a href="https://raw.githubusercontent.com/openai/codex/cc7a62e19cb4703495e3a6d01e1af076e8223833/codex-rs/apply-patch/src/lib.rs"><code>codex-rs/apply-patch/src/lib.rs</code></a>.</p>
<p>Boundary checking in strict mode is a comparison against the two marker constants, after trimming whitespace on the first and last lines. There are exactly two failure messages, and they are asserted in the module&rsquo;s own tests:</p>
<ul>
<li><code>The first line of the patch must be '*** Begin Patch'</code></li>
<li><code>The last line of the patch must be '*** End Patch'</code></li>
</ul>
<p>Because the comparison happens on trimmed lines, a trailing space after <code>*** Begin Patch</code> still parses in strict mode — one of the module&rsquo;s tests relies on that. The module comment describes the parser as &ldquo;a little more lenient than the explicit spec&rdquo;. Lenient mode adds a second acceptance path: a payload wrapped in a <code>&lt;&lt;'EOF'</code> … <code>EOF</code> heredoc is unwrapped first, then the inner lines face the same strict boundary check. That is why a transcript showing <code>apply_patch &lt;&lt;'EOF'</code> followed by a patch body is the same tool with a shell wrapper, not a different format.</p>
<h2 id="where-the-approval-decision-happens">Where the Approval Decision Happens</h2>
<p>Approval is not a property of the patch text. It is computed from the patch&rsquo;s target paths plus the session&rsquo;s approval policy and permission profile, in <code>assess_patch_safety()</code> in <a href="https://raw.githubusercontent.com/openai/codex/cc7a62e19cb4703495e3a6d01e1af076e8223833/codex-rs/core/src/safety.rs"><code>codex-rs/core/src/safety.rs</code></a>. The function is short enough to state as a decision table:</p>
<table>
	<thead>
			<tr>
					<th>Condition, evaluated in order</th>
					<th>Result</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>The patch has no file changes</td>
					<td><code>Reject</code> with reason <code>empty patch</code></td>
			</tr>
			<tr>
					<td>Approval policy is <code>UnlessTrusted</code></td>
					<td><code>AskUser</code>, before any path check</td>
			</tr>
			<tr>
					<td>Every target path is writable, and the permission profile is disabled/external or a platform sandbox is available</td>
					<td><code>AutoApprove</code></td>
			</tr>
			<tr>
					<td>Paths are not all writable, and the policy is <code>Never</code> (or <code>Granular</code> with sandbox approval off)</td>
					<td><code>Reject</code></td>
			</tr>
			<tr>
					<td>Anything else</td>
					<td><code>AskUser</code></td>
			</tr>
	</tbody>
</table>
<p>The two rejection strings are fixed text: <code>writing outside of the project; rejected by user approval settings</code>, and <code>writing is blocked by read-only sandbox; rejected by user approval settings</code> for a managed profile with no writable roots. Note what the third row is doing: <code>AutoApprove</code> skips the prompt, and it does so for two different reasons. The condition is a conjunction — the patch is constrained to writable paths, and then either a platform sandbox is available or the permission profile is <code>Disabled</code> or <code>External</code>. In the sandbox branch the code comment explains the reasoning: a path inside the writable set can still be a hard link to something outside it, so the patch is assumed to run in a sandbox. The comment&rsquo;s next line names the other branch outright — &ldquo;Disabled and External profiles intentionally do not apply an outer sandbox&rdquo; — so under those two profiles a patch can be auto-approved with no outer sandbox at all. (The path check itself short-circuits to true when the configured policy grants full-disk write access, since no per-path writable-root test can constrain that any further.)</p>
<p><a href="https://raw.githubusercontent.com/openai/codex/cc7a62e19cb4703495e3a6d01e1af076e8223833/codex-rs/core/src/apply_patch.rs"><code>codex-rs/core/src/apply_patch.rs</code></a> then maps those three outcomes onto the tool runtime:</p>
<ul>
<li><code>AutoApprove</code> becomes <code>ExecApprovalRequirement::Skip { bypass_sandbox: false }</code> — the approval prompt is skipped and the patch is not flagged to bypass the sandbox. Whether an outer sandbox exists at all is decided by the permission profile, per the <code>Disabled</code>/<code>External</code> branch above.</li>
<li><code>AskUser</code> becomes <code>ExecApprovalRequirement::NeedsApproval</code>, delegated to the tool runtime &ldquo;including cached approvals&rdquo;, so an earlier yes to the same shape of change can carry forward within the session.</li>
<li><code>Reject</code> becomes a <code>FunctionCallError::RespondToModel</code> whose message is <code>patch rejected: &lt;reason&gt;</code>.</li>
</ul>
<p>That last mapping has a practical consequence: a rejection is returned to the model as an error it can read, not a hard session failure. A model that reads <code>patch rejected: writing outside of the project</code> may respond by proposing a different path — which is a retry you did not ask for, not a crash.</p>
<h3 id="which-mode-name-maps-to-which-policy">Which mode name maps to which policy</h3>
<p>The policy type in the safety check is <code>AskForApproval</code>, and the configuration reference documents three values for <code>approval_policy</code>: <code>on-request</code>, <code>never</code>, and the struct form <code>{ granular = { ... } }</code>. The enum in <code>safety.rs</code> carries two further variants, <code>UnlessTrusted</code> and <code>Granular</code>, that the documented values do not reach through the <code>-a</code> flag; treat any other mode name you see in a tutorial as a pre-rename convention. What the source does establish is the boundary case worth remembering: <code>UnlessTrusted</code> asks before the writable-path check runs at all, while <code>Never</code> and <code>Granular</code> fall through to it — and <code>Never</code> rejects rather than asking when the paths fail the check.</p>
<h2 id="what-the-model-is-told-about-apply_patch">What the Model Is Told About apply_patch</h2>
<p>The tool description is not a mandate. The instruction the model reads is per-model prompt text, and at the pinned commit the <code>gpt-5.2-codex</code> prompt file (<a href="https://raw.githubusercontent.com/openai/codex/cc7a62e19cb4703495e3a6d01e1af076e8223833/codex-rs/core/gpt-5.2-codex_prompt.md"><code>codex-rs/core/gpt-5.2-codex_prompt.md</code></a>) says:</p>
<blockquote>
<p>Try to use apply_patch for single file edits, but it is fine to explore other options to make the edit if it does not work well. Do not use apply_patch for changes that are auto-generated (i.e. generating package.json or running a lint or format command like gofmt) or when scripting is more efficient (such as search and replacing a string across a codebase).</p>
</blockquote>
<p>Two carve-outs follow from that sentence, and they change how you should review a Codex session. First, a formatter, a codegen step or a <code>gofmt</code> run will not arrive as a patch — it arrives as a shell command. Second, a mechanical rename across many files is explicitly pointed at scripting rather than at the patch tool. So a review process that only inspects <code>*** Begin Patch</code> blocks is inspecting a subset of the edits, not all of them.</p>
<p>The prompt is also revision-specific. Prompt files are per-model artefacts inside the repository, and they change between releases; the quote above records the <code>gpt-5.2-codex</code> file at commit <code>cc7a62e19cb4703495e3a6d01e1af076e8223833</code> on 18 November 2026, not a stable product contract across models.</p>
<h2 id="reading-a-payload-line-by-line">Reading a Payload Line by Line</h2>
<p>The installed CLI in this environment is <code>codex-cli 0.170.0</code>, and it exposes no public <code>apply-patch</code> subcommand — the internal entry point is gated behind the <code>--codex-run-as-apply-patch</code> argv flag defined in the crate, and invoking it against a piped fixture exits with <code>stdin is not a terminal</code>. So the payload below is a hand-built illustration of the grammar, not a recorded run. Read it against the rules above:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-text" data-lang="text"><span style="display:flex;"><span>*** Begin Patch
</span></span><span style="display:flex;"><span>*** Update File: src/config.py
</span></span><span style="display:flex;"><span>@@ def load_config(path):
</span></span><span style="display:flex;"><span> import json
</span></span><span style="display:flex;"><span>-    return json.loads(path.read_text())
</span></span><span style="display:flex;"><span>+    return json.loads(Path(path).read_text())
</span></span><span style="display:flex;"><span>*** End Patch
</span></span></code></pre></div><p>What the parser does with each line: <code>*** Begin Patch</code> opens the envelope; <code>*** Update File: src/config.py</code> names the single target file; <code>@@ def load_config(path):</code> is a context anchor whose tail is a free-form hint, used to help locate the region in the file; the line beginning with one space is unchanged context; <code>-</code> is removed text; <code>+</code> is added text; <code>*** End Patch</code> closes the envelope. There is no line numbering and no hunk header count — the <code>@@</code> line carries a label, not a <code>-1,7 +1,7</code> range.</p>
<p>This is also where a common misreading dies. Because the payload looks diff-like, people assume the numbers or the anchor line are authoritative. In this grammar the anchor is a hint and the surrounding context lines are the actual match material, which is why an update hunk that renames an import can still land on the wrong function if the context you supplied is generic.</p>
<h2 id="practical-consequences-for-reviewers-and-ci">Practical Consequences for Reviewers and CI</h2>
<p>If you audit agent sessions, the envelope gives you cheap, reliable markers:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># Count file-edit tool calls in a captured session log</span>
</span></span><span style="display:flex;"><span>grep -c <span style="color:#e6db74">&#39;^\*\*\* Begin Patch&#39;</span> session.log
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># List every path a turn tried to modify, without parsing diffs</span>
</span></span><span style="display:flex;"><span>grep -E <span style="color:#e6db74">&#39;^\*\*\* (Add|Update|Delete) File: |^\*\*\* Move to: &#39;</span> session.log
</span></span></code></pre></div><p>Four operational points follow from the source read above:</p>
<ul>
<li><strong>Audit by resolved path, not by diff shape.</strong> The tool names files explicitly, so the question &ldquo;what could this turn write?&rdquo; is answerable from the markers. For a <code>*** Move to: </code> hunk, both the source and destination paths are checked against the writable set, which is why renames deserve a second look rather than being treated as in-place edits.</li>
<li><strong>Do not treat an absent prompt as an absent sandbox — or as a present one.</strong> <code>AutoApprove</code> is taken for two reasons: a sandbox will run the patch, or the permission profile is <code>Disabled</code>/<code>External</code>, which by design applies no outer sandbox. The skipped thing is the prompt. In the second case the hard-link caveat the code comment raises has no outer sandbox behind it, so the path check that approved the patch is the only gate the write passed. If you changed the profile to get a patch through, you also changed the meaning of the approval you saw.</li>
<li><strong>Separate syntactic rejection from policy rejection.</strong> <code>The first line of the patch must be '*** Begin Patch'</code> and <code>empty patch</code> are model-side formatting failures. <code>patch rejected: ...</code> is a policy outcome. They belong in different alert channels, because only the second one is a permissions question.</li>
<li><strong>Read the prompt file for the model you actually run.</strong> The carve-outs that push edits into shell commands live in the per-model prompt, not in the CLI, so the same session can behave differently after a model switch without any configuration change on your side.</li>
</ul>
<p>None of the above requires running the tool. It requires reading one grammar comment, two boundary tests, one safety function and one prompt file at a known commit — which is also the honest limit of this section: the mechanics are quoted from source, and no patch was executed in this environment.</p>
<hr>
<h2 id="agentsmd--how-to-configure-codex-for-your-project">AGENTS.md — How to Configure Codex for Your Project</h2>
<p>AGENTS.md is an open standard file that tells AI coding agents — including Codex CLI, Claude Code, and compatible third-party tools — how to work in your specific project. It lives at the root of your repository and is automatically read by Codex on every invocation, functioning like a persistent system prompt scoped to your codebase. An effective AGENTS.md eliminates the need to repeat project-specific context in every command: your test commands, coding conventions, forbidden patterns, branch naming rules, and environment setup instructions belong here. In April 2026, AGENTS.md is increasingly treated as a first-class project artifact, often committed alongside <code>package.json</code> and <code>.eslintrc</code>. Think of it as documentation that your AI teammates read before touching a single file — the more specific and opinionated, the better the output quality.</p>
<h3 id="example-agentsmd">Example AGENTS.md</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-markdown" data-lang="markdown"><span style="display:flex;"><span># Project: Payments API
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">## Setup
</span></span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Node.js 20 LTS
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Run <span style="color:#e6db74">`npm install`</span> before tasks
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Copy <span style="color:#e6db74">`.env.example`</span> to <span style="color:#e6db74">`.env`</span> and fill values for local DB
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">## Testing
</span></span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Unit tests: <span style="color:#e6db74">`npm test`</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Integration tests: <span style="color:#e6db74">`npm run test:int`</span> (requires Docker)
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Always run unit tests before committing
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">## Conventions
</span></span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> TypeScript strict mode — no <span style="color:#e6db74">`any`</span> types
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Use named exports only — no default exports
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Prefer <span style="color:#e6db74">`zod`</span> for runtime validation, not manual type guards
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">## Forbidden
</span></span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Never commit secrets or API keys
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Do not modify <span style="color:#e6db74">`src/migrations/`</span> directly — use the CLI
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Do not add <span style="color:#e6db74">`console.log`</span> to production code
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">## Branch Naming
</span></span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Features: <span style="color:#e6db74">`feat/TICKET-description`</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">-</span> Fixes: <span style="color:#e6db74">`fix/TICKET-description`</span>
</span></span></code></pre></div><h3 id="what-to-include-vs-omit">What to Include vs. Omit</h3>
<table>
	<thead>
			<tr>
					<th>Include</th>
					<th>Omit</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Test commands and flags</td>
					<td>Obvious conventions (use TypeScript → TS project)</td>
			</tr>
			<tr>
					<td>Forbidden directories/patterns</td>
					<td>Generic best practices</td>
			</tr>
			<tr>
					<td>Environment setup steps</td>
					<td>Framework documentation links</td>
			</tr>
			<tr>
					<td>Project-specific naming conventions</td>
					<td>Personal preferences</td>
			</tr>
	</tbody>
</table>
<hr>
<h2 id="mcp-integration-connecting-third-party-tools-to-codex">MCP Integration: Connecting Third-Party Tools to Codex</h2>
<p>Model Context Protocol (MCP) integration transforms Codex CLI from a file editor into a full-stack development assistant with access to live external systems. MCP is an open standard that lets AI agents call external tools — browsers, databases, APIs, design tools — via a consistent interface. With MCP configured, Codex can read the documentation of a library it needs to use, query your database schema before writing a migration, or read design specs before scaffolding a UI component. The integration requires adding server definitions to <code>~/.codex/config.toml</code> — Codex handles the protocol negotiation automatically. Each MCP server runs as a subprocess managed by Codex, so you get isolated, per-session tool access without any persistent background service. Credentials are passed via environment variables rather than stored in config files, keeping secrets out of your repository.</p>
<h3 id="configuring-mcp-servers">Configuring MCP Servers</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-toml" data-lang="toml"><span style="display:flex;"><span><span style="color:#75715e"># ~/.codex/config.toml</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[<span style="color:#a6e22e">mcp_servers</span>.<span style="color:#a6e22e">postgres</span>]
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">command</span> = <span style="color:#e6db74">&#34;npx&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">args</span> = [<span style="color:#e6db74">&#34;-y&#34;</span>, <span style="color:#e6db74">&#34;@modelcontextprotocol/server-postgres&#34;</span>]
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">env</span> = { <span style="color:#a6e22e">DATABASE_URL</span> = <span style="color:#e6db74">&#34;${DATABASE_URL}&#34;</span> }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[<span style="color:#a6e22e">mcp_servers</span>.<span style="color:#a6e22e">github</span>]
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">command</span> = <span style="color:#e6db74">&#34;npx&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">args</span> = [<span style="color:#e6db74">&#34;-y&#34;</span>, <span style="color:#e6db74">&#34;@modelcontextprotocol/server-github&#34;</span>]
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">env</span> = { <span style="color:#a6e22e">GITHUB_TOKEN</span> = <span style="color:#e6db74">&#34;${GITHUB_TOKEN}&#34;</span> }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[<span style="color:#a6e22e">mcp_servers</span>.<span style="color:#a6e22e">figma</span>]
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">command</span> = <span style="color:#e6db74">&#34;npx&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">args</span> = [<span style="color:#e6db74">&#34;-y&#34;</span>, <span style="color:#e6db74">&#34;@modelcontextprotocol/server-figma&#34;</span>]
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">env</span> = { <span style="color:#a6e22e">FIGMA_TOKEN</span> = <span style="color:#e6db74">&#34;${FIGMA_TOKEN}&#34;</span> }
</span></span></code></pre></div><h3 id="practical-mcp-use-cases">Practical MCP Use Cases</h3>
<p><strong>Database-aware migrations:</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>codex <span style="color:#e6db74">&#34;analyze the users table schema and write a safe migration to add an email_verified column&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Codex queries the live DB schema via MCP before writing migration code</span>
</span></span></code></pre></div><p><strong>Design-to-code:</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>codex <span style="color:#e6db74">&#34;implement the LoginForm component from the Figma frame at [frame-url]&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Codex reads actual design specs, not a screenshot</span>
</span></span></code></pre></div><p><strong>GitHub-integrated workflow:</strong></p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>codex <span style="color:#e6db74">&#34;open a PR for the feature branch, link it to issue #142&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Codex uses GitHub MCP to create the PR directly</span>
</span></span></code></pre></div><hr>
<h2 id="advanced-configuration-sandbox-approval-policies-and-configtoml">Advanced Configuration: Sandbox, Approval Policies, and config.toml</h2>
<p>Advanced Codex CLI configuration via <code>~/.codex/config.toml</code> gives you fine-grained control over sandbox behavior, filesystem access policies, network restrictions, and per-project overrides. The sandbox system is one of Codex CLI&rsquo;s most important safety features — it limits what the agent can touch beyond the paths you explicitly whitelist, preventing runaway automation from modifying system files or making unintended network calls. The sandbox is enforced at the process level, and the same policy objects feed the patch-approval check described in the mechanics section above. For teams deploying Codex in CI, sandbox configuration is not optional — it is the primary safeguard ensuring agents operate within the intended blast radius. You can also use <code>CODEX_HOME</code> to maintain project-specific config profiles without modifying your global settings. Config keys are hot-reloaded between Codex sessions, so changes take effect immediately without reinstalling the CLI. Granular approval is expressed on <code>approval_policy</code> itself — <code>approval_policy = { granular = { ... } }</code> with per-category booleans (<code>sandbox_approval</code>, <code>rules</code>, <code>mcp_elicitations</code>, <code>request_permissions</code>, <code>skill_approval</code>) — not as a separate <code>[approval_policies]</code> table.</p>
<h3 id="configtoml-reference-keys-the-upstream-reference-documents">config.toml Reference (keys the upstream reference documents)</h3>
<p>The keys below are the ones the <a href="https://developers.openai.com/codex/config-file/config-reference">configuration reference</a> documents, read on 18 November 2026. An earlier revision of this page printed a <code>[defaults]</code> table with <code>context_window</code>, a <code>[sandbox]</code> table with <code>allow_read</code> / <code>allow_write</code> / <code>allow_network</code> / <code>deny_write</code>, and an <code>[approval_policies]</code> table with per-operation modes. None of those keys exists in that reference, so they are removed rather than restated.</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-toml" data-lang="toml"><span style="display:flex;"><span><span style="color:#75715e"># ~/.codex/config.toml</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">model</span> = <span style="color:#e6db74">&#34;gpt-5.2-codex&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">approval_policy</span> = <span style="color:#e6db74">&#34;on-request&#34;</span>          <span style="color:#75715e"># on-request | never | { granular = { ... } }</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">model_context_window</span> = <span style="color:#ae81ff">128000</span>           <span style="color:#75715e"># optional per-model override</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Sandbox policy for model-generated shell commands</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">sandbox_mode</span> = <span style="color:#e6db74">&#34;workspace-write&#34;</span>        <span style="color:#75715e"># read-only | workspace-write | danger-full-access</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>[<span style="color:#a6e22e">sandbox_workspace_write</span>]
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Extra writable roots beyond the workspace directory</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">writable_roots</span> = [<span style="color:#e6db74">&#34;/home/me/projects/shared&#34;</span>]
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Keep network access off inside the sandbox</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">network_access</span> = <span style="color:#66d9ef">false</span>
</span></span></code></pre></div><h3 id="per-project-config-via-codex_home">Per-Project Config via CODEX_HOME</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># Use a project-specific config profile</span>
</span></span><span style="display:flex;"><span>export CODEX_HOME<span style="color:#f92672">=</span>~/projects/payments-api/.codex
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># codex now reads ~/.../payments-api/.codex/config.toml</span>
</span></span><span style="display:flex;"><span>codex <span style="color:#e6db74">&#34;add rate limiting to the auth endpoints&#34;</span>
</span></span></code></pre></div><hr>
<h2 id="multi-agent-workflows-with-the-openai-agents-sdk">Multi-Agent Workflows with the OpenAI Agents SDK</h2>
<p>Orchestrating Codex CLI as part of a multi-agent system via the OpenAI Agents SDK enables production-scale automation workflows that go far beyond what a single interactive session can accomplish. In a multi-agent setup, a coordinator agent breaks down a high-level goal (e.g., &ldquo;ship this feature&rdquo;) into subtasks, spawns specialized Codex agents for each, collects outputs, runs validation, and gates the final commit behind automated checks. This architecture is increasingly common for CI/CD pipelines where agents handle the full loop: branch → implement → test → review → PR. The Agents SDK provides typed Python and TypeScript interfaces for spawning, monitoring, and composing agents, with support for handoffs, guardrails and tracing. The example below is a sketch of the pattern, not a runnable integration: the <code>agents.CodexTool</code> and <code>Pipeline</code> surface shown here is illustrative, and the current SDK reference should be checked before wiring it into a pipeline.</p>
<h3 id="example-automated-bug-fix-pipeline">Example: Automated Bug Fix Pipeline</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-python" data-lang="python"><span style="display:flex;"><span><span style="color:#f92672">from</span> openai <span style="color:#f92672">import</span> agents
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>coordinator <span style="color:#f92672">=</span> agents<span style="color:#f92672">.</span>Agent(
</span></span><span style="display:flex;"><span>    name<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;BugFixCoordinator&#34;</span>,
</span></span><span style="display:flex;"><span>    instructions<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;Break down bug reports into implementation tasks and delegate to Codex agents&#34;</span>,
</span></span><span style="display:flex;"><span>    tools<span style="color:#f92672">=</span>[agents<span style="color:#f92672">.</span>CodexTool(approval_mode<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;full-auto&#34;</span>, sandbox<span style="color:#f92672">=</span><span style="color:#66d9ef">True</span>)]
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>pipeline <span style="color:#f92672">=</span> agents<span style="color:#f92672">.</span>Pipeline([
</span></span><span style="display:flex;"><span>    agents<span style="color:#f92672">.</span>Step(<span style="color:#e6db74">&#34;triage&#34;</span>, coordinator),
</span></span><span style="display:flex;"><span>    agents<span style="color:#f92672">.</span>Step(<span style="color:#e6db74">&#34;implement&#34;</span>, agents<span style="color:#f92672">.</span>CodexAgent(task<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;fix the bug described in the issue&#34;</span>)),
</span></span><span style="display:flex;"><span>    agents<span style="color:#f92672">.</span>Step(<span style="color:#e6db74">&#34;test&#34;</span>, agents<span style="color:#f92672">.</span>CodexAgent(task<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;run tests and fix any failures&#34;</span>)),
</span></span><span style="display:flex;"><span>    agents<span style="color:#f92672">.</span>Step(<span style="color:#e6db74">&#34;review&#34;</span>, agents<span style="color:#f92672">.</span>CodeReviewAgent()),
</span></span><span style="display:flex;"><span>    agents<span style="color:#f92672">.</span>Step(<span style="color:#e6db74">&#34;pr&#34;</span>, agents<span style="color:#f92672">.</span>GitHubAgent(action<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;create_pr&#34;</span>))
</span></span><span style="display:flex;"><span>])
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>result <span style="color:#f92672">=</span> pipeline<span style="color:#f92672">.</span>run(input<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;Fix bug #442: login form resets on validation error&#34;</span>)
</span></span></code></pre></div><h3 id="slash-commands-in-interactive-mode">Slash Commands in Interactive Mode</h3>
<p>When running Codex interactively, slash commands switch context without leaving the session. The enum in <a href="https://raw.githubusercontent.com/openai/codex/cc7a62e19cb4703495e3a6d01e1af076e8223833/codex-rs/tui/src/slash_command.rs"><code>codex-rs/tui/src/slash_command.rs</code></a> at the pinned commit declares 66 variants; the table below is a selection of eleven commonly used ones, not the enum itself. Every description quoted in it is the string that file&rsquo;s <code>description()</code> method returns for that variant:</p>
<table>
	<thead>
			<tr>
					<th>Command</th>
					<th>Action (source string)</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>/model</code></td>
					<td>&ldquo;choose what model and reasoning effort to use&rdquo; — opens a picker, takes no model argument</td>
			</tr>
			<tr>
					<td><code>/permissions</code></td>
					<td>&ldquo;choose what Codex is allowed to do&rdquo;</td>
			</tr>
			<tr>
					<td><code>/diff</code></td>
					<td>&ldquo;show git diff (including untracked files)&rdquo;</td>
			</tr>
			<tr>
					<td><code>/status</code></td>
					<td>&ldquo;show current session configuration and token usage&rdquo;</td>
			</tr>
			<tr>
					<td><code>/plan</code></td>
					<td>&ldquo;switch to Plan mode&rdquo;</td>
			</tr>
			<tr>
					<td><code>/review</code></td>
					<td>&ldquo;review my current changes and find issues&rdquo;</td>
			</tr>
			<tr>
					<td><code>/new</code></td>
					<td>&ldquo;start a new chat during a conversation&rdquo;</td>
			</tr>
			<tr>
					<td><code>/compact</code></td>
					<td>&ldquo;summarize conversation to prevent hitting the context limit&rdquo;</td>
			</tr>
			<tr>
					<td><code>/init</code></td>
					<td>&ldquo;create an AGENTS.md file with instructions for Codex&rdquo;</td>
			</tr>
			<tr>
					<td><code>/mcp</code></td>
					<td><code>list MCP tools; /mcp verbose or /mcp login &lt;name&gt; [--environment &lt;id&gt;]</code></td>
			</tr>
			<tr>
					<td><code>/quit</code></td>
					<td>&ldquo;exit Codex&rdquo;</td>
			</tr>
	</tbody>
</table>
<p>An earlier revision of this page listed <code>/approval</code>, <code>/context</code>, <code>/commit</code> and <code>/reset</code>. None of those four names exists among the enum&rsquo;s 66 variants, so they are removed instead of re-caveated.</p>
<hr>
<h2 id="codex-cli-vs-claude-code-how-to-use-both-effectively">Codex CLI vs Claude Code: How to Use Both Effectively</h2>
<p>Codex CLI and Claude Code are complementary tools that solve different parts of the AI-assisted development workflow, not direct competitors you must choose between. Codex CLI excels at autonomous, task-scoped operations: &ldquo;implement this feature,&rdquo; &ldquo;fix these failing tests,&rdquo; &ldquo;migrate this API to the new schema.&rdquo; It operates in a fire-and-review model where you define a goal and let the agent work. Claude Code, by contrast, excels at interactive, exploratory sessions: understanding an unfamiliar codebase, debugging complex multi-service interactions, or pair-programming through a nuanced architectural decision. The key practical difference is conversational depth versus autonomous execution depth: one tool is built to hold a long investigation open, the other to finish a scoped task. They overlap on project instructions — Codex reads <code>AGENTS.md</code>, Claude Code reads <code>CLAUDE.md</code> — so keeping conventions in a file each tool reads avoids maintaining two setups.</p>
<h3 id="side-by-side-comparison">Side-by-Side Comparison</h3>
<table>
	<thead>
			<tr>
					<th>Dimension</th>
					<th>Codex CLI</th>
					<th>Claude Code</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Primary mode</td>
					<td>Autonomous execution</td>
					<td>Interactive conversation</td>
			</tr>
			<tr>
					<td>Ideal for</td>
					<td>Feature implementation, test fixes, migrations</td>
					<td>Codebase exploration, debugging, architecture</td>
			</tr>
			<tr>
					<td>Context window</td>
					<td>Set by the selected model</td>
					<td>Set by the selected model</td>
			</tr>
			<tr>
					<td>Approval model</td>
					<td><code>-a</code> policy + <code>-s</code> sandbox flags</td>
					<td>Explicit approval per tool call</td>
			</tr>
			<tr>
					<td>MCP support</td>
					<td>Yes (config.toml)</td>
					<td>Yes (settings.json)</td>
			</tr>
			<tr>
					<td>CI/CD integration</td>
					<td>Native via <code>--approve-for-me</code> + sandbox</td>
					<td>Via headless mode</td>
			</tr>
			<tr>
					<td>AGENTS.md support</td>
					<td>Yes (primary format)</td>
					<td>Yes (CLAUDE.md alias)</td>
			</tr>
	</tbody>
</table>
<h3 id="recommended-split-workflow">Recommended Split Workflow</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># Phase 1: Understand with Claude Code</span>
</span></span><span style="display:flex;"><span>claude <span style="color:#e6db74">&#34;explain how the payment processing pipeline works and identify potential race conditions&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Phase 2: Implement with Codex CLI</span>
</span></span><span style="display:flex;"><span>codex -a on-request -s workspace-write <span style="color:#e6db74">&#34;add distributed locking to prevent the race condition in payment processing&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Phase 3: Review with Claude Code</span>
</span></span><span style="display:flex;"><span>claude <span style="color:#e6db74">&#34;review the changes in the last commit — do they fully address the race condition?&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># Phase 4: Ship with Codex CLI</span>
</span></span><span style="display:flex;"><span>codex -s workspace-write --approve-for-me <span style="color:#e6db74">&#34;run integration tests, fix any failures, open a PR&#34;</span>
</span></span></code></pre></div><hr>
<h2 id="real-developer-workflow-branch--code--review--commit">Real Developer Workflow: Branch → Code → Review → Commit</h2>
<p>A complete real-world Codex CLI workflow starts with a feature branch and ends with a reviewed, tested commit — the agent handles every step between those bookends. This workflow pattern takes advantage of Codex CLI&rsquo;s AGENTS.md awareness (no repeated context), its MCP integration (live database schema, GitHub API), and its two-knob approval setup (a read-only sandbox for sensitive changes, <code>--approve-for-me</code> for mechanical tasks). The key practice that experienced Codex users consistently recommend: always start a new git branch before invoking Codex with a writable sandbox. This ensures that every agent-made change is isolated, reviewable as a diff, and reversible with a single <code>git checkout main</code>. Teams that skip this step report harder-to-audit histories; teams that adopt it report higher confidence in merging agent-generated code.</p>
<h3 id="step-by-step-feature-implementation">Step-by-Step: Feature Implementation</h3>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span><span style="color:#75715e"># 1. Start from a clean branch</span>
</span></span><span style="display:flex;"><span>git checkout -b feat/PROJ-442-add-2fa
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 2. Run Codex with a writable sandbox and on-request approvals</span>
</span></span><span style="display:flex;"><span>codex -a on-request -s workspace-write <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span>  <span style="color:#e6db74">&#34;implement TOTP-based 2FA for the auth module. Use speakeasy library.
</span></span></span><span style="display:flex;"><span><span style="color:#e6db74">   Add unit tests. Follow the conventions in AGENTS.md.&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 3. Review the diff</span>
</span></span><span style="display:flex;"><span>git diff
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 4. Run tests manually or let Codex do it</span>
</span></span><span style="display:flex;"><span>codex <span style="color:#e6db74">&#34;run tests and summarize results&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 5. Approve the changes and commit</span>
</span></span><span style="display:flex;"><span>git add -p   <span style="color:#75715e"># Review each hunk</span>
</span></span><span style="display:flex;"><span>git commit -m <span style="color:#e6db74">&#34;feat(auth): add TOTP-based 2FA via speakeasy&#34;</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 6. Push and open PR (optionally via Codex + GitHub MCP)</span>
</span></span><span style="display:flex;"><span>codex -s workspace-write --approve-for-me <span style="color:#e6db74">&#34;push this branch and open a PR linking to PROJ-442&#34;</span>
</span></span></code></pre></div><h2 id="faq">FAQ</h2>
<p><strong>Q: Do I need an OpenAI API key to use Codex CLI?</strong></p>
<p>No — you can authenticate via your existing ChatGPT account using <code>codex login</code>, which opens a browser OAuth flow. An API key is required only for CI/CD pipelines, team environments, or contexts where a browser flow is unavailable. Codex is included in the Plus, Pro, Business, Edu and Enterprise ChatGPT plans; API key usage is billed per token.</p>
<p><strong>Q: How does Codex CLI handle large codebases with millions of lines?</strong></p>
<p>Codex CLI uses intelligent context selection rather than loading the entire codebase into the context window. It reads project structure, locates relevant files via semantic search and file tree analysis, and loads only the files pertinent to the current task — typically staying well inside the selected model&rsquo;s context window. You can supplement this with <code>AGENTS.md</code> to explicitly point Codex at critical files or directories.</p>
<p><strong>Q: Is it safe to run Codex with approvals turned off on production code?</strong></p>
<p>Only with sandbox restrictions configured. With approvals disabled (<code>-a never</code>) and a writable sandbox, the agent can modify any file the process can reach. The recommended practice is <code>sandbox_mode = &quot;workspace-write&quot;</code> with a <code>[sandbox_workspace_write]</code> <code>writable_roots</code> list in <code>config.toml</code> to bound where writes can land, and always run auto-approved sessions on a git branch — never directly on <code>main</code>. For CI/CD use, keep the workspace-write sandbox and add container isolation for defence in depth.</p>
<p><strong>Q: Can Codex CLI work offline?</strong></p>
<p>No. The binary runs locally, but model inference happens on OpenAI&rsquo;s servers, so the CLI needs network access to the inference API. Nothing in this guide covers an air-gapped deployment path.</p>
<p><strong>Q: How does AGENTS.md differ from a <code>.cursorrules</code> or <code>CLAUDE.md</code> file?</strong></p>
<p>AGENTS.md is the open standard proposed by OpenAI to unify AI agent configuration across tools. CLAUDE.md (used by Claude Code) and <code>.cursorrules</code> (used by Cursor) are tool-specific variants with similar purposes. Codex CLI reads <code>AGENTS.md</code> natively; Claude Code reads both <code>CLAUDE.md</code> and <code>AGENTS.md</code>. Writing your conventions in <code>AGENTS.md</code> maximizes compatibility across AI coding tools without duplicating configuration.</p>
]]></content:encoded></item></channel></rss>