實作指南 / 17 分鐘
用 Hooks 與 Rules 加入 guardrails
Trusted command 觀察 lifecycle;testable prefix rules 控制 escalation。
Hooks 在 PreToolUse、PostToolUse、Stop、SessionStart 等 event 執行 trusted command handlers。Rules 決定 matching command prefix 在 sandbox 外 allow、prompt 或 forbidden。Hooks stable;rules experimental,最嚴格 matching decision 優先。
CLI
已驗證 Terminal Sequence
- ›
/hooks - $
codex execpolicy check --pretty --rules ~/.codex/rules/default.rules -- gh pr view 7888
A
工作原則
信任 exact hook code
Non-managed hook 依 definition hash trust;變更後必須重審。
Rule match argv prefix
寫窄 prefix 與 positive/negative examples,不靠模糊 shell string。
Guardrail 不是 fortress
多個 matching hook 可並行;目前只有 command handler 執行。
B
實地程序
- 01
選 enforcement surface
Guidance 用 AGENTS、lifecycle 用 hooks、escalation 用 rules、authoritative check 用 CI。
- 02
建立單一 hook
先做 read-only validation/logging 與 narrow matcher。
- 03
在 /hooks review
檢查 source、exact command、trust state。
- 04
寫 prefix rule
包含 match/not_match 與最安全 decision。
- 05
用 execpolicy 測試
依賴 experimental rule 前先 check。
PASS / FAIL
驗收清單
- Hook source/hash 已 review。
- Matcher scope 狹窄。
- Rule examples 通過。
- Critical invariants 仍由 CI enforce。
WATCH / REJECT
失敗模式
- 正常互動工作 bypass hook trust。
- 宣稱 prompt/agent hook handler 已執行。
- 把 experimental rules 當唯一防線。