mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +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
@@ -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,
|
||||
)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user