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,5 +1,6 @@
//! 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.
//! Model policies review nested actions; legacy tool coverage remains unchanged.
use codex_extension_api::ToolPayload;
use codex_features::GuardianV2ReviewScopeConfigToml;
@@ -44,7 +45,6 @@ impl GuardianPolicy {
categories: GuardianModelPolicy {
computer_use: Some(Adaptive),
shell: Some(other),
code_mode: Some(other),
file_changes: Some(other),
mcp: Some(other),
network: Some(other),
@@ -95,7 +95,6 @@ impl GuardianPolicy {
[
self.categories.computer_use,
self.categories.shell,
self.categories.code_mode,
self.categories.file_changes,
self.categories.mcp,
self.categories.network,
@@ -108,7 +107,6 @@ impl GuardianPolicy {
for mode in [
&mut self.categories.computer_use,
&mut self.categories.shell,
&mut self.categories.code_mode,
&mut self.categories.file_changes,
&mut self.categories.mcp,
&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::plain("mcp__node_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::plain("read_file"), false),
(ToolName::plain("exec_command"), false),
@@ -980,7 +982,7 @@ struct GuardianFailureFixture {
}
#[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(()));
let fixture = GuardianFailureFixture::new().await?;
let thread_store = fixture.test.codex.thread_extension_data();
@@ -1009,25 +1011,25 @@ async fn disabled_code_mode_invalidates_cached_scores() -> Result<()> {
),
before,
);
fixture.score_tool(ToolName::plain("exec")).await;
assert_eq!(
(
progress.latest_tool_call.load(Ordering::Acquire),
progress.latest_failed_tool_call.load(Ordering::Acquire),
),
(before.0 + 1, before.0 + 1),
);
// An MCP tool with the same name remains in the MCP category.
fixture
.score_tool(ToolName::namespaced("mcp__ordinary", "wait"))
.await;
assert_eq!(
(
progress.latest_tool_call.load(Ordering::Acquire),
progress.latest_failed_tool_call.load(Ordering::Acquire),
),
(before.0 + 2, before.0 + 2),
);
// A dynamic function named exec is not a Code Mode wrapper. An MCP tool
// named wait is not a Code Mode poll. Both invalidate earlier scores.
for (index, tool) in [
ToolName::plain("exec"),
ToolName::namespaced("mcp__ordinary", "wait"),
]
.into_iter()
.enumerate()
{
fixture.score_tool(tool).await;
let expected = before.0 + index + 1;
assert_eq!(
(
progress.latest_tool_call.load(Ordering::Acquire),
progress.latest_failed_tool_call.load(Ordering::Acquire),
),
(expected, expected),
);
}
Ok(())
}
@@ -12,11 +12,13 @@ use codex_core::context::GuardianReviewEvidence;
use codex_core::context::NodeReplReviewEvidence;
use codex_extension_api::ExtensionWarning;
use codex_extension_api::GuardianV2Enabled;
use codex_extension_api::ToolCallSource;
use codex_extension_api::ToolPayload;
use codex_extension_api::ToolStartInput;
use codex_protocol::ThreadId;
use codex_protocol::config_types::ApprovalsReviewer;
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::ModelInfo;
use codex_protocol::security_risk::SecurityRiskScore;
@@ -62,6 +64,17 @@ impl GuardianV2Extension {
let scope = mcp_server
.map(GuardianScope::for_mcp_server)
.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) {
match policy.unscored_action {
UnscoredAction::Ignore => {}