mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Preserve originating context for yielded code-mode tool calls (#44866)
## Why A code-mode cell can outlive its turn. Nested tool calls dispatched during a later turn need the original step's tools and context, while approval handling must still work across that turn boundary. ## What changed - Retain each cell's originating `StepContext` for nested tool dispatch and cancel queued calls whose context has been released. - Keep MCP approval metadata available for the lifetime of the running invocation, keyed by server and call ID. - Apply Guardian denial accounting and interruption to the active turn, including reviews from an earlier cell. - Carry `turn_id` in legacy MCP begin/end events so history reconstruction and rollout migration assign late completions to their original turn. Preserve the current-turn fallback for older records without it. ## Testing Add regression coverage for originating tool selection across turns, callback and metadata cleanup, delayed MCP elicitation review, Guardian interruption of the active turn, and late MCP completion attribution with legacy compatibility. GitOrigin-RevId: 4533379aae6e4ecb4aabe5a25e4aca5d65385d76
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use std::sync::Weak;
|
||||
|
||||
use codex_code_mode::CellId;
|
||||
use codex_code_mode::CodeModeNestedToolCall;
|
||||
@@ -17,12 +18,12 @@ use tokio::sync::watch;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tracing::Instrument;
|
||||
|
||||
use super::ExecContext;
|
||||
use super::PUBLIC_TOOL_NAME;
|
||||
use super::submit_nested_tool;
|
||||
use super::telemetry::DispatchInterruption;
|
||||
use super::telemetry::NestedToolDispatchTrace;
|
||||
use super::telemetry::trace_id;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::tools::ExecutedToolCalls;
|
||||
use crate::tools::call_trace;
|
||||
@@ -37,6 +38,12 @@ pub(super) struct CodeModeDispatchBroker {
|
||||
executed_tool_calls: ExecutedToolCalls,
|
||||
}
|
||||
|
||||
/// Retains the step advertised to one execution, including callbacks after it yields.
|
||||
pub(super) struct CodeModeCellDelegate {
|
||||
pub(super) broker: Arc<CodeModeDispatchBroker>,
|
||||
pub(super) step_context: Arc<StepContext>,
|
||||
}
|
||||
|
||||
struct CellDispatchGate {
|
||||
ready: watch::Sender<bool>,
|
||||
// Keep the original exec item when later waits resume this cell.
|
||||
@@ -105,13 +112,15 @@ impl CodeModeDispatchBroker {
|
||||
|
||||
pub(super) fn start_turn_worker(
|
||||
&self,
|
||||
exec: ExecContext,
|
||||
session: Arc<Session>,
|
||||
step_context: Arc<StepContext>,
|
||||
tracker: SharedTurnDiffTracker,
|
||||
) -> CodeModeDispatchWorker {
|
||||
let track_completeness = ExecutedToolCalls::is_enabled(&exec.turn.config.features);
|
||||
let tool_runtime = ToolCallRuntime::new(Arc::clone(&exec.session), step_context, tracker);
|
||||
let host = Arc::new(CoreTurnHost { exec, tool_runtime });
|
||||
let tool_runtime = ToolCallRuntime::new(Arc::clone(&session), step_context, tracker);
|
||||
let host = Arc::new(CoreTurnHost {
|
||||
session,
|
||||
tool_runtime,
|
||||
});
|
||||
let dispatch_rx = self.dispatch_rx.clone();
|
||||
let dispatch_gates = Arc::clone(&self.dispatch_gates);
|
||||
let (shutdown_tx, mut shutdown_rx) = oneshot::channel();
|
||||
@@ -148,6 +157,7 @@ impl CodeModeDispatchBroker {
|
||||
}
|
||||
DispatchMessage::InvokeTool {
|
||||
invocation,
|
||||
step_context,
|
||||
mut dispatch_trace,
|
||||
cancellation_token,
|
||||
response_tx,
|
||||
@@ -169,6 +179,16 @@ impl CodeModeDispatchBroker {
|
||||
remove_dispatch_gate(&dispatch_gates, &cell_id);
|
||||
continue;
|
||||
}
|
||||
let Some(step_context) = step_context
|
||||
.upgrade()
|
||||
.filter(|_| !cancellation_token.is_cancelled())
|
||||
else {
|
||||
let _ = response_tx
|
||||
.send(Err("code mode nested tool call cancelled".to_string()));
|
||||
continue;
|
||||
};
|
||||
let track_completeness =
|
||||
ExecutedToolCalls::is_enabled(&step_context.turn.config.features);
|
||||
let host = Arc::clone(&host);
|
||||
let dispatch_gates = Arc::clone(&dispatch_gates);
|
||||
let span = dispatch_trace.span.clone();
|
||||
@@ -196,6 +216,7 @@ impl CodeModeDispatchBroker {
|
||||
dispatch_trace.interruption = None;
|
||||
host.submit_tool(
|
||||
invocation,
|
||||
step_context,
|
||||
dispatch_trace.call_id.clone(),
|
||||
cancellation_token.clone(),
|
||||
)
|
||||
@@ -273,13 +294,13 @@ async fn wait_until_cell_ready_for_dispatch(
|
||||
}
|
||||
}
|
||||
|
||||
impl CodeModeSessionDelegate for CodeModeDispatchBroker {
|
||||
impl CodeModeSessionDelegate for CodeModeCellDelegate {
|
||||
#[tracing::instrument(
|
||||
name = "code_mode.broker.invoke_tool",
|
||||
level = "info",
|
||||
skip_all,
|
||||
fields(
|
||||
conversation.id = %self.thread_id,
|
||||
conversation.id = %self.broker.thread_id,
|
||||
cell.id = %invocation.cell_id,
|
||||
runtime_tool_call_id = invocation.runtime_tool_call_id.as_str(),
|
||||
tool_name = invocation.tool_name.name.as_str(),
|
||||
@@ -294,7 +315,7 @@ impl CodeModeSessionDelegate for CodeModeDispatchBroker {
|
||||
Box::pin(async move {
|
||||
let call_id = format!("{PUBLIC_TOOL_NAME}-{}", uuid::Uuid::new_v4());
|
||||
call_trace::received(
|
||||
self.thread_id,
|
||||
self.broker.thread_id,
|
||||
&invocation.tool_name,
|
||||
&call_id,
|
||||
call_trace::Receipt::CodeModeBroker {
|
||||
@@ -303,16 +324,18 @@ impl CodeModeSessionDelegate for CodeModeDispatchBroker {
|
||||
},
|
||||
);
|
||||
let mut dispatch_trace =
|
||||
Box::new(NestedToolDispatchTrace::new(self.thread_id, call_id));
|
||||
Box::new(NestedToolDispatchTrace::new(self.broker.thread_id, call_id));
|
||||
if cancellation_token.is_cancelled() {
|
||||
dispatch_trace.interruption = Some(DispatchInterruption::Cancelled);
|
||||
return Err("code mode nested tool call cancelled".to_string());
|
||||
}
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
// Only the worker can tell whether dispatch beats cancellation once the call is queued.
|
||||
self.dispatch_tx
|
||||
self.broker
|
||||
.dispatch_tx
|
||||
.send(DispatchMessage::InvokeTool {
|
||||
invocation,
|
||||
step_context: Arc::downgrade(&self.step_context),
|
||||
dispatch_trace,
|
||||
cancellation_token: cancellation_token.clone(),
|
||||
response_tx,
|
||||
@@ -341,7 +364,8 @@ impl CodeModeSessionDelegate for CodeModeDispatchBroker {
|
||||
return Err("code mode notification cancelled".to_string());
|
||||
}
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
self.dispatch_tx
|
||||
self.broker
|
||||
.dispatch_tx
|
||||
.send(DispatchMessage::Notify {
|
||||
call_id,
|
||||
cell_id,
|
||||
@@ -362,13 +386,15 @@ impl CodeModeSessionDelegate for CodeModeDispatchBroker {
|
||||
}
|
||||
|
||||
fn cell_closed(&self, cell_id: &CellId) {
|
||||
self.close_cell(cell_id);
|
||||
self.broker.close_cell(cell_id);
|
||||
}
|
||||
}
|
||||
|
||||
enum DispatchMessage {
|
||||
InvokeTool {
|
||||
invocation: CodeModeNestedToolCall,
|
||||
// The delegate owns the step while the callback is live; stale queued work must not.
|
||||
step_context: Weak<StepContext>,
|
||||
dispatch_trace: Box<NestedToolDispatchTrace>,
|
||||
cancellation_token: CancellationToken,
|
||||
response_tx: oneshot::Sender<Result<JsonValue, String>>,
|
||||
@@ -395,7 +421,7 @@ impl Drop for CodeModeDispatchWorker {
|
||||
}
|
||||
|
||||
struct CoreTurnHost {
|
||||
exec: ExecContext,
|
||||
session: Arc<Session>,
|
||||
tool_runtime: ToolCallRuntime,
|
||||
}
|
||||
|
||||
@@ -403,11 +429,13 @@ impl CoreTurnHost {
|
||||
fn submit_tool(
|
||||
&self,
|
||||
invocation: CodeModeNestedToolCall,
|
||||
step_context: Arc<StepContext>,
|
||||
call_id: String,
|
||||
cancellation_token: CancellationToken,
|
||||
) -> impl std::future::Future<Output = Result<JsonValue, String>> + Send + 'static {
|
||||
let invocation = submit_nested_tool(
|
||||
self.exec.clone(),
|
||||
Arc::clone(&self.session),
|
||||
step_context,
|
||||
self.tool_runtime.clone(),
|
||||
invocation,
|
||||
call_id,
|
||||
@@ -421,8 +449,7 @@ impl CoreTurnHost {
|
||||
if text.trim().is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
self.exec
|
||||
.session
|
||||
self.session
|
||||
.inject_if_running(vec![ResponseItem::CustomToolCallOutput {
|
||||
id: None,
|
||||
call_id,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Tracing identity coverage for code-mode callbacks outside local turn spans.
|
||||
//! Callback tracing identity and originating-step lifetime coverage.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
@@ -20,8 +20,9 @@ use tracing_subscriber::layer::Context;
|
||||
use tracing_subscriber::prelude::*;
|
||||
use tracing_subscriber::registry::Registry;
|
||||
|
||||
use super::CodeModeCellDelegate;
|
||||
use super::CodeModeDispatchBroker;
|
||||
use super::ExecContext;
|
||||
use super::DispatchMessage;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::session::tests::make_session_and_context;
|
||||
use crate::tools::context::FunctionToolOutput;
|
||||
@@ -31,6 +32,7 @@ use crate::tools::registry::ToolExecutor;
|
||||
use crate::tools::registry::ToolRegistry;
|
||||
use crate::tools::router::ToolRouter;
|
||||
use crate::turn_diff_tracker::TurnDiffTracker;
|
||||
use tokio::sync::oneshot;
|
||||
|
||||
#[derive(Default)]
|
||||
struct DispatchFields(BTreeMap<String, String>);
|
||||
@@ -125,17 +127,18 @@ async fn detached_code_mode_callback_keeps_thread_id_on_dispatch_span() -> anyho
|
||||
let subscriber = tracing_subscriber::registry().with(DispatchCapture(Arc::clone(&records)));
|
||||
let _untraced = tracing::Dispatch::new(tracing::subscriber::NoSubscriber::default());
|
||||
let _subscriber = tracing::subscriber::set_default(subscriber);
|
||||
let delegate = CodeModeCellDelegate {
|
||||
broker: Arc::clone(&broker),
|
||||
step_context: Arc::clone(&step),
|
||||
};
|
||||
let _worker = broker.start_turn_worker(
|
||||
ExecContext {
|
||||
session: Arc::new(session),
|
||||
turn,
|
||||
},
|
||||
Arc::new(session),
|
||||
step,
|
||||
Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
);
|
||||
// Process-owned code-mode callbacks run on a fresh task without a turn span.
|
||||
let result = tokio::spawn(async move {
|
||||
broker
|
||||
delegate
|
||||
.invoke_tool(
|
||||
CodeModeNestedToolCall {
|
||||
cell_id,
|
||||
@@ -170,3 +173,63 @@ async fn detached_code_mode_callback_keeps_thread_id_on_dispatch_span() -> anyho
|
||||
uuid::Uuid::parse_str(uuid)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn dropped_tool_callbacks_release_the_origin_before_dispatch() {
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let session = Arc::new(session);
|
||||
let turn = Arc::new(turn);
|
||||
let broker = Arc::new(CodeModeDispatchBroker::new(
|
||||
session.thread_id,
|
||||
/*executed_tool_calls*/ Default::default(),
|
||||
));
|
||||
let delegate = CodeModeCellDelegate {
|
||||
broker: Arc::clone(&broker),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
};
|
||||
let origin = Arc::downgrade(&delegate.step_context);
|
||||
let cell_id = CellId::new("queued-cell".to_string());
|
||||
let cancellation_token = CancellationToken::new();
|
||||
let mut callback = delegate.invoke_tool(
|
||||
CodeModeNestedToolCall {
|
||||
cell_id: cell_id.clone(),
|
||||
runtime_tool_call_id: "call-a".to_string(),
|
||||
tool_name: codex_tools::ToolName::plain("test-tool"),
|
||||
tool_kind: codex_code_mode::CodeModeToolKind::Function,
|
||||
input: None,
|
||||
},
|
||||
cancellation_token.clone(),
|
||||
);
|
||||
assert!(futures::poll!(callback.as_mut()).is_pending());
|
||||
assert_eq!(broker.dispatch_rx.len(), 1);
|
||||
drop(callback);
|
||||
drop(delegate);
|
||||
assert!(
|
||||
origin.upgrade().is_none(),
|
||||
"queued work must not own the step"
|
||||
);
|
||||
|
||||
// Observe the queued message's result after its original receiver was dropped.
|
||||
let mut message = broker.dispatch_rx.try_recv().unwrap();
|
||||
let (response_tx, response_rx) = oneshot::channel();
|
||||
let DispatchMessage::InvokeTool {
|
||||
response_tx: queued_response,
|
||||
..
|
||||
} = &mut message
|
||||
else {
|
||||
panic!("expected tool invocation");
|
||||
};
|
||||
*queued_response = response_tx;
|
||||
broker.dispatch_tx.send(message).await.unwrap();
|
||||
broker.mark_cell_ready_for_dispatch(&cell_id, /*originating_item_id*/ None);
|
||||
let _worker = broker.start_turn_worker(
|
||||
Arc::clone(&session),
|
||||
StepContext::for_test(Arc::clone(&turn)),
|
||||
Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
);
|
||||
let response = response_rx.await;
|
||||
assert_eq!(
|
||||
response.unwrap(),
|
||||
Err("code mode nested tool call cancelled".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ impl CodeModeExecuteHandler {
|
||||
async fn execute(
|
||||
&self,
|
||||
session: std::sync::Arc<crate::session::session::Session>,
|
||||
step_context: std::sync::Arc<crate::session::step_context::StepContext>,
|
||||
step_context: Arc<crate::session::step_context::StepContext>,
|
||||
call_id: String,
|
||||
originating_item_id: Option<codex_protocol::ResponseItemId>,
|
||||
code: String,
|
||||
@@ -71,13 +71,16 @@ impl CodeModeExecuteHandler {
|
||||
.session
|
||||
.services
|
||||
.code_mode_service
|
||||
.execute(codex_code_mode::ExecuteRequest {
|
||||
tool_call_id: call_id.clone(),
|
||||
enabled_tools,
|
||||
source: args.code.clone(),
|
||||
yield_time_ms: args.yield_time_ms,
|
||||
max_output_tokens: args.max_output_tokens,
|
||||
})
|
||||
.execute(
|
||||
codex_code_mode::ExecuteRequest {
|
||||
tool_call_id: call_id.clone(),
|
||||
enabled_tools,
|
||||
source: args.code.clone(),
|
||||
yield_time_ms: args.yield_time_ms,
|
||||
max_output_tokens: args.max_output_tokens,
|
||||
},
|
||||
Arc::clone(&step_context),
|
||||
)
|
||||
.await
|
||||
.map_err(FunctionCallError::RespondToModel)?;
|
||||
let cell_id = started_cell.cell_id.clone();
|
||||
|
||||
@@ -47,6 +47,7 @@ use codex_utils_output_truncation::TruncationPolicy;
|
||||
use codex_utils_output_truncation::formatted_truncate_text_content_items_with_policy;
|
||||
use codex_utils_output_truncation::truncate_function_output_items_with_policy;
|
||||
|
||||
use delegate::CodeModeCellDelegate;
|
||||
use delegate::CodeModeDispatchBroker;
|
||||
use delegate::CodeModeDispatchWorker;
|
||||
pub(crate) use execute_handler::CodeModeExecuteHandler;
|
||||
@@ -125,14 +126,16 @@ impl CodeModeService {
|
||||
pub(crate) async fn execute(
|
||||
&self,
|
||||
mut request: codex_code_mode::ExecuteRequest,
|
||||
step_context: Arc<StepContext>,
|
||||
) -> Result<codex_code_mode::StartedCell, String> {
|
||||
request
|
||||
.yield_time_ms
|
||||
.get_or_insert(self.default_exec_yield_time_ms);
|
||||
self.session()
|
||||
.await?
|
||||
.execute(request, self.dispatch_broker.clone())
|
||||
.await
|
||||
let delegate = Arc::new(CodeModeCellDelegate {
|
||||
broker: Arc::clone(&self.dispatch_broker),
|
||||
step_context,
|
||||
});
|
||||
self.session().await?.execute(request, delegate).await
|
||||
}
|
||||
|
||||
pub(crate) async fn wait(
|
||||
@@ -209,18 +212,13 @@ impl CodeModeService {
|
||||
step_context: Arc<StepContext>,
|
||||
tracker: SharedTurnDiffTracker,
|
||||
) -> Option<CodeModeDispatchWorker> {
|
||||
let turn = &step_context.turn;
|
||||
if !step_context.tool_router.requires_code_mode_worker() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let exec = ExecContext {
|
||||
session: Arc::clone(session),
|
||||
turn: Arc::clone(turn),
|
||||
};
|
||||
Some(
|
||||
self.dispatch_broker
|
||||
.start_turn_worker(exec, step_context, tracker),
|
||||
.start_turn_worker(Arc::clone(session), step_context, tracker),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -346,7 +344,8 @@ fn truncate_code_mode_result(
|
||||
|
||||
// Submit synchronously so the recorder sees the call before the cell's dispatch gate closes.
|
||||
fn submit_nested_tool(
|
||||
exec: ExecContext,
|
||||
session: Arc<Session>,
|
||||
step_context: Arc<StepContext>,
|
||||
tool_runtime: ToolCallRuntime,
|
||||
invocation: CodeModeNestedToolCall,
|
||||
call_id: String,
|
||||
@@ -362,8 +361,8 @@ fn submit_nested_tool(
|
||||
tool_kind,
|
||||
input,
|
||||
} = invocation;
|
||||
let thread_id = exec.session.thread_id;
|
||||
let turn_id = exec.turn.sub_id.clone();
|
||||
let thread_id = session.thread_id;
|
||||
let turn_id = step_context.turn.sub_id.clone();
|
||||
let tool_name = tool_name.with_default_namespace();
|
||||
// A cell can outlive a turn; the broker records arrival before a dispatching turn is known.
|
||||
tracing::event!(
|
||||
@@ -402,16 +401,17 @@ fn submit_nested_tool(
|
||||
payload,
|
||||
encrypted_function_args: None,
|
||||
};
|
||||
exec.session
|
||||
session
|
||||
.services
|
||||
.analytics_events_client
|
||||
.track_code_mode_tool_call(codex_analytics::CodeModeToolCallFact::ChildStarted {
|
||||
thread_id: exec.session.thread_id.to_string(),
|
||||
turn_id: exec.turn.sub_id.clone(),
|
||||
thread_id: session.thread_id.to_string(),
|
||||
turn_id: step_context.turn.sub_id.clone(),
|
||||
call_id: call.call_id.clone(),
|
||||
cell_id: cell_id.to_string(),
|
||||
});
|
||||
let result = tool_runtime.handle_tool_call_with_source(
|
||||
step_context,
|
||||
call,
|
||||
ToolCallSource::CodeMode {
|
||||
cell_id: cell_id.to_string(),
|
||||
|
||||
Reference in New Issue
Block a user