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:
jif authored and copyberry committed 2026-09-14 11:03:22 +00:00
1 parent d77ebc7223
commit d761097734
11 files changed
+228 -40

No files matched your search

+11 -9
View File
@@ -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<bool>,
// Keep the original exec item when later waits resume this cell.
originating_item_id: Option<ResponseItemId>,
// 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<ToolCallOrigin>,
}
impl CodeModeDispatchBroker {
@@ -68,7 +70,7 @@ impl CodeModeDispatchBroker {
pub(super) fn mark_cell_ready_for_dispatch(
&self,
cell_id: &CellId,
originating_item_id: Option<ResponseItemId>,
originating_call: Option<ToolCallOrigin>,
) {
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<ResponseItemId> {
pub(super) fn cell_originating_call(&self, cell_id: &CellId) -> Option<ToolCallOrigin> {
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()
@@ -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)),
@@ -36,7 +36,7 @@ impl CodeModeExecuteHandler {
session: std::sync::Arc<crate::session::session::Session>,
step_context: Arc<crate::session::step_context::StepContext>,
call_id: String,
originating_item_id: Option<codex_protocol::ResponseItemId>,
originating_call: Option<crate::tools::context::ToolCallOrigin>,
code: String,
telemetry: &mut CodeModeToolCallGuard,
) -> Result<FunctionToolOutput, FunctionCallError> {
@@ -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<Box<dyn crate::tools::context::ToolOutput>, 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,
)
+5 -5
View File
@@ -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<codex_protocol::ResponseItemId>,
originating_call: Option<crate::tools::context::ToolCallOrigin>,
) {
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<codex_protocol::ResponseItemId> {
self.dispatch_broker.cell_originating_item_id(cell_id)
) -> Option<crate::tools::context::ToolCallOrigin> {
self.dispatch_broker.cell_originating_call(cell_id)
}
pub(crate) fn finish_cell_dispatch(&self, cell_id: &CellId) {
+19 -5
View File
@@ -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<ResponseItemId>,
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<ResponseItemId> {
/// Returns the item and window that requested this call or started its code-mode cell.
pub(crate) async fn originating_call(&self) -> Option<ToolCallOrigin> {
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,
})
}
}
+2 -2
View File
@@ -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(),
+4 -1
View File
@@ -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