Rules 管沙箱外命令,Hooks 管生命周期
命令规则在 Codex 请求出沙箱时生效。forbidden > prompt > allow。Hooks 更通用,能看补丁和 MCP。
规则文件:
- 用户:
~/.codex/rules/default.rules - 项目:
<repo>/.codex/rules/*.rules(需信任)
prefix_rule(pattern=["git", "push"], decision="prompt")
prefix_rule(pattern=["rm", "-rf"], decision="forbidden")
测试:
codex execpolicy check --pretty --rules ~/.codex/rules/default.rules -- git push origin main
agent 实际跑的常常是绝对路径。只写了 ["git", "push"] 时,/usr/bin/git push 默认对不上。仓库文档里的 --resolve-host-executables 会按 basename 回退;若定义了 host_executable(name="git", paths=[...]),只有列表里的路径才允许回退:
codex execpolicy check --pretty --rules ~/.codex/rules/default.rules \
--resolve-host-executables -- /usr/bin/git push origin main
Learn 页的旗标表目前只列了 --pretty 和 --rules;本机 codex execpolicy check --help 仍以当前二进制为准。
加载时用 match / not_match 当内联测试。pattern 必须是精确前缀:gh pr --repo x view 匹配不到 ["gh", "pr", "view"]。bash -lc 里只有线性 && || ; | 才会拆开分别套规则;有重定向、替换或变量时整段当一条。
选规则还是钩子:只要前缀匹配的命令策略,用 rules;要看补丁内容、MCP 调用或补上下文,用 hooks。codex exec --ignore-rules 会跳过用户和项目 .rules,只给已经审查过策略的自动化用。
权限档里只要有任意 deny-read,allow 规则仍可能留在沙箱里跑。execpolicy check 报 allow 不等于出沙箱,见 execpolicy allow 遇上 deny-read 那条。