Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

Discount an approval's own code-mode wrapper from Guardian score lag (#43527)

jif1e66885a1616
diff --git a/codex-rs/core/src/guardian/decision.rs b/codex-rs/core/src/guardian/decision.rs
index 11f9b641f2..aa3d039cde 100644
--- a/codex-rs/core/src/guardian/decision.rs
+++ b/codex-rs/core/src/guardian/decision.rs
@@ -119,6 +119,20 @@ pub(crate) async fn decide_approval(
};
let input = ApprovalDecisionInput {
approval_id: &review_id,
+ tool_call_id: request
+ .request
+ .as_ref()
+ .ok()
+ .and_then(|request| match request {
+ // Stdin's target is the terminal launch; freshness belongs to this write.
+ GuardianApprovalRequest::WriteStdin { approval_id, .. } => {
+ Some(approval_id.as_str())
+ }
+ // Intercepts retain the launch ID, not the current triggering call.
+ #[cfg(unix)]
+ GuardianApprovalRequest::Execve { .. } => None,
+ _ => super::approval_request::guardian_request_target_item_id(request),
+ }),
action,
thread_id: session.thread_id,
thread_store: &session.services.thread_extension_data,
diff --git a/codex-rs/core/src/tools/lifecycle.rs b/codex-rs/core/src/tools/lifecycle.rs
index bea620e154..0d3a0ecf1a 100644
--- a/codex-rs/core/src/tools/lifecycle.rs
+++ b/codex-rs/core/src/tools/lifecycle.rs
@@ -29,6 +29,7 @@ pub(crate) async fn notify_tool_start(
let thread_store = &invocation.session.services.thread_extension_data;
let conversation_history = invocation.session.conversation_history_snapshot().await;
let root_turn_id = invocation.turn.turn_metadata_state.root_turn_id();
+ let originating_item_id = invocation.originating_item_id().await;
for contributor in contributors {
contributor
@@ -39,6 +40,7 @@ pub(crate) async fn notify_tool_start(
turn_id: invocation.turn.sub_id.as_str(),
root_turn_id: root_turn_id.as_deref(),
call_id: invocation.call_id.as_str(),
+ originating_item_id: originating_item_id.as_ref(),
tool_name: &invocation.tool_name,
mcp_tool,
payload: &invocation.payload,
diff --git a/codex-rs/core/tests/suite/code_mode.rs b/codex-rs/core/tests/suite/code_mode.rs
index b7c6b3c1b9..1d5cc70a0e 100644
--- a/codex-rs/core/tests/suite/code_mode.rs
+++ b/codex-rs/core/tests/suite/code_mode.rs
@@ -2375,12 +2375,19 @@ async fn code_mode_wait_timeout_reconnects_on_next_exec() -> Result<()> {
#[derive(Default)]
struct ResponseIdObserver {
response_ids: Mutex<Vec<(String, Option<String>)>>,
+ originating_items: Mutex<Vec<(String, Option<codex_protocol::ResponseItemId>)>>,
wait_started: tokio::sync::Notify,
}
impl ToolLifecycleContributor for ResponseIdObserver {
fn on_tool_start<'a>(&'a self, input: ToolStartInput<'a>) -> ToolLifecycleFuture<'a> {
Box::pin(async move {
+ if matches!(input.tool_name.name.as_str(), "exec" | "exec_command") {
+ self.originating_items.lock().unwrap().push((
+ input.tool_name.name.clone(),
+ input.originating_item_id.cloned(),
+ ));
+ }
if matches!(input.tool_name.name.as_str(), "exec_command" | "wait") {
self.response_ids.lock().unwrap().push((
input.tool_name.name.clone(),
@@ -2551,6 +2558,20 @@ text((await tools.exec_command({{cmd: "printf 'phase 3'"}})).output);
);
assert_eq!(text_item(&third_items, /*index*/ 1), "phase 3");
+ // Resuming the cell changes the response id, but its nested calls retain
+ // the original exec item's identity for request-local freshness accounting.
+ let originating_items = observer.originating_items.lock().unwrap();
+ let wrapper_item = originating_items[0]
+ .1
+ .clone()
+ .expect("wrapper item identity");
+ assert!(originating_items.len() >= 3);
+ assert!(
+ originating_items
+ .iter()
+ .all(|(_, item)| item.as_ref() == Some(&wrapper_item))
+ );
+
let observed = observer.response_ids.lock().unwrap();
let mut nested_response_ids = observed
.iter()
diff --git a/codex-rs/core/tests/suite/guardian_review.rs b/codex-rs/core/tests/suite/guardian_review.rs
index ea0a27b8b4..e7645e2d55 100644
--- a/codex-rs/core/tests/suite/guardian_review.rs
+++ b/codex-rs/core/tests/suite/guardian_review.rs
@@ -1568,9 +1568,10 @@ async fn guardian_timeout_rejects_tool_call_with_acting_model_instructions(
impl codex_extension_api::ApprovalReviewContributor for TimedOutReviewContributor {
fn decide<'a>(
&'a self,
- _input: &'a codex_extension_api::ApprovalDecisionInput<'_>,
+ input: &'a codex_extension_api::ApprovalDecisionInput<'_>,
) -> codex_extension_api::ExtensionFuture<'a, Option<codex_extension_api::ApprovalDecision>>
{
+ assert_eq!(input.tool_call_id, Some("exec-call-timed-out"));
Box::pin(async {
Some(codex_extension_api::ApprovalDecision::Reviewed(
ReviewDecision::TimedOut,
diff --git a/codex-rs/core/tests/suite/unified_exec_stdin_approval.rs b/codex-rs/core/tests/suite/unified_exec_stdin_approval.rs
index dce26e37f5..d602b03a49 100644
--- a/codex-rs/core/tests/suite/unified_exec_stdin_approval.rs
+++ b/codex-rs/core/tests/suite/unified_exec_stdin_approval.rs
@@ -2,7 +2,9 @@
use anyhow::Result;
use codex_core::TurnInputRequest;
+use codex_core::config::Config;
use codex_core::config::Constrained;
+use codex_extension_api::ExtensionRegistryBuilder;
use codex_features::Feature;
use codex_protocol::approvals::ExecApprovalKind;
use codex_protocol::models::AdditionalPermissionProfile;
@@ -26,6 +28,7 @@ use core_test_support::responses::sse;
use core_test_support::skip_if_no_network;
use core_test_support::skip_if_sandbox;
use core_test_support::skip_if_target_windows;
+use core_test_support::test_codex::TestCodexBuilder;
use core_test_support::test_codex::TestCodexHarness;
use core_test_support::test_codex::test_codex;
use core_test_support::wait_for_event;
@@ -33,6 +36,8 @@ use core_test_support::wait_for_event_match;
use pretty_assertions::assert_eq;
use serde_json::Value;
use serde_json::json;
+use std::sync::Arc;
+use std::sync::Mutex;
fn tool_response(id: &str, tool: &str, args: Value) -> String {
sse(vec![
@@ -41,8 +46,8 @@ fn tool_response(id: &str, tool: &str, args: Value) -> String {
])
}
-async fn harness() -> Result<TestCodexHarness> {
- TestCodexHarness::with_auto_env_builder(test_codex().with_config(|config| {
+async fn harness(builder: TestCodexBuilder) -> Result<TestCodexHarness> {
+ TestCodexHarness::with_auto_env_builder(builder.with_config(|config| {
for feature in [
Feature::WriteStdinApproval,
Feature::ExecPermissionApprovals,
@@ -67,7 +72,7 @@ async fn stdin_reviews_retained_grants_after_turn_permissions_expire() -> Result
skip_if_target_windows!(Ok(()), "uses a POSIX interactive shell");
skip_if_no_network!(Ok(()));
skip_if_sandbox!(Ok(()));
- let harness = harness().await?;
+ let harness = harness(test_codex()).await?;
let test = harness.test();
harness.create_dir_all("allowed").await?;
let directory = test
@@ -193,7 +198,27 @@ async fn strict_stdin_review_reaches_guardian_with_sandbox_prompts_disabled() ->
skip_if_target_windows!(Ok(()), "uses a POSIX interactive shell");
skip_if_no_network!(Ok(()));
skip_if_sandbox!(Ok(()));
- let harness = harness().await?;
+ #[derive(Default)]
+ struct ReviewedCalls(Mutex<Vec<String>>);
+
+ impl codex_extension_api::ApprovalReviewContributor for ReviewedCalls {
+ fn decide<'a>(
+ &'a self,
+ input: &'a codex_extension_api::ApprovalDecisionInput<'_>,
+ ) -> codex_extension_api::ExtensionFuture<'a, Option<codex_extension_api::ApprovalDecision>>
+ {
+ self.0
+ .lock()
+ .unwrap()
+ .push(input.tool_call_id.expect("reviewed call id").to_owned());
+ Box::pin(async { None })
+ }
+ }
+
+ let calls = Arc::new(ReviewedCalls::default());
+ let mut extensions = ExtensionRegistryBuilder::<Config>::new();
+ extensions.approval_review_contributor(calls.clone());
+ let harness = harness(test_codex().with_extensions(Arc::new(extensions.build()))).await?;
let test = harness.test();
let opened = mount_sse_sequence(
harness.server(),
@@ -269,6 +294,10 @@ async fn strict_stdin_review_reaches_guardian_with_sandbox_prompts_disabled() ->
})
.await;
let requests = writes.requests();
+ assert_eq!(
+ *calls.0.lock().unwrap(),
+ vec!["strict", "denied", "allowed"]
+ );
assert_eq!(
requests
.iter()
diff --git a/codex-rs/core/tests/suite/unified_exec_zsh_fork_approvals.rs b/codex-rs/core/tests/suite/unified_exec_zsh_fork_approvals.rs
index cbef67fbb0..d8c8a9677b 100644
--- a/codex-rs/core/tests/suite/unified_exec_zsh_fork_approvals.rs
+++ b/codex-rs/core/tests/suite/unified_exec_zsh_fork_approvals.rs
@@ -4,7 +4,9 @@ use codex_config::permissions_toml::FilesystemPermissionToml;
use codex_config::permissions_toml::PermissionProfileToml;
use codex_config::types::ApprovalsReviewer;
use codex_core::TurnInputRequest;
+use codex_core::config::Config;
use codex_core::sandboxing::SandboxPermissions;
+use codex_extension_api::ExtensionRegistryBuilder;
use codex_features::Feature;
use codex_protocol::config_types::CollaborationMode;
use codex_protocol::config_types::ModeKind;
@@ -50,6 +52,7 @@ use serde_json::Value;
use serde_json::json;
use std::fs;
use std::path::Path;
+use std::sync::Arc;
use std::time::Duration;
use toml_edit::Key as TomlKey;
use wiremock::MockServer;
@@ -649,8 +652,26 @@ where
return Ok(None);
};
+ struct ExecveIdentityCheck;
+
+ impl codex_extension_api::ApprovalReviewContributor for ExecveIdentityCheck {
+ fn decide<'a>(
+ &'a self,
+ input: &'a codex_extension_api::ApprovalDecisionInput<'_>,
+ ) -> codex_extension_api::ExtensionFuture<'a, Option<codex_extension_api::ApprovalDecision>>
+ {
+ if input.action.get("program").is_some() {
+ assert_eq!(input.tool_call_id, None);
+ }
+ Box::pin(async { None })
+ }
+ }
+
+ let mut extensions = ExtensionRegistryBuilder::<Config>::new();
+ extensions.approval_review_contributor(Arc::new(ExecveIdentityCheck));
let server = start_mock_server().await;
let test = zsh_fork_test_builder(runtime, approval_policy)
+ .with_extensions(Arc::new(extensions.build()))
.with_pre_build_hook(pre_build_hook)
.with_config(move |config| {
config
diff --git a/codex-rs/ext/extension-api/src/contributors/approval_review.rs b/codex-rs/ext/extension-api/src/contributors/approval_review.rs
index fe1c342b26..218998c61e 100644
--- a/codex-rs/ext/extension-api/src/contributors/approval_review.rs
+++ b/codex-rs/ext/extension-api/src/contributors/approval_review.rs
@@ -74,6 +74,8 @@ pub trait SynchronousApprovalReviewer: Send + Sync {
/// Inputs to Guardian's policy choice. Conversation and scores stay thread-owned.
pub struct ApprovalDecisionInput<'a> {
pub approval_id: &'a str,
+ /// Host tool invocation being approved, absent for approvals without a tool call.
+ pub tool_call_id: Option<&'a str>,
pub action: &'a serde_json::Value,
pub thread_id: ThreadId,
pub thread_store: &'a ExtensionData,
diff --git a/codex-rs/ext/extension-api/src/contributors/tool_lifecycle.rs b/codex-rs/ext/extension-api/src/contributors/tool_lifecycle.rs
index 7842805c83..eb8c8154d4 100644
--- a/codex-rs/ext/extension-api/src/contributors/tool_lifecycle.rs
+++ b/codex-rs/ext/extension-api/src/contributors/tool_lifecycle.rs
@@ -127,6 +127,9 @@ pub struct ToolStartInput<'a> {
pub root_turn_id: Option<&'a str>,
/// Model-visible tool call id.
pub call_id: &'a str,
+ /// Responses item that issued this call or started its code-mode cell.
+ /// Hosts preserve the original wrapper identity across yields and waits.
+ pub originating_item_id: Option<&'a codex_protocol::ResponseItemId>,
/// Tool name as routed by the host.
pub tool_name: &'a ToolName,
/// Read-only metadata and provenance from the exact MCP call that will execute.
diff --git a/codex-rs/ext/extension-api/tests/registry.rs b/codex-rs/ext/extension-api/tests/registry.rs
index c354628382..b58f45384a 100644
--- a/codex-rs/ext/extension-api/tests/registry.rs
+++ b/codex-rs/ext/extension-api/tests/registry.rs
@@ -205,6 +205,7 @@ async fn build_round_trips_every_contributor_category() {
let thread_store = ExtensionData::new("thread");
let input = codex_extension_api::ApprovalDecisionInput {
approval_id: "approval-1",
+ tool_call_id: None,
action: &serde_json::Value::Null,
thread_id: codex_protocol::ThreadId::new(),
thread_store: &thread_store,
diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/approval.rs b/codex-rs/ext/guardian-v2/src/async_scorer/approval.rs
index fb96cbcac4..9f7ebb085f 100644
--- a/codex-rs/ext/guardian-v2/src/async_scorer/approval.rs
+++ b/codex-rs/ext/guardian-v2/src/async_scorer/approval.rs
@@ -183,7 +183,12 @@ async fn cached_evidence(
let lag = progress
.latest_tool_call
.load(Ordering::Acquire)
- .saturating_sub(latest_scored);
+ .saturating_sub(latest_scored)
+ .saturating_sub(
+ progress
+ .wrapper_lag
+ .discount(input.tool_call_id, latest_scored),
+ );
if let Some(metrics) = metrics {
metrics.histogram(
TOOL_CALL_LAG_METRIC,
diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs b/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs
index 08659e95a4..53936df385 100644
--- a/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs
+++ b/codex-rs/ext/guardian-v2/src/async_scorer/extension.rs
@@ -64,6 +64,7 @@ use super::truncation::ClassificationTruncations;
use super::trusted_skills::TrustedSkillInvocations;
use super::trusted_skills::TrustedSkillRoots;
use super::trusted_tools::trusted_tool_context;
+use super::wrapper_lag::WrapperLag;
enum ClassificationOutcome {
Scored,
@@ -72,6 +73,7 @@ enum ClassificationOutcome {
#[derive(Default)]
pub(super) struct GuardianV2ScoreProgress {
+ pub(super) wrapper_lag: WrapperLag,
pub(super) latest_tool_call: AtomicUsize,
// Setup and reset calls must not consume the first JS execution allowance.
pub(super) js_executions: AtomicUsize,
@@ -276,15 +278,18 @@ impl GuardianV2Extension {
match policy.unscored_action {
UnscoredAction::Ignore => {}
UnscoredAction::AgeScore => {
- score_progress
+ let index = score_progress
.latest_tool_call
- .fetch_add(/*val*/ 1, Ordering::Relaxed);
+ .fetch_add(/*val*/ 1, Ordering::Relaxed)
+ .saturating_add(/*rhs*/ 1);
+ score_progress.wrapper_lag.record(&input, index);
}
UnscoredAction::InvalidateScore => {
let index = score_progress
.latest_tool_call
.fetch_add(/*val*/ 1, Ordering::Relaxed)
.saturating_add(/*rhs*/ 1);
+ score_progress.wrapper_lag.record(&input, index);
score_progress
.latest_failed_tool_call
.fetch_max(index, Ordering::Release);
@@ -307,6 +312,7 @@ impl GuardianV2Extension {
.latest_tool_call
.fetch_add(/*val*/ 1, Ordering::Relaxed)
.saturating_add(/*rhs*/ 1);
+ score_progress.wrapper_lag.record(&input, tool_call_index);
let event_sink = Arc::clone(&self.event_sink);
let thread_id = input.thread_store.level_id().to_owned();
let turn_id = input.turn_id.to_owned();
diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs b/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs
index 19be463a6b..e3a7cc6314 100644
--- a/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs
+++ b/codex-rs/ext/guardian-v2/src/async_scorer/extension_tests.rs
@@ -270,6 +270,7 @@ async fn installed_extension_uses_http_after_warm_socket_auth_expires() -> Resul
turn_id: "turn-1",
root_turn_id: None,
call_id,
+ originating_item_id: None,
tool_name: &tool_name,
mcp_tool: None,
payload: &payload,
@@ -543,6 +544,7 @@ async fn sandboxed_shell_classification_respects_review_scope() -> Result<()> {
turn_id: "turn-1",
root_turn_id: None,
call_id: "call-2",
+ originating_item_id: None,
tool_name: &tool_name,
mcp_tool: None,
payload: &payload,
@@ -634,6 +636,7 @@ async fn computer_use_only_scores_cannot_approve_other_actions() -> Result<()> {
turn_id: "turn-1",
root_turn_id: None,
call_id: "ordinary-call",
+ originating_item_id: None,
tool_name: &ordinary_tool,
mcp_tool: None,
payload: &payload,
@@ -946,6 +949,7 @@ async fn sample_configured_conversation_history_with_source(
turn_id: "turn-1",
root_turn_id: Some("root-turn"),
call_id: "call-1",
+ originating_item_id: None,
tool_name: &tool_name,
mcp_tool: None,
payload: &tool_payload,
@@ -1080,6 +1084,7 @@ impl GuardianFailureFixture {
turn_id: "turn-1",
root_turn_id: None,
call_id: "call-1",
+ originating_item_id: None,
tool_name: &tool_name,
mcp_tool: None,
payload: &payload,
@@ -2248,6 +2253,7 @@ async fn contributor_skips_required_models_in_standard_scope() -> Result<()> {
turn_id: "turn-1",
root_turn_id: None,
call_id: "protected.md",
+ originating_item_id: None,
tool_name: &tool_name,
mcp_tool: None,
payload: &payload,
@@ -2505,6 +2511,7 @@ async fn contributor_counts_failed_thread_lookups_toward_score_lag() -> Result<(
turn_id: "turn-1",
root_turn_id: None,
call_id: "missing.md",
+ originating_item_id: None,
tool_name: &tool_name,
mcp_tool: None,
payload: &payload,
@@ -2997,6 +3004,7 @@ async fn assert_parent_compaction_reuse(thread_context_enabled: bool) -> Result<
turn_id: "turn-1",
root_turn_id: None,
call_id: "call-1",
+ originating_item_id: None,
tool_name: &tool_name,
mcp_tool: None,
payload: &tool_payload,
@@ -3072,6 +3080,7 @@ async fn assert_parent_compaction_reuse(thread_context_enabled: bool) -> Result<
turn_id: "turn-1",
root_turn_id: None,
call_id: "call-2",
+ originating_item_id: None,
tool_name: &tool_name,
mcp_tool: None,
payload: &tool_payload,
@@ -3249,6 +3258,113 @@ impl codex_extension_api::SynchronousApprovalReviewer for CacheMiss {
}
}
+#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
+async fn cached_approval_discounts_only_its_own_unscored_wrapper() -> Result<()> {
+ skip_if_no_network!(Ok(()));
+ let fixture = GuardianFailureFixture::new().await?;
+ let store = fixture.test.codex.thread_extension_data();
+ let progress = store.get::<GuardianV2ScoreProgress>().unwrap();
+ let mut score = store.get::<SecurityRiskScore>().unwrap().as_ref().clone();
+ score.scores.insert("action_risk".to_owned(), 0.0);
+ store.insert(score);
+ // Hold the cached score fixed while advancing real tool-start metadata.
+ let start = |call_id: &str, origin: &ResponseItemId, source: ToolCallSource| {
+ let tool_name = match source {
+ ToolCallSource::Direct => ToolName::plain("exec"),
+ ToolCallSource::CodeMode { .. } => ToolName::namespaced("mcp__example", "read"),
+ };
+ let payload = match source {
+ ToolCallSource::Direct => ToolPayload::Custom {
+ input: String::new(),
+ },
+ ToolCallSource::CodeMode { .. } => ToolPayload::Function {
+ arguments: "{}".to_owned(),
+ },
+ };
+ let index = progress
+ .latest_tool_call
+ .fetch_add(/*val*/ 1, Ordering::Relaxed)
+ + 1;
+ progress.wrapper_lag.record(
+ &ToolStartInput {
+ session_store: &fixture.session_store,
+ thread_store: store,
+ turn_store: &fixture.session_store,
+ turn_id: "turn",
+ root_turn_id: None,
+ call_id,
+ originating_item_id: Some(origin),
+ tool_name: &tool_name,
+ mcp_tool: None,
+ payload: &payload,
+ conversation_history: Arc::new(TestConversationHistory(Vec::new())),
+ source,
+ },
+ index,
+ );
+ index
+ };
+ let approve = async |call_id: &str| {
+ cached_approval(
+ &fixture.registry,
+ store,
+ &json!({"tool": "mcp_tool_call", "server": "example", "id": call_id}).to_string(),
+ /*metrics*/ None,
+ )
+ .await
+ };
+ let nested = ToolCallSource::CodeMode {
+ cell_id: "cell".to_owned(),
+ runtime_tool_call_id: "nested".to_owned(),
+ };
+ let origin = ResponseItemId::from_server("wrapper".to_owned());
+ let wrapper = start("wrapper", &origin, ToolCallSource::Direct);
+ for (call_id, expected) in [
+ ("first", Some(ReviewDecision::Approved)),
+ ("second", Some(ReviewDecision::Approved)),
+ ("third", None),
+ ] {
+ start(call_id, &origin, nested.clone());
+ assert_eq!(approve(call_id).await, expected);
+ }
+ // A score covering the wrapper must not receive another discount.
+ progress
+ .latest_scored_tool_call
+ .store(wrapper, Ordering::Release);
+ assert_eq!(approve("third").await, None);
+ progress
+ .latest_scored_tool_call
+ .store(wrapper + 3, Ordering::Release);
+ let output = start("output-only", &origin, ToolCallSource::Direct);
+ let other = ResponseItemId::from_server("other-wrapper".to_owned());
+ start("other-wrapper", &other, ToolCallSource::Direct);
+ start("other-first", &other, nested.clone());
+ assert_eq!(approve("other-first").await, Some(ReviewDecision::Approved));
+ assert_eq!(approve("unknown").await, None);
+ start("other-second", &other, nested.clone());
+ assert_eq!(approve("other-second").await, None);
+ // Covering a different wrapper still leaves this call's parent in its lag.
+ progress
+ .latest_scored_tool_call
+ .store(output, Ordering::Release);
+ assert_eq!(
+ approve("other-second").await,
+ Some(ReviewDecision::Approved)
+ );
+ // Evicted provenance falls back to the full lag.
+ for _ in 0..300 {
+ start("output-only", &origin, ToolCallSource::Direct);
+ }
+ start("late-child", &other, nested);
+ assert_eq!(
+ progress
+ .wrapper_lag
+ .discount(Some("late-child"), /*latest_scored*/ 0),
+ 0
+ );
+ Ok(())
+}
+
/// Exercises decision routing; a fresh review is observed as a cache miss.
async fn cached_approval(
registry: &codex_extension_api::ExtensionRegistry<Config>,
@@ -3275,6 +3391,7 @@ async fn cached_approval(
};
let input = codex_extension_api::ApprovalDecisionInput {
approval_id: "cache-probe",
+ tool_call_id: action.get("id").and_then(serde_json::Value::as_str),
action: &action,
thread_id: codex_protocol::ThreadId::from_string(store.level_id()).unwrap(),
thread_store: store,
diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs b/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs
index 82ce4d2036..5c404d2c92 100644
--- a/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs
+++ b/codex-rs/ext/guardian-v2/src/async_scorer/mod.rs
@@ -12,6 +12,7 @@ mod transcript;
mod truncation;
mod trusted_skills;
mod trusted_tools;
+mod wrapper_lag;
pub(crate) use config::DEFAULT_MODEL_CONTEXT_ITEM_TOKENS;
pub(crate) use config::GuardianV2Config;
diff --git a/codex-rs/ext/guardian-v2/src/async_scorer/wrapper_lag.rs b/codex-rs/ext/guardian-v2/src/async_scorer/wrapper_lag.rs
new file mode 100644
index 0000000000..bde42597b3
--- /dev/null
+++ b/codex-rs/ext/guardian-v2/src/async_scorer/wrapper_lag.rs
@@ -0,0 +1,75 @@
+//! Discounts only an approval's own unscored code-mode wrapper. Missing provenance
+//! keeps the full lag; the bounded history never changes scoring or failure order.
+
+use std::collections::VecDeque;
+use std::sync::Mutex;
+
+use codex_extension_api::ToolCallSource;
+use codex_extension_api::ToolPayload;
+use codex_extension_api::ToolStartInput;
+use codex_protocol::ResponseItemId;
+
+struct ToolStart {
+ call_id: String,
+ index: usize,
+ wrapper_item_id: Option<ResponseItemId>,
+ parent_wrapper_index: Option<usize>,
+}
+
+#[derive(Default)]
+pub(super) struct WrapperLag(Mutex<VecDeque<ToolStart>>);
+
+impl WrapperLag {
+ pub(super) fn record(&self, input: &ToolStartInput<'_>, index: usize) {
+ let mut starts = self
+ .0
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ let parent_wrapper_index = match &input.source {
+ ToolCallSource::CodeMode { .. } => input.originating_item_id.and_then(|item_id| {
+ starts
+ .iter()
+ .rev()
+ .find(|start| start.wrapper_item_id.as_ref() == Some(item_id))
+ .map(|start| start.index)
+ }),
+ ToolCallSource::Direct => None,
+ };
+ let wrapper_item_id = if input.tool_name.is_default_namespace()
+ && input.tool_name.name == "exec"
+ && matches!(input.payload, ToolPayload::Custom { .. })
+ && matches!(input.source, ToolCallSource::Direct)
+ {
+ input.originating_item_id.cloned()
+ } else {
+ None
+ };
+ if starts.len() == 256 {
+ starts.pop_front();
+ }
+ starts.push_back(ToolStart {
+ call_id: input.call_id.to_owned(),
+ index,
+ wrapper_item_id,
+ parent_wrapper_index,
+ });
+ }
+
+ pub(super) fn discount(&self, call_id: Option<&str>, latest_scored: usize) -> usize {
+ let starts = self
+ .0
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ usize::from(
+ call_id
+ .and_then(|call_id| {
+ starts
+ .iter()
+ .rev()
+ .find(|start| start.call_id == call_id)
+ .and_then(|start| start.parent_wrapper_index)
+ })
+ .is_some_and(|parent| parent > latest_scored),
+ )
+ }
+}