Claude Code で Codex CLI の review コマンドを呼び出す方法

に公開

Claude Code をメインで使っていてもコードレビューは Codex の review を使いたい。ただ Codex CLI を立ち上げてその中で review コマンドを実行してその結果をコピーして Claude Code で貼り付けて…というのは面倒なので、Claude Code から直接 Codex CLI の review コマンドを叩いて結果を得られないか調べました。

ちなみに、コンテキストに余計なものを含めたくないので、Codex からの出力は最終結果だけが欲しいという気持ちもありました。

前提:review コマンド以外の場合

Codex CLI を普通のプロンプトで呼び出すだけなら以下で OK です。

codex exec "your prompt" 2>/dev/null

本題:review コマンドの場合

review コマンドを呼び出したい場合、 codex exec review または codex review で可能(どちらもレビュー自体の挙動は同じ)なのですが、通常の codex exec と違って 2>/dev/null で最終結果が出力されないという問題があります(バグというよりは仕様のような実装でした)。

そのため、 codex exec--json オプションで全ての結果を JSON 形式で得た後 jq コマンドで最終結果だけを取り出すという方法が考えられます。なお、 codex review の方は --json オプションがないためこの用途では codex exec review を用いる必要があります。

codex exec review --uncommitted --json 2>/dev/null | jq -rs '[.[] | select(.item.type == "agent_message")] | last | .item.text'

なお、 codex exec reviewcodex review では以下のいずれかのオプションが必要なので用途によって適切なものを指定しましょう。

--uncommitted
    Review staged, unstaged, and untracked changes

--base <BRANCH>
    Review changes against the given base branch

--commit <SHA>
    Review the changes introduced by a commit

おまけ:Skills にした例

Claude Code で使う時は Skills や Slash Commands にすると便利です。実装後にコードレビューする用の codex-code-review と、プランモードで計画したものをレビューする用の codex-plan-review を作って使っています。

codex-code-review

---
name: codex-code-review
description: Review code changes using Codex. Use when user asks to review code, check code quality, or wants feedback before committing or creating a pull request.
allowed-tools: Bash(codex:*), Bash(jq:*), Bash(git:*)
---

Determine the appropriate review mode based on context:

1. **If there are uncommitted changes** (staged, unstaged, or untracked files): use `--uncommitted`
2. **If there are no uncommitted changes but the current branch differs from the main branch**: use `--base <main-branch>`

First, check git status to determine the appropriate mode:

```bash
git status --porcelain
```

Then run the appropriate codex review command:

```bash
# For uncommitted changes
codex exec review --uncommitted --json 2>/dev/null | jq -rs '[.[] | select(.item.type == "agent_message")] | last | .item.text'

# For base branch comparison (use main/master as appropriate)
codex exec review --base main --json 2>/dev/null | jq -rs '[.[] | select(.item.type == "agent_message")] | last | .item.text'
```

Based on the results, assess whether any changes need to be addressed. If unclear, ask the user using AskUserQuestion before making fixes.

If issues are found and fixed, re-run the review until no issues remain.

codex-plan-review

---
name: codex-plan-review
description: Review implementation plans using Codex. Use when user wants feedback on a plan before implementation.
allowed-tools: Bash(codex:*), Bash(cp:*), Bash(rm:*), Bash(ls:*), Bash(head:*)
---

Review the most recent plan file in `~/.claude/plans/` using Codex. Copy the plan into the repository temporarily so Codex can access it.

```bash
# Find and copy the most recent plan
ls -t ~/.claude/plans/*.md | head -1
cp <plan-file-path> .PLAN_REVIEW_TEMP.md

# Run Codex review
codex exec "Review the implementation plan in .PLAN_REVIEW_TEMP.md. Identify potential issues, risks, and areas for improvement." 2>/dev/null

# Clean up
rm -f .PLAN_REVIEW_TEMP.md
```

Based on the results, assess whether any changes need to be addressed. If unclear, ask the user using AskUserQuestion before making fixes.

If issues are found and fixed, re-run the review until no issues remain.

追記 (2025-01-16)

Claude Code 2.1.9 で plansDirectory の設定ができるようになり、デフォルトの ~/.claude/plans/ ではなくプロジェクト内にプランファイルを配置できるようになりました。これを設定していればプランファイルのコピーは不要です。

以下は "plansDirectory": "./plans" と設定した場合の例です。

---
name: codex-plan-review
description: Review implementation plans using Codex. Use when user wants feedback on a plan before implementation.
allowed-tools: Bash(codex:*), Bash(ls:*), Bash(head:*)
---

Review the most recent plan file in `./plans/` using Codex.

```bash
# Find the most recent plan
ls -t ./plans/*.md | head -1

# Run Codex review
codex exec "Review the implementation plan in <plan-file-path>. Identify potential issues, risks, and areas for improvement." 2>/dev/null
```

Based on the results, assess whether any changes need to be addressed. If unclear, ask the user using AskUserQuestion before making fixes.

If issues are found and fixed, re-run the review until no issues remain.

Discussion