Make Code Mode wrappers transparent to Guardian model policies (#45915)

## Why

Code Mode wrappers should preserve cached scores for nested actions so adaptive review can reuse them across cells.

## What changed

Remove `code_mode` from `GuardianModelPolicy` and its approval scopes. Skip scoring and cache invalidation for direct Code Mode `exec` wrappers under model policies, leaving nested tools governed by their own categories. Preserve legacy wrapper scoring behavior.

## Testing

Expand app-server coverage for score reuse across cells, synchronous nested reviews, required model policies, and legacy configurations. Retain checks that ordinary tools named `exec` or MCP tools named `wait` invalidate cached scores.

GitOrigin-RevId: 7a0692e5330f3d4b4bda988ecbe7061bc8b2318c
This commit is contained in:
jif authored and copyberry committed 2026-09-16 10:11:26 +00:00
1 parent 2aff7208fe
commit 8f38d5a877
7 files changed
+112 -50

No files matched your search

@@ -1,4 +1,4 @@
//! Verifies independent code-mode and nested-tool policy through the app-server API. //! Verifies transparent model-policy wrappers and unchanged legacy Code Mode behavior.
use super::*; use super::*;
use codex_protocol::openai_models::GuardianModelPolicy; use codex_protocol::openai_models::GuardianModelPolicy;
@@ -6,13 +6,31 @@ use codex_protocol::openai_models::GuardianReviewMode;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use test_case::test_case; use test_case::test_case;
#[test_case(GuardianReviewMode::Adaptive, GuardianReviewMode::Adaptive, 2, 0; "adaptive cells and cua reuse cached scores")] #[derive(Clone, Copy)]
#[test_case(GuardianReviewMode::Synchronous, GuardianReviewMode::Adaptive, 1, 2; "adaptive cells retain synchronous nested reviews")] enum PolicyConfig {
#[test_case(GuardianReviewMode::Adaptive, GuardianReviewMode::Disabled, 1, 1; "shell and cua do not enable code mode")] Model,
RequiredModel,
RequiredLegacy,
Legacy,
LegacyDisabled,
}
#[test_case(PolicyConfig::Model, GuardianReviewMode::Adaptive, GuardianReviewMode::Synchronous, 1, 0; "model policy scores cua and preserves its cache across cells")]
#[test_case(PolicyConfig::Model, GuardianReviewMode::Synchronous, GuardianReviewMode::Adaptive, 0, 2; "adaptive shell does not score browser wrappers")]
#[test_case(PolicyConfig::Model, GuardianReviewMode::Synchronous, GuardianReviewMode::Synchronous, 0, 2; "synchronous policies do not score wrappers")]
#[test_case(PolicyConfig::RequiredModel, GuardianReviewMode::Adaptive, GuardianReviewMode::Synchronous, 1, 0; "required model preserves cua cache across cells")]
#[test_case(PolicyConfig::RequiredModel, GuardianReviewMode::Synchronous, GuardianReviewMode::Adaptive, 0, 2; "required model keeps non cua policies synchronous")]
#[test_case(PolicyConfig::Legacy, GuardianReviewMode::Adaptive, GuardianReviewMode::Synchronous, 1, 0; "legacy cua only still ignores wrappers")]
#[test_case(PolicyConfig::Legacy, GuardianReviewMode::Synchronous, GuardianReviewMode::Synchronous, 0, 0; "legacy models without cua review still do not score")]
#[test_case(PolicyConfig::Legacy, GuardianReviewMode::Adaptive, GuardianReviewMode::Adaptive, 2, 1; "legacy all tools still scores wrappers")]
#[test_case(PolicyConfig::LegacyDisabled, GuardianReviewMode::Adaptive, GuardianReviewMode::Synchronous, 0, 2; "legacy off still uses synchronous cua review")]
#[test_case(PolicyConfig::RequiredLegacy, GuardianReviewMode::Adaptive, GuardianReviewMode::Synchronous, 1, 0; "required legacy cua only still ignores wrappers")]
#[test_case(PolicyConfig::RequiredLegacy, GuardianReviewMode::Adaptive, GuardianReviewMode::Adaptive, 0, 2; "required legacy all tools still uses synchronous review")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn model_guardian_policy_scores_code_mode_cells( async fn model_guardian_policy_scores_code_mode_cells(
policy_config: PolicyConfig,
computer_use: GuardianReviewMode, computer_use: GuardianReviewMode,
code_mode: GuardianReviewMode, shell: GuardianReviewMode,
scores_per_cell: usize, scores_per_cell: usize,
expected_reviews: usize, expected_reviews: usize,
) -> Result<()> { ) -> Result<()> {
@@ -103,6 +121,26 @@ async fn model_guardian_policy_scores_code_mode_cells(
let (mcp_url, mcp_server) = let (mcp_url, mcp_server) =
start_mcp_server_with_tools(&["js"], /*sensitive_action*/ None).await?; start_mcp_server_with_tools(&["js"], /*sensitive_action*/ None).await?;
let codex_home = TempDir::new()?; let codex_home = TempDir::new()?;
if matches!(
policy_config,
PolicyConfig::RequiredModel | PolicyConfig::RequiredLegacy
) {
std::fs::write(
codex_home.path().join("requirements.toml"),
format!("[auto_review]\nrequired_on_models = [\"{MODEL}\"]\n"),
)?;
}
let legacy_config = match policy_config {
PolicyConfig::Model => String::new(),
PolicyConfig::RequiredModel => "\n[features.guardianv2]\nenabled = true".to_owned(),
PolicyConfig::Legacy | PolicyConfig::RequiredLegacy | PolicyConfig::LegacyDisabled => {
let enabled = !matches!(policy_config, PolicyConfig::LegacyDisabled);
let computer_use_only = shell != GuardianReviewMode::Adaptive;
format!(
"\n[features.guardianv2]\nenabled = {enabled}\n\n[features.guardianv2.review_scope]\ncomputer_use_only = {computer_use_only}"
)
}
};
let analytics_server = responses::start_mock_server().await; let analytics_server = responses::start_mock_server().await;
mount_analytics_capture(&analytics_server, codex_home.path()).await?; mount_analytics_capture(&analytics_server, codex_home.path()).await?;
MockResponsesConfig::new(&responses_url) MockResponsesConfig::new(&responses_url)
@@ -114,19 +152,24 @@ async fn model_guardian_policy_scores_code_mode_cells(
analytics_server.uri(), analytics_server.uri(),
)) ))
.with_extra_config(&format!( .with_extra_config(&format!(
"[mcp_servers.node_repl]\nurl = \"{mcp_url}/mcp\"\ndefault_tools_approval_mode = \"auto\"\n\n[analytics]\nenabled = true" "[mcp_servers.node_repl]\nurl = \"{mcp_url}/mcp\"\ndefault_tools_approval_mode = \"auto\"\n\n[analytics]\nenabled = true{legacy_config}"
)) ))
.enable_feature(Feature::GuardianApproval) .enable_feature(Feature::GuardianApproval)
.enable_feature(Feature::CodeModeOnly) .enable_feature(Feature::CodeModeOnly)
.write(codex_home.path())?; .write(codex_home.path())?;
let config = load_default_config_for_test(&codex_home).await; let config = load_default_config_for_test(&codex_home).await;
let mut model = codex_core::test_support::construct_model_info_offline(MODEL, &config); let mut model = codex_core::test_support::construct_model_info_offline(MODEL, &config);
model.guardian = Some(GuardianModelPolicy { model.guardian = match policy_config {
computer_use: Some(computer_use), PolicyConfig::Legacy | PolicyConfig::RequiredLegacy | PolicyConfig::LegacyDisabled => {
code_mode: Some(code_mode), model.node_repl_auto_review_required = computer_use == GuardianReviewMode::Adaptive;
shell: Some(GuardianReviewMode::Adaptive), None
..Default::default() }
}); PolicyConfig::Model | PolicyConfig::RequiredModel => Some(GuardianModelPolicy {
computer_use: Some(computer_use),
shell: Some(shell),
..Default::default()
}),
};
write_models_cache_with_models(codex_home.path(), vec![model]).await?; write_models_cache_with_models(codex_home.path(), vec![model]).await?;
let mut app_server = TestAppServer::builder() let mut app_server = TestAppServer::builder()
.with_codex_home(codex_home.path()) .with_codex_home(codex_home.path())
@@ -151,7 +194,13 @@ async fn model_guardian_policy_scores_code_mode_cells(
// Publish the first cell's scores before polling it and starting the second // Publish the first cell's scores before polling it and starting the second
// cell. Polling must not classify or invalidate that score. Keep second-cell // cell. Polling must not classify or invalidate that score. Keep second-cell
// classifiers pending so its approval must use the thread's latest score. // classifiers pending so its approval must use the thread's latest score.
wait_for_luna_request(&state, scores_per_cell - 1).await?; if scores_per_cell > 0 {
wait_for_luna_request(&state, scores_per_cell - 1).await?;
}
if scores_per_cell == 2 {
// Legacy all-tools mode reviews the first call before a score is available.
wait_for_guardian_reviews(&state, /*expected*/ 1).await?;
}
for completed_scores in 1..=scores_per_cell { for completed_scores in 1..=scores_per_cell {
state.allow_luna.notify_one(); state.allow_luna.notify_one();
timeout(TIMEOUT, async { timeout(TIMEOUT, async {
@@ -180,7 +229,9 @@ async fn model_guardian_policy_scores_code_mode_cells(
let completed: TurnCompletedNotification = let completed: TurnCompletedNotification =
timeout(TIMEOUT, app_server.read_notification("turn/completed")).await??; timeout(TIMEOUT, app_server.read_notification("turn/completed")).await??;
assert_eq!(completed.turn.status, TurnStatus::Completed); assert_eq!(completed.turn.status, TurnStatus::Completed);
wait_for_luna_request(&state, 2 * scores_per_cell - 1).await?; if scores_per_cell > 0 {
wait_for_luna_request(&state, 2 * scores_per_cell - 1).await?;
}
app_server.shutdown_gracefully().await?; app_server.shutdown_gracefully().await?;
let requests = state.luna_requests.lock().expect("score requests"); let requests = state.luna_requests.lock().expect("score requests");
assert_eq!( assert_eq!(
@@ -190,11 +241,13 @@ async fn model_guardian_policy_scores_code_mode_cells(
), ),
(2 * scores_per_cell, expected_reviews) (2 * scores_per_cell, expected_reviews)
); );
assert!(requests.iter().any(|request| { if scores_per_cell > 0 {
request assert!(requests.iter().any(|request| {
.to_string() request
.contains("const message = ['browser', '0'].join('-')") .to_string()
})); .contains("const message = ['browser', '0'].join('-')")
}));
}
mcp_server.abort(); mcp_server.abort();
responses_server.abort(); responses_server.abort();
Ok(()) Ok(())
@@ -1,5 +1,6 @@
//! Owns Guardian's category policy and translates legacy config at the boundary. //! Owns Guardian's category policy and translates legacy config at the boundary.
//! Scoring and approval consume the same policy; host-required reviews still take precedence. //! Scoring and approval consume the same policy; host-required reviews still take precedence.
//! Model policies review nested actions; legacy tool coverage remains unchanged.
use codex_extension_api::ToolPayload; use codex_extension_api::ToolPayload;
use codex_features::GuardianV2ReviewScopeConfigToml; use codex_features::GuardianV2ReviewScopeConfigToml;
@@ -44,7 +45,6 @@ impl GuardianPolicy {
categories: GuardianModelPolicy { categories: GuardianModelPolicy {
computer_use: Some(Adaptive), computer_use: Some(Adaptive),
shell: Some(other), shell: Some(other),
code_mode: Some(other),
file_changes: Some(other), file_changes: Some(other),
mcp: Some(other), mcp: Some(other),
network: Some(other), network: Some(other),
@@ -95,7 +95,6 @@ impl GuardianPolicy {
[ [
self.categories.computer_use, self.categories.computer_use,
self.categories.shell, self.categories.shell,
self.categories.code_mode,
self.categories.file_changes, self.categories.file_changes,
self.categories.mcp, self.categories.mcp,
self.categories.network, self.categories.network,
@@ -108,7 +107,6 @@ impl GuardianPolicy {
for mode in [ for mode in [
&mut self.categories.computer_use, &mut self.categories.computer_use,
&mut self.categories.shell, &mut self.categories.shell,
&mut self.categories.code_mode,
&mut self.categories.file_changes, &mut self.categories.file_changes,
&mut self.categories.mcp, &mut self.categories.mcp,
&mut self.categories.network, &mut self.categories.network,
@@ -580,6 +580,8 @@ fn computer_use_only_classification_recognizes_direct_and_code_mode_tools() {
(ToolName::namespaced("mcp__cua_repl__", "js"), true), (ToolName::namespaced("mcp__cua_repl__", "js"), true),
(ToolName::plain("mcp__node_repl__js"), true), (ToolName::plain("mcp__node_repl__js"), true),
(ToolName::plain("mcp__cua_repl__js"), true), (ToolName::plain("mcp__cua_repl__js"), true),
(ToolName::plain("exec"), false),
(ToolName::namespaced("mcp__ordinary__", "exec"), false),
(ToolName::namespaced("mcp__ordinary__", "js"), false), (ToolName::namespaced("mcp__ordinary__", "js"), false),
(ToolName::plain("read_file"), false), (ToolName::plain("read_file"), false),
(ToolName::plain("exec_command"), false), (ToolName::plain("exec_command"), false),
@@ -980,7 +982,7 @@ struct GuardianFailureFixture {
} }
#[tokio::test(flavor = "multi_thread", worker_threads = 2)] #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn disabled_code_mode_invalidates_cached_scores() -> Result<()> { async fn unscored_tools_invalidate_cached_scores() -> Result<()> {
skip_if_no_network!(Ok(())); skip_if_no_network!(Ok(()));
let fixture = GuardianFailureFixture::new().await?; let fixture = GuardianFailureFixture::new().await?;
let thread_store = fixture.test.codex.thread_extension_data(); let thread_store = fixture.test.codex.thread_extension_data();
@@ -1009,25 +1011,25 @@ async fn disabled_code_mode_invalidates_cached_scores() -> Result<()> {
), ),
before, before,
); );
fixture.score_tool(ToolName::plain("exec")).await; // A dynamic function named exec is not a Code Mode wrapper. An MCP tool
assert_eq!( // named wait is not a Code Mode poll. Both invalidate earlier scores.
( for (index, tool) in [
progress.latest_tool_call.load(Ordering::Acquire), ToolName::plain("exec"),
progress.latest_failed_tool_call.load(Ordering::Acquire), ToolName::namespaced("mcp__ordinary", "wait"),
), ]
(before.0 + 1, before.0 + 1), .into_iter()
); .enumerate()
// An MCP tool with the same name remains in the MCP category. {
fixture fixture.score_tool(tool).await;
.score_tool(ToolName::namespaced("mcp__ordinary", "wait")) let expected = before.0 + index + 1;
.await; assert_eq!(
assert_eq!( (
( progress.latest_tool_call.load(Ordering::Acquire),
progress.latest_tool_call.load(Ordering::Acquire), progress.latest_failed_tool_call.load(Ordering::Acquire),
progress.latest_failed_tool_call.load(Ordering::Acquire), ),
), (expected, expected),
(before.0 + 2, before.0 + 2), );
); }
Ok(()) Ok(())
} }
@@ -12,11 +12,13 @@ use codex_core::context::GuardianReviewEvidence;
use codex_core::context::NodeReplReviewEvidence; use codex_core::context::NodeReplReviewEvidence;
use codex_extension_api::ExtensionWarning; use codex_extension_api::ExtensionWarning;
use codex_extension_api::GuardianV2Enabled; use codex_extension_api::GuardianV2Enabled;
use codex_extension_api::ToolCallSource;
use codex_extension_api::ToolPayload; use codex_extension_api::ToolPayload;
use codex_extension_api::ToolStartInput; use codex_extension_api::ToolStartInput;
use codex_protocol::ThreadId; use codex_protocol::ThreadId;
use codex_protocol::config_types::ApprovalsReviewer; use codex_protocol::config_types::ApprovalsReviewer;
use codex_protocol::mcp::is_node_repl_backed_server; use codex_protocol::mcp::is_node_repl_backed_server;
use codex_protocol::openai_models::GuardianReviewMode;
use codex_protocol::openai_models::GuardianScope; use codex_protocol::openai_models::GuardianScope;
use codex_protocol::openai_models::ModelInfo; use codex_protocol::openai_models::ModelInfo;
use codex_protocol::security_risk::SecurityRiskScore; use codex_protocol::security_risk::SecurityRiskScore;
@@ -62,6 +64,17 @@ impl GuardianV2Extension {
let scope = mcp_server let scope = mcp_server
.map(GuardianScope::for_mcp_server) .map(GuardianScope::for_mcp_server)
.or_else(|| GuardianScope::for_tool(input.tool_name)); .or_else(|| GuardianScope::for_tool(input.tool_name));
// Model policies review nested actions; the Code Mode wrapper leaves their scores alone.
// The legacy all-tools policy still scores wrappers through `other_tools`.
if scope.is_none()
&& input.tool_name.is_default_namespace()
&& input.tool_name.name == "exec"
&& matches!(input.payload, ToolPayload::Custom { .. })
&& matches!(input.source, ToolCallSource::Direct)
&& policy.other_tools == GuardianReviewMode::Disabled
{
return;
}
if !policy.scores_tool(input.tool_name, input.payload, scope) { if !policy.scores_tool(input.tool_name, input.payload, scope) {
match policy.unscored_action { match policy.unscored_action {
UnscoredAction::Ignore => {} UnscoredAction::Ignore => {}
+1 -1
View File
@@ -403,7 +403,7 @@ const fn is_true(value: &bool) -> bool {
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)] #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct ModelInfo { pub struct ModelInfo {
/// Model-owned approval coverage. Absent preserves legacy settings; an empty map disables /// Model-owned approval coverage. Absent preserves legacy settings; an empty map disables
/// ordinary Guardian review. Keys are computer_use, shell, code_mode, file_changes, mcp, network, /// ordinary Guardian review. Keys are computer_use, shell, file_changes, mcp, network,
/// and permissions. This does not override mandatory safety or administrator requirements. /// and permissions. This does not override mandatory safety or administrator requirements.
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub guardian: Option<GuardianModelPolicy>, pub guardian: Option<GuardianModelPolicy>,
@@ -24,6 +24,7 @@ pub enum GuardianReviewMode {
} }
/// A complete model policy. Omitted scopes are disabled; unknown fields are ignored. /// A complete model policy. Omitted scopes are disabled; unknown fields are ignored.
/// Code Mode wrappers have no approval scope; their nested tools follow this policy.
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)] #[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq, TS, JsonSchema)]
pub struct GuardianModelPolicy { pub struct GuardianModelPolicy {
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
@@ -31,8 +32,6 @@ pub struct GuardianModelPolicy {
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub shell: Option<GuardianReviewMode>, pub shell: Option<GuardianReviewMode>,
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub code_mode: Option<GuardianReviewMode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub file_changes: Option<GuardianReviewMode>, pub file_changes: Option<GuardianReviewMode>,
#[serde(default, skip_serializing_if = "Option::is_none")] #[serde(default, skip_serializing_if = "Option::is_none")]
pub mcp: Option<GuardianReviewMode>, pub mcp: Option<GuardianReviewMode>,
@@ -47,7 +46,6 @@ impl GuardianModelPolicy {
match scope { match scope {
GuardianScope::ComputerUse => self.computer_use, GuardianScope::ComputerUse => self.computer_use,
GuardianScope::Shell => self.shell, GuardianScope::Shell => self.shell,
GuardianScope::CodeMode => self.code_mode,
GuardianScope::FileChanges => self.file_changes, GuardianScope::FileChanges => self.file_changes,
GuardianScope::Mcp => self.mcp, GuardianScope::Mcp => self.mcp,
GuardianScope::Network => self.network, GuardianScope::Network => self.network,
@@ -62,7 +60,6 @@ impl GuardianModelPolicy {
pub enum GuardianScope { pub enum GuardianScope {
ComputerUse, ComputerUse,
Shell, Shell,
CodeMode,
FileChanges, FileChanges,
Mcp, Mcp,
Network, Network,
@@ -97,7 +94,6 @@ impl GuardianScope {
"shell" | "shell_command" | "exec_command" | "write_stdin" | "execve" => { "shell" | "shell_command" | "exec_command" | "write_stdin" | "execve" => {
Some(Self::Shell) Some(Self::Shell)
} }
"exec" | "wait" => Some(Self::CodeMode),
"apply_patch" => Some(Self::FileChanges), "apply_patch" => Some(Self::FileChanges),
"request_permissions" => Some(Self::Permissions), "request_permissions" => Some(Self::Permissions),
_ => None, _ => None,
@@ -16,7 +16,7 @@ fn guardian_policy_is_optional_and_tolerates_future_config() -> anyhow::Result<(
wire["guardian"] = serde_json::json!({ wire["guardian"] = serde_json::json!({
"computer_use": "adaptive", "shell": "future_mode", "future_scope": {"version": 2}, "computer_use": "adaptive", "shell": "future_mode", "future_scope": {"version": 2},
"mcp": null, "code_mode": "adaptive", "mcp": null, "permissions": "adaptive",
}); });
let model: ModelInfo = serde_json::from_value(wire)?; let model: ModelInfo = serde_json::from_value(wire)?;
assert_eq!( assert_eq!(
@@ -24,7 +24,7 @@ fn guardian_policy_is_optional_and_tolerates_future_config() -> anyhow::Result<(
Some(GuardianModelPolicy { Some(GuardianModelPolicy {
computer_use: Some(GuardianReviewMode::Adaptive), computer_use: Some(GuardianReviewMode::Adaptive),
shell: Some(GuardianReviewMode::Unknown), shell: Some(GuardianReviewMode::Unknown),
code_mode: Some(GuardianReviewMode::Adaptive), permissions: Some(GuardianReviewMode::Adaptive),
..Default::default() ..Default::default()
}) })
); );