Use captured step settings for tool planning and execution (#44242)

## Why

After a model change within a turn, tools could still use the turn's original model capabilities, output limits, and telemetry attribution.

## What changed

- Build each step's tool plan from its captured model settings.
- Use those settings for image support and detail, command and MCP output truncation, extension metadata, and tool telemetry.
- Preserve the issuing step's model for delayed command completion events while allowing later `write_stdin` calls to use their own step's output limits.

## Testing

Add regression coverage for model changes within a turn, including tool replanning, Code Mode execution, image handling, MCP tool and resource output limits, extension settings, and delayed command completion.

GitOrigin-RevId: ef991598e89a846889baf770a0c9e12e56886d5d
This commit is contained in:
Abhinav
2026-09-09 17:45:37 +00:00
committed by copyberry
parent ed4ca07ba6
commit 205f3671e1
24 changed files with 1053 additions and 241 deletions
@@ -33,7 +33,7 @@ impl CodeModeExecuteHandler {
async fn execute(
&self,
session: std::sync::Arc<crate::session::session::Session>,
turn: std::sync::Arc<crate::session::turn_context::TurnContext>,
step_context: std::sync::Arc<crate::session::step_context::StepContext>,
call_id: String,
originating_item_id: Option<codex_protocol::ResponseItemId>,
code: String,
@@ -41,7 +41,10 @@ impl CodeModeExecuteHandler {
) -> Result<FunctionToolOutput, FunctionCallError> {
let args =
codex_code_mode::parse_exec_source(&code).map_err(FunctionCallError::RespondToModel)?;
let exec = ExecContext { session, turn };
let exec = ExecContext {
session,
turn: Arc::clone(&step_context.turn),
};
let mut enabled_tools = Vec::with_capacity(self.nested_tool_specs.len());
for (spec, cached_runtime) in &self.nested_tool_specs {
if let Some(cached_definitions) = cached_runtime
@@ -138,9 +141,14 @@ impl CodeModeExecuteHandler {
let wall_time = response
.code_mode_host_duration()
.unwrap_or_else(|| started_at.elapsed());
handle_runtime_response(&exec, response, args.max_output_tokens, wall_time)
.await
.map_err(FunctionCallError::RespondToModel)
handle_runtime_response(
&step_context.settings.model_info,
response,
args.max_output_tokens,
wall_time,
)
.await
.map_err(FunctionCallError::RespondToModel)
}
}
@@ -170,6 +178,7 @@ impl CodeModeExecuteHandler {
let ToolInvocation {
session,
turn,
step_context,
call_id,
tool_name,
payload,
@@ -188,7 +197,7 @@ impl CodeModeExecuteHandler {
ToolPayload::Custom { input } if is_exec_tool_name(&tool_name) => self
.execute(
session,
turn,
step_context,
call_id,
originating_item_id,
input,
+5 -8
View File
@@ -248,24 +248,25 @@ impl CodeModeService {
}
pub(super) async fn handle_runtime_response(
exec: &ExecContext,
model_info: &codex_protocol::openai_models::ModelInfo,
response: RuntimeResponse,
max_output_tokens: Option<usize>,
wall_time: Duration,
) -> Result<FunctionToolOutput, String> {
let script_status = format_script_status(&response);
let supports_original = can_request_original_image_detail(model_info);
match response {
RuntimeResponse::Yielded { content_items, .. } => {
let mut content_items = into_function_call_output_content_items(content_items);
sanitize_runtime_image_detail(exec.turn.as_ref(), &mut content_items);
sanitize_image_detail_items(supports_original, &mut content_items);
content_items = truncate_code_mode_result(content_items, max_output_tokens);
prepend_script_status(&mut content_items, &script_status, wall_time);
Ok(FunctionToolOutput::from_content(content_items, Some(true)))
}
RuntimeResponse::Terminated { content_items, .. } => {
let mut content_items = into_function_call_output_content_items(content_items);
sanitize_runtime_image_detail(exec.turn.as_ref(), &mut content_items);
sanitize_image_detail_items(supports_original, &mut content_items);
content_items = truncate_code_mode_result(content_items, max_output_tokens);
prepend_script_status(&mut content_items, &script_status, wall_time);
Ok(FunctionToolOutput::from_content(content_items, Some(true)))
@@ -276,7 +277,7 @@ pub(super) async fn handle_runtime_response(
..
} => {
let mut content_items = into_function_call_output_content_items(content_items);
sanitize_runtime_image_detail(exec.turn.as_ref(), &mut content_items);
sanitize_image_detail_items(supports_original, &mut content_items);
let success = error_text.is_none();
if let Some(error_text) = error_text {
content_items.push(FunctionCallOutputContentItem::InputText {
@@ -293,10 +294,6 @@ pub(super) async fn handle_runtime_response(
}
}
fn sanitize_runtime_image_detail(turn: &TurnContext, items: &mut [FunctionCallOutputContentItem]) {
sanitize_image_detail_items(can_request_original_image_detail(turn.model_info()), items);
}
fn format_script_status(response: &RuntimeResponse) -> String {
match response {
RuntimeResponse::Yielded { cell_id, .. } => {
@@ -70,6 +70,7 @@ impl CodeModeWaitHandler {
let ToolInvocation {
session,
turn,
step_context,
call_id,
tool_name,
payload,
@@ -159,10 +160,15 @@ impl CodeModeWaitHandler {
let wall_time = wait_response
.code_mode_host_duration()
.unwrap_or_else(|| started_at.elapsed());
handle_runtime_response(&exec, wait_response.into(), args.max_tokens, wall_time)
.await
.map_err(FunctionCallError::RespondToModel)
.map(boxed_tool_output)
handle_runtime_response(
&step_context.settings.model_info,
wait_response.into(),
args.max_tokens,
wall_time,
)
.await
.map_err(FunctionCallError::RespondToModel)
.map(boxed_tool_output)
}
_ => Err(FunctionCallError::RespondToModel(format!(
"{WAIT_TOOL_NAME} expects JSON arguments"
+36 -8
View File
@@ -18,6 +18,7 @@ use codex_protocol::items::CommandExecutionItem;
use codex_protocol::items::CommandExecutionStatus;
use codex_protocol::items::FileChangeItem;
use codex_protocol::items::TurnItem;
use codex_protocol::openai_models::ModelInfo;
use codex_protocol::parse_command::ParsedCommand;
use codex_protocol::protocol::EventMsg;
use codex_protocol::protocol::ExecCommandSource;
@@ -44,6 +45,8 @@ pub(super) fn truncate_rejection_message(message: &str) -> String {
pub(crate) struct ToolEventCtx<'a> {
pub session: &'a Session,
pub turn: &'a TurnContext,
/// Model captured by the step that issued this call, including delayed completion events.
pub model_info: &'a ModelInfo,
pub call_id: &'a str,
pub turn_diff_tracker: Option<&'a SharedTurnDiffTracker>,
}
@@ -52,12 +55,14 @@ impl<'a> ToolEventCtx<'a> {
pub fn new(
session: &'a Session,
turn: &'a TurnContext,
model_info: &'a ModelInfo,
call_id: &'a str,
turn_diff_tracker: Option<&'a SharedTurnDiffTracker>,
) -> Self {
Self {
session,
turn,
model_info,
call_id,
turn_diff_tracker,
}
@@ -117,12 +122,17 @@ async fn emit_exec_command_begin(ctx: ToolEventCtx<'_>, exec_input: &ExecCommand
("output_format", operation.output_format),
("execution_backend", "unified_exec"),
];
ctx.turn.session_telemetry.counter(
let session_telemetry = ctx
.turn
.session_telemetry
.clone()
.with_model(&ctx.model_info.slug, &ctx.model_info.slug);
session_telemetry.counter(
ARTIFACT_OPERATION_STARTED_METRIC,
/*inc*/ 1,
&metric_tags,
);
ctx.turn.session_telemetry.histogram(
session_telemetry.histogram(
ARTIFACT_OPERATION_EXPECTED_OUTPUT_COUNT_METRIC,
i64::from(operation.expected_output_count),
&metric_tags,
@@ -132,7 +142,7 @@ async fn emit_exec_command_begin(ctx: ToolEventCtx<'_>, exec_input: &ExecCommand
.analytics_events_client
.track_artifact_operation(
build_track_events_context(
ctx.turn.model_info().slug.clone(),
ctx.model_info.slug.clone(),
ctx.session.thread_id.to_string(),
ctx.turn.sub_id.clone(),
ctx.turn.originator.clone(),
@@ -373,7 +383,7 @@ impl ToolEmitter {
output: &ExecToolCallOutput,
ctx: ToolEventCtx<'_>,
) -> String {
super::format_exec_output_for_model(output, ctx.turn.model_info().truncation_policy.into())
super::format_exec_output_for_model(output, ctx.model_info.truncation_policy.into())
}
pub async fn finish(
@@ -521,7 +531,7 @@ async fn emit_exec_stage(
duration: output.duration,
formatted_output: format_exec_output_str(
&output,
ctx.turn.model_info().truncation_policy.into(),
ctx.model_info.truncation_policy.into(),
),
status: if output.exit_code == 0 {
ExecCommandStatus::Completed
@@ -697,7 +707,13 @@ mod tests {
environment_id: None,
}
.finish(
ToolEventCtx::new(session.as_ref(), turn.as_ref(), "call-id", Some(&tracker)),
ToolEventCtx::new(
session.as_ref(),
turn.as_ref(),
turn.model_info(),
"call-id",
Some(&tracker),
),
out,
Some(&delta),
)
@@ -781,7 +797,13 @@ mod tests {
.expect("apply patch");
emit_patch_end(
ToolEventCtx::new(session.as_ref(), turn.as_ref(), "call-id", Some(&tracker)),
ToolEventCtx::new(
session.as_ref(),
turn.as_ref(),
turn.model_info(),
"call-id",
Some(&tracker),
),
HashMap::new(),
String::new(),
String::new(),
@@ -830,7 +852,13 @@ mod tests {
tracker.lock().await.track_delta("", &delta);
emit_patch_end(
ToolEventCtx::new(session.as_ref(), turn.as_ref(), "call-id", Some(&tracker)),
ToolEventCtx::new(
session.as_ref(),
turn.as_ref(),
turn.model_info(),
"call-id",
Some(&tracker),
),
HashMap::new(),
String::new(),
String::new(),
@@ -589,6 +589,7 @@ async fn execute_verified_patch(
let event_ctx = ToolEventCtx::new(
tool_ctx.session.as_ref(),
tool_ctx.step_context.turn.as_ref(),
&tool_ctx.step_context.settings.model_info,
&tool_ctx.call_id,
tracker,
);
@@ -616,6 +617,7 @@ async fn execute_verified_patch(
let event_ctx = ToolEventCtx::new(
tool_ctx.session.as_ref(),
tool_ctx.step_context.turn.as_ref(),
&tool_ctx.step_context.settings.model_info,
&tool_ctx.call_id,
tracker,
);
@@ -166,13 +166,14 @@ impl TurnItemEmitter for CoreTurnItemEmitter {
async fn to_extension_call(invocation: &ToolInvocation) -> ExtensionToolCall<'_> {
let conversation_history =
ConversationHistory::new(invocation.session.clone_history().await.into_raw_items());
let settings = &invocation.step_context.settings;
let codex_turn_metadata = invocation
.turn
.turn_metadata_state
.current_meta_value_for_mcp_request(McpTurnMetadataContext {
model: invocation.turn.model_info().slug.as_str(),
reasoning_effort: invocation.turn.effective_reasoning_effort(),
node_repl_disabled: invocation.turn.model_info().node_repl_disabled,
model: settings.model_info.slug.as_str(),
reasoning_effort: settings.effective_reasoning_effort(),
node_repl_disabled: settings.model_info.node_repl_disabled,
})
.and_then(|metadata| to_ascii_json_string(&metadata).ok());
let mut environments = Vec::new();
@@ -204,9 +205,9 @@ async fn to_extension_call(invocation: &ToolInvocation) -> ExtensionToolCall<'_>
turn_id: invocation.turn.sub_id.clone(),
call_id: invocation.call_id.clone(),
tool_name: invocation.tool_name.clone(),
model: invocation.turn.model_info().slug.clone(),
model: settings.model_info.slug.clone(),
codex_turn_metadata,
truncation_policy: invocation.turn.model_info().truncation_policy.into(),
truncation_policy: settings.model_info.truncation_policy.into(),
source: extension_tool_call_source(invocation.source.clone()),
conversation_history,
turn_item_emitter: Arc::new(CoreTurnItemEmitter {
@@ -417,7 +418,7 @@ mod tests {
}
#[tokio::test]
async fn passes_turn_fields_and_scoped_turn_item_emitter_to_extension_call() {
async fn passes_step_node_repl_flag_and_scoped_turn_item_emitter_to_extension_call() {
let captured_call = Arc::new(Mutex::new(None));
let captured_sandbox_cwds = Arc::new(Mutex::new(Vec::new()));
let handler = ExtensionToolAdapter::new(Arc::new(CapturingExtensionExecutor {
@@ -464,7 +465,15 @@ mod tests {
strip_response_item_id(raw_history_item.item),
expected_history_item
);
let step_context = StepContext::for_test(Arc::clone(&turn));
let mut step_context = StepContext::for_test(Arc::clone(&turn));
let settings = Arc::make_mut(
&mut Arc::get_mut(&mut step_context)
.expect("unshared step")
.settings,
);
let model_info = Arc::make_mut(&mut settings.model_info);
model_info.node_repl_disabled = !turn.model_info().node_repl_disabled;
let node_repl_disabled = model_info.node_repl_disabled;
let invocation = ToolInvocation {
session,
step_context,
@@ -497,6 +506,14 @@ mod tests {
);
assert_eq!(captured_call.model, model);
assert_eq!(captured_call.truncation_policy, truncation_policy);
let metadata: serde_json::Value = serde_json::from_str(
captured_call
.codex_turn_metadata
.as_deref()
.expect("turn metadata"),
)
.expect("metadata JSON");
assert_eq!(metadata["node_repl_disabled"], json!(node_repl_disabled));
assert_eq!(
captured_call.source,
ExtensionToolCallSource::CodeMode {
+4 -3
View File
@@ -207,7 +207,6 @@ impl McpHandler {
payload,
..
} = invocation;
let turn = Arc::clone(&step_context.turn);
let payload = match payload {
ToolPayload::Function { arguments } => arguments,
@@ -223,7 +222,7 @@ impl McpHandler {
.as_ref()
.and_then(codex_mcp::PreparedMcpCall::output_token_limit)
.map(TruncationPolicy::Tokens)
.unwrap_or(turn.model_info().truncation_policy.into());
.unwrap_or(step_context.settings.model_info.truncation_policy.into());
let started = Instant::now();
let result = handle_mcp_tool_call(
Arc::clone(&session),
@@ -243,7 +242,9 @@ impl McpHandler {
result: result.result,
tool_input: result.tool_input,
wall_time: started.elapsed(),
original_image_detail_supported: can_request_original_image_detail(turn.model_info()),
original_image_detail_supported: can_request_original_image_detail(
&step_context.settings.model_info,
),
truncation_policy,
}))
}
@@ -26,6 +26,7 @@ use serde_json::Value;
use crate::function_tool::FunctionCallError;
use crate::session::session::Session;
use crate::session::step_context::StepContext;
use crate::session::turn_context::TurnContext;
use crate::tools::context::FunctionToolOutput;
use crate::tools::context::ToolOutput;
@@ -279,7 +280,7 @@ async fn emit_tool_call_end(
async fn run_resource_operation<T>(
session: &Arc<Session>,
turn: &TurnContext,
step_context: &StepContext,
call_id: &str,
invocation: McpInvocation,
operation: impl Future<Output = Result<T, FunctionCallError>>,
@@ -287,10 +288,14 @@ async fn run_resource_operation<T>(
where
T: Serialize,
{
let turn = &step_context.turn;
emit_tool_call_begin(session, turn, call_id, invocation.clone()).await;
let start = Instant::now();
let result = operation.await.and_then(|payload| {
serialize_function_output(payload, turn.model_info().truncation_policy.into())
serialize_function_output(
payload,
step_context.settings.model_info.truncation_policy.into(),
)
});
match result {
@@ -72,7 +72,7 @@ impl ListMcpResourceTemplatesHandler {
arguments: arguments.clone(),
};
run_resource_operation(&session, turn.as_ref(), &call_id, invocation, async {
run_resource_operation(&session, &step_context, &call_id, invocation, async {
if let Some((server_name, params)) = args.target(turn.as_ref())? {
let result = mcp
.list_resource_templates(&server_name, params)
@@ -72,7 +72,7 @@ impl ListMcpResourcesHandler {
arguments: arguments.clone(),
};
run_resource_operation(&session, turn.as_ref(), &call_id, invocation, async {
run_resource_operation(&session, &step_context, &call_id, invocation, async {
if let Some((server_name, params)) = args.target(turn.as_ref())? {
let result = mcp
.list_resources(&server_name, params)
@@ -77,7 +77,7 @@ impl ReadMcpResourceHandler {
arguments: arguments.clone(),
};
run_resource_operation(&session, turn.as_ref(), &call_id, invocation, async {
run_resource_operation(&session, &step_context, &call_id, invocation, async {
ensure_model_can_access_mcp_server(turn.as_ref(), &server)?;
let result = mcp
.read_resource(&server, ReadResourceRequestParams::new(uri.clone()))
@@ -220,7 +220,7 @@ impl RequestPluginInstallHandler {
.analytics_events_client
.track_plugin_install_requested(
build_track_events_context(
turn.model_info().slug.clone(),
step_context.settings.model_info.slug.clone(),
session.thread_id.to_string(),
turn.sub_id.clone(),
turn.originator.clone(),
@@ -283,14 +283,16 @@ impl RequestPluginInstallHandler {
Some(_) => "unknown",
None => "unavailable",
};
turn.session_telemetry.record_plugin_install_suggestion(
tool_type,
tool.id(),
tool.name(),
response_action,
user_confirmed,
completed,
);
step_context
.session_telemetry
.record_plugin_install_suggestion(
tool_type,
tool.id(),
tool.name(),
response_action,
user_confirmed,
completed,
);
}
let content = serde_json::to_string(&RequestPluginInstallResult {
@@ -407,7 +407,7 @@ impl ExecCommandHandler {
chunk_id: String::new(),
wall_time: std::time::Duration::ZERO,
raw_output: output.into_text().into_bytes(),
truncation_policy: turn.model_info().truncation_policy.into(),
truncation_policy: step_context.settings.model_info.truncation_policy.into(),
max_output_tokens,
process_id: None,
exit_code: None,
@@ -417,7 +417,7 @@ impl ExecCommandHandler {
}));
}
emit_unified_exec_tty_metric(&turn.session_telemetry, tty);
emit_unified_exec_tty_metric(&step_context.session_telemetry, tty);
let request = ExecCommandRequest {
command,
shell_type,
@@ -461,7 +461,7 @@ impl ExecCommandHandler {
chunk_id: generate_chunk_id(),
wall_time: output.duration,
raw_output: output_text.into_bytes(),
truncation_policy: turn.model_info().truncation_policy.into(),
truncation_policy: step_context.settings.model_info.truncation_policy.into(),
max_output_tokens,
// Sandbox denial is terminal, so there is no live
// process for write_stdin to resume.
@@ -91,7 +91,12 @@ impl WriteStdinHandler {
input: &args.chars,
yield_time_ms: args.yield_time_ms,
max_output_tokens: args.max_output_tokens,
truncation_policy: turn.model_info().truncation_policy.into(),
truncation_policy: context
.step_context
.settings
.model_info
.truncation_policy
.into(),
interaction_event: Some(WriteStdinInteractionEvent {
session: &session,
turn: &turn,
@@ -95,8 +95,9 @@ impl ViewImageHandler {
invocation: ToolInvocation,
) -> Result<Box<dyn crate::tools::context::ToolOutput>, FunctionCallError> {
if !invocation
.turn
.model_info()
.step_context
.settings
.model_info
.input_modalities
.contains(&InputModality::Image)
{
@@ -185,7 +186,8 @@ impl ViewImageHandler {
FunctionCallError::RespondToModel(VIEW_IMAGE_INVALID_MESSAGE.to_string())
})?;
let can_request_original_detail = can_request_original_image_detail(turn.model_info());
let can_request_original_detail =
can_request_original_image_detail(&step_context.settings.model_info);
let use_original_detail = self.options.unified_image_budget
|| can_request_original_detail && matches!(detail, Some(ViewImageDetail::Original));
let image_detail = if use_original_detail {
+1 -1
View File
@@ -499,7 +499,7 @@ impl ToolRegistry {
) -> Result<AnyToolResult, FunctionCallError> {
let tool_name = invocation.tool_name.clone();
let call_id_owned = invocation.call_id.clone();
let otel = invocation.turn.session_telemetry.clone();
let otel = invocation.step_context.session_telemetry.clone();
// TODO(anp): Reconcile these tags with TurnEnvironment::sandbox_context
// instead of reporting the thread-wide backend for environment-scoped tools.
let sandbox_tags = invocation.turn.turn_metadata_state.sandbox_tags;
-2
View File
@@ -160,7 +160,6 @@ impl ToolRouter {
}
/// Whether the model can both start and interact with a terminal process.
// Consumed by the follow-up live tool-plan selection.
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn has_terminal_controls(&self) -> bool {
self.exposes_tool(&ToolName::plain("exec_command"))
@@ -168,7 +167,6 @@ impl ToolRouter {
}
/// Whether the configured collaboration backend's child-management tools remain exposed.
// Consumed by the follow-up live tool-plan selection.
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) fn can_manage_children(&self) -> bool {
self.can_manage_children