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 authored and copyberry committed 2026-09-09 17:45:37 +00:00
1 parent ed4ca07ba6
commit 205f3671e1
24 files changed
+1053 -241

No files matched your search

+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(),