OpenAI Codex 适合把它当成有明确上下文的编码代理来用,先说清楚目标和“完成标准”,再让它按工作流处理代码理解、Bug 修复、写测试、UI 原型、重构和代码审查。IDE 扩展会自动带上打开文件,CLI 通常需要你明确写出路径或用 /mention、@ 绑定文件;每个流程都要用复现、检查或 review 验证结果。
OpenAI Codex 工作流:代码理解、修复 Bug、写测试
如果你刚开始用 OpenAI Codex,先看 Prompting,再回到这里找具体场景。
怎么读这些示例
每个工作流都包含:
- 适合场景:什么时候用,以及更适合 IDE、CLI 还是 cloud
- 步骤:带示例 Prompt
- 上下文说明:Codex 自动能看到什么,你还需要补什么
- 验证方法:怎么确认输出可用
提示:IDE extension 会自动把你打开的文件作为上下文。CLI 里通常要你明确提路径,或者用
/mention和@路径自动补全来附加文件。
解释代码库
适合你在接手服务、理解协议、数据模型或请求流时使用。
IDE extension 工作流(本地探索最快)
- 打开最相关的文件。
- 选中你关心的代码(可选,但推荐)。
- 给 Codex 发 Prompt:
Explain how the request flows through the selected code.
Include:
- a short summary of the responsibilities of each module involved
- what data is validated and where
- one or two "gotchas" to watch for when changing this
验证方式:
Summarize the request flow as a numbered list of steps. Then list the files involved.
CLI 工作流(适合要记录对话和执行 shell 命令)
- 在交互式会话中启动 Codex:
codex
- 附加文件后再提需求:
I need to understand the protocol used by this service. Read @foo.ts @schema.ts and explain the schema and request/response flow. Focus on required vs optional fields and backward compatibility rules.
上下文说明:
- 你可以在 composer 里用
@插入工作区文件路径,或者用/mention附加指定文件。
修复 Bug
适合你已经能在本地复现问题的场景。
CLI 工作流(复现和验证最紧凑)
- 在仓库根目录启动 Codex:
codex
- 给出复现步骤和可疑文件:
Bug: Clicking "Save" on the settings screen sometimes shows "Saved" but doesn't persist the change.
Repro:
1) Start the app: npm run dev
2) Go to /settings
3) Toggle "Enable alerts"
4) Click Save
5) Refresh the page: the toggle resets
Constraints:
- Do not change the API shape.
- Keep the fix minimal and add a regression test if feasible.
Start by reproducing the bug locally, then propose a patch and run checks.
上下文说明:
- 你提供的内容:复现步骤和约束条件,这些比高层描述更重要。
- Codex 提供的内容:命令输出、发现的调用点、触发的 stack trace。
验证方式:
- 让 Codex 在修复后重新跑复现步骤。
- 如果你有标准检查流水线,可以明确要求它执行:
After the fix, run lint + the smallest relevant test suite. Report the commands and results.
IDE extension 工作流
- 打开你怀疑出问题的文件,以及它最近的调用方。
- 给 Codex 发 Prompt:
Find the bug causing "Saved" to show without persisting changes. After proposing the fix, tell me how to verify it in the UI.
写测试
适合你想把测试范围说得很明确的时候。
IDE extension 工作流(基于选中代码)
- 打开包含函数的文件。
- 选中函数定义所在的行,在命令面板里选择 Add to Codex Thread,把这些行加入上下文。
- 给 Codex 发 Prompt:
Write a unit test for this function. Follow conventions used in other tests.
上下文说明:
- Add to Codex Thread 会把你选中的代码行和打开的文件一起传给 Codex。
CLI 工作流(在 Prompt 里描述路径和范围)
- 启动 Codex:
codex
- 按函数名发起需求:
Add a test for the invert_list function in @transform.ts. Cover the happy path plus edge cases.
从截图生成原型
适合你有设计稿、截图或 UI 参考,想快速做出可运行原型的时候。
CLI 工作流(图片 + Prompt)
- 把截图保存到本地,比如
./specs/ui.png。 - 启动 Codex:
codex
- 把图片拖进终端,附加到 Prompt。
- 再补充约束和交付物:
Create a new dashboard based on this image.
Constraints:
- Use react, vite, and tailwind. Write the code in typescript.
- Match spacing, typography, and layout as closely as possible.
Deliverables:
- A new route/page that renders the UI
- Any small components needed
- README.md with instructions to run it locally
上下文说明:
- 图片只提供视觉要求,你仍然要写清实现约束,比如框架、路由、组件风格。
- 如果有不明显的行为,比如 hover 状态、校验规则、键盘交互,要用文字补充。
验证方式:
Start the dev server and tell me the local URL/route to view the prototype.
IDE extension 工作流(图片 + 现有文件)
- 在 Codex 对话里附加图片,支持拖入或粘贴。
- 给 Codex 发 Prompt:
Create a new settings page. Use the attached screenshot as the target UI.
Follow design and visual patterns from other files in this project.
用 live 更新迭代 UI
适合你想在“设计 → 调整 → 刷新 → 再调整”的循环里让 Codex 持续改代码。
CLI 工作流(先跑 Vite,再小步迭代)
- 启动 Codex:
codex
- 在另一个终端启动开发服务器:
npm run dev
- 让 Codex 先给出几个方向:
Propose 2-3 styling improvements for the landing page.
- 选一个方向后,给更具体的小改动:
Go with option 2.
Change only the header:
- make the typography more editorial
- increase whitespace
- ensure it still looks good on mobile
- 继续用聚焦的请求迭代:
Next iteration: reduce visual noise.
Keep the layout, but simplify colors and remove any redundant borders.
验证方式:
- 在浏览器里实时查看代码更新后的效果。
- 保留你满意的改动,回滚你不满意的改动。
- 如果你回滚或手动改了某个结果,要告诉 Codex,不然它下一次可能会覆盖你的修改。
把重构委托到 cloud
适合你先在本地做规划和快速检查,再把长耗时实现交给 cloud 任务并行处理。
本地规划(IDE)
- 确认当前改动已经提交,或者至少已经 stash,这样后面更容易对比。
- 让 Codex 先产出重构计划。如果你有
$planskill,可以直接调用:
$plan
We need to refactor the auth subsystem to:
- split responsibilities (token parsing vs session loading vs permissions)
- reduce circular imports
- improve testability
Constraints:
- No user-visible behavior changes
- Keep public APIs stable
- Include a step-by-step migration plan
- Review 计划后继续调整:
Revise the plan to:
- specify exactly which files move in each milestone
- include a rollback strategy
上下文说明:
- 规划阶段里,Codex 在本地扫描代码效果最好,尤其是入口点、模块边界和依赖关系线索。
委托给 cloud(IDE → Cloud)
- 如果还没做,先设置一个 Codex cloud environment。
- 点击 Prompt 输入框下方的云端图标,选择你的 cloud environment。
- 发送下一条 Prompt 后,Codex 会在 cloud 里新建一个线程,并继承当前线程上下文,包括计划和本地源码改动。
Implement Milestone 1 from the plan.
- Review cloud diff,必要时继续迭代。
- 可以直接在 cloud 里创建 PR,也可以把改动拉回本地测试后再收尾。
- 继续执行计划里的后续 milestone。
做本地代码审查
适合你在提交或创建 PR 前先让 Codex 帮你看一遍。
CLI 工作流(审查当前工作区)
- 启动 Codex:
codex
- 执行 review 命令:
/review
- 也可以指定关注点:
/review Focus on edge cases and security issues
验证方式:
- 按 review 建议修复问题后,再运行一次
/review确认问题已经解决。
审查 GitHub pull request
适合你不想把分支拉到本地,也想拿到 review 反馈的场景。
使用前,需要先在仓库里启用 Codex Code review。参考 Code review。
GitHub 工作流(评论驱动)
- 打开 GitHub 上的 pull request。
- 留一条评论,明确让 Codex 参与审查:
@codex review
- 也可以补充更具体的关注点:
@codex review for security vulnerabilities and security concerns
更新文档
适合你需要改文档,而且要保证准确和清楚的时候。
IDE 或 CLI 工作流(本地修改 + 本地验证)
- 找到要修改的文档文件并打开它们;如果是在 IDE 或 CLI 里,也可以用
@提及文件。 - 给 Codex 明确 scope 和验证要求:
Update the "advanced features" documentation to provide authentication troubleshooting guidance. Verify that all links are valid.
- Codex 起草修改后,自己 review 文档并按需继续调整。
验证方式:
- 直接阅读渲染后的页面。
常见问题
OpenAI Codex 怎么开始理解一个陌生代码库?
先让 Codex 读最相关的文件,再要求它说明请求流、模块职责、数据校验位置和需要注意的坑。IDE 适合边看边问,CLI 适合把文件和命令一起串起来看。
OpenAI Codex 怎么用来修复本地可复现的 Bug?
把复现步骤写具体,最好包括启动命令、访问路径、点击顺序和刷新后的表现,再补上约束,比如不要改 API shape、尽量最小化修改。修复后让 Codex 重新跑复现流程,再跑 lint 或最小相关测试集。
OpenAI Codex CLI、IDE extension 和 cloud 分别适合什么场景?
IDE extension 适合本地编辑时快速理解上下文,因为它会自动带上打开文件。CLI 更适合需要完整终端记录、组合命令或手动控制文件输入的场景。cloud 适合先本地规划,再把长任务交给云端并行执行。