diff --git a/codex-rs/core/src/mcp_tool_call.rs b/codex-rs/core/src/mcp_tool_call.rs index 178c147dcf..86995eea81 100644 --- a/codex-rs/core/src/mcp_tool_call.rs +++ b/codex-rs/core/src/mcp_tool_call.rs @@ -16,6 +16,7 @@ use crate::session::session::Session; use crate::session::step_context::StepContext; use crate::session::turn_context::TurnContext; use crate::tools::ApprovalContext; +use crate::tools::context::ToolCallOrigin; use crate::tools::hook_names::HookToolName; use crate::tools::lifecycle::process_mcp_tool_result; use crate::tools::sandboxing::ApprovalAction; @@ -41,7 +42,6 @@ use codex_mcp::ToolInfo; use codex_mcp::auth_elicitation_completed_result; use codex_mcp::build_auth_elicitation_plan; use codex_mcp::mcp_permission_prompt_is_auto_approved; -use codex_protocol::ResponseItemId; use codex_protocol::approvals::ElicitationRequest; use codex_protocol::items::McpToolCallError; use codex_protocol::items::McpToolCallItem; @@ -123,7 +123,7 @@ pub(crate) async fn handle_mcp_tool_call( step_context: &Arc, cancellation_token: &CancellationToken, call_id: String, - originating_item_id: Option, + originating_call: Option, tool_info: &ToolInfo, prepared_call: Option, hook_tool_name: HookToolName, @@ -294,7 +294,7 @@ pub(crate) async fn handle_mcp_tool_call( &sess, step_context.as_ref(), &call_id, - originating_item_id.as_ref(), + originating_call.as_ref(), invocation, prepared_call, metadata, @@ -368,7 +368,7 @@ pub(crate) async fn handle_mcp_tool_call( &sess, step_context.as_ref(), &call_id, - originating_item_id.as_ref(), + originating_call.as_ref(), invocation, prepared_call, metadata, @@ -431,7 +431,7 @@ async fn handle_approved_mcp_tool_call( sess: &Arc, step_context: &StepContext, call_id: &str, - originating_item_id: Option<&ResponseItemId>, + originating_call: Option<&ToolCallOrigin>, invocation: McpInvocation, prepared_call: PreparedMcpCall, metadata: McpToolApprovalMetadata, @@ -512,7 +512,8 @@ async fn handle_approved_mcp_tool_call( let request_meta = with_mcp_tool_call_ids_meta( request_meta, &sess.thread_id.to_string(), - originating_item_id, + &sess.session_id().to_string(), + originating_call, ); let request_meta = augment_mcp_tool_request_meta_with_sandbox_state( step_context, @@ -1183,6 +1184,8 @@ const MCP_TOOL_LINK_ID_META_KEY: &str = "link_id"; const MCP_TOOL_LINK_IS_IMPLICIT_META_KEY: &str = "link_is_implicit"; const MCP_TOOL_PLUGIN_ID_META_KEY: &str = "plugin_id"; const MCP_TOOL_ITEM_ID_META_KEY: &str = "itemId"; +const MCP_TOOL_SESSION_ID_META_KEY: &str = "sessionId"; +const MCP_TOOL_WINDOW_ID_META_KEY: &str = "windowId"; const MCP_TOOL_THREAD_ID_META_KEY: &str = "threadId"; const MCP_TOOL_CONNECTED_ACCOUNT_EMAIL_META_KEY: &str = "connected_account_email"; const MCP_TOOL_RESOURCE_URI_META_KEY: &str = "resource_uri"; @@ -1328,7 +1331,8 @@ fn build_confirmation_policies_request_meta( fn with_mcp_tool_call_ids_meta( meta: Option, thread_id: &str, - originating_item_id: Option<&ResponseItemId>, + session_id: &str, + originating_call: Option<&ToolCallOrigin>, ) -> Option { let mut map = match meta { Some(serde_json::Value::Object(map)) => map, @@ -1339,7 +1343,17 @@ fn with_mcp_tool_call_ids_meta( MCP_TOOL_THREAD_ID_META_KEY.to_string(), serde_json::Value::String(thread_id.to_string()), ); - if let Some(item_id) = originating_item_id { + map.insert( + MCP_TOOL_SESSION_ID_META_KEY.to_string(), + serde_json::Value::String(session_id.to_string()), + ); + if let Some(origin) = originating_call { + map.insert( + MCP_TOOL_WINDOW_ID_META_KEY.to_string(), + serde_json::Value::String(origin.window_id.clone()), + ); + } + if let Some(item_id) = originating_call.and_then(|origin| origin.item_id.as_ref()) { map.insert( MCP_TOOL_ITEM_ID_META_KEY.to_string(), serde_json::Value::String(item_id.to_string()), diff --git a/codex-rs/core/src/mcp_tool_call_tests.rs b/codex-rs/core/src/mcp_tool_call_tests.rs index 8afe24ef34..2e7fee3950 100644 --- a/codex-rs/core/src/mcp_tool_call_tests.rs +++ b/codex-rs/core/src/mcp_tool_call_tests.rs @@ -27,6 +27,7 @@ use codex_config::types::McpServerToolConfig; use codex_features::Features; use codex_hooks::HooksConfig; use codex_model_provider::create_model_provider; +use codex_protocol::ResponseItemId; use codex_protocol::models::PermissionProfile; use codex_protocol::openai_models::ReasoningEffort as ReasoningEffortConfig; use codex_protocol::protocol::AskForApproval; @@ -1770,22 +1771,30 @@ async fn codex_apps_auth_elicitation_enabled_by_default_requests_elicitation() { #[test] fn mcp_tool_call_ids_are_added_to_request_meta() { - let item_id = ResponseItemId::from_server("fc-live".to_string()); + let origin = crate::tools::context::ToolCallOrigin { + item_id: Some(ResponseItemId::from_server("fc-live".to_string())), + window_id: "thread-live:2".to_string(), + }; assert_eq!( with_mcp_tool_call_ids_meta( Some(serde_json::json!({ "source": "test-client", "threadId": "stale-thread", + "sessionId": "stale-session", + "windowId": "stale-window", "itemId": "stale-item", })), "thread-live", - Some(&item_id), + "session-live", + Some(&origin), ), Some(serde_json::json!({ "source": "test-client", "threadId": "thread-live", + "sessionId": "session-live", "itemId": "fc-live", + "windowId": "thread-live:2", })) ); @@ -1793,10 +1802,12 @@ fn mcp_tool_call_ids_are_added_to_request_meta() { with_mcp_tool_call_ids_meta( /*meta*/ None, "thread-live", - /*originating_item_id*/ None, + "session-live", + /*originating_call*/ None, ), Some(serde_json::json!({ "threadId": "thread-live", + "sessionId": "session-live", })) ); @@ -1804,7 +1815,8 @@ fn mcp_tool_call_ids_are_added_to_request_meta() { with_mcp_tool_call_ids_meta( Some(serde_json::json!("invalid-meta")), "thread-live", - /*originating_item_id*/ None, + "session-live", + /*originating_call*/ None, ), Some(serde_json::json!("invalid-meta")) ); diff --git a/codex-rs/core/src/tools/code_mode/delegate.rs b/codex-rs/core/src/tools/code_mode/delegate.rs index 384aa94894..9e4429a4b1 100644 --- a/codex-rs/core/src/tools/code_mode/delegate.rs +++ b/codex-rs/core/src/tools/code_mode/delegate.rs @@ -3,6 +3,7 @@ use std::sync::Arc; use std::sync::Mutex; use std::sync::Weak; +use crate::tools::context::ToolCallOrigin; use codex_code_mode::CellId; use codex_code_mode::CodeModeNestedToolCall; use codex_code_mode::CodeModeSessionDelegate; @@ -10,7 +11,6 @@ use codex_code_mode::NotificationFuture; use codex_code_mode::ToolInvocationFuture; use codex_history::CodexHarnessMetadata; use codex_history::ResponseItemEnvelope; -use codex_protocol::ResponseItemId; use codex_protocol::ThreadId; use codex_protocol::models::FunctionCallOutputPayload; use codex_protocol::models::ResponseItem; @@ -49,8 +49,10 @@ pub(super) struct CodeModeCellDelegate { struct CellDispatchGate { ready: watch::Sender, - // Keep the original exec item when later waits resume this cell. - originating_item_id: Option, + // Callbacks may create the gate before exec attaches its origin. None means + // no origin is attached; Some retains the original item and window across + // waits, even if the best-effort item lookup found no ID. + originating_call: Option, } impl CodeModeDispatchBroker { @@ -68,7 +70,7 @@ impl CodeModeDispatchBroker { pub(super) fn mark_cell_ready_for_dispatch( &self, cell_id: &CellId, - originating_item_id: Option, + originating_call: Option, ) { let ready = { let mut dispatch_gates = self @@ -79,20 +81,20 @@ impl CodeModeDispatchBroker { .entry(cell_id.clone()) .or_insert_with(|| CellDispatchGate { ready: watch::channel(false).0, - originating_item_id: None, + originating_call: None, }); - gate.originating_item_id = originating_item_id; + gate.originating_call = originating_call; gate.ready.clone() }; ready.send_replace(true); } - pub(super) fn cell_originating_item_id(&self, cell_id: &CellId) -> Option { + pub(super) fn cell_originating_call(&self, cell_id: &CellId) -> Option { self.dispatch_gates .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .get(cell_id) - .and_then(|gate| gate.originating_item_id.clone()) + .and_then(|gate| gate.originating_call.clone()) } pub(super) fn close_cell(&self, cell_id: &CellId) { @@ -258,7 +260,7 @@ fn dispatch_gate( .entry(cell_id.clone()) .or_insert_with(|| CellDispatchGate { ready: watch::channel(false).0, - originating_item_id: None, + originating_call: None, }) .ready .clone() diff --git a/codex-rs/core/src/tools/code_mode/delegate_tests.rs b/codex-rs/core/src/tools/code_mode/delegate_tests.rs index 1dc6201623..afa7718b01 100644 --- a/codex-rs/core/src/tools/code_mode/delegate_tests.rs +++ b/codex-rs/core/src/tools/code_mode/delegate_tests.rs @@ -122,7 +122,7 @@ async fn detached_code_mode_callback_keeps_thread_id_on_dispatch_span() -> anyho /*executed_tool_calls*/ Default::default(), )); let cell_id = CellId::new("audit-cell".to_string()); - broker.mark_cell_ready_for_dispatch(&cell_id, /*originating_item_id*/ None); + broker.mark_cell_ready_for_dispatch(&cell_id, /*originating_call*/ None); let records = DispatchRecords::default(); let subscriber = tracing_subscriber::registry().with(DispatchCapture(Arc::clone(&records))); let _untraced = tracing::Dispatch::new(tracing::subscriber::NoSubscriber::default()); @@ -221,7 +221,7 @@ async fn dropped_tool_callbacks_release_the_origin_before_dispatch() { }; *queued_response = response_tx; broker.dispatch_tx.send(message).await.unwrap(); - broker.mark_cell_ready_for_dispatch(&cell_id, /*originating_item_id*/ None); + broker.mark_cell_ready_for_dispatch(&cell_id, /*originating_call*/ None); let _worker = broker.start_turn_worker( Arc::clone(&session), StepContext::for_test(Arc::clone(&turn)), diff --git a/codex-rs/core/src/tools/code_mode/execute_handler.rs b/codex-rs/core/src/tools/code_mode/execute_handler.rs index d131019fd5..2a7c4811f1 100644 --- a/codex-rs/core/src/tools/code_mode/execute_handler.rs +++ b/codex-rs/core/src/tools/code_mode/execute_handler.rs @@ -36,7 +36,7 @@ impl CodeModeExecuteHandler { session: std::sync::Arc, step_context: Arc, call_id: String, - originating_item_id: Option, + originating_call: Option, code: String, telemetry: &mut CodeModeToolCallGuard, ) -> Result { @@ -113,7 +113,7 @@ impl CodeModeExecuteHandler { exec.session .services .code_mode_service - .mark_cell_ready_for_dispatch(&cell_id, originating_item_id); + .mark_cell_ready_for_dispatch(&cell_id, originating_call); let response = started_cell .initial_response() .await @@ -194,7 +194,7 @@ impl CodeModeExecuteHandler { invocation: ToolInvocation, ) -> Result, FunctionCallError> { let handler_span = tracing::Span::current(); - let originating_item_id = invocation.originating_item_id().await; + let originating_call = invocation.originating_call().await; let ToolInvocation { session, turn, @@ -220,7 +220,7 @@ impl CodeModeExecuteHandler { session, step_context, call_id, - originating_item_id, + originating_call, input, &mut telemetry, ) diff --git a/codex-rs/core/src/tools/code_mode/mod.rs b/codex-rs/core/src/tools/code_mode/mod.rs index ab100b4d9f..c314c45c3d 100644 --- a/codex-rs/core/src/tools/code_mode/mod.rs +++ b/codex-rs/core/src/tools/code_mode/mod.rs @@ -189,17 +189,17 @@ impl CodeModeService { pub(crate) fn mark_cell_ready_for_dispatch( &self, cell_id: &codex_code_mode::CellId, - originating_item_id: Option, + originating_call: Option, ) { self.dispatch_broker - .mark_cell_ready_for_dispatch(cell_id, originating_item_id); + .mark_cell_ready_for_dispatch(cell_id, originating_call); } - pub(crate) fn cell_originating_item_id( + pub(crate) fn cell_originating_call( &self, cell_id: &codex_code_mode::CellId, - ) -> Option { - self.dispatch_broker.cell_originating_item_id(cell_id) + ) -> Option { + self.dispatch_broker.cell_originating_call(cell_id) } pub(crate) fn finish_cell_dispatch(&self, cell_id: &CellId) { diff --git a/codex-rs/core/src/tools/context.rs b/codex-rs/core/src/tools/context.rs index 937277b87e..0b73875bd8 100644 --- a/codex-rs/core/src/tools/context.rs +++ b/codex-rs/core/src/tools/context.rs @@ -70,18 +70,28 @@ pub struct ToolInvocation { pub payload: ToolPayload, } +/// Identity of the model call that started a tool invocation, retained across code-mode waits. +#[derive(Clone)] +pub(crate) struct ToolCallOrigin { + /// Best-effort lookup: invocations without a matching history item still + /// retain their known window ID. + pub(crate) item_id: Option, + pub(crate) window_id: String, +} + impl ToolInvocation { - /// Returns the Responses item that requested this call or started its code-mode cell. - pub(crate) async fn originating_item_id(&self) -> Option { + /// Returns the item and window that requested this call or started its code-mode cell. + pub(crate) async fn originating_call(&self) -> Option { if let ToolCallSource::CodeMode { cell_id, .. } = &self.source { return self .session .services .code_mode_service - .cell_originating_item_id(&codex_code_mode::CellId::new(cell_id.clone())); + .cell_originating_call(&codex_code_mode::CellId::new(cell_id.clone())); } - self.session + let item_id = self + .session .clone_history() .await .raw_items() @@ -94,7 +104,11 @@ impl ToolInvocation { id.clone() } _ => None, - }) + }); + Some(ToolCallOrigin { + item_id, + window_id: self.session.current_window_id().await, + }) } } diff --git a/codex-rs/core/src/tools/handlers/mcp.rs b/codex-rs/core/src/tools/handlers/mcp.rs index ced537f87c..c7d5c098f0 100644 --- a/codex-rs/core/src/tools/handlers/mcp.rs +++ b/codex-rs/core/src/tools/handlers/mcp.rs @@ -200,7 +200,7 @@ impl McpHandler { }); notify_tool_start(&invocation, mcp_tool.as_ref()).await; - let originating_item_id = invocation.originating_item_id().await; + let originating_call = invocation.originating_call().await; let ToolInvocation { session, step_context, @@ -232,7 +232,7 @@ impl McpHandler { &step_context, &cancellation_token, call_id.clone(), - originating_item_id, + originating_call, &self.tool_info, prepared_mcp_call, self.hook_tool_name(), diff --git a/codex-rs/core/src/tools/lifecycle.rs b/codex-rs/core/src/tools/lifecycle.rs index 0d3a0ecf1a..55288f7559 100644 --- a/codex-rs/core/src/tools/lifecycle.rs +++ b/codex-rs/core/src/tools/lifecycle.rs @@ -29,7 +29,10 @@ 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; + let originating_item_id = invocation + .originating_call() + .await + .and_then(|origin| origin.item_id); for contributor in contributors { contributor diff --git a/codex-rs/core/tests/suite/code_mode.rs b/codex-rs/core/tests/suite/code_mode.rs index b7a4c57fef..1663fa0a1a 100644 --- a/codex-rs/core/tests/suite/code_mode.rs +++ b/codex-rs/core/tests/suite/code_mode.rs @@ -1600,6 +1600,137 @@ fn result_metadata_apps_builder(base_url: String, account_email: &str) -> TestCo }) } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn code_mode_mcp_metadata_keeps_originating_window_after_compaction() -> Result<()> { + skip_if_no_network!(Ok(())); + let server = responses::start_mock_server().await; + let apps_server = mount_result_metadata_app( + &server, /*result_metadata*/ None, /*is_error*/ false, + ) + .await?; + let (reached_tx, reached_rx) = oneshot::channel(); + let (release_tx, release_rx) = oneshot::channel(); + let control = Arc::new(ResultMetadataTestControl { + server: Mutex::new(McpServerContribution::HostedApps { + config: Box::new(codex_apps_mcp_server_config( + &apps_server.chatgpt_base_url, + /*apps_mcp_product_sku*/ None, + /*originator*/ None, + )), + }), + gate: Mutex::new(Some((reached_tx, release_rx))), + }); + let mut extensions = ExtensionRegistryBuilder::::new(); + extensions.tool_lifecycle_contributor(control); + let mut builder = + result_metadata_apps_builder(apps_server.chatgpt_base_url, "user@example.com") + .with_model("test-gpt-5.1-codex") + .with_extensions(Arc::new(extensions.build())); + let test = builder.build_with_auto_env(&server).await?; + let originating_item_id = "ctc_before_compaction"; + let mut exec_call = ev_custom_tool_call( + "call-exec", + "exec", + &format!( + r#"const tool = ALL_TOOLS.find(({{ name }}) => name.endsWith("{RESULT_METADATA_TOOL}")); +const pending = tools[tool.name]({{}}); +yield_control(); +await pending; +await tools[tool.name]({{}}); +text("done");"#, + ), + ); + exec_call["item"]["id"] = serde_json::json!(originating_item_id); + let started = + responses::mount_sse_once(&server, sse(vec![exec_call, ev_completed("resp-start")])).await; + let yielded = responses::mount_sse_once( + &server, + sse(vec![ + ev_assistant_message("msg-start", "waiting"), + ev_completed("resp-yield"), + ]), + ) + .await; + test.submit_turn("Start an MCP call in code mode").await?; + tokio::time::timeout(Duration::from_secs(10), reached_rx).await??; + let first_items = custom_tool_output_items(&yielded.single_request(), "call-exec"); + let cell_id = extract_running_cell_id(text_item(&first_items, /*index*/ 0)); + let originating_window_id = + started.single_request().body_json()["client_metadata"]["x-codex-window-id"].clone(); + assert!(originating_window_id.is_string()); + + let compact = responses::mount_sse_once( + &server, + sse(vec![ + serde_json::json!({ + "type": "response.output_item.done", + "item": {"type": "compaction", "encrypted_content": "compacted history"}, + }), + ev_completed("resp-compact"), + ]), + ) + .await; + test.codex.submit(Op::Compact).await?; + wait_for_event(&test.codex, |event| { + matches!(event, EventMsg::TurnComplete(_)) + }) + .await; + assert_eq!( + compact + .single_request() + .inputs_of_type("compaction_trigger") + .len(), + 1 + ); + + let resumed = responses::mount_function_call_agent_response( + &server, + "call-wait", + &serde_json::json!({"cell_id": cell_id, "yield_time_ms": 10_000}).to_string(), + "wait", + ) + .await; + release_tx.send(()).unwrap(); + test.submit_turn("Finish the code-mode cell").await?; + let resumed_request = resumed.function_call.single_request(); + assert_ne!( + resumed_request.body_json()["client_metadata"]["x-codex-window-id"], + originating_window_id + ); + assert!( + resumed_request + .input() + .iter() + .all(|item| item["call_id"] != "call-exec") + ); + assert!( + function_tool_output_items(&resumed.completion.single_request(), "call-wait") + .iter() + .any(|item| item["text"] == "done") + ); + let calls = recorded_apps_tool_calls(&server).await; + assert_eq!(calls.len(), 2); + for call in calls { + let meta = &call["params"]["_meta"]; + assert_eq!( + serde_json::json!({ + "sessionId": meta["sessionId"], + "threadId": meta["threadId"], + "itemId": meta["itemId"], + "windowId": meta["windowId"], + }), + serde_json::json!({ + "sessionId": test.session_configured.session_id, + "threadId": test.session_configured.thread_id, + "itemId": originating_item_id, + "windowId": originating_window_id, + }), + ); + } + test.codex.shutdown_and_wait().await?; + Ok(()) +} + fn assert_result_metadata_call( output: &Value, arguments: &Value, @@ -2390,6 +2521,10 @@ if (!tool) { assert!( apps_tool_calls.iter().any(|call| { call.pointer("/params/_meta/itemId") == Some(&serde_json::json!(originating_item_id)) + && call.pointer("/params/_meta/sessionId") + == Some(&serde_json::json!(test.session_configured.session_id)) + && call.pointer("/params/_meta/windowId") + == Some(&first_body["client_metadata"]["x-codex-window-id"]) }), "the nested MCP call should inherit its code cell's originating Responses item" ); diff --git a/codex-rs/core/tests/suite/mcp_turn_metadata.rs b/codex-rs/core/tests/suite/mcp_turn_metadata.rs index 9b5eedb7d9..a41884709b 100644 --- a/codex-rs/core/tests/suite/mcp_turn_metadata.rs +++ b/codex-rs/core/tests/suite/mcp_turn_metadata.rs @@ -611,6 +611,14 @@ async fn approved_mcp_tool_call_metadata_records_prior_user_input_request( apps_tool_call.pointer("/params/_meta/itemId"), Some(&json!(originating_item_id)) ); + assert_eq!( + apps_tool_call.pointer("/params/_meta/sessionId"), + Some(&json!(test.session_configured.session_id)) + ); + assert_eq!( + apps_tool_call.pointer("/params/_meta/windowId"), + Some(&response_body["client_metadata"]["x-codex-window-id"]) + ); assert_eq!( apps_tool_call .pointer("/params/_meta/x-codex-turn-metadata/user_input_requested_during_turn"),