name: codex description: 将任务交给Codex CLI进行自主执行。当任务需要能力强的子代理来实现、修复、调查或审查代码时使用。Codex拥有完整的代码库访问权限,可以进行更改。 argument-hint: <任务描述> [–model <模型>] [–sandbox <模式>] disable-model-invocation: false allowed-tools: Bash(codex:), Bash(git:), Bash(pwd:), Bash(mkdir:), Bash(cat:), Bash(head:), Bash(tail:), Bash(wc:), Read, Grep, Glob
Codex子代理
会话ID: ${CLAUDE_SESSION_ID}
输出: ~/.claude/codex/${CLAUDE_SESSION_ID}/
参数
任务: $ARGUMENTS
可选标志(仅在用户明确要求时使用):
--model <模型>:gpt-5.2-codex(默认),gpt-5.2,gpt-5-mini,o3--sandbox <模式>:read-only,workspace-write,danger-full-access
省略标志以使用用户的配置默认值。
上下文深度
最小: 特定文件/函数 → 仅git状态 中等: 功能/多文件 → 添加最近更改 完整: 调查/不明确 → 添加会话摘要
收集上下文
始终执行:
pwd && git rev-parse --show-toplevel 2>/dev/null || echo "不是git仓库"
git branch --show-current 2>/dev/null && git status --short 2>/dev/null | head -20
中等/完整:
git diff --stat 2>/dev/null | tail -20
git log --oneline -5 --since="4小时前" 2>/dev/null
仅完整: 会话摘要(已完成的工作、决策、阻碍)
提示结构
使用CTCO格式(上下文 → 任务 → 约束 → 输出):
<context>
工作目录: {cwd}
仓库: {repo_name}
分支: {branch}
{git_status}
{recent_changes if medium/full}
{session_summary if full}
</context>
<task>
{来自参数的任务}
</task>
<constraints>
- 精确实现所请求的内容
- 更改前先读取文件
- 运行测试/检查器进行验证
- 如果模糊则说明解释
</constraints>
<output>
摘要(≤5个要点):
- **更改内容**: 文件和更改
- **位置**: 文件:行号引用
- **验证**: 运行的测试/检查器
- **风险**: 需要注意的边缘情况
- **后续步骤**: 跟进或"无"
</output>
执行
设置:
mkdir -p ~/.claude/codex/${CLAUDE_SESSION_ID}
git rev-parse --show-toplevel 2>/dev/null && IN_GIT=true || IN_GIT=false
运行:
codex exec --json \
-o ~/.claude/codex/${CLAUDE_SESSION_ID}/summary-{timestamp}.txt \
{--skip-git-repo-check if not in git} \
{--full-auto OR --sandbox <mode>} \
{-m <model> if requested} \
- <<'CODEX_PROMPT'
{prompt}
CODEX_PROMPT > ~/.claude/codex/${CLAUDE_SESSION_ID}/progress-{timestamp}.jsonl
标志:
- 不在git中: 添加
--skip-git-repo-check - 默认:
--full-auto(workspace-write + 自动批准) - 用户请求:
--sandbox <模式>或-m <模型>
后台任务 vs 前台任务
后台任务(预计>30秒):
- 多文件更改、调查、测试、功能工作
- 使用
run_in_background: true→ 返回task_id
前台任务(<30秒):
- 单行修复、简单查询
监控后台任务
令牌高效方法:
- 使用
TaskOutput(task_id, block=true)等待完成 - 忽略TaskOutput的内容(stdout重定向到进度文件)
- 直接读取摘要文件:
cat ~/.claude/codex/${CLAUDE_SESSION_ID}/summary-*.txt
摘要文件仅包含Codex的最终消息(令牌高效)。
进度检查(如果需要完成前检查):
TaskOutput(task_id, block=false)- 检查是否仍在运行tail -n 3 ~/.claude/codex/${CLAUDE_SESSION_ID}/progress-*.jsonl- 仅最后3个事件
请勿读取整个进度文件或使用 tail -f。
报告结果
读取摘要:
cat ~/.claude/codex/${CLAUDE_SESSION_ID}/summary-*.txt
报告格式(≤5个要点):
**状态:** {成功/错误/部分}
**更改:** {文件和更改}
**验证:** {测试/检查器}
**风险:** {如果有}
**后续:** {跟进或"无"}
示例
# 简单修复
/codex 修复 utils/parser.ts 第42行的空指针
# 功能工作
/codex 为 /api/submit 端点添加速率限制
# 调查(后台)
/codex 调查为什么CI构建在arm64上失败
# 模型覆盖
/codex --model o3 设计缓存策略
# 只读模式
/codex --sandbox read-only 审查身份验证实现