Adds an optional `justification` parameter to the `prefix_rule()`
execpolicy DSL so policy authors can attach human-readable rationale to
a rule. That justification is propagated through parsing/matching and
can be surfaced to the model (or approval UI) when a command is blocked
or requires approval.
When a command is rejected (or gated behind approval) due to policy, a
generic message makes it hard for the model/user to understand what went
wrong and what to do instead. Allowing policy authors to supply a short
justification improves debuggability and helps guide the model toward
compliant alternatives.
Example:
```python
prefix_rule(
pattern = ["git", "push"],
decision = "forbidden",
justification = "pushing is blocked in this repo",
)
```
If Codex tried to run `git push origin main`, now the failure would
include:
```
`git push origin main` rejected: pushing is blocked in this repo
```
whereas previously, all it was told was:
```
execpolicy forbids this command
```
119 lines
3 KiB
Rust
119 lines
3 KiB
Rust
use std::fs;
|
|
|
|
use assert_cmd::Command;
|
|
use pretty_assertions::assert_eq;
|
|
use serde_json::json;
|
|
use tempfile::TempDir;
|
|
|
|
#[test]
|
|
fn execpolicy_check_matches_expected_json() -> Result<(), Box<dyn std::error::Error>> {
|
|
let codex_home = TempDir::new()?;
|
|
let policy_path = codex_home.path().join("rules").join("policy.rules");
|
|
fs::create_dir_all(
|
|
policy_path
|
|
.parent()
|
|
.expect("policy path should have a parent"),
|
|
)?;
|
|
fs::write(
|
|
&policy_path,
|
|
r#"
|
|
prefix_rule(
|
|
pattern = ["git", "push"],
|
|
decision = "forbidden",
|
|
)
|
|
"#,
|
|
)?;
|
|
|
|
let output = Command::new(codex_utils_cargo_bin::cargo_bin("codex")?)
|
|
.env("CODEX_HOME", codex_home.path())
|
|
.args([
|
|
"execpolicy",
|
|
"check",
|
|
"--rules",
|
|
policy_path
|
|
.to_str()
|
|
.expect("policy path should be valid UTF-8"),
|
|
"git",
|
|
"push",
|
|
"origin",
|
|
"main",
|
|
])
|
|
.output()?;
|
|
|
|
assert!(output.status.success());
|
|
let result: serde_json::Value = serde_json::from_slice(&output.stdout)?;
|
|
assert_eq!(
|
|
result,
|
|
json!({
|
|
"decision": "forbidden",
|
|
"matchedRules": [
|
|
{
|
|
"prefixRuleMatch": {
|
|
"matchedPrefix": ["git", "push"],
|
|
"decision": "forbidden"
|
|
}
|
|
}
|
|
]
|
|
})
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn execpolicy_check_includes_justification_when_present() -> Result<(), Box<dyn std::error::Error>>
|
|
{
|
|
let codex_home = TempDir::new()?;
|
|
let policy_path = codex_home.path().join("rules").join("policy.rules");
|
|
fs::create_dir_all(
|
|
policy_path
|
|
.parent()
|
|
.expect("policy path should have a parent"),
|
|
)?;
|
|
fs::write(
|
|
&policy_path,
|
|
r#"
|
|
prefix_rule(
|
|
pattern = ["git", "push"],
|
|
decision = "forbidden",
|
|
justification = "pushing is blocked in this repo",
|
|
)
|
|
"#,
|
|
)?;
|
|
|
|
let output = Command::new(codex_utils_cargo_bin::cargo_bin("codex")?)
|
|
.env("CODEX_HOME", codex_home.path())
|
|
.args([
|
|
"execpolicy",
|
|
"check",
|
|
"--rules",
|
|
policy_path
|
|
.to_str()
|
|
.expect("policy path should be valid UTF-8"),
|
|
"git",
|
|
"push",
|
|
"origin",
|
|
"main",
|
|
])
|
|
.output()?;
|
|
|
|
assert!(output.status.success());
|
|
let result: serde_json::Value = serde_json::from_slice(&output.stdout)?;
|
|
assert_eq!(
|
|
result,
|
|
json!({
|
|
"decision": "forbidden",
|
|
"matchedRules": [
|
|
{
|
|
"prefixRuleMatch": {
|
|
"matchedPrefix": ["git", "push"],
|
|
"decision": "forbidden",
|
|
"justification": "pushing is blocked in this repo"
|
|
}
|
|
}
|
|
]
|
|
})
|
|
);
|
|
|
|
Ok(())
|
|
}
|