實作指南 / 12 分鐘
用 codex exec 執行可重複的 headless 工作
把已驗證的 repository 程序放進 script,同時保留清楚的權限與失敗邊界。
codex exec 是 script 與 CI 的非互動入口。它可從 argument 或 stdin 讀取 prompt,沿用 repository guidance 與 sandbox 模型,回傳 process status,也能延續已保存的 exec session。可靠性不來自「自動化」三個字,而來自固定 working directory、明確權限、隔離 credential,以及不依賴對話語氣的驗證。
已驗證 Terminal Sequence
- $
codex exec "Run the affected tests and report evidence" - $
codex exec --ephemeral "Inspect the repository and make no changes" - $
codex exec resume --last "Continue with the failed checks"
工作原則
先驗證再自動化
移除 live operator 前,先在受監督 session 證明 prompt、repo context 與 acceptance checks。
Exit status 是 contract
Non-zero、timeout、malformed output 或缺少 artifact 都算失敗,不能被樂觀文字蓋過。
持久化是選擇
不應留下 session file 時用 --ephemeral;只有 checkout 相容且確實要延續歷史時才 resume。
實地程序
- 01
固定工作根目錄
在單一 checkout 執行或使用 -C;不要讓 job 從寬廣父目錄探索多個不相關 repository。
- 02
寫出驗收證據
列出允許修改的檔案、必跑命令與必須回傳的 test/artifact。
- 03
限制執行權限
選擇 sandbox、approval posture、timeout 與 credential scope;不讓 repo code 繼承部署金鑰。
- 04
處理 process result
分開保存 stdout/stderr、限制時間、保留診斷,並讓 non-zero 使 pipeline fail。
- 05
刻意 retry
只重試 transient failure;使用新 ephemeral run 或明確 session,不猜「上一個」歷史。
PASS / FAIL
驗收清單
- 只在一個已驗證 checkout 執行。
- 權限與 credential 最小化。
- Timeout/non-zero fail closed。
- 修改仍通過 repository tests。
- Retry 不重複外部副作用。
WATCH / REJECT
失敗模式
- 在一般 CI runner 使用 --yolo。
- 把 write credential 交給不可信 PR code。
- 需要 typed contract 卻解析自然語言。