title: “Codex — Delegate coding to OpenAI Codex CLI (features, PRs)” sidebar_label: “Codex” description: “Delegate coding to OpenAI Codex CLI (features, PRs)”
{/* 本页面由 website/scripts/generate-skill-docs.py 根据技能对应的 SKILL.md 文件自动生成。请直接编辑源文件 SKILL.md,而非此页面。 */}
Codex
通过 OpenAI Codex CLI 授权任务进行编程(支持功能开发与代码提交)。
技能元数据
| 来源 | 内置(默认已安装) |
| 路径 | skills/autonomous-ai-agents/codex |
| 版本 | 1.0.0 |
| 开发者 | Hermes Agent |
| 许可协议 | MIT |
| 支持平台 | linux、macos、windows |
| 标签 | Coding-Agent、Codex、OpenAI、Code-Review、Refactoring |
| 相关技能 | claude-code、hermes-agent |
参考:完整 SKILL.md 内容
Codex CLI
通过 Hermes 终端将编程任务委托给 Codex。Codex 是 OpenAI 开发的自主编程智能体 CLI 工具。
适用场景
- 功能开发
- 代码重构
- 代码提交审核
- 批量问题修复
使用前需准备 Codex CLI 及对应的 Git 仓库。
先决条件
- 已安装 Codex:
npm install -g @openai/codex - 已配置 OpenAI 认证信息:可使用
OPENAI_API_KEY,或通过 Codex CLI 登录流程获取的 Codex OAuth 凭证 - 必须运行在 Git 仓库内部——Codex 不支持在仓库外部运行
- 在终端调用时需添加
pty=true参数——因为 Codex 是交互式终端应用
对于 Hermes 本身,若设置 model.provider: openai-codex,则会在执行 hermes auth add openai-codex 后使用来自 ~/.hermes/auth.json 的 Hermes 管理型 Codex OAuth 凭证。而对于独立的 Codex CLI,有效的 CLI OAuth 会存储在 ~/.codex/auth.json 文件中;请勿仅因未找到 OPENAI_API_KEY 就判定 Codex 认证缺失。
单次任务处理
terminal(command="codex exec 'Add dark mode toggle to settings'", workdir="~/project", pty=true)
用于初步开发(Codex 需要一个 Git 仓库):
terminal(command="cd $(mktemp -d) && git init && codex exec 'Build a snake game in Python'", pty=true)
后台模式(长时间任务)
# Start in background with PTY
terminal(command="codex exec --full-auto 'Refactor the auth module'", workdir="~/project", background=true, pty=true)
# Returns session_id
# Monitor progress
process(action="poll", session_id="<id>")
process(action="log", session_id="<id>")
# Send input if Codex asks a question
process(action="submit", session_id="<id>", data="yes")
# Kill if needed
process(action="kill", session_id="<id>")
主要标志位
| 标志位 | 效果 |
|---|---|
exec "prompt" | 单次执行,任务完成后退出 |
--full-auto | 使用沙箱机制,但会自动批准工作区中的文件更改 |
--yolo | 不使用沙箱且无需审批(速度最快,但也最危险) |
--sandbox danger-full-access | 不启用 Codex 沙箱;当主机服务环境破坏了隔离层时非常有用 |
Hermes Gateway 的注意事项
当从 Hermes gateway 或服务环境(例如由 Telegram 驱动的智能体会话)调用 Codex CLI 时,即使相同的命令在用户的交互式 shell 中可以正常运行,Codex 的 workspace-write 沙箱机制仍可能失效。典型的异常表现为与隔离层/用户命名空间相关的错误,例如 setting up uid map: Permission denied 或 loopback: Failed RTM_NEWADDR: Operation not permitted。
在这种情况下,建议使用:
codex exec --sandbox danger-full-access "<task>"
不妨采用进程隔离作为安全防护层:明确指定workdir,在启动前确保Git状态整洁,缩小任务提示范围,通过git diff进行审查,执行针对性测试,并在提交重大变更之前由人工或智能代理进行确认。
PR审查
为确保审查安全,请将代码克隆到临时目录中:
terminal(command="REVIEW=$(mktemp -d) && git clone https://github.com/user/repo.git $REVIEW && cd $REVIEW && gh pr checkout 42 && codex review --base origin/main", pty=true)
利用工作树并行处理问题修复
# Create worktrees
terminal(command="git worktree add -b fix/issue-78 /tmp/issue-78 main", workdir="~/project")
terminal(command="git worktree add -b fix/issue-99 /tmp/issue-99 main", workdir="~/project")
# Launch Codex in each
terminal(command="codex --yolo exec 'Fix issue #78: <description>. Commit when done.'", workdir="/tmp/issue-78", background=true, pty=true)
terminal(command="codex --yolo exec 'Fix issue #99: <description>. Commit when done.'", workdir="/tmp/issue-99", background=true, pty=true)
# Monitor
process(action="list")
# After completion, push and create PRs
terminal(command="cd /tmp/issue-78 && git push -u origin fix/issue-78")
terminal(command="gh pr create --repo user/repo --head fix/issue-78 --title 'fix: ...' --body '...'")
# Cleanup
terminal(command="git worktree remove /tmp/issue-78", workdir="~/project")
批量代码审查
# Fetch all PR refs
terminal(command="git fetch origin '+refs/pull/*/head:refs/remotes/origin/pr/*'", workdir="~/project")
# Review multiple PRs in parallel
terminal(command="codex exec 'Review PR #86. git diff origin/main...origin/pr/86'", workdir="~/project", background=true, pty=true)
terminal(command="codex exec 'Review PR #87. git diff origin/main...origin/pr/87'", workdir="~/project", background=true, pty=true)
# Post results
terminal(command="gh pr comment 86 --body '<review>'", workdir="~/project")
规则
- 始终使用
pty=true— Codex 是一款交互式终端应用,若没有伪终端(PTY)则会挂起。 - 需要 Git 仓库 — Codex 无法在非 Git 目录下运行。如需临时创建环境,可使用
mktemp -d && git init命令。 - 一次性任务请使用
exec— 使用codex exec "prompt"可让命令正常运行并立即退出。 - 构建任务使用
--full-auto— 该选项可在沙箱环境中自动批准所有更改。 - 长时间任务请在后台运行 — 请使用
background=true参数,并通过process工具进行监控。 - 避免干扰 — 通过
poll/log方法进行监控,对长时间运行的任务保持耐心。 - 并行运行也可 — 如需批量处理任务,可同时启动多个 Codex 进程。