Codex: Codex CLI スキル、サンドボックスと実例

最終更新:2026-08-31

CLI は Codex の最も柔軟な使用モードです。このレッスンでは CLI 環境でのスキル、サンドボックス設定、実践的な例を説明します。

📋 前提条件: Codex CLI の基本操作を理解していること

1. 学ぶ内容


2. CLI スキル管理

(1) 利用可能なスキルの一覧

BASH
codex --list-skills

(2) スキルの使用

BASH
# スキルを指定してタスクを実行
codex --skill code-review "Review src/auth.ts"

# オプションの組み合わせ
codex --skill security --sandbox readonly "Scan for security vulnerabilities"

(3) カスタムスキルの場所

BASH
# スキルディレクトリを指定
codex --skills-dir ./my-skills "Add login functionality"

(4) スキルファイルフォーマット

MARKDOWN
<!-- .codex/skills/api-endpoint.md -->
# API Endpoint Generator

## Role
FastAPI endpoint development expert

## Rules
- Use Pydantic models
- Include error handling
- Add Swagger documentation
- Write pytest tests

## Workflow
1. Define request/response models
2. Implement endpoint logic
3. Add error handling
4. Write tests
5. Ensure pytest passes

3. サンドボックスセキュリティの詳細

(1) サンドボックスの実装方法

方法 説明 セキュリティレベル
Docker コンテナ分離 最高
chroot ディレクトリ分離 中程度
プロセスレベル 権限制限 基本

(2) Docker サンドボックス設定

TOML
[sandbox]
type = "docker"
image = "codex-sandbox:latest"
memory = "2g"
cpus = 2
timeout = 600

# ボリュームのマウント
volumes = [
  "./src:/workspace/src",
  "./tests:/workspace/tests",
]

# 環境変数
env = { "NODE_ENV" = "test" }

(3) サンドボックスパーミッションマトリックス

操作 readonly workspace-write full-access
ファイルの読み取り
ワークスペースへの書き込み
システムへの書き込み
コマンドの実行 ⚠️ 確認
ネットワークアクセス

▶ 例1:Alice のセキュリティ設定

TOML
# Alice のサンドボックス設定
[sandbox]
type = "docker"
memory = "4g"
cpus = 4

# 許可する操作
allowed_commands = [
  "npm test",
  "npm run lint",
  "git status",
  "git diff",
  "tsc --noEmit",
]

# ブロックする操作
blocked_commands = [
  "rm -rf /",
  "sudo",
  "curl * | sh",
]

# リソース制限
max_file_size = "10MB"
max_execution_time = 300

4. 実践的な例のコレクション

(1) プロジェクト初期化

BASH
codex "Initialize Next.js 14 project: TypeScript + Tailwind + ESLint + Prettier, create standard directory structure"

(2) バグ修正

BASH
# エラーログから修正
cat error.log | codex --full-auto "Analyze error log, locate root cause, fix code"

# Issue から修正
codex "Fix Issue #42: Users not receiving verification email after registration"

(3) テスト生成

BASH
# すべてのソースファイルのテストを生成
codex --full-auto "Generate corresponding .test.ts for all .ts files under src/, using vitest"

# テストカバレッジを補充
codex "Check test coverage, supplement tests for files below 80%"

(4) コードリファクタリング

BASH
# フレームワーク移行
codex "Refactor all Class components to functional components + Hooks"

# API 移行
codex "Migrate REST API calls to tRPC, maintain functionality"

(5) ドキュメント生成

BASH
# API ドキュメント
codex "Generate OpenAPI/Swagger docs for all API endpoints"

# README
codex "Generate README.md based on project structure and code"

▶ 例2:Bob の一日

BASH
# 朝:コードをプル + lint 修正
git pull
codex --full-auto "Fix all lint errors"

# 午前:新機能
codex "Add user avatar upload with crop and compression"

# 午後:バグ修正
codex "Fix payment timeout issue, add retry mechanism"

# 夕方:テスト
codex "Supplement integration tests for today's changes"

# 夜:ドキュメント
codex "Update API docs and CHANGELOG"

5. CLI プロンプトテクニック

(1) 精密なファイル参照

BASH
# 良い慣例
codex "Modify src/auth/login.ts, add 2FA support"

# 悪い慣例
codex "Add a feature to login"

(2) 検証を含める

BASH
codex "Optimize database query performance, ensure npm test passes, compare execution time before and after optimization"

(3) ステップバイステップの実行

BASH
# 複雑なタスクをステップで
codex "Step 1: Create the Order model. Output the model definition when done, I'll confirm before continuing to step 2."

(4) 既存のコードを参照

BASH
codex "Create UserProfile component, follow the style of src/components/UserCard.tsx"

❓ よくある質問

Q CLI のスキルと App のスキルは同じですか?
A フォーマットは同じですが、CLI では --skill パラメータで指定する必要があり、App では UI から選択できます。
Q サンドボックスはパフォーマンスに影響しますか?
A Docker サンドボックスには起動オーバーヘッド(約2〜5秒)がありますが、実行時のパフォーマンス差はわずかです。プロセスレベルのサンドボックスはほぼオーバーヘッドなしです。
Q CLI で複数のタスクを同時に実行できますか?
A 複数のターミナルウィンドウを開き、それぞれで Codex インスタンスを実行します。ファイルの競合を避けるために Worktrees を使用してください。
Q CLI の詳細な実行ログを確認するには?
A --verbose パラメータを使用するか、--log-file でログファイルを指定してください。
Q CLI の非インタラクティブモードで確認要求を処理できますか?
A --approval-policy approve または --full-auto が必要です。そうでなければ非インタラクティブモードは確認待ちでハングします。

📖 まとめ


📝 練習問題

  1. 基本 (⭐):CLI スキルを使ってコードレビューを完了する。
  2. 中級 (⭐⭐):Docker サンドボックスを設定し、その中でタスクを実行する。
  3. 上級 (⭐⭐⭐):日常開発のすべての Codex タスクをカバーする CLI 自動化スクリプトを作成する。
Web-Tutorial.com

Web-Tutorial 技術チーム

複数の開発者によって共同維持されているプログラミングチュートリアルプラットフォーム。各チュートリアルは専門分野の開発者が執筆・レビューしています。正確で信頼性の高いコンテンツを目指しています — 問題を見つけた場合はお知らせください。

100%