mirror of
https://github.com/openai/codex.git
synced 2026-11-21 15:10:25 +00:00
Add session and originating window IDs to MCP request metadata (#45409)
## What changed Include `sessionId` and the originating `windowId` alongside `threadId` and optional `itemId` in MCP request metadata. Retain the originating item and window for code-mode cells across waits and compaction, including the window when no matching history item is found. ## Testing Extend metadata assertions for direct and nested MCP calls. Add a regression test verifying that a code-mode cell retains its original item and window IDs when it resumes after compaction. GitOrigin-RevId: 5e3cf761f2bed4c16565334c94654496367325fd
This commit is contained in:
1 parent
d77ebc7223
commit
d761097734
11 files changed
+228
-40
No files matched your search
@@ -16,6 +16,7 @@ use crate::session::session::Session;
|
|||||||
use crate::session::step_context::StepContext;
|
use crate::session::step_context::StepContext;
|
||||||
use crate::session::turn_context::TurnContext;
|
use crate::session::turn_context::TurnContext;
|
||||||
use crate::tools::ApprovalContext;
|
use crate::tools::ApprovalContext;
|
||||||
|
use crate::tools::context::ToolCallOrigin;
|
||||||
use crate::tools::hook_names::HookToolName;
|
use crate::tools::hook_names::HookToolName;
|
||||||
use crate::tools::lifecycle::process_mcp_tool_result;
|
use crate::tools::lifecycle::process_mcp_tool_result;
|
||||||
use crate::tools::sandboxing::ApprovalAction;
|
use crate::tools::sandboxing::ApprovalAction;
|
||||||
@@ -41,7 +42,6 @@ use codex_mcp::ToolInfo;
|
|||||||
use codex_mcp::auth_elicitation_completed_result;
|
use codex_mcp::auth_elicitation_completed_result;
|
||||||
use codex_mcp::build_auth_elicitation_plan;
|
use codex_mcp::build_auth_elicitation_plan;
|
||||||
use codex_mcp::mcp_permission_prompt_is_auto_approved;
|
use codex_mcp::mcp_permission_prompt_is_auto_approved;
|
||||||
use codex_protocol::ResponseItemId;
|
|
||||||
use codex_protocol::approvals::ElicitationRequest;
|
use codex_protocol::approvals::ElicitationRequest;
|
||||||
use codex_protocol::items::McpToolCallError;
|
use codex_protocol::items::McpToolCallError;
|
||||||
use codex_protocol::items::McpToolCallItem;
|
use codex_protocol::items::McpToolCallItem;
|
||||||
@@ -123,7 +123,7 @@ pub(crate) async fn handle_mcp_tool_call(
|
|||||||
step_context: &Arc<StepContext>,
|
step_context: &Arc<StepContext>,
|
||||||
cancellation_token: &CancellationToken,
|
cancellation_token: &CancellationToken,
|
||||||
call_id: String,
|
call_id: String,
|
||||||
originating_item_id: Option<ResponseItemId>,
|
originating_call: Option<ToolCallOrigin>,
|
||||||
tool_info: &ToolInfo,
|
tool_info: &ToolInfo,
|
||||||
prepared_call: Option<PreparedMcpCall>,
|
prepared_call: Option<PreparedMcpCall>,
|
||||||
hook_tool_name: HookToolName,
|
hook_tool_name: HookToolName,
|
||||||
@@ -294,7 +294,7 @@ pub(crate) async fn handle_mcp_tool_call(
|
|||||||
&sess,
|
&sess,
|
||||||
step_context.as_ref(),
|
step_context.as_ref(),
|
||||||
&call_id,
|
&call_id,
|
||||||
originating_item_id.as_ref(),
|
originating_call.as_ref(),
|
||||||
invocation,
|
invocation,
|
||||||
prepared_call,
|
prepared_call,
|
||||||
metadata,
|
metadata,
|
||||||
@@ -368,7 +368,7 @@ pub(crate) async fn handle_mcp_tool_call(
|
|||||||
&sess,
|
&sess,
|
||||||
step_context.as_ref(),
|
step_context.as_ref(),
|
||||||
&call_id,
|
&call_id,
|
||||||
originating_item_id.as_ref(),
|
originating_call.as_ref(),
|
||||||
invocation,
|
invocation,
|
||||||
prepared_call,
|
prepared_call,
|
||||||
metadata,
|
metadata,
|
||||||
@@ -431,7 +431,7 @@ async fn handle_approved_mcp_tool_call(
|
|||||||
sess: &Arc<Session>,
|
sess: &Arc<Session>,
|
||||||
step_context: &StepContext,
|
step_context: &StepContext,
|
||||||
call_id: &str,
|
call_id: &str,
|
||||||
originating_item_id: Option<&ResponseItemId>,
|
originating_call: Option<&ToolCallOrigin>,
|
||||||
invocation: McpInvocation,
|
invocation: McpInvocation,
|
||||||
prepared_call: PreparedMcpCall,
|
prepared_call: PreparedMcpCall,
|
||||||
metadata: McpToolApprovalMetadata,
|
metadata: McpToolApprovalMetadata,
|
||||||
@@ -512,7 +512,8 @@ async fn handle_approved_mcp_tool_call(
|
|||||||
let request_meta = with_mcp_tool_call_ids_meta(
|
let request_meta = with_mcp_tool_call_ids_meta(
|
||||||
request_meta,
|
request_meta,
|
||||||
&sess.thread_id.to_string(),
|
&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(
|
let request_meta = augment_mcp_tool_request_meta_with_sandbox_state(
|
||||||
step_context,
|
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_LINK_IS_IMPLICIT_META_KEY: &str = "link_is_implicit";
|
||||||
const MCP_TOOL_PLUGIN_ID_META_KEY: &str = "plugin_id";
|
const MCP_TOOL_PLUGIN_ID_META_KEY: &str = "plugin_id";
|
||||||
const MCP_TOOL_ITEM_ID_META_KEY: &str = "itemId";
|
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_THREAD_ID_META_KEY: &str = "threadId";
|
||||||
const MCP_TOOL_CONNECTED_ACCOUNT_EMAIL_META_KEY: &str = "connected_account_email";
|
const MCP_TOOL_CONNECTED_ACCOUNT_EMAIL_META_KEY: &str = "connected_account_email";
|
||||||
const MCP_TOOL_RESOURCE_URI_META_KEY: &str = "resource_uri";
|
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(
|
fn with_mcp_tool_call_ids_meta(
|
||||||
meta: Option<serde_json::Value>,
|
meta: Option<serde_json::Value>,
|
||||||
thread_id: &str,
|
thread_id: &str,
|
||||||
originating_item_id: Option<&ResponseItemId>,
|
session_id: &str,
|
||||||
|
originating_call: Option<&ToolCallOrigin>,
|
||||||
) -> Option<serde_json::Value> {
|
) -> Option<serde_json::Value> {
|
||||||
let mut map = match meta {
|
let mut map = match meta {
|
||||||
Some(serde_json::Value::Object(map)) => map,
|
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(),
|
MCP_TOOL_THREAD_ID_META_KEY.to_string(),
|
||||||
serde_json::Value::String(thread_id.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(
|
map.insert(
|
||||||
MCP_TOOL_ITEM_ID_META_KEY.to_string(),
|
MCP_TOOL_ITEM_ID_META_KEY.to_string(),
|
||||||
serde_json::Value::String(item_id.to_string()),
|
serde_json::Value::String(item_id.to_string()),
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ use codex_config::types::McpServerToolConfig;
|
|||||||
use codex_features::Features;
|
use codex_features::Features;
|
||||||
use codex_hooks::HooksConfig;
|
use codex_hooks::HooksConfig;
|
||||||
use codex_model_provider::create_model_provider;
|
use codex_model_provider::create_model_provider;
|
||||||
|
use codex_protocol::ResponseItemId;
|
||||||
use codex_protocol::models::PermissionProfile;
|
use codex_protocol::models::PermissionProfile;
|
||||||
use codex_protocol::openai_models::ReasoningEffort as ReasoningEffortConfig;
|
use codex_protocol::openai_models::ReasoningEffort as ReasoningEffortConfig;
|
||||||
use codex_protocol::protocol::AskForApproval;
|
use codex_protocol::protocol::AskForApproval;
|
||||||
@@ -1770,22 +1771,30 @@ async fn codex_apps_auth_elicitation_enabled_by_default_requests_elicitation() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn mcp_tool_call_ids_are_added_to_request_meta() {
|
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!(
|
assert_eq!(
|
||||||
with_mcp_tool_call_ids_meta(
|
with_mcp_tool_call_ids_meta(
|
||||||
Some(serde_json::json!({
|
Some(serde_json::json!({
|
||||||
"source": "test-client",
|
"source": "test-client",
|
||||||
"threadId": "stale-thread",
|
"threadId": "stale-thread",
|
||||||
|
"sessionId": "stale-session",
|
||||||
|
"windowId": "stale-window",
|
||||||
"itemId": "stale-item",
|
"itemId": "stale-item",
|
||||||
})),
|
})),
|
||||||
"thread-live",
|
"thread-live",
|
||||||
Some(&item_id),
|
"session-live",
|
||||||
|
Some(&origin),
|
||||||
),
|
),
|
||||||
Some(serde_json::json!({
|
Some(serde_json::json!({
|
||||||
"source": "test-client",
|
"source": "test-client",
|
||||||
"threadId": "thread-live",
|
"threadId": "thread-live",
|
||||||
|
"sessionId": "session-live",
|
||||||
"itemId": "fc-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(
|
with_mcp_tool_call_ids_meta(
|
||||||
/*meta*/ None,
|
/*meta*/ None,
|
||||||
"thread-live",
|
"thread-live",
|
||||||
/*originating_item_id*/ None,
|
"session-live",
|
||||||
|
/*originating_call*/ None,
|
||||||
),
|
),
|
||||||
Some(serde_json::json!({
|
Some(serde_json::json!({
|
||||||
"threadId": "thread-live",
|
"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(
|
with_mcp_tool_call_ids_meta(
|
||||||
Some(serde_json::json!("invalid-meta")),
|
Some(serde_json::json!("invalid-meta")),
|
||||||
"thread-live",
|
"thread-live",
|
||||||
/*originating_item_id*/ None,
|
"session-live",
|
||||||
|
/*originating_call*/ None,
|
||||||
),
|
),
|
||||||
Some(serde_json::json!("invalid-meta"))
|
Some(serde_json::json!("invalid-meta"))
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ use std::sync::Arc;
|
|||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use std::sync::Weak;
|
use std::sync::Weak;
|
||||||
|
|
||||||
|
use crate::tools::context::ToolCallOrigin;
|
||||||
use codex_code_mode::CellId;
|
use codex_code_mode::CellId;
|
||||||
use codex_code_mode::CodeModeNestedToolCall;
|
use codex_code_mode::CodeModeNestedToolCall;
|
||||||
use codex_code_mode::CodeModeSessionDelegate;
|
use codex_code_mode::CodeModeSessionDelegate;
|
||||||
@@ -10,7 +11,6 @@ use codex_code_mode::NotificationFuture;
|
|||||||
use codex_code_mode::ToolInvocationFuture;
|
use codex_code_mode::ToolInvocationFuture;
|
||||||
use codex_history::CodexHarnessMetadata;
|
use codex_history::CodexHarnessMetadata;
|
||||||
use codex_history::ResponseItemEnvelope;
|
use codex_history::ResponseItemEnvelope;
|
||||||
use codex_protocol::ResponseItemId;
|
|
||||||
use codex_protocol::ThreadId;
|
use codex_protocol::ThreadId;
|
||||||
use codex_protocol::models::FunctionCallOutputPayload;
|
use codex_protocol::models::FunctionCallOutputPayload;
|
||||||
use codex_protocol::models::ResponseItem;
|
use codex_protocol::models::ResponseItem;
|
||||||
@@ -49,8 +49,10 @@ pub(super) struct CodeModeCellDelegate {
|
|||||||
|
|
||||||
struct CellDispatchGate {
|
struct CellDispatchGate {
|
||||||
ready: watch::Sender<bool>,
|
ready: watch::Sender<bool>,
|
||||||
// Keep the original exec item when later waits resume this cell.
|
// Callbacks may create the gate before exec attaches its origin. None means
|
||||||
originating_item_id: Option<ResponseItemId>,
|
// 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<ToolCallOrigin>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CodeModeDispatchBroker {
|
impl CodeModeDispatchBroker {
|
||||||
@@ -68,7 +70,7 @@ impl CodeModeDispatchBroker {
|
|||||||
pub(super) fn mark_cell_ready_for_dispatch(
|
pub(super) fn mark_cell_ready_for_dispatch(
|
||||||
&self,
|
&self,
|
||||||
cell_id: &CellId,
|
cell_id: &CellId,
|
||||||
originating_item_id: Option<ResponseItemId>,
|
originating_call: Option<ToolCallOrigin>,
|
||||||
) {
|
) {
|
||||||
let ready = {
|
let ready = {
|
||||||
let mut dispatch_gates = self
|
let mut dispatch_gates = self
|
||||||
@@ -79,20 +81,20 @@ impl CodeModeDispatchBroker {
|
|||||||
.entry(cell_id.clone())
|
.entry(cell_id.clone())
|
||||||
.or_insert_with(|| CellDispatchGate {
|
.or_insert_with(|| CellDispatchGate {
|
||||||
ready: watch::channel(false).0,
|
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()
|
gate.ready.clone()
|
||||||
};
|
};
|
||||||
ready.send_replace(true);
|
ready.send_replace(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn cell_originating_item_id(&self, cell_id: &CellId) -> Option<ResponseItemId> {
|
pub(super) fn cell_originating_call(&self, cell_id: &CellId) -> Option<ToolCallOrigin> {
|
||||||
self.dispatch_gates
|
self.dispatch_gates
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
.unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||||
.get(cell_id)
|
.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) {
|
pub(super) fn close_cell(&self, cell_id: &CellId) {
|
||||||
@@ -258,7 +260,7 @@ fn dispatch_gate(
|
|||||||
.entry(cell_id.clone())
|
.entry(cell_id.clone())
|
||||||
.or_insert_with(|| CellDispatchGate {
|
.or_insert_with(|| CellDispatchGate {
|
||||||
ready: watch::channel(false).0,
|
ready: watch::channel(false).0,
|
||||||
originating_item_id: None,
|
originating_call: None,
|
||||||
})
|
})
|
||||||
.ready
|
.ready
|
||||||
.clone()
|
.clone()
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ async fn detached_code_mode_callback_keeps_thread_id_on_dispatch_span() -> anyho
|
|||||||
/*executed_tool_calls*/ Default::default(),
|
/*executed_tool_calls*/ Default::default(),
|
||||||
));
|
));
|
||||||
let cell_id = CellId::new("audit-cell".to_string());
|
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 records = DispatchRecords::default();
|
||||||
let subscriber = tracing_subscriber::registry().with(DispatchCapture(Arc::clone(&records)));
|
let subscriber = tracing_subscriber::registry().with(DispatchCapture(Arc::clone(&records)));
|
||||||
let _untraced = tracing::Dispatch::new(tracing::subscriber::NoSubscriber::default());
|
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;
|
*queued_response = response_tx;
|
||||||
broker.dispatch_tx.send(message).await.unwrap();
|
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(
|
let _worker = broker.start_turn_worker(
|
||||||
Arc::clone(&session),
|
Arc::clone(&session),
|
||||||
StepContext::for_test(Arc::clone(&turn)),
|
StepContext::for_test(Arc::clone(&turn)),
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ impl CodeModeExecuteHandler {
|
|||||||
session: std::sync::Arc<crate::session::session::Session>,
|
session: std::sync::Arc<crate::session::session::Session>,
|
||||||
step_context: Arc<crate::session::step_context::StepContext>,
|
step_context: Arc<crate::session::step_context::StepContext>,
|
||||||
call_id: String,
|
call_id: String,
|
||||||
originating_item_id: Option<codex_protocol::ResponseItemId>,
|
originating_call: Option<crate::tools::context::ToolCallOrigin>,
|
||||||
code: String,
|
code: String,
|
||||||
telemetry: &mut CodeModeToolCallGuard,
|
telemetry: &mut CodeModeToolCallGuard,
|
||||||
) -> Result<FunctionToolOutput, FunctionCallError> {
|
) -> Result<FunctionToolOutput, FunctionCallError> {
|
||||||
@@ -113,7 +113,7 @@ impl CodeModeExecuteHandler {
|
|||||||
exec.session
|
exec.session
|
||||||
.services
|
.services
|
||||||
.code_mode_service
|
.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
|
let response = started_cell
|
||||||
.initial_response()
|
.initial_response()
|
||||||
.await
|
.await
|
||||||
@@ -194,7 +194,7 @@ impl CodeModeExecuteHandler {
|
|||||||
invocation: ToolInvocation,
|
invocation: ToolInvocation,
|
||||||
) -> Result<Box<dyn crate::tools::context::ToolOutput>, FunctionCallError> {
|
) -> Result<Box<dyn crate::tools::context::ToolOutput>, FunctionCallError> {
|
||||||
let handler_span = tracing::Span::current();
|
let handler_span = tracing::Span::current();
|
||||||
let originating_item_id = invocation.originating_item_id().await;
|
let originating_call = invocation.originating_call().await;
|
||||||
let ToolInvocation {
|
let ToolInvocation {
|
||||||
session,
|
session,
|
||||||
turn,
|
turn,
|
||||||
@@ -220,7 +220,7 @@ impl CodeModeExecuteHandler {
|
|||||||
session,
|
session,
|
||||||
step_context,
|
step_context,
|
||||||
call_id,
|
call_id,
|
||||||
originating_item_id,
|
originating_call,
|
||||||
input,
|
input,
|
||||||
&mut telemetry,
|
&mut telemetry,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -189,17 +189,17 @@ impl CodeModeService {
|
|||||||
pub(crate) fn mark_cell_ready_for_dispatch(
|
pub(crate) fn mark_cell_ready_for_dispatch(
|
||||||
&self,
|
&self,
|
||||||
cell_id: &codex_code_mode::CellId,
|
cell_id: &codex_code_mode::CellId,
|
||||||
originating_item_id: Option<codex_protocol::ResponseItemId>,
|
originating_call: Option<crate::tools::context::ToolCallOrigin>,
|
||||||
) {
|
) {
|
||||||
self.dispatch_broker
|
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,
|
&self,
|
||||||
cell_id: &codex_code_mode::CellId,
|
cell_id: &codex_code_mode::CellId,
|
||||||
) -> Option<codex_protocol::ResponseItemId> {
|
) -> Option<crate::tools::context::ToolCallOrigin> {
|
||||||
self.dispatch_broker.cell_originating_item_id(cell_id)
|
self.dispatch_broker.cell_originating_call(cell_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn finish_cell_dispatch(&self, cell_id: &CellId) {
|
pub(crate) fn finish_cell_dispatch(&self, cell_id: &CellId) {
|
||||||
|
|||||||
@@ -70,18 +70,28 @@ pub struct ToolInvocation {
|
|||||||
pub payload: ToolPayload,
|
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<ResponseItemId>,
|
||||||
|
pub(crate) window_id: String,
|
||||||
|
}
|
||||||
|
|
||||||
impl ToolInvocation {
|
impl ToolInvocation {
|
||||||
/// Returns the Responses item that requested this call or started its code-mode cell.
|
/// Returns the item and window that requested this call or started its code-mode cell.
|
||||||
pub(crate) async fn originating_item_id(&self) -> Option<ResponseItemId> {
|
pub(crate) async fn originating_call(&self) -> Option<ToolCallOrigin> {
|
||||||
if let ToolCallSource::CodeMode { cell_id, .. } = &self.source {
|
if let ToolCallSource::CodeMode { cell_id, .. } = &self.source {
|
||||||
return self
|
return self
|
||||||
.session
|
.session
|
||||||
.services
|
.services
|
||||||
.code_mode_service
|
.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()
|
.clone_history()
|
||||||
.await
|
.await
|
||||||
.raw_items()
|
.raw_items()
|
||||||
@@ -94,7 +104,11 @@ impl ToolInvocation {
|
|||||||
id.clone()
|
id.clone()
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
});
|
||||||
|
Some(ToolCallOrigin {
|
||||||
|
item_id,
|
||||||
|
window_id: self.session.current_window_id().await,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -200,7 +200,7 @@ impl McpHandler {
|
|||||||
});
|
});
|
||||||
notify_tool_start(&invocation, mcp_tool.as_ref()).await;
|
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 {
|
let ToolInvocation {
|
||||||
session,
|
session,
|
||||||
step_context,
|
step_context,
|
||||||
@@ -232,7 +232,7 @@ impl McpHandler {
|
|||||||
&step_context,
|
&step_context,
|
||||||
&cancellation_token,
|
&cancellation_token,
|
||||||
call_id.clone(),
|
call_id.clone(),
|
||||||
originating_item_id,
|
originating_call,
|
||||||
&self.tool_info,
|
&self.tool_info,
|
||||||
prepared_mcp_call,
|
prepared_mcp_call,
|
||||||
self.hook_tool_name(),
|
self.hook_tool_name(),
|
||||||
|
|||||||
@@ -29,7 +29,10 @@ pub(crate) async fn notify_tool_start(
|
|||||||
let thread_store = &invocation.session.services.thread_extension_data;
|
let thread_store = &invocation.session.services.thread_extension_data;
|
||||||
let conversation_history = invocation.session.conversation_history_snapshot().await;
|
let conversation_history = invocation.session.conversation_history_snapshot().await;
|
||||||
let root_turn_id = invocation.turn.turn_metadata_state.root_turn_id();
|
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 {
|
for contributor in contributors {
|
||||||
contributor
|
contributor
|
||||||
|
|||||||
@@ -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::<Config>::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(
|
fn assert_result_metadata_call(
|
||||||
output: &Value,
|
output: &Value,
|
||||||
arguments: &Value,
|
arguments: &Value,
|
||||||
@@ -2390,6 +2521,10 @@ if (!tool) {
|
|||||||
assert!(
|
assert!(
|
||||||
apps_tool_calls.iter().any(|call| {
|
apps_tool_calls.iter().any(|call| {
|
||||||
call.pointer("/params/_meta/itemId") == Some(&serde_json::json!(originating_item_id))
|
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"
|
"the nested MCP call should inherit its code cell's originating Responses item"
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -611,6 +611,14 @@ async fn approved_mcp_tool_call_metadata_records_prior_user_input_request(
|
|||||||
apps_tool_call.pointer("/params/_meta/itemId"),
|
apps_tool_call.pointer("/params/_meta/itemId"),
|
||||||
Some(&json!(originating_item_id))
|
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!(
|
assert_eq!(
|
||||||
apps_tool_call
|
apps_tool_call
|
||||||
.pointer("/params/_meta/x-codex-turn-metadata/user_input_requested_during_turn"),
|
.pointer("/params/_meta/x-codex-turn-metadata/user_input_requested_during_turn"),
|
||||||
|
|||||||
Reference in new issue
Block a user