diff --git a/codex-rs/core/src/compact.rs b/codex-rs/core/src/compact.rs index 7bb79a93b0..d049fd49f8 100644 --- a/codex-rs/core/src/compact.rs +++ b/codex-rs/core/src/compact.rs @@ -61,24 +61,24 @@ const COMPACT_USER_MESSAGE_MAX_TOKENS: usize = 20_000; /// Mid-turn compaction must use `BeforeLastUserMessage` because the model is trained to see the /// compaction summary as the last item in history after mid-turn compaction; we therefore inject /// initial context into the replacement history just above the last real user message. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Debug)] pub(crate) enum InitialContextInjection { - BeforeLastUserMessage, + BeforeLastUserMessage(Arc), DoNotInject, } pub(crate) async fn build_compaction_initial_context( sess: &Session, turn_context: &TurnContext, - initial_context_injection: InitialContextInjection, -) -> (Vec, Option) { + initial_context_injection: &InitialContextInjection, +) -> (Vec, Option>) { // Return the rendered state with its items so history and its baseline stay identical. match initial_context_injection { - InitialContextInjection::BeforeLastUserMessage => { - let (items, world_state) = sess - .build_initial_context_for_current_step(turn_context) + InitialContextInjection::BeforeLastUserMessage(world_state) => { + let items = sess + .build_initial_context_with_world_state(turn_context, world_state.as_ref()) .await; - (items, Some(world_state)) + (items, Some(Arc::clone(world_state))) } InitialContextInjection::DoNotInject => (Vec::new(), None), } @@ -330,7 +330,7 @@ async fn run_compact_task_inner_impl( let (initial_context, world_state_baseline) = build_compaction_initial_context( sess.as_ref(), turn_context.as_ref(), - initial_context_injection, + &initial_context_injection, ) .await; if !initial_context.is_empty() { @@ -339,7 +339,9 @@ async fn run_compact_task_inner_impl( } let reference_context_item = match initial_context_injection { InitialContextInjection::DoNotInject => None, - InitialContextInjection::BeforeLastUserMessage => Some(turn_context.to_turn_context_item()), + InitialContextInjection::BeforeLastUserMessage(_) => { + Some(turn_context.to_turn_context_item()) + } }; let compacted_item = CompactedItem { message: summary_text.clone(), diff --git a/codex-rs/core/src/compact_remote.rs b/codex-rs/core/src/compact_remote.rs index 2a433bafa6..a8f6abc0ca 100644 --- a/codex-rs/core/src/compact_remote.rs +++ b/codex-rs/core/src/compact_remote.rs @@ -265,13 +265,15 @@ async fn run_remote_compact_task_inner_impl( sess.as_ref(), turn_context.as_ref(), new_history, - initial_context_injection, + &initial_context_injection, ) .await; let reference_context_item = match initial_context_injection { InitialContextInjection::DoNotInject => None, - InitialContextInjection::BeforeLastUserMessage => Some(turn_context.to_turn_context_item()), + InitialContextInjection::BeforeLastUserMessage(_) => { + Some(turn_context.to_turn_context_item()) + } }; let compacted_item = CompactedItem { message: String::new(), @@ -307,8 +309,8 @@ pub(crate) async fn process_compacted_history( sess: &Session, turn_context: &TurnContext, mut compacted_history: Vec, - initial_context_injection: InitialContextInjection, -) -> (Vec, Option) { + initial_context_injection: &InitialContextInjection, +) -> (Vec, Option>) { // Mid-turn compaction is the only path that must inject initial context above the last user // message in the replacement history. Pre-turn compaction instead injects context after the // compaction item, but mid-turn compaction keeps the compaction item last for model training. diff --git a/codex-rs/core/src/compact_remote_v2.rs b/codex-rs/core/src/compact_remote_v2.rs index 8d2d6a7cd5..9cd7a456be 100644 --- a/codex-rs/core/src/compact_remote_v2.rs +++ b/codex-rs/core/src/compact_remote_v2.rs @@ -297,13 +297,15 @@ async fn run_remote_compact_task_inner_impl( sess.as_ref(), turn_context.as_ref(), compacted_history, - initial_context_injection, + &initial_context_injection, ) .await; let reference_context_item = match initial_context_injection { InitialContextInjection::DoNotInject => None, - InitialContextInjection::BeforeLastUserMessage => Some(turn_context.to_turn_context_item()), + InitialContextInjection::BeforeLastUserMessage(_) => { + Some(turn_context.to_turn_context_item()) + } }; let compacted_item = CompactedItem { message: String::new(), diff --git a/codex-rs/core/src/compact_tests.rs b/codex-rs/core/src/compact_tests.rs index ac6fec73ea..b17361a0ea 100644 --- a/codex-rs/core/src/compact_tests.rs +++ b/codex-rs/core/src/compact_tests.rs @@ -4,6 +4,7 @@ use codex_model_provider_info::WireApi; use codex_protocol::models::DEFAULT_IMAGE_DETAIL; use codex_protocol::models::InternalChatMessageMetadataPassthrough; use pretty_assertions::assert_eq; +use std::sync::Arc; async fn process_compacted_history_with_test_session( compacted_history: Vec, @@ -13,12 +14,16 @@ async fn process_compacted_history_with_test_session( session .set_previous_turn_settings(previous_turn_settings.cloned()) .await; - let initial_context = session.build_initial_context(&turn_context).await; + let world_state = Arc::new(session.build_world_state(&turn_context).await); + let initial_context = session + .build_initial_context_with_world_state(&turn_context, world_state.as_ref()) + .await; + let initial_context_injection = InitialContextInjection::BeforeLastUserMessage(world_state); let (refreshed, _) = crate::compact_remote::process_compacted_history( &session, &turn_context, compacted_history, - InitialContextInjection::BeforeLastUserMessage, + &initial_context_injection, ) .await; (refreshed, initial_context) diff --git a/codex-rs/core/src/context/world_state/environment.rs b/codex-rs/core/src/context/world_state/environment.rs index 589e8dbadf..d6e3284667 100644 --- a/codex-rs/core/src/context/world_state/environment.rs +++ b/codex-rs/core/src/context/world_state/environment.rs @@ -24,10 +24,6 @@ pub(crate) struct EnvironmentsState { } impl EnvironmentsState { - pub(crate) fn from_turn_context(turn_context: &TurnContext) -> Self { - Self::from_turn_context_with_environments(turn_context, &turn_context.environments) - } - pub(crate) fn from_turn_context_with_environments( turn_context: &TurnContext, environments: &TurnEnvironmentSnapshot, diff --git a/codex-rs/core/src/context_manager/history.rs b/codex-rs/core/src/context_manager/history.rs index 0244f7f96f..56e95888f4 100644 --- a/codex-rs/core/src/context_manager/history.rs +++ b/codex-rs/core/src/context_manager/history.rs @@ -86,18 +86,19 @@ impl ContextManager { pub(crate) fn update_world_state( &mut self, - world_state: WorldState, + world_state: impl Into>, ) -> Vec> { + let world_state = world_state.into(); let fragments = self.world_state_baseline.as_deref().map_or_else( || world_state.render_full(), |previous| world_state.render_diff(previous), ); - self.world_state_baseline = Some(Arc::new(world_state)); + self.world_state_baseline = Some(world_state); fragments } - pub(crate) fn set_world_state_baseline(&mut self, world_state: WorldState) { - self.world_state_baseline = Some(Arc::new(world_state)); + pub(crate) fn set_world_state_baseline(&mut self, world_state: impl Into>) { + self.world_state_baseline = Some(world_state.into()); } pub(crate) fn set_token_usage_full(&mut self, context_window: i64) { diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index 9ca9bea5e0..d96f8ec5c2 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -32,7 +32,6 @@ use crate::context::NetworkRuleSaved; use crate::context::PermissionsInstructions; use crate::context::PersonalitySpecInstructions; use crate::context::RecommendedPluginsInstructions; -use crate::context::world_state::EnvironmentsState; use crate::context::world_state::WorldState; use crate::current_time::TimeProvider; use crate::default_skill_metadata_budget; @@ -239,7 +238,6 @@ use self::session::AppServerClientMetadata; use self::session::Session; use self::session::SessionConfiguration; pub(crate) use self::session::SessionSettingsUpdate; -use self::step_context::StepContext; #[cfg(test)] use self::turn::AssistantMessageStreamParsers; #[cfg(test)] @@ -248,7 +246,6 @@ use self::turn::realtime_text_for_event; use self::turn_context::TurnContext; use self::turn_context::TurnSkillsContext; use self::world_state::build_world_state_from_environment_snapshot; -use self::world_state::build_world_state_from_turn_context; use self::world_state::build_world_state_from_turn_context_item; #[cfg(test)] mod rollout_reconstruction_tests; @@ -2766,38 +2763,27 @@ impl Session { pub(crate) async fn record_step_environment_context_if_changed( &self, turn_context: &TurnContext, + previous_world_state: &Arc, step_context: &step_context::StepContext, - ) { - if !turn_context.config.include_environment_context { - return; + ) -> Arc { + let world_state = Arc::new( + self.build_world_state_for_environments(turn_context, &step_context.environments) + .await, + ); + let items = crate::context_manager::updates::merge_contextual_fragments( + world_state.render_diff(previous_world_state.as_ref()), + ); + if !items.is_empty() { + self.record_conversation_items(turn_context, &items).await; } - let world_state = - build_world_state_from_environment_snapshot(turn_context, &step_context.environments); - let items = { - let mut state = self.state.lock().await; - crate::context_manager::updates::merge_contextual_fragments( - state.history.update_world_state(world_state), - ) - }; - if items.is_empty() { - return; - } - - self.record_conversation_items(turn_context, &items).await; - } - - pub(crate) async fn capture_step_context(&self, turn_context: &TurnContext) -> StepContext { - let environments = if turn_context - .config - .features - .enabled(Feature::DeferredExecutor) - { - self.services.turn_environments.snapshot().await - } else { - turn_context.environments.clone() - }; - StepContext { environments } + // ContextManager remembers this for later turns; run_turn owns the live value. + self.state + .lock() + .await + .history + .set_world_state_baseline(Arc::clone(&world_state)); + world_state } pub(crate) async fn record_inter_agent_communication( @@ -2899,7 +2885,7 @@ impl Session { turn_context: &TurnContext, items: Vec, reference_context_item: Option, - world_state_baseline: Option, + world_state_baseline: Option>, compacted_item: CompactedItem, ) { let items = if turn_context.config.features.enabled(Feature::ItemIds) { @@ -3042,43 +3028,11 @@ impl Session { items } - #[cfg(test)] - pub(crate) async fn build_initial_context( + async fn build_world_state_for_environments( &self, turn_context: &TurnContext, - ) -> Vec { - let world_state = self.build_world_state(turn_context).await; - self.build_initial_context_with_world_state(turn_context, &world_state) - .await - } - - pub(crate) async fn build_initial_context_for_current_step( - &self, - turn_context: &TurnContext, - ) -> (Vec, WorldState) { - let step_context = self.capture_step_context(turn_context).await; - let mut world_state = WorldState::default(); - if turn_context.config.include_environment_context { - let environment_subagents = self - .services - .agent_control - .format_environment_context_subagents(self.thread_id) - .await; - world_state.add_section( - EnvironmentsState::from_turn_context_with_environments( - turn_context, - &step_context.environments, - ) - .with_subagents(environment_subagents), - ); - } - let items = self - .build_initial_context_with_world_state(turn_context, &world_state) - .await; - (items, world_state) - } - - async fn build_world_state(&self, turn_context: &TurnContext) -> WorldState { + environments: &TurnEnvironmentSnapshot, + ) -> WorldState { let environment_subagents = if turn_context.config.include_environment_context { self.services .agent_control @@ -3087,10 +3041,19 @@ impl Session { } else { String::new() }; - build_world_state_from_turn_context(turn_context, &environment_subagents) + build_world_state_from_environment_snapshot( + turn_context, + environments, + &environment_subagents, + ) } - async fn build_initial_context_with_world_state( + pub(crate) async fn build_world_state(&self, turn_context: &TurnContext) -> WorldState { + self.build_world_state_for_environments(turn_context, &turn_context.environments) + .await + } + + pub(crate) async fn build_initial_context_with_world_state( &self, turn_context: &TurnContext, world_state: &WorldState, @@ -3439,14 +3402,15 @@ impl Session { pub(crate) async fn maybe_start_new_context_window( &self, turn_context: &TurnContext, + world_state: Arc, ) -> Option { let window = { let mut state = self.state.lock().await; state.start_new_context_window_if_requested() }; let (window_number, window_ids) = window?; - let (context_items, world_state) = self - .build_initial_context_for_current_step(turn_context) + let context_items = self + .build_initial_context_with_world_state(turn_context, world_state.as_ref()) .await; let turn_context_item = turn_context.to_turn_context_item(); let replacement_history = context_items; @@ -3497,7 +3461,7 @@ impl Session { pub(crate) async fn record_context_updates_and_set_reference_context_item( &self, turn_context: &TurnContext, - ) { + ) -> Arc { let reference_context_item = { let state = self.state.lock().await; state.reference_context_item() @@ -3505,16 +3469,16 @@ impl Session { let turn_context_item = turn_context.to_turn_context_item(); let turn_context_changed = reference_context_item.as_ref() != Some(&turn_context_item); let should_inject_full_context = reference_context_item.is_none(); - let world_state = self.build_world_state(turn_context).await; + let world_state = Arc::new(self.build_world_state(turn_context).await); let mut context_items = if should_inject_full_context { let context_items = self - .build_initial_context_with_world_state(turn_context, &world_state) + .build_initial_context_with_world_state(turn_context, world_state.as_ref()) .await; self.state .lock() .await .history - .set_world_state_baseline(world_state); + .set_world_state_baseline(Arc::clone(&world_state)); context_items } else { // Steady-state path: append only built-in context diffs here; turn-scoped extension @@ -3525,7 +3489,7 @@ impl Session { let world_state_items = { let mut state = self.state.lock().await; crate::context_manager::updates::merge_contextual_fragments( - state.history.update_world_state(world_state), + state.history.update_world_state(Arc::clone(&world_state)), ) }; context_items.extend(world_state_items); @@ -3538,7 +3502,7 @@ impl Session { ); } if !turn_context_changed && context_items.is_empty() { - return; + return world_state; } if !context_items.is_empty() { self.record_conversation_items(turn_context, &context_items) @@ -3553,6 +3517,7 @@ impl Session { // context items. let mut state = self.state.lock().await; state.set_reference_context_item(Some(turn_context_item)); + world_state } pub(crate) async fn update_token_usage_info( diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index 378dff58c9..b4003c43ad 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -1989,7 +1989,7 @@ async fn resumed_history_injects_initial_context_on_first_context_update_only() session .record_context_updates_and_set_reference_context_item(&turn_context) .await; - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; expected.extend(initial_context); let history_after_seed = session.clone_history().await; assert_eq!(expected, history_after_seed.raw_items()); @@ -2874,7 +2874,7 @@ async fn thread_rollback_drops_last_turn_from_history() { ) .await; - let initial_context = sess.build_initial_context(tc.as_ref()).await; + let initial_context = build_initial_context(&sess, tc.as_ref()).await; let turn_1 = vec![ user_message("turn 1 user"), assistant_message("turn 1 assistant"), @@ -2942,7 +2942,7 @@ async fn thread_rollback_clears_history_when_num_turns_exceeds_existing_turns() ) .await; - let initial_context = sess.build_initial_context(tc.as_ref()).await; + let initial_context = build_initial_context(&sess, tc.as_ref()).await; let turn_1 = vec![user_message("turn 1 user")]; let mut full_history = Vec::new(); full_history.extend(initial_context.clone()); @@ -2968,7 +2968,7 @@ async fn thread_rollback_clears_history_when_num_turns_exceeds_existing_turns() async fn thread_rollback_fails_without_persisted_thread_history() { let (sess, tc, rx) = make_session_and_context_with_rx().await; - let initial_context = sess.build_initial_context(tc.as_ref()).await; + let initial_context = build_initial_context(&sess, tc.as_ref()).await; sess.record_conversation_items(tc.as_ref(), &initial_context) .await; @@ -3378,7 +3378,7 @@ async fn thread_rollback_persists_marker_and_replays_cumulatively() { async fn thread_rollback_fails_when_turn_in_progress() { let (sess, tc, rx) = make_session_and_context_with_rx().await; - let initial_context = sess.build_initial_context(tc.as_ref()).await; + let initial_context = build_initial_context(&sess, tc.as_ref()).await; sess.record_conversation_items(tc.as_ref(), &initial_context) .await; @@ -3399,7 +3399,7 @@ async fn thread_rollback_fails_when_turn_in_progress() { async fn thread_rollback_fails_when_num_turns_is_zero() { let (sess, tc, rx) = make_session_and_context_with_rx().await; - let initial_context = sess.build_initial_context(tc.as_ref()).await; + let initial_context = build_initial_context(&sess, tc.as_ref()).await; sess.record_conversation_items(tc.as_ref(), &initial_context) .await; @@ -5020,6 +5020,13 @@ async fn session_new_fails_when_zsh_fork_enabled_without_packaged_zsh() { assert!(msg.contains("zsh fork feature enabled, but no packaged zsh fork is available")); } +async fn build_initial_context(session: &Session, turn_context: &TurnContext) -> Vec { + let world_state = session.build_world_state(turn_context).await; + session + .build_initial_context_with_world_state(turn_context, &world_state) + .await +} + // todo: use online model info pub(crate) async fn make_session_and_context() -> (Session, TurnContext) { let (tx_event, _rx_event) = async_channel::unbounded(); @@ -7680,7 +7687,7 @@ async fn build_initial_context_uses_previous_realtime_state() { let (session, mut turn_context) = make_session_and_context().await; turn_context.realtime_active = true; - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; let developer_texts = developer_input_texts(&initial_context); assert!( developer_texts @@ -7694,7 +7701,7 @@ async fn build_initial_context_uses_previous_realtime_state() { let mut state = session.state.lock().await; state.set_reference_context_item(Some(previous_context_item)); } - let resumed_context = session.build_initial_context(&turn_context).await; + let resumed_context = build_initial_context(&session, &turn_context).await; let resumed_developer_texts = developer_input_texts(&resumed_context); assert!( !resumed_developer_texts @@ -7793,7 +7800,7 @@ async fn build_initial_context_includes_prompt_fragments_from_extensions() { .thread_extension_data .insert(PromptExtensionTestState); - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; let developer_messages = developer_message_texts(&initial_context); assert!( @@ -7819,7 +7826,7 @@ async fn build_initial_context_includes_turn_context_fragments_from_extensions() expected_model_context_window: Some(50), }); - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; let developer_messages = developer_message_texts(&initial_context); assert!( @@ -7873,7 +7880,7 @@ async fn build_initial_context_omits_prompt_fragments_without_extension_state() let (mut session, turn_context) = make_session_and_context().await; session.services.extensions = prompt_extension_test_registry(); - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; let developer_messages = developer_message_texts(&initial_context); assert!( @@ -7890,7 +7897,7 @@ async fn build_initial_context_adds_multi_agent_v2_root_usage_hint_as_developer_ let (session, turn_context) = make_multi_agent_v2_usage_hint_test_session(/*enable_multi_agent_v2*/ true).await; - let initial_context = session.build_initial_context(turn_context.as_ref()).await; + let initial_context = build_initial_context(&session, turn_context.as_ref()).await; let developer_messages = developer_message_texts(&initial_context); assert!( @@ -7928,7 +7935,7 @@ async fn build_initial_context_adds_multi_agent_v2_subagent_usage_hint_as_develo .expect("thread settings should not be shared") .session_source = session_source; - let initial_context = session.build_initial_context(turn_context.as_ref()).await; + let initial_context = build_initial_context(&session, turn_context.as_ref()).await; let developer_messages = developer_message_texts(&initial_context); assert!( @@ -7950,7 +7957,7 @@ async fn build_initial_context_omits_multi_agent_v2_usage_hints_when_feature_dis let (session, turn_context) = make_multi_agent_v2_usage_hint_test_session(/*enable_multi_agent_v2*/ false).await; - let initial_context = session.build_initial_context(turn_context.as_ref()).await; + let initial_context = build_initial_context(&session, turn_context.as_ref()).await; let developer_messages = developer_message_texts(&initial_context); assert!( @@ -7977,7 +7984,7 @@ async fn build_initial_context_omits_multi_agent_v2_usage_hints_when_hint_is_emp ) .await; - let initial_context = session.build_initial_context(turn_context.as_ref()).await; + let initial_context = build_initial_context(&session, turn_context.as_ref()).await; let developer_messages = developer_message_texts(&initial_context); assert!( @@ -8007,7 +8014,7 @@ async fn build_initial_context_omits_default_image_save_location_with_image_hist ) .await; - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; let developer_texts = developer_input_texts(&initial_context); assert!( !developer_texts @@ -8021,7 +8028,7 @@ async fn build_initial_context_omits_default_image_save_location_with_image_hist async fn build_initial_context_omits_default_image_save_location_without_image_history() { let (session, turn_context) = make_session_and_context().await; - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; let developer_texts = developer_input_texts(&initial_context); assert!( @@ -8063,7 +8070,7 @@ async fn build_initial_context_trims_skill_metadata_from_context_window_budget() turn_context.model_info.context_window = Some(100); turn_context.turn_skills = TurnSkillsContext::new(HostSkillsSnapshot::new(Arc::new(outcome))); - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; let developer_texts = developer_input_texts(&initial_context); assert!( @@ -8214,7 +8221,7 @@ async fn build_initial_context_emits_thread_start_skill_warning_on_repeated_buil turn_context.model_info.context_window = Some(100); turn_context.turn_skills = TurnSkillsContext::new(HostSkillsSnapshot::new(Arc::new(outcome))); - let _ = session.build_initial_context(&turn_context).await; + let _ = build_initial_context(&session, &turn_context).await; let warning_event = timeout(Duration::from_secs(1), rx.recv()) .await .expect("warning event should arrive") @@ -8225,7 +8232,7 @@ async fn build_initial_context_emits_thread_start_skill_warning_on_repeated_buil if message == "Exceeded skills context budget of 2%. All skill descriptions were removed and 2 additional skills were not included in the model-visible skills list." )); - let _ = session.build_initial_context(&turn_context).await; + let _ = build_initial_context(&session, &turn_context).await; let warning_event = timeout(Duration::from_secs(1), rx.recv()) .await .expect("warning event should arrive on repeated build") @@ -8345,7 +8352,7 @@ async fn build_initial_context_uses_previous_turn_settings_for_realtime_end() { session .set_previous_turn_settings(Some(previous_turn_settings)) .await; - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; let developer_texts = developer_input_texts(&initial_context); assert!( developer_texts @@ -8368,7 +8375,7 @@ async fn build_initial_context_restates_realtime_start_when_reference_context_is session .set_previous_turn_settings(Some(previous_turn_settings)) .await; - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; let developer_texts = developer_input_texts(&initial_context); assert!( developer_texts @@ -8455,7 +8462,7 @@ async fn record_context_updates_and_set_reference_context_item_injects_full_cont .record_context_updates_and_set_reference_context_item(&turn_context) .await; let history = session.clone_history().await; - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; assert_eq!(history.raw_items().to_vec(), initial_context); let current_context = session.reference_context_item().await; @@ -8502,7 +8509,7 @@ async fn record_context_updates_and_set_reference_context_item_reinjects_full_co let history = session.clone_history().await; let mut expected_history = vec![compacted_summary]; - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; expected_history.extend(initial_context); assert_eq!(history.raw_items().to_vec(), expected_history); } @@ -8614,7 +8621,7 @@ async fn build_initial_context_prepends_model_switch_message() { session .set_previous_turn_settings(Some(previous_turn_settings)) .await; - let initial_context = session.build_initial_context(&turn_context).await; + let initial_context = build_initial_context(&session, &turn_context).await; let ResponseItem::Message { role, content, .. } = &initial_context[0] else { panic!("expected developer message"); @@ -9939,9 +9946,7 @@ async fn sample_rollout( // Use the same turn_context source as record_initial_history so model_info (and thus // personality_spec) matches reconstruction. let reconstruction_turn = session.new_default_turn().await; - let mut initial_context = session - .build_initial_context(reconstruction_turn.as_ref()) - .await; + let mut initial_context = build_initial_context(session, reconstruction_turn.as_ref()).await; // Ensure personality_spec is present when Personality is enabled, so expected matches // what reconstruction produces (build_initial_context may omit it when baked into model). if !initial_context.iter().any(|m| { diff --git a/codex-rs/core/src/session/tests/guardian_tests.rs b/codex-rs/core/src/session/tests/guardian_tests.rs index dc0979676c..b4668b358e 100644 --- a/codex-rs/core/src/session/tests/guardian_tests.rs +++ b/codex-rs/core/src/session/tests/guardian_tests.rs @@ -523,6 +523,8 @@ async fn process_compacted_history_preserves_separate_guardian_developer_message } turn_context.session_source = guardian_source; turn_context.developer_instructions = Some(guardian_policy.clone()); + let world_state = Arc::new(session.build_world_state(&turn_context).await); + let initial_context_injection = InitialContextInjection::BeforeLastUserMessage(world_state); let (refreshed, _) = crate::compact_remote::process_compacted_history( &session, @@ -547,7 +549,7 @@ async fn process_compacted_history_preserves_separate_guardian_developer_message internal_chat_message_metadata_passthrough: None, }, ], - InitialContextInjection::BeforeLastUserMessage, + &initial_context_injection, ) .await; diff --git a/codex-rs/core/src/session/turn.rs b/codex-rs/core/src/session/turn.rs index 40aa96cea9..607e2ad79e 100644 --- a/codex-rs/core/src/session/turn.rs +++ b/codex-rs/core/src/session/turn.rs @@ -42,6 +42,7 @@ use crate::responses_retry::handle_retryable_response_stream_error; use crate::session::PreviousTurnSettings; use crate::session::TurnInput; use crate::session::session::Session; +use crate::session::step_context::StepContext; use crate::session::turn_context::TurnContext; use crate::stream_events_utils::HandleOutputCtx; use crate::stream_events_utils::TurnItemContributorPolicy; @@ -163,7 +164,9 @@ pub(crate) async fn run_turn( return Ok(None); } - sess.record_context_updates_and_set_reference_context_item(turn_context.as_ref()) + // Keep the exact model-visible state used by this turn and its inline compactions. + let mut world_state = sess + .record_context_updates_and_set_reference_context_item(turn_context.as_ref()) .await; let Some((injection_items, explicitly_enabled_connectors)) = @@ -246,12 +249,16 @@ pub(crate) async fn run_turn( .features .enabled(Feature::DeferredExecutor) { - let step_context = sess.capture_step_context(turn_context.as_ref()).await; - sess.record_step_environment_context_if_changed( - turn_context.as_ref(), - &step_context, - ) - .await; + let step_context = StepContext { + environments: sess.services.turn_environments.snapshot().await, + }; + world_state = sess + .record_step_environment_context_if_changed( + turn_context.as_ref(), + &world_state, + &step_context, + ) + .await; } // Construct the input that we will send to the model. @@ -338,7 +345,7 @@ pub(crate) async fn run_turn( .await; let started_new_context_window = sess - .maybe_start_new_context_window(turn_context.as_ref()) + .maybe_start_new_context_window(turn_context.as_ref(), Arc::clone(&world_state)) .await .is_some(); if started_new_context_window && needs_follow_up { @@ -358,7 +365,7 @@ pub(crate) async fn run_turn( &sess, &turn_context, &mut client_session, - InitialContextInjection::BeforeLastUserMessage, + InitialContextInjection::BeforeLastUserMessage(Arc::clone(&world_state)), CompactionReason::ContextLimit, CompactionPhase::MidTurn, ) diff --git a/codex-rs/core/src/session/world_state.rs b/codex-rs/core/src/session/world_state.rs index 5d12857748..3d46522b8e 100644 --- a/codex-rs/core/src/session/world_state.rs +++ b/codex-rs/core/src/session/world_state.rs @@ -4,34 +4,21 @@ use crate::context::world_state::WorldState; use crate::environment_selection::TurnEnvironmentSnapshot; use codex_protocol::protocol::TurnContextItem; -pub(super) fn build_world_state_from_turn_context( +pub(super) fn build_world_state_from_environment_snapshot( turn_context: &TurnContext, + environments: &TurnEnvironmentSnapshot, environment_subagents: &str, ) -> WorldState { let mut world_state = WorldState::default(); if turn_context.config.include_environment_context { world_state.add_section( - EnvironmentsState::from_turn_context(turn_context) + EnvironmentsState::from_turn_context_with_environments(turn_context, environments) .with_subagents(environment_subagents.to_string()), ); } world_state } -pub(super) fn build_world_state_from_environment_snapshot( - turn_context: &TurnContext, - environments: &TurnEnvironmentSnapshot, -) -> WorldState { - let mut world_state = WorldState::default(); - if turn_context.config.include_environment_context { - world_state.add_section(EnvironmentsState::from_turn_context_with_environments( - turn_context, - environments, - )); - } - world_state -} - pub(super) fn build_world_state_from_turn_context_item( turn_context_item: &TurnContextItem, ) -> WorldState { diff --git a/codex-rs/core/src/thread_manager_tests.rs b/codex-rs/core/src/thread_manager_tests.rs index afb103b883..9092d63689 100644 --- a/codex-rs/core/src/thread_manager_tests.rs +++ b/codex-rs/core/src/thread_manager_tests.rs @@ -225,7 +225,10 @@ fn out_of_range_truncation_drops_pre_user_active_turn_prefix() { #[tokio::test] async fn ignores_session_prefix_messages_when_truncating() { let (session, turn_context) = make_session_and_context().await; - let mut items = session.build_initial_context(&turn_context).await; + let world_state = session.build_world_state(&turn_context).await; + let mut items = session + .build_initial_context_with_world_state(&turn_context, &world_state) + .await; items.push(user_msg("feature request")); items.push(assistant_msg("ack")); items.push(user_msg("second question")); diff --git a/codex-rs/core/src/thread_rollout_truncation_tests.rs b/codex-rs/core/src/thread_rollout_truncation_tests.rs index 2df3d1c98e..ca3cabe60c 100644 --- a/codex-rs/core/src/thread_rollout_truncation_tests.rs +++ b/codex-rs/core/src/thread_rollout_truncation_tests.rs @@ -166,7 +166,10 @@ fn truncates_rollout_from_start_applies_thread_rollback_markers() { #[tokio::test] async fn ignores_session_prefix_messages_when_truncating_rollout_from_start() { let (session, turn_context) = make_session_and_context().await; - let mut items = session.build_initial_context(&turn_context).await; + let world_state = session.build_world_state(&turn_context).await; + let mut items = session + .build_initial_context_with_world_state(&turn_context, &world_state) + .await; items.push(user_msg("feature request")); items.push(assistant_msg("ack")); items.push(user_msg("second question")); diff --git a/codex-rs/core/tests/suite/remote_env.rs b/codex-rs/core/tests/suite/remote_env.rs index 63cca20b68..4f02001d8c 100644 --- a/codex-rs/core/tests/suite/remote_env.rs +++ b/codex-rs/core/tests/suite/remote_env.rs @@ -686,7 +686,7 @@ async fn deferred_executor_updates_model_context_after_startup() -> Result<()> { } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn deferred_executor_compaction_reinjects_current_environment_once() -> Result<()> { +async fn deferred_executor_compaction_preserves_then_updates_environment_once() -> Result<()> { let listener = TcpListener::bind("127.0.0.1:0").await?; let server = start_mock_server().await; let response_mock = mount_sse_sequence( @@ -798,7 +798,7 @@ async fn deferred_executor_compaction_reinjects_current_environment_once() -> Re .iter() .filter(|text| text.contains("starting")) .count(), - 0 + 1 ); assert_eq!( post_compaction_context @@ -807,6 +807,15 @@ async fn deferred_executor_compaction_reinjects_current_environment_once() -> Re .count(), 1 ); + let starting_index = post_compaction_context + .iter() + .position(|text| text.contains("starting")) + .expect("compaction should preserve the prior environment state"); + let ready_index = post_compaction_context + .iter() + .position(|text| text.contains("zsh")) + .expect("the next sampling step should report that the environment is ready"); + assert!(starting_index < ready_index); Ok(()) }