實作指南 / 11 分鐘
串流 JSONL,並約束最終結果
把執行事件與 automation 真正消費的 schema-checked answer 分開。
codex exec --json 會把 newline-delimited JSON events 寫到 stdout;--output-schema 用 JSON Schema 約束最終 response,--output-last-message 則把最後訊息另存到指定檔案。事件串流用於 progress/diagnostics,最終 artifact 用於穩定 consumer contract;兩者都不能單獨證明 code change 正確。
已驗證 Terminal Sequence
- $
codex exec --json "Audit the current diff" | jq -c . - $
codex exec --json --output-schema schema.json --output-last-message final.txt "Return the audit result"
工作原則
JSONL 是 stream
逐行解析完整 JSON;不要把 stdout 當單一 JSON document,也不要假設每個 event shape 相同。
Schema 只控制形狀
合法 object 仍可能得出錯誤結論,必須另外驗證 domain rules、tests 與 files。
保持 channel 乾淨
Machine output 留給 parser;operator diagnostics 走其他 channel,不混入色碼或 debug prose。
實地程序
- 01
設計 consumer object
只定義下一個系統需要的欄位,包含 success、blocked 與 evidence 等明確狀態。
- 02
分離 outputs
把 --json 串給 event consumer;只有真的需要獨立 final artifact 時才用 --output-last-message。
- 03
增量解析
拒絕 malformed line、限制 retained events,保留觀測所需 type 與 stable ID。
- 04
雙重驗證
先驗 schema,再依現實檢查 repo state、paths、counts 與 test evidence。
- 05
副作用前 fail
Partial output 不得觸發 publish/merge/notify;必須等 process 完成且 final result 合法。
PASS / FAIL
驗收清單
- stdout 每行獨立解析。
- Schema 受版本控制。
- Refusal/timeout/malformed 有明確狀態。
- 語意證據另行驗證。
- 保存 event 前已移除 secrets。
WATCH / REJECT
失敗模式
- 對整段 JSONL 使用 JSON.parse。
- 把 schema valid 當作 task success。
- 中間 event 就觸發 write。