mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
core: flow step context through tool execution
This commit is contained in:
@@ -46,7 +46,7 @@ const CONTEXT_WINDOW_TRUNCATED_OUTPUT_MESSAGE: &str =
|
||||
|
||||
pub(crate) async fn run_inline_remote_auto_compact_task(
|
||||
sess: Arc<Session>,
|
||||
turn_context: Arc<TurnContext>,
|
||||
step_context: Arc<StepContext>,
|
||||
turn_state: Arc<OnceLock<String>>,
|
||||
initial_context_injection: InitialContextInjection,
|
||||
reason: CompactionReason,
|
||||
@@ -54,7 +54,7 @@ pub(crate) async fn run_inline_remote_auto_compact_task(
|
||||
) -> CodexResult<()> {
|
||||
run_remote_compact_task_inner(
|
||||
&sess,
|
||||
&turn_context,
|
||||
&step_context,
|
||||
Some(turn_state),
|
||||
initial_context_injection,
|
||||
CompactionTrigger::Auto,
|
||||
@@ -69,6 +69,8 @@ pub(crate) async fn run_remote_compact_task(
|
||||
sess: Arc<Session>,
|
||||
turn_context: Arc<TurnContext>,
|
||||
) -> CodexResult<()> {
|
||||
// Standalone compaction is its own request boundary, so it captures a fresh step.
|
||||
let step_context = sess.capture_step_context(Arc::clone(&turn_context)).await;
|
||||
let start_event = EventMsg::TurnStarted(TurnStartedEvent {
|
||||
turn_id: turn_context.sub_id.clone(),
|
||||
trace_id: turn_context.trace_id.clone(),
|
||||
@@ -80,7 +82,7 @@ pub(crate) async fn run_remote_compact_task(
|
||||
|
||||
run_remote_compact_task_inner(
|
||||
&sess,
|
||||
&turn_context,
|
||||
&step_context,
|
||||
/*turn_state*/ None,
|
||||
InitialContextInjection::DoNotInject,
|
||||
CompactionTrigger::Manual,
|
||||
@@ -93,13 +95,14 @@ pub(crate) async fn run_remote_compact_task(
|
||||
|
||||
async fn run_remote_compact_task_inner(
|
||||
sess: &Arc<Session>,
|
||||
turn_context: &Arc<TurnContext>,
|
||||
step_context: &Arc<StepContext>,
|
||||
turn_state: Option<Arc<OnceLock<String>>>,
|
||||
initial_context_injection: InitialContextInjection,
|
||||
trigger: CompactionTrigger,
|
||||
reason: CompactionReason,
|
||||
phase: CompactionPhase,
|
||||
) -> CodexResult<()> {
|
||||
let turn_context = &step_context.turn;
|
||||
let compaction_metadata = CompactionTurnMetadata::new(
|
||||
trigger,
|
||||
reason,
|
||||
@@ -137,7 +140,7 @@ async fn run_remote_compact_task_inner(
|
||||
}
|
||||
let result = run_remote_compact_task_inner_impl(
|
||||
sess,
|
||||
turn_context,
|
||||
step_context,
|
||||
turn_state,
|
||||
initial_context_injection,
|
||||
compaction_metadata,
|
||||
@@ -171,12 +174,13 @@ async fn run_remote_compact_task_inner(
|
||||
|
||||
async fn run_remote_compact_task_inner_impl(
|
||||
sess: &Arc<Session>,
|
||||
turn_context: &Arc<TurnContext>,
|
||||
step_context: &Arc<StepContext>,
|
||||
turn_state: Option<Arc<OnceLock<String>>>,
|
||||
initial_context_injection: InitialContextInjection,
|
||||
compaction_metadata: CompactionTurnMetadata,
|
||||
analytics_details: &mut CompactionAnalyticsDetails,
|
||||
) -> CodexResult<()> {
|
||||
let turn_context = &step_context.turn;
|
||||
let context_compaction_item = ContextCompactionItem::new();
|
||||
// Use the UI compaction item ID as the trace compaction ID so protocol lifecycle events,
|
||||
// endpoint attempts, and the installed history checkpoint all have one join key.
|
||||
@@ -222,8 +226,7 @@ async fn run_remote_compact_task_inner_impl(
|
||||
let prompt_input = history.for_prompt(&turn_context.model_info.input_modalities);
|
||||
let tool_router = built_tools(
|
||||
sess.as_ref(),
|
||||
turn_context.as_ref(),
|
||||
Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
step_context.as_ref(),
|
||||
&CancellationToken::new(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -56,7 +56,7 @@ const MAX_REMOTE_COMPACTION_V2_STREAM_RETRIES: u64 = 2;
|
||||
|
||||
pub(crate) async fn run_inline_remote_auto_compact_task(
|
||||
sess: Arc<Session>,
|
||||
turn_context: Arc<TurnContext>,
|
||||
step_context: Arc<StepContext>,
|
||||
client_session: &mut ModelClientSession,
|
||||
initial_context_injection: InitialContextInjection,
|
||||
reason: CompactionReason,
|
||||
@@ -64,7 +64,7 @@ pub(crate) async fn run_inline_remote_auto_compact_task(
|
||||
) -> CodexResult<()> {
|
||||
run_remote_compact_task_inner(
|
||||
&sess,
|
||||
&turn_context,
|
||||
&step_context,
|
||||
Some(client_session),
|
||||
initial_context_injection,
|
||||
CompactionTrigger::Auto,
|
||||
@@ -78,6 +78,8 @@ pub(crate) async fn run_remote_compact_task(
|
||||
sess: Arc<Session>,
|
||||
turn_context: Arc<TurnContext>,
|
||||
) -> CodexResult<()> {
|
||||
// Standalone compaction is its own request boundary, so it captures a fresh step.
|
||||
let step_context = sess.capture_step_context(Arc::clone(&turn_context)).await;
|
||||
let start_event = EventMsg::TurnStarted(TurnStartedEvent {
|
||||
turn_id: turn_context.sub_id.clone(),
|
||||
trace_id: turn_context.trace_id.clone(),
|
||||
@@ -89,7 +91,7 @@ pub(crate) async fn run_remote_compact_task(
|
||||
|
||||
run_remote_compact_task_inner(
|
||||
&sess,
|
||||
&turn_context,
|
||||
&step_context,
|
||||
/*client_session*/ None,
|
||||
InitialContextInjection::DoNotInject,
|
||||
CompactionTrigger::Manual,
|
||||
@@ -101,13 +103,14 @@ pub(crate) async fn run_remote_compact_task(
|
||||
|
||||
async fn run_remote_compact_task_inner(
|
||||
sess: &Arc<Session>,
|
||||
turn_context: &Arc<TurnContext>,
|
||||
step_context: &Arc<StepContext>,
|
||||
client_session: Option<&mut ModelClientSession>,
|
||||
initial_context_injection: InitialContextInjection,
|
||||
trigger: CompactionTrigger,
|
||||
reason: CompactionReason,
|
||||
phase: CompactionPhase,
|
||||
) -> CodexResult<()> {
|
||||
let turn_context = &step_context.turn;
|
||||
let compaction_metadata = CompactionTurnMetadata::new(
|
||||
trigger,
|
||||
reason,
|
||||
@@ -145,7 +148,7 @@ async fn run_remote_compact_task_inner(
|
||||
}
|
||||
let result = run_remote_compact_task_inner_impl(
|
||||
sess,
|
||||
turn_context,
|
||||
step_context,
|
||||
client_session,
|
||||
initial_context_injection,
|
||||
compaction_metadata,
|
||||
@@ -182,12 +185,13 @@ async fn run_remote_compact_task_inner(
|
||||
|
||||
async fn run_remote_compact_task_inner_impl(
|
||||
sess: &Arc<Session>,
|
||||
turn_context: &Arc<TurnContext>,
|
||||
step_context: &Arc<StepContext>,
|
||||
client_session: Option<&mut ModelClientSession>,
|
||||
initial_context_injection: InitialContextInjection,
|
||||
compaction_metadata: CompactionTurnMetadata,
|
||||
analytics_details: &mut CompactionAnalyticsDetails,
|
||||
) -> CodexResult<()> {
|
||||
let turn_context = &step_context.turn;
|
||||
let context_compaction_item = ContextCompactionItem::new();
|
||||
let compaction_trace = sess.services.rollout_thread_trace.compaction_trace_context(
|
||||
turn_context.sub_id.as_str(),
|
||||
@@ -230,8 +234,7 @@ async fn run_remote_compact_task_inner_impl(
|
||||
let prompt_input = history.for_prompt(&turn_context.model_info.input_modalities);
|
||||
let tool_router = built_tools(
|
||||
sess.as_ref(),
|
||||
turn_context.as_ref(),
|
||||
Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
step_context.as_ref(),
|
||||
&CancellationToken::new(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -3,6 +3,7 @@ use std::sync::Arc;
|
||||
use codex_exec_server::EnvironmentManager;
|
||||
use codex_exec_server::ExecServerRuntimePaths;
|
||||
use codex_extension_api::UserInstructionsProvider;
|
||||
use codex_features::Feature;
|
||||
use codex_login::AuthManager;
|
||||
use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::Result as CodexResult;
|
||||
@@ -14,7 +15,6 @@ use tokio_util::sync::CancellationToken;
|
||||
use crate::config::Config;
|
||||
use crate::resolve_installation_id;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::session::turn::build_prompt;
|
||||
use crate::session::turn::built_tools;
|
||||
use crate::state_db_bridge::StateDbHandle;
|
||||
@@ -78,7 +78,8 @@ pub(crate) async fn build_prompt_input_from_session(
|
||||
input: Vec<UserInput>,
|
||||
) -> CodexResult<Vec<ResponseItem>> {
|
||||
let turn_context = sess.new_default_turn().await;
|
||||
sess.record_context_updates_and_set_reference_context_item(turn_context.as_ref())
|
||||
let world_state = sess
|
||||
.record_context_updates_and_set_reference_context_item(turn_context.as_ref())
|
||||
.await;
|
||||
|
||||
if !input.is_empty() {
|
||||
@@ -87,17 +88,21 @@ pub(crate) async fn build_prompt_input_from_session(
|
||||
.await;
|
||||
}
|
||||
|
||||
let step_context = sess.capture_step_context(Arc::clone(&turn_context)).await;
|
||||
if turn_context
|
||||
.config
|
||||
.features
|
||||
.enabled(Feature::DeferredExecutor)
|
||||
{
|
||||
sess.record_step_environment_context_if_changed(&world_state, step_context.as_ref())
|
||||
.await;
|
||||
}
|
||||
|
||||
let prompt_input = sess
|
||||
.clone_history()
|
||||
.await
|
||||
.for_prompt(&turn_context.model_info.input_modalities);
|
||||
let router = built_tools(
|
||||
sess,
|
||||
turn_context.as_ref(),
|
||||
Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
&CancellationToken::new(),
|
||||
)
|
||||
.await?;
|
||||
let router = built_tools(sess, step_context.as_ref(), &CancellationToken::new()).await?;
|
||||
let base_instructions = sess.get_base_instructions().await;
|
||||
let prompt = build_prompt(
|
||||
prompt_input,
|
||||
|
||||
@@ -40,6 +40,7 @@ use crate::exec_policy::ExecPolicyManager;
|
||||
use crate::image_preparation::prepare_response_items;
|
||||
use crate::parse_turn_item;
|
||||
use crate::realtime_conversation::RealtimeConversationManager;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::session::turn_context::TurnEnvironment;
|
||||
use crate::session_prefix::format_inter_agent_completion_message;
|
||||
use crate::skills::SkillRenderSideEffects;
|
||||
@@ -2763,10 +2764,11 @@ impl Session {
|
||||
|
||||
pub(crate) async fn record_step_environment_context_if_changed(
|
||||
&self,
|
||||
turn_context: &TurnContext,
|
||||
previous_world_state: &Arc<WorldState>,
|
||||
step_context: &step_context::StepContext,
|
||||
) -> Arc<WorldState> {
|
||||
let turn_context = step_context.turn.as_ref();
|
||||
// Render model-visible state from the same step used to build and run tools.
|
||||
let world_state = Arc::new(
|
||||
self.build_world_state_for_environments(turn_context, &step_context.environments)
|
||||
.await,
|
||||
@@ -2789,20 +2791,19 @@ impl Session {
|
||||
|
||||
pub(crate) async fn capture_step_context(
|
||||
&self,
|
||||
turn_context: &TurnContext,
|
||||
turn_context: Arc<TurnContext>,
|
||||
) -> Arc<StepContext> {
|
||||
let step_context = if turn_context
|
||||
// Keep the old turn-frozen view unless deferred executors are explicitly enabled.
|
||||
let environments = if turn_context
|
||||
.config
|
||||
.features
|
||||
.enabled(Feature::DeferredExecutor)
|
||||
{
|
||||
StepContext {
|
||||
environments: self.services.turn_environments.snapshot().await,
|
||||
}
|
||||
self.services.turn_environments.snapshot().await
|
||||
} else {
|
||||
StepContext::from_turn_context(turn_context)
|
||||
turn_context.environments.clone()
|
||||
};
|
||||
Arc::new(step_context)
|
||||
Arc::new(StepContext::new(turn_context, environments))
|
||||
}
|
||||
|
||||
pub(crate) async fn record_inter_agent_communication(
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::environment_selection::TurnEnvironmentSnapshot;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
|
||||
/// Request-scoped state that may change between model sampling requests.
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct StepContext {
|
||||
pub(crate) turn: Arc<TurnContext>,
|
||||
pub(crate) environments: TurnEnvironmentSnapshot,
|
||||
}
|
||||
|
||||
impl StepContext {
|
||||
pub(crate) fn from_turn_context(turn_context: &TurnContext) -> Self {
|
||||
Self {
|
||||
environments: turn_context.environments.clone(),
|
||||
}
|
||||
pub(crate) fn new(turn: Arc<TurnContext>, environments: TurnEnvironmentSnapshot) -> Self {
|
||||
Self { turn, environments }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -183,6 +183,13 @@ use std::sync::Arc;
|
||||
use std::sync::OnceLock;
|
||||
use std::time::Duration as StdDuration;
|
||||
|
||||
impl StepContext {
|
||||
pub(crate) fn for_test(turn: Arc<TurnContext>) -> Arc<Self> {
|
||||
let environments = turn.environments.clone();
|
||||
Arc::new(Self::new(turn, environments))
|
||||
}
|
||||
}
|
||||
|
||||
mod guardian_tests;
|
||||
|
||||
struct InstructionsTestCase {
|
||||
@@ -596,9 +603,9 @@ async fn preview_session_start_hooks(
|
||||
}
|
||||
|
||||
fn test_tool_runtime(session: Arc<Session>, turn_context: Arc<TurnContext>) -> ToolCallRuntime {
|
||||
let router = Arc::new(ToolRouter::from_contexts(
|
||||
&turn_context,
|
||||
Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let router = Arc::new(ToolRouter::from_context(
|
||||
step_context.as_ref(),
|
||||
crate::tools::router::ToolRouterParams {
|
||||
tool_suggest_candidates: None,
|
||||
mcp_tools: None,
|
||||
@@ -609,7 +616,7 @@ fn test_tool_runtime(session: Arc<Session>, turn_context: Arc<TurnContext>) -> T
|
||||
&Default::default(),
|
||||
));
|
||||
let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new()));
|
||||
ToolCallRuntime::new(router, session, turn_context, tracker)
|
||||
ToolCallRuntime::new(router, session, step_context, tracker)
|
||||
}
|
||||
|
||||
fn make_connector(id: &str, name: &str) -> AppInfo {
|
||||
@@ -5892,16 +5899,18 @@ async fn request_permissions_tool_resolves_relative_paths_against_selected_envir
|
||||
let call_id = "call-1".to_string();
|
||||
let handler = RequestPermissionsHandler;
|
||||
let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new()));
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let handle = tokio::spawn({
|
||||
let session = Arc::clone(&session);
|
||||
let turn_context = Arc::clone(&turn_context);
|
||||
let step_context = Arc::clone(&step_context);
|
||||
let tracker = Arc::clone(&tracker);
|
||||
let call_id = call_id.clone();
|
||||
async move {
|
||||
handler
|
||||
.handle(ToolInvocation {
|
||||
session,
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
step_context,
|
||||
turn: turn_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker,
|
||||
@@ -5973,11 +5982,13 @@ async fn request_permissions_tool_resolves_relative_paths_against_selected_envir
|
||||
#[tokio::test]
|
||||
async fn request_permissions_tool_rejects_unknown_environment_id() {
|
||||
let (session, turn_context) = make_session_and_context().await;
|
||||
let turn_context = Arc::new(turn_context);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let result = RequestPermissionsHandler
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::new(session),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn_context)),
|
||||
turn: Arc::new(turn_context),
|
||||
step_context,
|
||||
turn: turn_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-1".to_string(),
|
||||
@@ -9902,9 +9913,9 @@ async fn fatal_tool_error_stops_turn_and_reports_error() {
|
||||
.await
|
||||
};
|
||||
let deferred_mcp_tools = Some(tools.clone());
|
||||
let router = ToolRouter::from_contexts(
|
||||
&turn_context,
|
||||
Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let router = ToolRouter::from_context(
|
||||
step_context.as_ref(),
|
||||
crate::tools::router::ToolRouterParams {
|
||||
tool_suggest_candidates: None,
|
||||
deferred_mcp_tools,
|
||||
@@ -9930,7 +9941,7 @@ async fn fatal_tool_error_stops_turn_and_reports_error() {
|
||||
let err = router
|
||||
.dispatch_tool_call_with_code_mode_result(
|
||||
Arc::clone(&session),
|
||||
Arc::clone(&turn_context),
|
||||
step_context,
|
||||
CancellationToken::new(),
|
||||
tracker,
|
||||
call,
|
||||
@@ -10143,6 +10154,7 @@ async fn rejects_escalated_permissions_when_policy_not_on_request() {
|
||||
.expect("test setup should allow updating approval policy");
|
||||
let session = Arc::new(session);
|
||||
let mut turn_context = Arc::new(turn_context_raw);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
|
||||
let command_script = "echo hi";
|
||||
let timeout_ms = 1000;
|
||||
@@ -10160,7 +10172,7 @@ async fn rejects_escalated_permissions_when_policy_not_on_request() {
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::clone(&session),
|
||||
turn: Arc::clone(&turn_context),
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
step_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker: Arc::clone(&turn_diff_tracker),
|
||||
call_id,
|
||||
@@ -10307,6 +10319,7 @@ async fn unified_exec_rejects_escalated_permissions_when_policy_not_on_request()
|
||||
.expect("test setup should allow updating approval policy");
|
||||
let session = Arc::new(session);
|
||||
let turn_context = Arc::new(turn_context_raw);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new()));
|
||||
|
||||
let handler = ExecCommandHandler::default();
|
||||
@@ -10314,7 +10327,7 @@ async fn unified_exec_rejects_escalated_permissions_when_policy_not_on_request()
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::clone(&session),
|
||||
turn: Arc::clone(&turn_context),
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
step_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker: Arc::clone(&tracker),
|
||||
call_id: "exec-call".to_string(),
|
||||
|
||||
@@ -330,11 +330,12 @@ async fn guardian_allows_shell_command_additional_permissions_requests_past_poli
|
||||
);
|
||||
#[allow(deprecated)]
|
||||
let workdir = Some(turn_context.cwd.to_string_lossy().to_string());
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let resp = handler
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::clone(&session),
|
||||
turn: Arc::clone(&turn_context),
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
step_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "test-call".to_string(),
|
||||
@@ -435,11 +436,12 @@ async fn strict_auto_review_turn_grant_forces_guardian_for_shell_command_policy_
|
||||
);
|
||||
#[allow(deprecated)]
|
||||
let workdir = Some(turn_context.cwd.to_string_lossy().to_string());
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let resp = handler
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::clone(&session),
|
||||
turn: Arc::clone(&turn_context),
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
step_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "strict-shell-command-call".to_string(),
|
||||
@@ -481,13 +483,14 @@ async fn guardian_allows_unified_exec_additional_permissions_requests_past_polic
|
||||
let session = Arc::new(session);
|
||||
let turn_context = Arc::new(turn_context_raw);
|
||||
let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new()));
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
|
||||
let handler = ExecCommandHandler::default();
|
||||
let resp = handler
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::clone(&session),
|
||||
turn: Arc::clone(&turn_context),
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
step_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker: Arc::clone(&tracker),
|
||||
call_id: "exec-call".to_string(),
|
||||
@@ -612,11 +615,12 @@ async fn shell_command_allows_sticky_turn_permissions_without_inline_request_per
|
||||
);
|
||||
#[allow(deprecated)]
|
||||
let workdir = Some(turn_context.cwd.to_string_lossy().to_string());
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let resp = handler
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::clone(&session),
|
||||
turn: Arc::clone(&turn_context),
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
step_context,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "sticky-turn-grant".to_string(),
|
||||
|
||||
@@ -236,6 +236,8 @@ pub(crate) async fn run_turn(
|
||||
)
|
||||
.await;
|
||||
|
||||
// Capture once so context, advertised tools, and tool calls share one request view.
|
||||
let step_context = sess.capture_step_context(Arc::clone(&turn_context)).await;
|
||||
let sampling_request_result: CodexResult<_> = async {
|
||||
super::time_reminder::maybe_record_current_time_reminder(
|
||||
sess.as_ref(),
|
||||
@@ -244,18 +246,13 @@ pub(crate) async fn run_turn(
|
||||
)
|
||||
.await?;
|
||||
|
||||
let step_context = sess.capture_step_context(turn_context.as_ref()).await;
|
||||
if turn_context
|
||||
.config
|
||||
.features
|
||||
.enabled(Feature::DeferredExecutor)
|
||||
{
|
||||
world_state = sess
|
||||
.record_step_environment_context_if_changed(
|
||||
turn_context.as_ref(),
|
||||
&world_state,
|
||||
&step_context,
|
||||
)
|
||||
.record_step_environment_context_if_changed(&world_state, step_context.as_ref())
|
||||
.await;
|
||||
}
|
||||
|
||||
@@ -275,8 +272,7 @@ pub(crate) async fn run_turn(
|
||||
);
|
||||
run_sampling_request(
|
||||
Arc::clone(&sess),
|
||||
Arc::clone(&turn_context),
|
||||
step_context,
|
||||
Arc::clone(&step_context),
|
||||
Arc::clone(&turn_extension_data),
|
||||
Arc::clone(&turn_diff_tracker),
|
||||
&mut client_session,
|
||||
@@ -362,7 +358,7 @@ pub(crate) async fn run_turn(
|
||||
{
|
||||
if let Err(err) = run_auto_compact(
|
||||
&sess,
|
||||
&turn_context,
|
||||
Arc::clone(&step_context),
|
||||
&mut client_session,
|
||||
InitialContextInjection::BeforeLastUserMessage(Arc::clone(&world_state)),
|
||||
CompactionReason::ContextLimit,
|
||||
@@ -885,9 +881,10 @@ async fn run_pre_sampling_compact(
|
||||
let token_status = auto_compact_token_status(sess.as_ref(), turn_context.as_ref()).await;
|
||||
// Compact if the configured auto-compaction budget or usable context window is exhausted.
|
||||
if token_status.token_limit_reached {
|
||||
let step_context = sess.capture_step_context(Arc::clone(turn_context)).await;
|
||||
run_auto_compact(
|
||||
sess,
|
||||
turn_context,
|
||||
step_context,
|
||||
client_session,
|
||||
InitialContextInjection::DoNotInject,
|
||||
CompactionReason::ContextLimit,
|
||||
@@ -929,9 +926,12 @@ async fn maybe_run_previous_model_inline_compact(
|
||||
);
|
||||
|
||||
if should_compact_for_comp_hash_change {
|
||||
let step_context = sess
|
||||
.capture_step_context(Arc::clone(&previous_model_turn_context))
|
||||
.await;
|
||||
run_auto_compact(
|
||||
sess,
|
||||
&previous_model_turn_context,
|
||||
step_context,
|
||||
client_session,
|
||||
InitialContextInjection::DoNotInject,
|
||||
CompactionReason::CompHashChanged,
|
||||
@@ -966,9 +966,12 @@ async fn maybe_run_previous_model_inline_compact(
|
||||
&& previous_model_turn_context.model_info.slug != turn_context.model_info.slug
|
||||
&& old_context_window > new_context_window;
|
||||
if should_run {
|
||||
let step_context = sess
|
||||
.capture_step_context(Arc::clone(&previous_model_turn_context))
|
||||
.await;
|
||||
run_auto_compact(
|
||||
sess,
|
||||
&previous_model_turn_context,
|
||||
step_context,
|
||||
client_session,
|
||||
InitialContextInjection::DoNotInject,
|
||||
CompactionReason::ModelDownshift,
|
||||
@@ -986,12 +989,13 @@ async fn maybe_run_previous_model_inline_compact(
|
||||
)]
|
||||
async fn run_auto_compact(
|
||||
sess: &Arc<Session>,
|
||||
turn_context: &Arc<TurnContext>,
|
||||
step_context: Arc<StepContext>,
|
||||
client_session: &mut ModelClientSession,
|
||||
initial_context_injection: InitialContextInjection,
|
||||
reason: CompactionReason,
|
||||
phase: CompactionPhase,
|
||||
) -> CodexResult<()> {
|
||||
let turn_context = &step_context.turn;
|
||||
if should_use_remote_compact_task(turn_context.provider.info()) {
|
||||
if turn_context
|
||||
.config
|
||||
@@ -1005,7 +1009,7 @@ async fn run_auto_compact(
|
||||
);
|
||||
run_inline_remote_auto_compact_task_v2(
|
||||
Arc::clone(sess),
|
||||
Arc::clone(turn_context),
|
||||
step_context,
|
||||
client_session,
|
||||
initial_context_injection,
|
||||
reason,
|
||||
@@ -1021,7 +1025,7 @@ async fn run_auto_compact(
|
||||
);
|
||||
run_inline_remote_auto_compact_task(
|
||||
Arc::clone(sess),
|
||||
Arc::clone(turn_context),
|
||||
step_context,
|
||||
client_session.turn_state(),
|
||||
initial_context_injection,
|
||||
reason,
|
||||
@@ -1121,14 +1125,13 @@ pub(crate) fn build_prompt(
|
||||
#[instrument(level = "trace",
|
||||
skip_all,
|
||||
fields(
|
||||
turn_id = %turn_context.sub_id,
|
||||
model = %turn_context.model_info.slug,
|
||||
cwd = %turn_context.cwd.display()
|
||||
turn_id = %step_context.turn.sub_id,
|
||||
model = %step_context.turn.model_info.slug,
|
||||
cwd = %step_context.turn.cwd.display()
|
||||
)
|
||||
)]
|
||||
async fn run_sampling_request(
|
||||
sess: Arc<Session>,
|
||||
turn_context: Arc<TurnContext>,
|
||||
step_context: Arc<StepContext>,
|
||||
turn_store: Arc<codex_extension_api::ExtensionData>,
|
||||
turn_diff_tracker: SharedTurnDiffTracker,
|
||||
@@ -1137,25 +1140,20 @@ async fn run_sampling_request(
|
||||
input: Vec<ResponseItem>,
|
||||
cancellation_token: CancellationToken,
|
||||
) -> CodexResult<(SamplingRequestResult, Vec<ResponseItem>)> {
|
||||
let router = built_tools(
|
||||
sess.as_ref(),
|
||||
turn_context.as_ref(),
|
||||
step_context,
|
||||
&cancellation_token,
|
||||
)
|
||||
.await?;
|
||||
let turn_context = Arc::clone(&step_context.turn);
|
||||
let router = built_tools(sess.as_ref(), step_context.as_ref(), &cancellation_token).await?;
|
||||
|
||||
let base_instructions = sess.get_base_instructions().await;
|
||||
|
||||
let tool_runtime = ToolCallRuntime::new(
|
||||
Arc::clone(&router),
|
||||
Arc::clone(&sess),
|
||||
Arc::clone(&turn_context),
|
||||
Arc::clone(&step_context),
|
||||
Arc::clone(&turn_diff_tracker),
|
||||
);
|
||||
let _code_mode_worker = sess.services.code_mode_service.start_turn_worker(
|
||||
&sess,
|
||||
&turn_context,
|
||||
Arc::clone(&step_context),
|
||||
Arc::clone(&router),
|
||||
Arc::clone(&turn_diff_tracker),
|
||||
);
|
||||
@@ -1232,17 +1230,17 @@ async fn run_sampling_request(
|
||||
#[instrument(level = "trace",
|
||||
skip_all,
|
||||
fields(
|
||||
turn_id = %turn_context.sub_id,
|
||||
model = %turn_context.model_info.slug,
|
||||
apps_enabled = turn_context.apps_enabled()
|
||||
turn_id = %step_context.turn.sub_id,
|
||||
model = %step_context.turn.model_info.slug,
|
||||
apps_enabled = step_context.turn.apps_enabled()
|
||||
)
|
||||
)]
|
||||
pub(crate) async fn built_tools(
|
||||
sess: &Session,
|
||||
turn_context: &TurnContext,
|
||||
step_context: Arc<StepContext>,
|
||||
step_context: &StepContext,
|
||||
cancellation_token: &CancellationToken,
|
||||
) -> CodexResult<Arc<ToolRouter>> {
|
||||
let turn_context = step_context.turn.as_ref();
|
||||
let mcp_connection_manager = sess.services.mcp_connection_manager.load_full();
|
||||
let has_mcp_servers = mcp_connection_manager.has_servers();
|
||||
let all_mcp_tools = mcp_connection_manager
|
||||
@@ -1358,8 +1356,7 @@ pub(crate) async fn built_tools(
|
||||
);
|
||||
let mcp_tools = has_mcp_servers.then_some(mcp_tool_exposure.direct_tools);
|
||||
let deferred_mcp_tools = mcp_tool_exposure.deferred_tools;
|
||||
Ok(Arc::new(ToolRouter::from_contexts(
|
||||
turn_context,
|
||||
Ok(Arc::new(ToolRouter::from_context(
|
||||
step_context,
|
||||
ToolRouterParams {
|
||||
mcp_tools,
|
||||
|
||||
@@ -14,7 +14,6 @@ use crate::guardian::routes_approval_to_guardian;
|
||||
use crate::responses_metadata::CodexResponsesRequestKind;
|
||||
use crate::session::INITIAL_SUBMIT_ID;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::session::turn::build_prompt;
|
||||
use crate::session::turn::built_tools;
|
||||
use codex_otel::STARTUP_PREWARM_AGE_AT_FIRST_TURN_METRIC;
|
||||
@@ -259,12 +258,12 @@ async fn schedule_startup_prewarm_inner(
|
||||
}
|
||||
let startup_cancellation_token = CancellationToken::new();
|
||||
let built_tools_started_at = Instant::now();
|
||||
let step_context = session
|
||||
.capture_step_context(Arc::clone(&startup_turn_context))
|
||||
.await;
|
||||
let startup_router = built_tools(
|
||||
session.as_ref(),
|
||||
startup_turn_context.as_ref(),
|
||||
Arc::new(StepContext::from_turn_context(
|
||||
startup_turn_context.as_ref(),
|
||||
)),
|
||||
step_context.as_ref(),
|
||||
&startup_cancellation_token,
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -280,9 +280,9 @@ async fn handle_output_item_done_returns_contributed_last_agent_message() {
|
||||
session.services.extensions = Arc::new(builder.build());
|
||||
let session = Arc::new(session);
|
||||
let turn_context = Arc::new(turn_context);
|
||||
let router = Arc::new(ToolRouter::from_contexts(
|
||||
&turn_context,
|
||||
Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let router = Arc::new(ToolRouter::from_context(
|
||||
step_context.as_ref(),
|
||||
crate::tools::router::ToolRouterParams {
|
||||
tool_suggest_candidates: None,
|
||||
mcp_tools: None,
|
||||
@@ -293,12 +293,7 @@ async fn handle_output_item_done_returns_contributed_last_agent_message() {
|
||||
&Default::default(),
|
||||
));
|
||||
let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new()));
|
||||
let tool_runtime = ToolCallRuntime::new(
|
||||
router,
|
||||
Arc::clone(&session),
|
||||
Arc::clone(&turn_context),
|
||||
tracker,
|
||||
);
|
||||
let tool_runtime = ToolCallRuntime::new(router, Arc::clone(&session), step_context, tracker);
|
||||
let item = assistant_output_text("original assistant text");
|
||||
let mut ctx = HandleOutputCtx {
|
||||
sess: session,
|
||||
|
||||
@@ -17,6 +17,7 @@ use tokio_util::sync::CancellationToken;
|
||||
use super::ExecContext;
|
||||
use super::PUBLIC_TOOL_NAME;
|
||||
use super::call_nested_tool;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::tools::ToolRouter;
|
||||
use crate::tools::context::SharedTurnDiffTracker;
|
||||
use crate::tools::parallel::ToolCallRuntime;
|
||||
@@ -49,14 +50,11 @@ impl CodeModeDispatchBroker {
|
||||
&self,
|
||||
exec: ExecContext,
|
||||
router: Arc<ToolRouter>,
|
||||
step_context: Arc<StepContext>,
|
||||
tracker: SharedTurnDiffTracker,
|
||||
) -> CodeModeDispatchWorker {
|
||||
let tool_runtime = ToolCallRuntime::new(
|
||||
router,
|
||||
Arc::clone(&exec.session),
|
||||
Arc::clone(&exec.turn),
|
||||
tracker,
|
||||
);
|
||||
let tool_runtime =
|
||||
ToolCallRuntime::new(router, Arc::clone(&exec.session), step_context, tracker);
|
||||
let host = Arc::new(CoreTurnHost { exec, tool_runtime });
|
||||
let dispatch_rx = self.dispatch_rx.clone();
|
||||
let dispatch_gates = Arc::clone(&self.dispatch_gates);
|
||||
|
||||
@@ -21,6 +21,7 @@ use crate::function_tool::FunctionCallError;
|
||||
use crate::original_image_detail::can_request_original_image_detail;
|
||||
use crate::original_image_detail::sanitize_original_image_detail as sanitize_image_detail_items;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use crate::tools::ToolRouter;
|
||||
use crate::tools::context::FunctionToolOutput;
|
||||
@@ -113,10 +114,11 @@ impl CodeModeService {
|
||||
pub(crate) fn start_turn_worker(
|
||||
&self,
|
||||
session: &Arc<Session>,
|
||||
turn: &Arc<TurnContext>,
|
||||
step_context: Arc<StepContext>,
|
||||
router: Arc<ToolRouter>,
|
||||
tracker: SharedTurnDiffTracker,
|
||||
) -> Option<CodeModeDispatchWorker> {
|
||||
let turn = &step_context.turn;
|
||||
let tool_mode = effective_tool_mode(turn);
|
||||
if !matches!(tool_mode, ToolMode::CodeMode | ToolMode::CodeModeOnly)
|
||||
|| self.session.is_none()
|
||||
@@ -130,7 +132,7 @@ impl CodeModeService {
|
||||
};
|
||||
Some(
|
||||
self.dispatch_broker
|
||||
.start_turn_worker(exec, router, tracker),
|
||||
.start_turn_worker(exec, router, step_context, tracker),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,7 @@ pub enum ToolCallSource {
|
||||
#[derive(Clone)]
|
||||
pub struct ToolInvocation {
|
||||
pub session: Arc<Session>,
|
||||
// TODO(sayan): Remove this compatibility field once handlers use `step_context.turn`.
|
||||
pub turn: Arc<TurnContext>,
|
||||
pub(crate) step_context: Arc<StepContext>,
|
||||
pub cancellation_token: CancellationToken,
|
||||
|
||||
@@ -30,10 +30,11 @@ fn sample_patch() -> &'static str {
|
||||
|
||||
async fn invocation_for_payload(payload: ToolPayload) -> ToolInvocation {
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
ToolInvocation {
|
||||
session: session.into(),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: turn.into(),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-apply-patch".to_string(),
|
||||
|
||||
@@ -274,10 +274,11 @@ mod tests {
|
||||
async fn exposes_generic_hook_payloads() {
|
||||
let handler = ExtensionToolAdapter::new(Arc::new(StubExtensionExecutor));
|
||||
let (session, turn) = crate::session::tests::make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
let invocation = ToolInvocation {
|
||||
session: session.into(),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: turn.into(),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-extension".to_string(),
|
||||
@@ -344,9 +345,10 @@ mod tests {
|
||||
panic!("expected raw response item event");
|
||||
};
|
||||
assert_eq!(raw_history_item.item, expected_history_item);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn));
|
||||
let invocation = ToolInvocation {
|
||||
session,
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn.as_ref())),
|
||||
step_context,
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
@@ -546,9 +548,10 @@ mod tests {
|
||||
&session.thread_id.to_string(),
|
||||
"call-image",
|
||||
);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn));
|
||||
let invocation = ToolInvocation {
|
||||
session,
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn.as_ref())),
|
||||
step_context,
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
|
||||
@@ -140,6 +140,7 @@ impl McpHandler {
|
||||
};
|
||||
|
||||
let started = Instant::now();
|
||||
// TODO(sayan): Use StepContext for MCP file arguments when MCP follows dynamic environments.
|
||||
let result = handle_mcp_tool_call(
|
||||
Arc::clone(&session),
|
||||
&turn,
|
||||
@@ -341,13 +342,14 @@ mod tests {
|
||||
.to_string(),
|
||||
};
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
let handler = McpHandler::new(tool_info("memory", "memory", "create_entities"))
|
||||
.expect("MCP tool spec should build");
|
||||
assert_eq!(
|
||||
handler.pre_tool_use_payload(&ToolInvocation {
|
||||
session: session.into(),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: turn.into(),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-mcp-pre".to_string(),
|
||||
@@ -373,14 +375,15 @@ mod tests {
|
||||
arguments: json!({ "message": "hello" }).to_string(),
|
||||
};
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
let handler = McpHandler::new(tool_info("foo", "mcp__foo", "exec_command"))
|
||||
.expect("MCP tool spec should build");
|
||||
|
||||
assert_eq!(
|
||||
handler.pre_tool_use_payload(&ToolInvocation {
|
||||
session: session.into(),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: turn.into(),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-mcp-pre-builtin-like".to_string(),
|
||||
@@ -401,6 +404,7 @@ mod tests {
|
||||
arguments: json!({ "message": "hello" }).to_string(),
|
||||
};
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
let handler = McpHandler::new(tool_info("foo", "mcp__foo", "exec_command"))
|
||||
.expect("MCP tool spec should build");
|
||||
|
||||
@@ -408,8 +412,8 @@ mod tests {
|
||||
.with_updated_hook_input(
|
||||
ToolInvocation {
|
||||
session: session.into(),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: turn.into(),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-mcp-rewrite-builtin-like".to_string(),
|
||||
@@ -452,12 +456,13 @@ mod tests {
|
||||
truncation_policy: codex_utils_output_truncation::TruncationPolicy::Bytes(1024),
|
||||
};
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
let handler = McpHandler::new(tool_info("filesystem", "filesystem", "read_file"))
|
||||
.expect("MCP tool spec should build");
|
||||
let invocation = ToolInvocation {
|
||||
session: session.into(),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: turn.into(),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-mcp-post".to_string(),
|
||||
|
||||
@@ -74,9 +74,10 @@ fn invocation(
|
||||
tool_name: &str,
|
||||
payload: ToolPayload,
|
||||
) -> ToolInvocation {
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn));
|
||||
ToolInvocation {
|
||||
session,
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn.as_ref())),
|
||||
step_context,
|
||||
turn,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::default())),
|
||||
|
||||
@@ -22,14 +22,15 @@ async fn multi_agent_v2_request_user_input_rejects_subagent_threads() {
|
||||
agent_nickname: None,
|
||||
agent_role: None,
|
||||
});
|
||||
let turn = Arc::new(turn);
|
||||
|
||||
let result = RequestUserInputHandler {
|
||||
available_modes: Vec::new(),
|
||||
}
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::new(session),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: Arc::new(turn),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::default())),
|
||||
call_id: "call-1".to_string(),
|
||||
|
||||
@@ -227,13 +227,14 @@ async fn shell_command_pre_tool_use_payload_uses_raw_command() {
|
||||
arguments: json!({ "command": "printf shell command" }).to_string(),
|
||||
};
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
let handler = ShellCommandHandler::from(codex_tools::ShellCommandBackendConfig::Classic);
|
||||
|
||||
assert_eq!(
|
||||
handler.pre_tool_use_payload(&ToolInvocation {
|
||||
session: session.into(),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: turn.into(),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-42".to_string(),
|
||||
@@ -260,10 +261,11 @@ async fn build_post_tool_use_payload_uses_tool_output_wire_value() {
|
||||
};
|
||||
let handler = ShellCommandHandler::from(codex_tools::ShellCommandBackendConfig::Classic);
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
let invocation = ToolInvocation {
|
||||
session: session.into(),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: turn.into(),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-42".to_string(),
|
||||
|
||||
@@ -28,10 +28,11 @@ async fn invocation_for_payload(
|
||||
payload: ToolPayload,
|
||||
) -> ToolInvocation {
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
ToolInvocation {
|
||||
session: session.into(),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: turn.into(),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: call_id.to_string(),
|
||||
@@ -238,13 +239,14 @@ async fn exec_command_pre_tool_use_payload_uses_raw_command() {
|
||||
arguments: serde_json::json!({ "cmd": "printf exec command" }).to_string(),
|
||||
};
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
let handler = ExecCommandHandler::default();
|
||||
|
||||
assert_eq!(
|
||||
handler.pre_tool_use_payload(&ToolInvocation {
|
||||
session: session.into(),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: turn.into(),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-43".to_string(),
|
||||
@@ -265,13 +267,14 @@ async fn exec_command_pre_tool_use_payload_skips_write_stdin() {
|
||||
arguments: serde_json::json!({ "chars": "echo hi" }).to_string(),
|
||||
};
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
let handler = WriteStdinHandler;
|
||||
|
||||
assert_eq!(
|
||||
handler.pre_tool_use_payload(&ToolInvocation {
|
||||
session: session.into(),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: turn.into(),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-44".to_string(),
|
||||
|
||||
@@ -324,12 +324,13 @@ mod tests {
|
||||
let image_path = image_cwd.join("image.png");
|
||||
std::fs::write(image_path.as_path(), b"not a real image").expect("write test image");
|
||||
turn.permission_profile = PermissionProfile::read_only();
|
||||
let turn = Arc::new(turn);
|
||||
|
||||
let result = ViewImageHandler::default()
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::new(session),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: Arc::new(turn),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-view-image".to_string(),
|
||||
@@ -353,12 +354,13 @@ mod tests {
|
||||
#[tokio::test]
|
||||
async fn handle_rejects_unsupported_detail() {
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
|
||||
let result = ViewImageHandler::default()
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::new(session),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: Arc::new(turn),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-view-image".to_string(),
|
||||
@@ -389,12 +391,13 @@ mod tests {
|
||||
let image_path = image_cwd.join("image.png");
|
||||
std::fs::write(image_path.as_path(), b"not a real image").expect("write test image");
|
||||
turn.permission_profile = PermissionProfile::Disabled;
|
||||
let turn = Arc::new(turn);
|
||||
|
||||
let result = ViewImageHandler::default()
|
||||
.handle(ToolInvocation {
|
||||
session: Arc::new(session),
|
||||
step_context: Arc::new(StepContext::from_turn_context(&turn)),
|
||||
turn: Arc::new(turn),
|
||||
step_context: StepContext::for_test(Arc::clone(&turn)),
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(Mutex::new(TurnDiffTracker::new())),
|
||||
call_id: "call-view-image".to_string(),
|
||||
|
||||
@@ -14,7 +14,7 @@ use tracing::trace_span;
|
||||
|
||||
use crate::function_tool::FunctionCallError;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use crate::session::step_context::StepContext;
|
||||
use crate::tools::context::AbortedToolOutput;
|
||||
use crate::tools::context::SharedTurnDiffTracker;
|
||||
use crate::tools::context::ToolPayload;
|
||||
@@ -31,7 +31,8 @@ use codex_protocol::models::ResponseInputItem;
|
||||
pub(crate) struct ToolCallRuntime {
|
||||
router: Arc<ToolRouter>,
|
||||
session: Arc<Session>,
|
||||
turn_context: Arc<TurnContext>,
|
||||
// Tool calls may run later, so retain the step whose tool list advertised them.
|
||||
step_context: Arc<StepContext>,
|
||||
tracker: SharedTurnDiffTracker,
|
||||
parallel_execution: Arc<RwLock<()>>,
|
||||
}
|
||||
@@ -40,13 +41,13 @@ impl ToolCallRuntime {
|
||||
pub(crate) fn new(
|
||||
router: Arc<ToolRouter>,
|
||||
session: Arc<Session>,
|
||||
turn_context: Arc<TurnContext>,
|
||||
step_context: Arc<StepContext>,
|
||||
tracker: SharedTurnDiffTracker,
|
||||
) -> Self {
|
||||
Self {
|
||||
router,
|
||||
session,
|
||||
turn_context,
|
||||
step_context,
|
||||
tracker,
|
||||
parallel_execution: Arc::new(RwLock::new(())),
|
||||
}
|
||||
@@ -88,7 +89,8 @@ impl ToolCallRuntime {
|
||||
let supports_parallel = self.router.tool_supports_parallel(&call);
|
||||
let router = Arc::clone(&self.router);
|
||||
let session = Arc::clone(&self.session);
|
||||
let turn = Arc::clone(&self.turn_context);
|
||||
let step_context = Arc::clone(&self.step_context);
|
||||
let turn = Arc::clone(&step_context.turn);
|
||||
let tracker = Arc::clone(&self.tracker);
|
||||
let lock = Arc::clone(&self.parallel_execution);
|
||||
let invocation_cancellation_token = cancellation_token.clone();
|
||||
@@ -121,7 +123,7 @@ impl ToolCallRuntime {
|
||||
router
|
||||
.dispatch_tool_call_with_terminal_outcome(
|
||||
session,
|
||||
turn,
|
||||
step_context,
|
||||
invocation_cancellation_token,
|
||||
tracker,
|
||||
dispatch_call,
|
||||
@@ -424,13 +426,13 @@ mod tests {
|
||||
let handler = Arc::new(ImmediateHandler {
|
||||
tool_name: tool_name.clone(),
|
||||
}) as Arc<dyn CoreToolRuntime>;
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let router = Arc::new(ToolRouter::from_parts(
|
||||
ToolRegistry::from_tools([handler]),
|
||||
Vec::new(),
|
||||
Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
));
|
||||
let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new()));
|
||||
let runtime = ToolCallRuntime::new(router, session, turn_context, tracker);
|
||||
let runtime = ToolCallRuntime::new(router, session, step_context, tracker);
|
||||
let cancellation_token = CancellationToken::new();
|
||||
let call = ToolCall {
|
||||
tool_name,
|
||||
@@ -497,13 +499,13 @@ mod tests {
|
||||
cleanup_started: std::sync::Mutex::new(Some(cleanup_started_tx)),
|
||||
allow_cleanup: Arc::clone(&allow_cleanup),
|
||||
}) as Arc<dyn CoreToolRuntime>;
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn_context));
|
||||
let router = Arc::new(ToolRouter::from_parts(
|
||||
ToolRegistry::from_tools([handler]),
|
||||
Vec::new(),
|
||||
Arc::new(StepContext::from_turn_context(turn_context.as_ref())),
|
||||
));
|
||||
let tracker = Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new()));
|
||||
let runtime = ToolCallRuntime::new(router, session, turn_context, tracker);
|
||||
let runtime = ToolCallRuntime::new(router, session, step_context, tracker);
|
||||
let cancellation_token = CancellationToken::new();
|
||||
let call = ToolCall {
|
||||
tool_name,
|
||||
|
||||
@@ -458,9 +458,10 @@ fn test_invocation(
|
||||
call_id: &str,
|
||||
tool_name: codex_tools::ToolName,
|
||||
) -> ToolInvocation {
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn));
|
||||
ToolInvocation {
|
||||
session,
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn.as_ref())),
|
||||
step_context,
|
||||
turn,
|
||||
cancellation_token: tokio_util::sync::CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
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::SharedTurnDiffTracker;
|
||||
use crate::tools::context::ToolInvocation;
|
||||
use crate::tools::context::ToolPayload;
|
||||
@@ -36,7 +35,6 @@ pub struct ToolCall {
|
||||
pub struct ToolRouter {
|
||||
registry: ToolRegistry,
|
||||
model_visible_specs: Vec<ToolSpec>,
|
||||
step_context: Arc<StepContext>,
|
||||
}
|
||||
|
||||
pub(crate) struct ToolRouterParams<'a> {
|
||||
@@ -60,29 +58,18 @@ pub(crate) struct ToolSuggestCandidates {
|
||||
}
|
||||
|
||||
impl ToolRouter {
|
||||
pub(crate) fn from_contexts(
|
||||
turn_context: &TurnContext,
|
||||
step_context: Arc<StepContext>,
|
||||
pub(crate) fn from_context(
|
||||
step_context: &StepContext,
|
||||
params: ToolRouterParams<'_>,
|
||||
tool_search_handler_cache: &ToolSearchHandlerCache,
|
||||
) -> Self {
|
||||
build_tool_router(
|
||||
turn_context,
|
||||
step_context,
|
||||
params,
|
||||
tool_search_handler_cache,
|
||||
)
|
||||
build_tool_router(step_context, params, tool_search_handler_cache)
|
||||
}
|
||||
|
||||
pub(crate) fn from_parts(
|
||||
registry: ToolRegistry,
|
||||
model_visible_specs: Vec<ToolSpec>,
|
||||
step_context: Arc<StepContext>,
|
||||
) -> Self {
|
||||
pub(crate) fn from_parts(registry: ToolRegistry, model_visible_specs: Vec<ToolSpec>) -> Self {
|
||||
Self {
|
||||
registry,
|
||||
model_visible_specs,
|
||||
step_context,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,7 +164,7 @@ impl ToolRouter {
|
||||
pub async fn dispatch_tool_call_with_code_mode_result(
|
||||
&self,
|
||||
session: Arc<Session>,
|
||||
turn: Arc<TurnContext>,
|
||||
step_context: Arc<StepContext>,
|
||||
cancellation_token: CancellationToken,
|
||||
tracker: SharedTurnDiffTracker,
|
||||
call: ToolCall,
|
||||
@@ -185,7 +172,7 @@ impl ToolRouter {
|
||||
) -> Result<AnyToolResult, FunctionCallError> {
|
||||
self.dispatch_tool_call_with_code_mode_result_inner(
|
||||
session,
|
||||
turn,
|
||||
step_context,
|
||||
cancellation_token,
|
||||
tracker,
|
||||
call,
|
||||
@@ -200,7 +187,7 @@ impl ToolRouter {
|
||||
pub(crate) async fn dispatch_tool_call_with_terminal_outcome(
|
||||
&self,
|
||||
session: Arc<Session>,
|
||||
turn: Arc<TurnContext>,
|
||||
step_context: Arc<StepContext>,
|
||||
cancellation_token: CancellationToken,
|
||||
tracker: SharedTurnDiffTracker,
|
||||
call: ToolCall,
|
||||
@@ -209,7 +196,7 @@ impl ToolRouter {
|
||||
) -> Result<AnyToolResult, FunctionCallError> {
|
||||
self.dispatch_tool_call_with_code_mode_result_inner(
|
||||
session,
|
||||
turn,
|
||||
step_context,
|
||||
cancellation_token,
|
||||
tracker,
|
||||
call,
|
||||
@@ -223,7 +210,7 @@ impl ToolRouter {
|
||||
async fn dispatch_tool_call_with_code_mode_result_inner(
|
||||
&self,
|
||||
session: Arc<Session>,
|
||||
turn: Arc<TurnContext>,
|
||||
step_context: Arc<StepContext>,
|
||||
cancellation_token: CancellationToken,
|
||||
tracker: SharedTurnDiffTracker,
|
||||
call: ToolCall,
|
||||
@@ -236,10 +223,12 @@ impl ToolRouter {
|
||||
payload,
|
||||
} = call;
|
||||
|
||||
// Keep the legacy ToolInvocation.turn field tied to the same request state until handlers migrate.
|
||||
let turn = Arc::clone(&step_context.turn);
|
||||
let invocation = ToolInvocation {
|
||||
session,
|
||||
turn,
|
||||
step_context: Arc::clone(&self.step_context),
|
||||
step_context,
|
||||
cancellation_token,
|
||||
tracker,
|
||||
call_id,
|
||||
|
||||
@@ -106,15 +106,16 @@ fn extension_tool_test_registry() -> Arc<ExtensionRegistry<Config>> {
|
||||
#[tokio::test]
|
||||
async fn parallel_support_does_not_match_namespaced_local_tool_names() -> anyhow::Result<()> {
|
||||
let (session, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn));
|
||||
let mcp_tools = session
|
||||
.services
|
||||
.mcp_connection_manager
|
||||
.load_full()
|
||||
.list_all_tools()
|
||||
.await;
|
||||
let router = ToolRouter::from_contexts(
|
||||
&turn,
|
||||
Arc::new(StepContext::from_turn_context(&turn)),
|
||||
let router = ToolRouter::from_context(
|
||||
step_context.as_ref(),
|
||||
ToolRouterParams {
|
||||
tool_suggest_candidates: None,
|
||||
deferred_mcp_tools: None,
|
||||
@@ -181,9 +182,10 @@ async fn build_tool_call_uses_namespace_for_registry_name() -> anyhow::Result<()
|
||||
#[tokio::test]
|
||||
async fn mcp_parallel_support_uses_handler_data() -> anyhow::Result<()> {
|
||||
let (_, turn) = make_session_and_context().await;
|
||||
let router = ToolRouter::from_contexts(
|
||||
&turn,
|
||||
Arc::new(StepContext::from_turn_context(&turn)),
|
||||
let turn = Arc::new(turn);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn));
|
||||
let router = ToolRouter::from_context(
|
||||
step_context.as_ref(),
|
||||
ToolRouterParams {
|
||||
tool_suggest_candidates: None,
|
||||
deferred_mcp_tools: None,
|
||||
@@ -231,9 +233,10 @@ async fn mcp_parallel_support_uses_handler_data() -> anyhow::Result<()> {
|
||||
#[tokio::test]
|
||||
async fn tools_without_handlers_do_not_support_parallel() -> anyhow::Result<()> {
|
||||
let (_, turn) = make_session_and_context().await;
|
||||
let router = ToolRouter::from_contexts(
|
||||
&turn,
|
||||
Arc::new(StepContext::from_turn_context(&turn)),
|
||||
let turn = Arc::new(turn);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn));
|
||||
let router = ToolRouter::from_context(
|
||||
step_context.as_ref(),
|
||||
ToolRouterParams {
|
||||
tool_suggest_candidates: None,
|
||||
deferred_mcp_tools: None,
|
||||
@@ -258,6 +261,8 @@ async fn tools_without_handlers_do_not_support_parallel() -> anyhow::Result<()>
|
||||
#[tokio::test]
|
||||
async fn specs_filter_deferred_dynamic_tools() -> anyhow::Result<()> {
|
||||
let (_, turn) = make_session_and_context().await;
|
||||
let turn = Arc::new(turn);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn));
|
||||
let hidden_tool = "hidden_dynamic_tool";
|
||||
let visible_tool = "visible_dynamic_tool";
|
||||
let dynamic_tools = vec![DynamicToolSpec::Namespace(DynamicToolNamespaceSpec {
|
||||
@@ -287,9 +292,8 @@ async fn specs_filter_deferred_dynamic_tools() -> anyhow::Result<()> {
|
||||
],
|
||||
})];
|
||||
|
||||
let router = ToolRouter::from_contexts(
|
||||
&turn,
|
||||
Arc::new(StepContext::from_turn_context(&turn)),
|
||||
let router = ToolRouter::from_context(
|
||||
step_context.as_ref(),
|
||||
ToolRouterParams {
|
||||
tool_suggest_candidates: None,
|
||||
deferred_mcp_tools: None,
|
||||
@@ -338,6 +342,8 @@ fn mcp_tool_info(
|
||||
async fn extension_tool_executors_are_model_visible_and_dispatchable() -> anyhow::Result<()> {
|
||||
let (mut session, turn) = make_session_and_context().await;
|
||||
session.services.extensions = extension_tool_test_registry();
|
||||
let turn = Arc::new(turn);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn));
|
||||
let history_item = ResponseItem::Message {
|
||||
id: None,
|
||||
role: "user".to_string(),
|
||||
@@ -353,9 +359,8 @@ async fn extension_tool_executors_are_model_visible_and_dispatchable() -> anyhow
|
||||
let mut expected_history_item = history_item.clone();
|
||||
expected_history_item.set_turn_id_if_missing(&turn.sub_id);
|
||||
|
||||
let router = ToolRouter::from_contexts(
|
||||
&turn,
|
||||
Arc::new(StepContext::from_turn_context(&turn)),
|
||||
let router = ToolRouter::from_context(
|
||||
step_context.as_ref(),
|
||||
ToolRouterParams {
|
||||
tool_suggest_candidates: None,
|
||||
deferred_mcp_tools: None,
|
||||
@@ -390,7 +395,7 @@ async fn extension_tool_executors_are_model_visible_and_dispatchable() -> anyhow
|
||||
let result = router
|
||||
.dispatch_tool_call_with_code_mode_result(
|
||||
Arc::new(session),
|
||||
Arc::new(turn),
|
||||
step_context,
|
||||
CancellationToken::new(),
|
||||
Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
call,
|
||||
|
||||
@@ -144,7 +144,6 @@ impl PlannedTools {
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
struct CoreToolPlanContext<'a> {
|
||||
turn_context: &'a TurnContext,
|
||||
step_context: &'a StepContext,
|
||||
mcp_tools: Option<&'a [ToolInfo]>,
|
||||
deferred_mcp_tools: Option<&'a [ToolInfo]>,
|
||||
@@ -158,27 +157,22 @@ struct CoreToolPlanContext<'a> {
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
pub(crate) fn build_tool_router(
|
||||
turn_context: &TurnContext,
|
||||
step_context: Arc<StepContext>,
|
||||
step_context: &StepContext,
|
||||
params: ToolRouterParams<'_>,
|
||||
tool_search_handler_cache: &ToolSearchHandlerCache,
|
||||
) -> ToolRouter {
|
||||
let (model_visible_specs, registry) = build_tool_specs_and_registry(
|
||||
turn_context,
|
||||
&step_context,
|
||||
params,
|
||||
tool_search_handler_cache,
|
||||
);
|
||||
ToolRouter::from_parts(registry, model_visible_specs, step_context)
|
||||
let (model_visible_specs, registry) =
|
||||
build_tool_specs_and_registry(step_context, params, tool_search_handler_cache);
|
||||
ToolRouter::from_parts(registry, model_visible_specs)
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
fn build_tool_specs_and_registry(
|
||||
turn_context: &TurnContext,
|
||||
step_context: &StepContext,
|
||||
params: ToolRouterParams<'_>,
|
||||
tool_search_handler_cache: &ToolSearchHandlerCache,
|
||||
) -> (Vec<ToolSpec>, ToolRegistry) {
|
||||
let turn_context = step_context.turn.as_ref();
|
||||
let ToolRouterParams {
|
||||
mcp_tools,
|
||||
deferred_mcp_tools,
|
||||
@@ -189,7 +183,6 @@ fn build_tool_specs_and_registry(
|
||||
let default_agent_type_description =
|
||||
crate::agent::role::spawn_tool_spec::build(&std::collections::BTreeMap::new());
|
||||
let context = CoreToolPlanContext {
|
||||
turn_context,
|
||||
step_context,
|
||||
mcp_tools: mcp_tools.as_deref(),
|
||||
deferred_mcp_tools: deferred_mcp_tools.as_deref(),
|
||||
@@ -298,7 +291,7 @@ fn spec_for_model_request(
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
fn hosted_model_tool_specs(context: &CoreToolPlanContext<'_>) -> Vec<ToolSpec> {
|
||||
let turn_context = context.turn_context;
|
||||
let turn_context = context.step_context.turn.as_ref();
|
||||
// Responses Lite accepts schemas for client-executed tools, not hosted Responses tools.
|
||||
if turn_context.model_info.use_responses_lite {
|
||||
return Vec::new();
|
||||
@@ -640,7 +633,7 @@ fn tool_environment_mode(step_context: &StepContext) -> ToolEnvironmentMode {
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
fn add_shell_tools(context: &CoreToolPlanContext<'_>, planned_tools: &mut PlannedTools) {
|
||||
let turn_context = context.turn_context;
|
||||
let turn_context = context.step_context.turn.as_ref();
|
||||
let features = turn_context.config.features.get();
|
||||
let environment_mode = tool_environment_mode(context.step_context);
|
||||
if !environment_mode.has_environment() {
|
||||
@@ -707,7 +700,7 @@ fn add_mcp_resource_tools(context: &CoreToolPlanContext<'_>, planned_tools: &mut
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
fn add_core_utility_tools(context: &CoreToolPlanContext<'_>, planned_tools: &mut PlannedTools) {
|
||||
let turn_context = context.turn_context;
|
||||
let turn_context = context.step_context.turn.as_ref();
|
||||
let features = turn_context.config.features.get();
|
||||
let environment_mode = tool_environment_mode(context.step_context);
|
||||
|
||||
@@ -722,7 +715,7 @@ fn add_core_utility_tools(context: &CoreToolPlanContext<'_>, planned_tools: &mut
|
||||
);
|
||||
}
|
||||
|
||||
if features.enabled(Feature::RequestPermissionsTool) {
|
||||
if environment_mode.has_environment() && features.enabled(Feature::RequestPermissionsTool) {
|
||||
planned_tools.add(RequestPermissionsHandler);
|
||||
}
|
||||
|
||||
@@ -785,7 +778,7 @@ fn add_core_utility_tools(context: &CoreToolPlanContext<'_>, planned_tools: &mut
|
||||
|
||||
#[instrument(level = "trace", skip_all)]
|
||||
fn add_collaboration_tools(context: &CoreToolPlanContext<'_>, planned_tools: &mut PlannedTools) {
|
||||
let turn_context = context.turn_context;
|
||||
let turn_context = context.step_context.turn.as_ref();
|
||||
if collab_tools_enabled(turn_context) {
|
||||
if multi_agent_v2_enabled(turn_context) {
|
||||
let exposure = if turn_context.config.multi_agent_v2.non_code_mode_only {
|
||||
@@ -949,7 +942,7 @@ fn add_extension_tools(context: &CoreToolPlanContext<'_>, planned_tools: &mut Pl
|
||||
// Extension ToolContributor implementations are resolved into executors
|
||||
// before planning. Core only adapts those executors into its runtime set.
|
||||
append_extension_tool_executors(
|
||||
context.turn_context,
|
||||
context.step_context.turn.as_ref(),
|
||||
context.extension_tool_executors,
|
||||
planned_tools,
|
||||
);
|
||||
@@ -960,7 +953,7 @@ fn append_tool_search_executor(
|
||||
context: &CoreToolPlanContext<'_>,
|
||||
planned_tools: &mut PlannedTools,
|
||||
) {
|
||||
let turn_context = context.turn_context;
|
||||
let turn_context = context.step_context.turn.as_ref();
|
||||
if !search_tool_enabled(turn_context) {
|
||||
return;
|
||||
}
|
||||
@@ -983,7 +976,7 @@ fn prepend_code_mode_executors(
|
||||
context: &CoreToolPlanContext<'_>,
|
||||
planned_tools: &mut PlannedTools,
|
||||
) {
|
||||
let turn_context = context.turn_context;
|
||||
let turn_context = context.step_context.turn.as_ref();
|
||||
let code_mode_executors = build_code_mode_executors(turn_context, planned_tools.runtimes());
|
||||
planned_tools.runtimes.splice(0..0, code_mode_executors);
|
||||
}
|
||||
|
||||
@@ -179,9 +179,10 @@ async fn probe_with(
|
||||
) -> ToolPlanProbe {
|
||||
let (_session, mut turn) = make_session_and_context().await;
|
||||
configure_turn(&mut turn);
|
||||
let router = ToolRouter::from_contexts(
|
||||
&turn,
|
||||
Arc::new(StepContext::from_turn_context(&turn)),
|
||||
let turn = Arc::new(turn);
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn));
|
||||
let router = ToolRouter::from_context(
|
||||
step_context.as_ref(),
|
||||
ToolRouterParams {
|
||||
tool_suggest_candidates: inputs.tool_suggest_candidates,
|
||||
mcp_tools: inputs.mcp_tools,
|
||||
@@ -604,6 +605,7 @@ async fn environment_count_controls_environment_backed_tools() {
|
||||
let no_environment = probe(|turn| {
|
||||
turn.environments.turn_environments.clear();
|
||||
set_feature(turn, Feature::ShellTool, /*enabled*/ true);
|
||||
set_feature(turn, Feature::RequestPermissionsTool, /*enabled*/ true);
|
||||
turn.model_info.apply_patch_tool_type = Some(ApplyPatchToolType::Freeform);
|
||||
})
|
||||
.await;
|
||||
@@ -612,22 +614,30 @@ async fn environment_count_controls_environment_backed_tools() {
|
||||
"exec_command",
|
||||
"apply_patch",
|
||||
"view_image",
|
||||
"request_permissions",
|
||||
]);
|
||||
no_environment.assert_registered_lacks(&[
|
||||
"shell_command",
|
||||
"exec_command",
|
||||
"apply_patch",
|
||||
"view_image",
|
||||
"request_permissions",
|
||||
]);
|
||||
|
||||
let multiple_environments = probe(|turn| {
|
||||
duplicate_primary_environment(turn);
|
||||
set_feature(turn, Feature::ShellTool, /*enabled*/ true);
|
||||
set_feature(turn, Feature::UnifiedExec, /*enabled*/ true);
|
||||
set_feature(turn, Feature::RequestPermissionsTool, /*enabled*/ true);
|
||||
turn.model_info.apply_patch_tool_type = Some(ApplyPatchToolType::Freeform);
|
||||
})
|
||||
.await;
|
||||
multiple_environments.assert_visible_contains(&["exec_command", "apply_patch", "view_image"]);
|
||||
multiple_environments.assert_visible_contains(&[
|
||||
"exec_command",
|
||||
"apply_patch",
|
||||
"view_image",
|
||||
"request_permissions",
|
||||
]);
|
||||
assert!(has_parameter(
|
||||
multiple_environments.visible_spec("exec_command"),
|
||||
"environment_id"
|
||||
@@ -647,12 +657,12 @@ async fn environment_tools_follow_the_step_context() {
|
||||
set_feature(&mut turn, Feature::UnifiedExec, /*enabled*/ true);
|
||||
turn.model_info.apply_patch_tool_type = Some(ApplyPatchToolType::Freeform);
|
||||
|
||||
let step_context = Arc::new(StepContext::from_turn_context(&turn));
|
||||
let environments = turn.environments.clone();
|
||||
turn.environments.turn_environments.clear();
|
||||
let step_context = Arc::new(StepContext::new(Arc::new(turn), environments));
|
||||
|
||||
let plan = ToolPlanProbe::from_router(ToolRouter::from_contexts(
|
||||
&turn,
|
||||
step_context,
|
||||
let plan = ToolPlanProbe::from_router(ToolRouter::from_context(
|
||||
step_context.as_ref(),
|
||||
ToolRouterParams {
|
||||
mcp_tools: None,
|
||||
deferred_mcp_tools: None,
|
||||
@@ -800,9 +810,10 @@ async fn tool_search_cache_rebuilds_when_deferred_sources_change() {
|
||||
|
||||
let (_session, mut first_turn) = make_session_and_context().await;
|
||||
first_turn.model_info.supports_search_tool = true;
|
||||
let first_router = ToolRouter::from_contexts(
|
||||
&first_turn,
|
||||
Arc::new(StepContext::from_turn_context(&first_turn)),
|
||||
let first_turn = Arc::new(first_turn);
|
||||
let first_step_context = StepContext::for_test(Arc::clone(&first_turn));
|
||||
let first_router = ToolRouter::from_context(
|
||||
first_step_context.as_ref(),
|
||||
ToolRouterParams {
|
||||
mcp_tools: None,
|
||||
deferred_mcp_tools: Some(vec![mcp_tool("first", "mcp__first", "lookup")]),
|
||||
@@ -816,9 +827,10 @@ async fn tool_search_cache_rebuilds_when_deferred_sources_change() {
|
||||
|
||||
let (_session, mut second_turn) = make_session_and_context().await;
|
||||
second_turn.model_info.supports_search_tool = true;
|
||||
let second_router = ToolRouter::from_contexts(
|
||||
&second_turn,
|
||||
Arc::new(StepContext::from_turn_context(&second_turn)),
|
||||
let second_turn = Arc::new(second_turn);
|
||||
let second_step_context = StepContext::for_test(Arc::clone(&second_turn));
|
||||
let second_router = ToolRouter::from_context(
|
||||
second_step_context.as_ref(),
|
||||
ToolRouterParams {
|
||||
mcp_tools: None,
|
||||
deferred_mcp_tools: Some(vec![mcp_tool("second", "mcp__second", "lookup")]),
|
||||
|
||||
@@ -270,9 +270,10 @@ fn test_invocation_with_payload(
|
||||
source: ToolCallSource,
|
||||
payload: ToolPayload,
|
||||
) -> ToolInvocation {
|
||||
let step_context = StepContext::for_test(Arc::clone(&turn));
|
||||
ToolInvocation {
|
||||
session,
|
||||
step_context: Arc::new(StepContext::from_turn_context(turn.as_ref())),
|
||||
step_context,
|
||||
turn,
|
||||
cancellation_token: CancellationToken::new(),
|
||||
tracker: Arc::new(tokio::sync::Mutex::new(TurnDiffTracker::new())),
|
||||
|
||||
Reference in New Issue
Block a user