From 62131c07f00da3d83c22089f96c584704adacb4d Mon Sep 17 00:00:00 2001 From: Roy Han Date: Mon, 16 Mar 2026 12:28:14 -0700 Subject: [PATCH] initial draft --- codex-rs/core/src/agent/control_tests.rs | 3 + codex-rs/core/src/arc_monitor_tests.rs | 2 + codex-rs/core/src/client_common.rs | 1 + codex-rs/core/src/client_common_tests.rs | 4 + codex-rs/core/src/codex.rs | 158 ++++++++++++++++++ codex-rs/core/src/codex_delegate_tests.rs | 1 + codex-rs/core/src/codex_tests.rs | 132 +++++++++++++++ .../core/src/context_manager/history_tests.rs | 28 ++++ codex-rs/core/src/guardian/tests.rs | 1 + codex-rs/core/src/rollout/truncation_tests.rs | 1 + codex-rs/core/src/state/turn.rs | 39 +++++ codex-rs/core/src/thread_manager_tests.rs | 1 + codex-rs/core/src/tools/router_tests.rs | 1 + codex-rs/core/src/turn_timing_tests.rs | 2 + codex-rs/core/tests/suite/client.rs | 6 + codex-rs/features/src/tests.rs | 1 - codex-rs/protocol/src/models.rs | 35 ++++ 17 files changed, 415 insertions(+), 1 deletion(-) diff --git a/codex-rs/core/src/agent/control_tests.rs b/codex-rs/core/src/agent/control_tests.rs index 24344db719..4f54ed28d1 100644 --- a/codex-rs/core/src/agent/control_tests.rs +++ b/codex-rs/core/src/agent/control_tests.rs @@ -428,6 +428,7 @@ async fn spawn_agent_can_fork_parent_thread_history() { namespace: None, arguments: "{}".to_string(), call_id: parent_spawn_call_id.clone(), + metadata: None, }; parent_thread .codex @@ -511,6 +512,7 @@ async fn spawn_agent_fork_injects_output_for_parent_spawn_call() { namespace: None, arguments: "{}".to_string(), call_id: parent_spawn_call_id.clone(), + metadata: None, }; parent_thread .codex @@ -587,6 +589,7 @@ async fn spawn_agent_fork_flushes_parent_rollout_before_loading_history() { namespace: None, arguments: "{}".to_string(), call_id: parent_spawn_call_id.clone(), + metadata: None, }; parent_thread .codex diff --git a/codex-rs/core/src/arc_monitor_tests.rs b/codex-rs/core/src/arc_monitor_tests.rs index cd0cb80b14..0a6fd3caa2 100644 --- a/codex-rs/core/src/arc_monitor_tests.rs +++ b/codex-rs/core/src/arc_monitor_tests.rs @@ -133,6 +133,7 @@ async fn build_arc_monitor_request_includes_relevant_history_and_null_policies() namespace: None, arguments: "{\"old\":true}".to_string(), call_id: "call_old".to_string(), + metadata: None, }], &turn_context, ) @@ -161,6 +162,7 @@ async fn build_arc_monitor_request_includes_relevant_history_and_null_policies() env: None, user: None, }), + metadata: None, }], &turn_context, ) diff --git a/codex-rs/core/src/client_common.rs b/codex-rs/core/src/client_common.rs index 33a1f535c6..ea6a209e2e 100644 --- a/codex-rs/core/src/client_common.rs +++ b/codex-rs/core/src/client_common.rs @@ -79,6 +79,7 @@ fn reserialize_shell_outputs(items: &mut [ResponseItem]) { call_id, name, input: _, + .. } => { if name == "apply_patch" { shell_call_ids.insert(call_id.clone()); diff --git a/codex-rs/core/src/client_common_tests.rs b/codex-rs/core/src/client_common_tests.rs index 0ce7caf273..351ac8fbc3 100644 --- a/codex-rs/core/src/client_common_tests.rs +++ b/codex-rs/core/src/client_common_tests.rs @@ -150,6 +150,7 @@ fn reserializes_shell_outputs_for_function_and_custom_tool_calls() { namespace: None, arguments: "{}".to_string(), call_id: "call-1".to_string(), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "call-1".to_string(), @@ -161,6 +162,7 @@ fn reserializes_shell_outputs_for_function_and_custom_tool_calls() { call_id: "call-2".to_string(), name: "apply_patch".to_string(), input: "*** Begin Patch".to_string(), + metadata: None, }, ResponseItem::CustomToolCallOutput { call_id: "call-2".to_string(), @@ -180,6 +182,7 @@ fn reserializes_shell_outputs_for_function_and_custom_tool_calls() { namespace: None, arguments: "{}".to_string(), call_id: "call-1".to_string(), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "call-1".to_string(), @@ -191,6 +194,7 @@ fn reserializes_shell_outputs_for_function_and_custom_tool_calls() { call_id: "call-2".to_string(), name: "apply_patch".to_string(), input: "*** Begin Patch".to_string(), + metadata: None, }, ResponseItem::CustomToolCallOutput { call_id: "call-2".to_string(), diff --git a/codex-rs/core/src/codex.rs b/codex-rs/core/src/codex.rs index 254d5708bb..44759eb7d0 100644 --- a/codex-rs/core/src/codex.rs +++ b/codex-rs/core/src/codex.rs @@ -333,6 +333,8 @@ use codex_protocol::models::ContentItem; use codex_protocol::models::DeveloperInstructions; use codex_protocol::models::ResponseInputItem; use codex_protocol::models::ResponseItem; +use codex_protocol::models::ResponseItemMetadata; +use codex_protocol::models::ReviewDecisionMetadata; use codex_protocol::openai_models::ReasoningEffort as ReasoningEffortConfig; use codex_protocol::protocol::CodexErrorInfo; use codex_protocol::protocol::InitialHistory; @@ -1003,6 +1005,91 @@ fn local_time_context() -> (String, String) { } } +fn review_decision_to_metadata(decision: &ReviewDecision) -> ReviewDecisionMetadata { + match decision { + ReviewDecision::Approved => ReviewDecisionMetadata::Approved, + ReviewDecision::ApprovedExecpolicyAmendment { .. } => { + ReviewDecisionMetadata::ApprovedWithAmendment + } + ReviewDecision::ApprovedForSession => ReviewDecisionMetadata::ApprovedForSession, + ReviewDecision::NetworkPolicyAmendment { + network_policy_amendment, + } => match network_policy_amendment.action { + codex_protocol::protocol::NetworkPolicyRuleAction::Allow => { + ReviewDecisionMetadata::ApprovedWithNetworkPolicyAllow + } + codex_protocol::protocol::NetworkPolicyRuleAction::Deny => { + ReviewDecisionMetadata::DeniedWithNetworkPolicyDeny + } + }, + ReviewDecision::Denied => ReviewDecisionMetadata::Denied, + ReviewDecision::Abort => ReviewDecisionMetadata::Abort, + } +} + +fn response_item_tool_call_id(item: &ResponseItem) -> Option<&str> { + match item { + ResponseItem::LocalShellCall { + call_id: Some(call_id), + .. + } => Some(call_id), + ResponseItem::FunctionCall { call_id, .. } => Some(call_id), + ResponseItem::CustomToolCall { call_id, .. } => Some(call_id), + _ => None, + } +} + +fn stamp_tool_metadata_on_response_item( + item: ResponseItem, + metadata: ResponseItemMetadata, +) -> ResponseItem { + match item { + ResponseItem::LocalShellCall { + id, + call_id, + status, + action, + .. + } => ResponseItem::LocalShellCall { + id, + call_id, + status, + action, + metadata: Some(metadata), + }, + ResponseItem::FunctionCall { + id, + name, + namespace, + arguments, + call_id, + .. + } => ResponseItem::FunctionCall { + id, + name, + namespace, + arguments, + call_id, + metadata: Some(metadata), + }, + ResponseItem::CustomToolCall { + id, + status, + call_id, + name, + input, + .. + } => ResponseItem::CustomToolCall { + id, + status, + call_id, + name, + input, + metadata: Some(metadata), + }, + other => other, + } +} #[derive(Clone)] pub(crate) struct SessionConfiguration { /// Provider identifier ("openai", "openrouter", ...). @@ -2869,6 +2956,10 @@ impl Session { match active.as_mut() { Some(at) => { let mut ts = at.turn_state.lock().await; + ts.insert_pending_approval_call_id( + effective_approval_id.clone(), + call_id.clone(), + ); ts.insert_pending_approval(effective_approval_id.clone(), tx_approve) } None => None, @@ -2934,6 +3025,7 @@ impl Session { match active.as_mut() { Some(at) => { let mut ts = at.turn_state.lock().await; + ts.insert_pending_approval_call_id(approval_id.clone(), call_id.clone()); ts.insert_pending_approval(approval_id.clone(), tx_approve) } None => None, @@ -3219,7 +3311,20 @@ impl Session { } } + pub async fn record_approval_outcome(&self, approval_id: &str, decision: &ReviewDecision) { + let mut active = self.active_turn.lock().await; + let Some(at) = active.as_mut() else { + return; + }; + let mut ts = at.turn_state.lock().await; + let call_id = ts + .remove_pending_approval_call_id(approval_id) + .unwrap_or_else(|| approval_id.to_string()); + ts.record_approval_outcome(call_id, review_decision_to_metadata(decision)); + } + pub async fn notify_approval(&self, approval_id: &str, decision: ReviewDecision) { + self.record_approval_outcome(approval_id, &decision).await; let entry = { let mut active = self.active_turn.lock().await; match active.as_mut() { @@ -3240,6 +3345,53 @@ impl Session { } } + async fn stamp_tool_approval_metadata( + &self, + _turn_context: &TurnContext, + response_item: ResponseItem, + ) -> ResponseItem { + if !self.enabled(Feature::ItemMetadata) { + return response_item; + } + let Some(call_id) = response_item_tool_call_id(&response_item) else { + return response_item; + }; + + let (outcome, has_pending_approval) = { + let active = self.active_turn.lock().await; + let Some(at) = active.as_ref() else { + return response_item; + }; + let ts = at.turn_state.lock().await; + let outcome = ts.approval_outcome_for_call_id(call_id); + let has_pending_approval = ts.has_pending_approval_for_call_id(call_id); + (outcome, has_pending_approval) + }; + + let mut metadata = match &response_item { + ResponseItem::LocalShellCall { metadata, .. } + | ResponseItem::FunctionCall { metadata, .. } + | ResponseItem::CustomToolCall { metadata, .. } => metadata.clone().unwrap_or_default(), + _ => return response_item, + }; + + match outcome { + Some(review_decision) => { + metadata.is_tool_call_escalated = Some(true); + metadata.review_decision = Some(review_decision); + } + None if !has_pending_approval => { + metadata.is_tool_call_escalated = Some(false); + metadata.review_decision = None; + } + None => { + return response_item; + } + } + + stamp_tool_metadata_on_response_item(response_item, metadata) + } + pub async fn resolve_elicitation( &self, server_name: String, @@ -3792,6 +3944,10 @@ impl Session { turn_context: &TurnContext, response_item: ResponseItem, ) { + let response_item = self + .stamp_tool_approval_metadata(turn_context, response_item) + .await; + // Add to conversation history and persist response item to rollout. self.record_conversation_items(turn_context, std::slice::from_ref(&response_item)) .await; @@ -4715,6 +4871,8 @@ mod handlers { } match decision { ReviewDecision::Abort => { + sess.record_approval_outcome(&approval_id, &ReviewDecision::Abort) + .await; sess.interrupt_task().await; } other => sess.notify_approval(&approval_id, other).await, diff --git a/codex-rs/core/src/codex_delegate_tests.rs b/codex-rs/core/src/codex_delegate_tests.rs index 8201424d8e..e0ad0d4a3c 100644 --- a/codex-rs/core/src/codex_delegate_tests.rs +++ b/codex-rs/core/src/codex_delegate_tests.rs @@ -77,6 +77,7 @@ async fn forward_events_cancelled_while_send_blocked_shuts_down_delegate() { call_id: "call-1".to_string(), name: "tool".to_string(), input: "{}".to_string(), + metadata: None, }, }), }) diff --git a/codex-rs/core/src/codex_tests.rs b/codex-rs/core/src/codex_tests.rs index 5fd782fa45..0c3ffbf7b9 100644 --- a/codex-rs/core/src/codex_tests.rs +++ b/codex-rs/core/src/codex_tests.rs @@ -4414,6 +4414,137 @@ async fn task_finish_emits_prompt_queued_metadata_for_injected_user_input_when_f )); } +#[test] +fn review_decision_metadata_mapping_is_stable() { + assert_eq!( + review_decision_to_metadata(&ReviewDecision::Approved), + codex_protocol::models::ReviewDecisionMetadata::Approved + ); + assert_eq!( + review_decision_to_metadata(&ReviewDecision::Denied), + codex_protocol::models::ReviewDecisionMetadata::Denied + ); + assert_eq!( + review_decision_to_metadata(&ReviewDecision::Abort), + codex_protocol::models::ReviewDecisionMetadata::Abort + ); + assert_eq!( + review_decision_to_metadata(&ReviewDecision::ApprovedForSession), + codex_protocol::models::ReviewDecisionMetadata::ApprovedForSession + ); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn tool_call_metadata_stamps_escalated_review_decision_when_feature_enabled() { + let (mut sess, tc, rx) = make_session_and_context_with_rx().await; + Arc::get_mut(&mut sess) + .expect("session should be uniquely owned in this test") + .features + .enable(crate::features::Feature::ItemMetadata) + .expect("feature flag should be enabled for this test"); + + sess.spawn_task( + Arc::clone(&tc), + vec![UserInput::Text { + text: "start".to_string(), + text_elements: Vec::new(), + }], + NeverEndingTask { + kind: TaskKind::Regular, + listen_to_cancellation_token: false, + }, + ) + .await; + while rx.try_recv().is_ok() {} + + sess.record_approval_outcome("call-1", &ReviewDecision::Denied) + .await; + sess.record_response_item_and_emit_turn_item( + tc.as_ref(), + ResponseItem::FunctionCall { + id: None, + name: "shell".to_string(), + namespace: None, + arguments: "{}".to_string(), + call_id: "call-1".to_string(), + metadata: None, + }, + ) + .await; + + let event = tokio::time::timeout(std::time::Duration::from_secs(2), rx.recv()) + .await + .expect("expected raw response item event") + .expect("channel open"); + assert!(matches!( + event.msg, + EventMsg::RawResponseItem(ref ev) + if matches!( + &ev.item, + ResponseItem::FunctionCall { + metadata: Some(metadata), + .. + } if metadata.is_tool_call_escalated == Some(true) + && metadata.review_decision + == Some(codex_protocol::models::ReviewDecisionMetadata::Denied) + ) + )); +} + +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn tool_call_metadata_stamps_non_escalated_false_when_feature_enabled() { + let (mut sess, tc, rx) = make_session_and_context_with_rx().await; + Arc::get_mut(&mut sess) + .expect("session should be uniquely owned in this test") + .features + .enable(crate::features::Feature::ItemMetadata) + .expect("feature flag should be enabled for this test"); + + sess.spawn_task( + Arc::clone(&tc), + vec![UserInput::Text { + text: "start".to_string(), + text_elements: Vec::new(), + }], + NeverEndingTask { + kind: TaskKind::Regular, + listen_to_cancellation_token: false, + }, + ) + .await; + while rx.try_recv().is_ok() {} + + sess.record_response_item_and_emit_turn_item( + tc.as_ref(), + ResponseItem::FunctionCall { + id: None, + name: "shell".to_string(), + namespace: None, + arguments: "{}".to_string(), + call_id: "call-2".to_string(), + metadata: None, + }, + ) + .await; + + let event = tokio::time::timeout(std::time::Duration::from_secs(2), rx.recv()) + .await + .expect("expected raw response item event") + .expect("channel open"); + assert!(matches!( + event.msg, + EventMsg::RawResponseItem(ref ev) + if matches!( + &ev.item, + ResponseItem::FunctionCall { + metadata: Some(metadata), + .. + } if metadata.is_tool_call_escalated == Some(false) + && metadata.review_decision.is_none() + ) + )); +} + #[tokio::test] async fn steer_input_requires_active_turn() { let (sess, _tc, _rx) = make_session_and_context_with_rx().await; @@ -4745,6 +4876,7 @@ async fn fatal_tool_error_stops_turn_and_reports_error() { call_id: "call-1".to_string(), name: "shell".to_string(), input: "{}".to_string(), + metadata: None, }; let call = ToolRouter::build_tool_call(session.as_ref(), item.clone()) diff --git a/codex-rs/core/src/context_manager/history_tests.rs b/codex-rs/core/src/context_manager/history_tests.rs index d91c168d00..ddbb5b8c62 100644 --- a/codex-rs/core/src/context_manager/history_tests.rs +++ b/codex-rs/core/src/context_manager/history_tests.rs @@ -282,6 +282,7 @@ fn for_prompt_strips_images_when_model_does_not_support_images() { namespace: None, arguments: "{}".to_string(), call_id: "call-1".to_string(), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "call-1".to_string(), @@ -301,6 +302,7 @@ fn for_prompt_strips_images_when_model_does_not_support_images() { call_id: "tool-1".to_string(), name: "js_repl".to_string(), input: "view_image".to_string(), + metadata: None, }, ResponseItem::CustomToolCallOutput { call_id: "tool-1".to_string(), @@ -346,6 +348,7 @@ fn for_prompt_strips_images_when_model_does_not_support_images() { namespace: None, arguments: "{}".to_string(), call_id: "call-1".to_string(), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "call-1".to_string(), @@ -365,6 +368,7 @@ fn for_prompt_strips_images_when_model_does_not_support_images() { call_id: "tool-1".to_string(), name: "js_repl".to_string(), input: "view_image".to_string(), + metadata: None, }, ResponseItem::CustomToolCallOutput { call_id: "tool-1".to_string(), @@ -539,6 +543,7 @@ fn remove_first_item_removes_matching_output_for_function_call() { namespace: None, arguments: "{}".to_string(), call_id: "call-1".to_string(), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "call-1".to_string(), @@ -563,6 +568,7 @@ fn remove_first_item_removes_matching_call_for_output() { namespace: None, arguments: "{}".to_string(), call_id: "call-2".to_string(), + metadata: None, }, ]; let mut h = create_history_with_items(items); @@ -580,6 +586,7 @@ fn remove_last_item_removes_matching_call_for_output() { namespace: None, arguments: "{}".to_string(), call_id: "call-delete-last".to_string(), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "call-delete-last".to_string(), @@ -664,6 +671,7 @@ fn remove_first_item_handles_local_shell_pair() { env: None, user: None, }), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "call-3".to_string(), @@ -820,6 +828,7 @@ fn remove_first_item_handles_custom_tool_pair() { call_id: "tool-1".to_string(), name: "my_tool".to_string(), input: "{}".to_string(), + metadata: None, }, ResponseItem::CustomToolCallOutput { call_id: "tool-1".to_string(), @@ -846,6 +855,7 @@ fn normalization_retains_local_shell_outputs() { env: None, user: None, }), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "shell-1".to_string(), @@ -1057,6 +1067,7 @@ fn normalize_adds_missing_output_for_function_call() { namespace: None, arguments: "{}".to_string(), call_id: "call-x".to_string(), + metadata: None, }]; let mut h = create_history_with_items(items); @@ -1071,6 +1082,7 @@ fn normalize_adds_missing_output_for_function_call() { namespace: None, arguments: "{}".to_string(), call_id: "call-x".to_string(), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "call-x".to_string(), @@ -1089,6 +1101,7 @@ fn normalize_adds_missing_output_for_custom_tool_call() { call_id: "tool-x".to_string(), name: "custom".to_string(), input: "{}".to_string(), + metadata: None, }]; let mut h = create_history_with_items(items); @@ -1103,6 +1116,7 @@ fn normalize_adds_missing_output_for_custom_tool_call() { call_id: "tool-x".to_string(), name: "custom".to_string(), input: "{}".to_string(), + metadata: None, }, ResponseItem::CustomToolCallOutput { call_id: "tool-x".to_string(), @@ -1127,6 +1141,7 @@ fn normalize_adds_missing_output_for_local_shell_call_with_id() { env: None, user: None, }), + metadata: None, }]; let mut h = create_history_with_items(items); @@ -1146,6 +1161,7 @@ fn normalize_adds_missing_output_for_local_shell_call_with_id() { env: None, user: None, }), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "shell-1".to_string(), @@ -1195,6 +1211,7 @@ fn normalize_mixed_inserts_and_removals() { namespace: None, arguments: "{}".to_string(), call_id: "c1".to_string(), + metadata: None, }, // Orphan output that should be removed ResponseItem::FunctionCallOutput { @@ -1208,6 +1225,7 @@ fn normalize_mixed_inserts_and_removals() { call_id: "t1".to_string(), name: "tool".to_string(), input: "{}".to_string(), + metadata: None, }, // Local shell call also gets an inserted function call output ResponseItem::LocalShellCall { @@ -1221,6 +1239,7 @@ fn normalize_mixed_inserts_and_removals() { env: None, user: None, }), + metadata: None, }, ]; let mut h = create_history_with_items(items); @@ -1236,6 +1255,7 @@ fn normalize_mixed_inserts_and_removals() { namespace: None, arguments: "{}".to_string(), call_id: "c1".to_string(), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "c1".to_string(), @@ -1247,6 +1267,7 @@ fn normalize_mixed_inserts_and_removals() { call_id: "t1".to_string(), name: "tool".to_string(), input: "{}".to_string(), + metadata: None, }, ResponseItem::CustomToolCallOutput { call_id: "t1".to_string(), @@ -1281,6 +1302,7 @@ fn normalize_adds_missing_output_for_function_call_inserts_output() { namespace: None, arguments: "{}".to_string(), call_id: "call-x".to_string(), + metadata: None, }]; let mut h = create_history_with_items(items); h.normalize_history(&default_input_modalities()); @@ -1293,6 +1315,7 @@ fn normalize_adds_missing_output_for_function_call_inserts_output() { namespace: None, arguments: "{}".to_string(), call_id: "call-x".to_string(), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "call-x".to_string(), @@ -1345,6 +1368,7 @@ fn normalize_adds_missing_output_for_custom_tool_call_panics_in_debug() { call_id: "tool-x".to_string(), name: "custom".to_string(), input: "{}".to_string(), + metadata: None, }]; let mut h = create_history_with_items(items); h.normalize_history(&default_input_modalities()); @@ -1365,6 +1389,7 @@ fn normalize_adds_missing_output_for_local_shell_call_with_id_panics_in_debug() env: None, user: None, }), + metadata: None, }]; let mut h = create_history_with_items(items); h.normalize_history(&default_input_modalities()); @@ -1459,6 +1484,7 @@ fn normalize_mixed_inserts_and_removals_panics_in_debug() { namespace: None, arguments: "{}".to_string(), call_id: "c1".to_string(), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "c2".to_string(), @@ -1470,6 +1496,7 @@ fn normalize_mixed_inserts_and_removals_panics_in_debug() { call_id: "t1".to_string(), name: "tool".to_string(), input: "{}".to_string(), + metadata: None, }, ResponseItem::LocalShellCall { id: None, @@ -1482,6 +1509,7 @@ fn normalize_mixed_inserts_and_removals_panics_in_debug() { env: None, user: None, }), + metadata: None, }, ]; let mut h = create_history_with_items(items); diff --git a/codex-rs/core/src/guardian/tests.rs b/codex-rs/core/src/guardian/tests.rs index ca8c6eebd9..c3e9997a29 100644 --- a/codex-rs/core/src/guardian/tests.rs +++ b/codex-rs/core/src/guardian/tests.rs @@ -211,6 +211,7 @@ fn collect_guardian_transcript_entries_includes_recent_tool_calls_and_output() { namespace: None, arguments: "{\"path\":\"README.md\"}".to_string(), call_id: "call-1".to_string(), + metadata: None, }, ResponseItem::FunctionCallOutput { call_id: "call-1".to_string(), diff --git a/codex-rs/core/src/rollout/truncation_tests.rs b/codex-rs/core/src/rollout/truncation_tests.rs index 2550fd50c4..6f3ca2fdac 100644 --- a/codex-rs/core/src/rollout/truncation_tests.rs +++ b/codex-rs/core/src/rollout/truncation_tests.rs @@ -54,6 +54,7 @@ fn truncates_rollout_from_start_before_nth_user_only() { name: "tool".to_string(), namespace: None, arguments: "{}".to_string(), + metadata: None, }, assistant_msg("a4"), ]; diff --git a/codex-rs/core/src/state/turn.rs b/codex-rs/core/src/state/turn.rs index a6ae1ba8ca..f13d2fc878 100644 --- a/codex-rs/core/src/state/turn.rs +++ b/codex-rs/core/src/state/turn.rs @@ -10,6 +10,8 @@ use tokio_util::task::AbortOnDropHandle; use codex_protocol::dynamic_tools::DynamicToolResponse; use codex_protocol::models::ResponseInputItem; +use codex_protocol::models::ReviewDecisionMetadata; +use codex_protocol::models::UserMessageType; use codex_protocol::request_permissions::RequestPermissionsResponse; use codex_protocol::request_user_input::RequestUserInputResponse; use codex_rmcp_client::ElicitationResponse; @@ -76,6 +78,8 @@ impl ActiveTurn { #[derive(Default)] pub(crate) struct TurnState { pending_approvals: HashMap>, + pending_approval_call_ids: HashMap, + approval_outcomes_by_call_id: HashMap, pending_request_permissions: HashMap>, pending_user_input: HashMap>, pending_elicitations: HashMap<(String, RequestId), oneshot::Sender>, @@ -95,6 +99,39 @@ impl TurnState { self.pending_approvals.insert(key, tx) } + pub(crate) fn insert_pending_approval_call_id( + &mut self, + approval_key: String, + call_id: String, + ) -> Option { + self.pending_approval_call_ids.insert(approval_key, call_id) + } + + pub(crate) fn remove_pending_approval_call_id(&mut self, approval_key: &str) -> Option { + self.pending_approval_call_ids.remove(approval_key) + } + + pub(crate) fn record_approval_outcome( + &mut self, + call_id: String, + decision: ReviewDecisionMetadata, + ) { + self.approval_outcomes_by_call_id.insert(call_id, decision); + } + + pub(crate) fn approval_outcome_for_call_id( + &self, + call_id: &str, + ) -> Option { + self.approval_outcomes_by_call_id.get(call_id).cloned() + } + + pub(crate) fn has_pending_approval_for_call_id(&self, call_id: &str) -> bool { + self.pending_approval_call_ids + .values() + .any(|pending_call_id| pending_call_id == call_id) + } + pub(crate) fn remove_pending_approval( &mut self, key: &str, @@ -104,6 +141,8 @@ impl TurnState { pub(crate) fn clear_pending(&mut self) { self.pending_approvals.clear(); + self.pending_approval_call_ids.clear(); + self.approval_outcomes_by_call_id.clear(); self.pending_request_permissions.clear(); self.pending_user_input.clear(); self.pending_elicitations.clear(); diff --git a/codex-rs/core/src/thread_manager_tests.rs b/codex-rs/core/src/thread_manager_tests.rs index 577bfdbd45..ae389b49e0 100644 --- a/codex-rs/core/src/thread_manager_tests.rs +++ b/codex-rs/core/src/thread_manager_tests.rs @@ -61,6 +61,7 @@ fn drops_from_last_user_only() { name: "tool".to_string(), namespace: None, arguments: "{}".to_string(), + metadata: None, }, assistant_msg("a4"), ]; diff --git a/codex-rs/core/src/tools/router_tests.rs b/codex-rs/core/src/tools/router_tests.rs index 641adb56de..8b71b2be37 100644 --- a/codex-rs/core/src/tools/router_tests.rs +++ b/codex-rs/core/src/tools/router_tests.rs @@ -142,6 +142,7 @@ async fn build_tool_call_uses_namespace_for_registry_name() -> anyhow::Result<() namespace: Some("mcp__codex_apps__calendar".to_string()), arguments: "{}".to_string(), call_id: "call-namespace".to_string(), + metadata: None, }, ) .await? diff --git a/codex-rs/core/src/turn_timing_tests.rs b/codex-rs/core/src/turn_timing_tests.rs index 2a5362fd3e..77b2c80f77 100644 --- a/codex-rs/core/src/turn_timing_tests.rs +++ b/codex-rs/core/src/turn_timing_tests.rs @@ -85,6 +85,7 @@ fn response_item_records_turn_ttft_for_first_output_signals() { namespace: None, arguments: "{}".to_string(), call_id: "call-1".to_string(), + metadata: None, } )); assert!(response_item_records_turn_ttft( @@ -94,6 +95,7 @@ fn response_item_records_turn_ttft_for_first_output_signals() { call_id: "call-2".to_string(), name: "custom".to_string(), input: "echo hi".to_string(), + metadata: None, } )); assert!(response_item_records_turn_ttft(&ResponseItem::Message { diff --git a/codex-rs/core/tests/suite/client.rs b/codex-rs/core/tests/suite/client.rs index 75a741bc15..ffa040b168 100644 --- a/codex-rs/core/tests/suite/client.rs +++ b/codex-rs/core/tests/suite/client.rs @@ -366,6 +366,7 @@ async fn resume_replays_legacy_js_repl_image_rollout_shapes() { call_id: "legacy-js-call".to_string(), name: "js_repl".to_string(), input: "console.log('legacy image flow')".to_string(), + metadata: None, }; let legacy_image_url = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z8DwHwAFAAH/iZk9HQAAAABJRU5ErkJggg=="; let rollout = vec![ @@ -523,6 +524,7 @@ async fn resume_replays_image_tool_outputs_with_detail() { namespace: None, arguments: "{\"path\":\"/tmp/example.webp\"}".to_string(), call_id: function_call_id.to_string(), + metadata: None, }), }, RolloutLine { @@ -545,6 +547,7 @@ async fn resume_replays_image_tool_outputs_with_detail() { call_id: custom_call_id.to_string(), name: "js_repl".to_string(), input: "console.log('image flow')".to_string(), + metadata: None, }), }, RolloutLine { @@ -1879,6 +1882,7 @@ async fn azure_responses_request_includes_store_and_reasoning_ids() { namespace: None, arguments: "{}".into(), call_id: "function-call-id".into(), + metadata: None, }); prompt.input.push(ResponseItem::FunctionCallOutput { call_id: "function-call-id".into(), @@ -1895,6 +1899,7 @@ async fn azure_responses_request_includes_store_and_reasoning_ids() { env: None, user: None, }), + metadata: None, }); prompt.input.push(ResponseItem::CustomToolCall { id: Some("custom-tool-id".into()), @@ -1902,6 +1907,7 @@ async fn azure_responses_request_includes_store_and_reasoning_ids() { call_id: "custom-tool-call-id".into(), name: "custom_tool".into(), input: "{}".into(), + metadata: None, }); prompt.input.push(ResponseItem::CustomToolCallOutput { call_id: "custom-tool-call-id".into(), diff --git a/codex-rs/features/src/tests.rs b/codex-rs/features/src/tests.rs index 01f49dbf33..e9f46f5cf7 100644 --- a/codex-rs/features/src/tests.rs +++ b/codex-rs/features/src/tests.rs @@ -147,7 +147,6 @@ fn image_detail_original_feature_is_under_development() { assert_eq!(Feature::ImageDetailOriginal.default_enabled(), false); } -#[test] fn item_metadata_is_under_development_and_disabled_by_default() { assert_eq!(Feature::ItemMetadata.stage(), Stage::UnderDevelopment); assert_eq!(Feature::ItemMetadata.default_enabled(), false); diff --git a/codex-rs/protocol/src/models.rs b/codex-rs/protocol/src/models.rs index 4d542c82ad..0a89c6ccb2 100644 --- a/codex-rs/protocol/src/models.rs +++ b/codex-rs/protocol/src/models.rs @@ -287,6 +287,32 @@ impl ResponseItemMessageMetadata { } } +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema, TS)] +#[serde(rename_all = "snake_case")] +#[ts(rename_all = "snake_case")] +pub enum ReviewDecisionMetadata { + Approved, + Denied, + Abort, + ApprovedForSession, + ApprovedWithAmendment, + ApprovedWithNetworkPolicyAllow, + DeniedWithNetworkPolicyDeny, +} + +#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema, TS)] +pub struct ResponseItemMetadata { + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub user_message_type: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub is_tool_call_escalated: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + pub review_decision: Option, +} + #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, JsonSchema, TS)] #[serde(tag = "type", rename_all = "snake_case")] pub enum ContentItem { @@ -363,6 +389,9 @@ pub enum ResponseItem { call_id: Option, status: LocalShellStatus, action: LocalShellAction, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + metadata: Option, }, FunctionCall { #[serde(default, skip_serializing)] @@ -377,6 +406,9 @@ pub enum ResponseItem { // Session::handle_function_call parse it into a Value. arguments: String, call_id: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + metadata: Option, }, ToolSearchCall { #[serde(default, skip_serializing)] @@ -412,6 +444,9 @@ pub enum ResponseItem { call_id: String, name: String, input: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + #[ts(optional)] + metadata: Option, }, // `custom_tool_call_output.output` uses the same wire encoding as // `function_call_output.output` so freeform tools can return either plain