From 876ade044554a5f4ae845fec99590548981715a7 Mon Sep 17 00:00:00 2001 From: kevin zhao Date: Wed, 19 Nov 2025 21:34:39 +0000 Subject: [PATCH] serializing noMatch as an object --- codex-rs/core/src/exec_policy.rs | 6 +++--- codex-rs/execpolicy2/README.md | 4 ++-- codex-rs/execpolicy2/src/lib.rs | 4 ++-- codex-rs/execpolicy2/src/policy.rs | 12 ++++++------ codex-rs/execpolicy2/tests/basic.rs | 11 ++++++++++- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/codex-rs/core/src/exec_policy.rs b/codex-rs/core/src/exec_policy.rs index c55dbfed23..14a5771cbb 100644 --- a/codex-rs/core/src/exec_policy.rs +++ b/codex-rs/core/src/exec_policy.rs @@ -109,7 +109,7 @@ fn evaluate_with_policy( } Decision::Allow => Some(ApprovalRequirement::Skip), }, - Evaluation::NoMatch => None, + Evaluation::NoMatch { .. } => None, } } @@ -206,7 +206,7 @@ mod tests { let commands = [vec!["rm".to_string()]]; assert!(matches!( policy.check_multiple(commands.iter()), - Evaluation::NoMatch + Evaluation::NoMatch { .. } )); assert!(!temp_dir.path().join(POLICY_DIR_NAME).exists()); } @@ -259,7 +259,7 @@ mod tests { let command = [vec!["ls".to_string()]]; assert!(matches!( policy.check_multiple(command.iter()), - Evaluation::NoMatch + Evaluation::NoMatch { .. } )); } diff --git a/codex-rs/execpolicy2/README.md b/codex-rs/execpolicy2/README.md index 96e67b1e0a..dbfd4f3d21 100644 --- a/codex-rs/execpolicy2/README.md +++ b/codex-rs/execpolicy2/README.md @@ -34,7 +34,7 @@ cargo run -p codex-execpolicy2 -- check --policy path/to/policy.codexpolicy git ``` - Example outcomes: - Match: `{"match": { ... "decision": "allow" ... }}` - - No match: `"noMatch"` + - No match: `{"noMatch": {}}` ## Response shapes - Match: @@ -56,7 +56,7 @@ cargo run -p codex-execpolicy2 -- check --policy path/to/policy.codexpolicy git - No match: ```json -"noMatch" +{"noMatch": {}} ``` - `matchedRules` lists every rule whose prefix matched the command; `matchedPrefix` is the exact prefix that matched. diff --git a/codex-rs/execpolicy2/src/lib.rs b/codex-rs/execpolicy2/src/lib.rs index 2516e1e9f4..b459caea16 100644 --- a/codex-rs/execpolicy2/src/lib.rs +++ b/codex-rs/execpolicy2/src/lib.rs @@ -1,14 +1,14 @@ -pub mod execpolicycheck; pub mod decision; pub mod error; +pub mod execpolicycheck; pub mod parser; pub mod policy; pub mod rule; -pub use execpolicycheck::ExecPolicyCheckCommand; pub use decision::Decision; pub use error::Error; pub use error::Result; +pub use execpolicycheck::ExecPolicyCheckCommand; pub use parser::PolicyParser; pub use policy::Evaluation; pub use policy::Policy; diff --git a/codex-rs/execpolicy2/src/policy.rs b/codex-rs/execpolicy2/src/policy.rs index a543e5c4b0..e048fce1ff 100644 --- a/codex-rs/execpolicy2/src/policy.rs +++ b/codex-rs/execpolicy2/src/policy.rs @@ -27,9 +27,9 @@ impl Policy { let rules = match cmd.first() { Some(first) => match self.rules_by_program.get_vec(first) { Some(rules) => rules, - None => return Evaluation::NoMatch, + None => return Evaluation::NoMatch {}, }, - None => return Evaluation::NoMatch, + None => return Evaluation::NoMatch {}, }; let matched_rules: Vec = @@ -39,7 +39,7 @@ impl Policy { decision, matched_rules, }, - None => Evaluation::NoMatch, + None => Evaluation::NoMatch {}, } } @@ -52,7 +52,7 @@ impl Policy { .into_iter() .flat_map(|command| match self.check(command.as_ref()) { Evaluation::Match { matched_rules, .. } => matched_rules, - Evaluation::NoMatch => Vec::new(), + Evaluation::NoMatch { .. } => Vec::new(), }) .collect(); @@ -61,7 +61,7 @@ impl Policy { decision, matched_rules, }, - None => Evaluation::NoMatch, + None => Evaluation::NoMatch {}, } } } @@ -69,7 +69,7 @@ impl Policy { #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub enum Evaluation { - NoMatch, + NoMatch {}, Match { decision: Decision, #[serde(rename = "matchedRules")] diff --git a/codex-rs/execpolicy2/tests/basic.rs b/codex-rs/execpolicy2/tests/basic.rs index 75921ecd57..79f11bafba 100644 --- a/codex-rs/execpolicy2/tests/basic.rs +++ b/codex-rs/execpolicy2/tests/basic.rs @@ -10,6 +10,7 @@ use codex_execpolicy2::rule::PatternToken; use codex_execpolicy2::rule::PrefixPattern; use codex_execpolicy2::rule::PrefixRule; use pretty_assertions::assert_eq; +use serde_json::json; fn tokens(cmd: &[&str]) -> Vec { cmd.iter().map(std::string::ToString::to_string).collect() @@ -60,6 +61,14 @@ prefix_rule( ); } +#[test] +fn serializes_no_match_as_object() { + let serialized = + serde_json::to_value(&Evaluation::NoMatch {}).expect("should serialize evaluation"); + + assert_eq!(json!({"noMatch": {}}), serialized); +} + #[test] fn parses_multiple_policy_files() { let first_policy = r#" @@ -288,7 +297,7 @@ prefix_rule( "color.status=always", "status", ])); - assert_eq!(Evaluation::NoMatch, no_match_eval); + assert_eq!(Evaluation::NoMatch {}, no_match_eval); } #[test]