Nightlatch / Guides

Codex CLI rules: prefix_rule examples that block dangerous commands

Updated 2026-11-29 · Codex CLI

Short version: put a .rules file in ~/.codex/rules/, write one prefix_rule() per command you want to forbid or prompt for, and test it with codex execpolicy check. Codex marks rules as experimental, so check the official rules page when you upgrade.

Where the file goes

Codex scans a rules/ folder next to each active config layer at startup. For your user, that’s ~/.codex/rules/ (for example ~/.codex/rules/default.rules). Project rules in .codex/rules/ load only when the project’s .codex layer is trusted. Restart Codex after editing.

A starter rules file

The file is Starlark, which reads like Python. We checked every rule below with codex execpolicy check (codex-cli 0.172.0).

~/.codex/rules/default.rules
# ~/.codex/rules/default.rules (or .codex/rules/*.rules in a trusted project)

# Never force push.
prefix_rule(
    pattern = ["git", "push", ["-f", "--force"]],
    decision = "forbidden",
    justification = "Force push rewrites shared history. Push a new branch instead.",
    match = ["git push -f", "git push --force origin main"],
    not_match = ["git push origin feat", "git push --force-with-lease"],
)
prefix_rule(
    pattern = ["git", "push", "origin", ["-f", "--force"]],
    decision = "forbidden",
    justification = "Force push rewrites shared history.",
    match = ["git push origin --force main"],
)

# Ask before pushing straight to main.
prefix_rule(
    pattern = ["git", "push", "origin", ["main", "master"]],
    decision = "prompt",
    justification = "Push a feature branch and open a PR instead.",
)

# Ask before throwing away work.
prefix_rule(
    pattern = ["git", "reset", "--hard"],
    decision = "prompt",
    justification = "Discards uncommitted changes. Run git stash first.",
)
prefix_rule(
    pattern = ["git", "clean"],
    decision = "prompt",
    justification = "Deletes untracked files. Run git clean -n to preview.",
)

# Recursive deletes of root, home or the whole project.
prefix_rule(
    pattern = ["rm", ["-rf", "-fr", "-Rf"], ["/", "~", ".", "..", "*"]],
    decision = "forbidden",
    justification = "Recursive delete of a root, home or project directory.",
    match = ["rm -rf /", "rm -rf ~", "rm -fr ."],
    not_match = ["rm -rf dist", "rm -r build"],
)

# Publishing is for humans or CI.
prefix_rule(
    pattern = [["npm", "pnpm", "yarn"], "publish"],
    decision = "forbidden",
    justification = "Releases are cut by a human or tagged CI.",
)

# Routine read-only commands can skip the prompt.
prefix_rule(
    pattern = ["git", ["status", "diff", "log"]],
    decision = "allow",
)

How the fields work

The inline tests are worth writing. When we gave a rule for -f a match example of git push --force, the file refused to load:

output
Error: failed to parse policy at bad.rules

Caused by:
    expected every example to match at least one rule. ... unmatched examples: ["git push --force"]

Test a command against your rules

terminal
codex execpolicy check --rules ~/.codex/rules/default.rules -- git push --force origin main
output
{"matchedRules":[{"prefixRuleMatch":{"matchedPrefix":["git","push","--force"],"decision":"forbidden","justification":"Force push rewrites shared history. Push a new branch instead."}}],"decision":"forbidden"}

Add --pretty for readable output, and repeat --rules to combine files.

The limits of prefix matching

~/.codex/config.toml
# ~/.codex/config.toml
sandbox_mode = "workspace-write"
approval_policy = "on-request"

[sandbox_workspace_write]
network_access = false