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"