mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
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:
1 parent
ed4ca07ba6
commit
205f3671e1
24 files changed
+1053
-241
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user