diff --git a/codex-rs/core/src/session/inject.rs b/codex-rs/core/src/session/inject.rs index d7d9bc6fe5..5c2d142142 100644 --- a/codex-rs/core/src/session/inject.rs +++ b/codex-rs/core/src/session/inject.rs @@ -39,8 +39,7 @@ impl Session { let step = self .capture_step_context(Arc::clone(&turn), &CancellationToken::new()) .await?; - self.record_context_updates_and_set_reference_context_item(step.as_ref()) - .await?; + self.try_publish_initial_context(step.as_ref()).await?; } Ok(turn) } diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index 8852fc7ed4..9eb4d831c7 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -5579,49 +5579,83 @@ impl Session { &self, step_context: &StepContext, ) -> CodexResult> { - let turn_context = step_context.turn.as_ref(); - let reference_context_item = { - let state = self.state.lock().await; - state.reference_context_item() + let previous = loop { + if let Some(previous) = self.reference_context_item().await { + break previous; + } + if let Some(world_state) = self.try_publish_initial_context(step_context).await? { + return Ok(world_state); + } + // Another publisher installed the prefix. Render this turn against its baseline. }; - let turn_context_item = step_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_for_step(step_context, should_inject_full_context) + .build_world_state_for_step(step_context, /*new_window*/ false) .await?; - // Full initial context resets the baseline; later turns persist only its changes. - let (mut context_items, world_state_item) = if should_inject_full_context { - let (context_updates, snapshot) = self - .build_initial_context_with_world_state(step_context, &world_state) - .await; - let context_items = - crate::context_manager::updates::merge_world_state_updates(context_updates); - self.state - .lock() - .await - .history - .set_world_state_baseline(snapshot.clone()); - ( - context_items, - Some(WorldStateItem::full(snapshot.into_object())), - ) - } else { - let (snapshot, mut fragments, world_state_item) = self - .state - .lock() - .await - .history - .render_step_world_state(&world_state); - world_state.resolve_host_roster(&mut fragments).await; - let items = crate::context_manager::updates::merge_world_state_updates(fragments); - self.state - .lock() - .await - .history - .set_world_state_baseline(snapshot); - (items, world_state_item) - }; + let (snapshot, mut fragments, world_state_item) = self + .state + .lock() + .await + .history + .render_step_world_state(&world_state); + world_state.resolve_host_roster(&mut fragments).await; + let items = crate::context_manager::updates::merge_world_state_updates(fragments); + self.state + .lock() + .await + .history + .set_world_state_baseline(snapshot); + self.publish_step_context(step_context, items, world_state_item, Some(&previous)) + .await; + Ok(Arc::new(world_state)) + } + + /// Render before acquiring the publication permit so extension and host reads can progress concurrently. + /// Only the winner appends the prefix and publishes its world-state and turn checkpoints. + async fn try_publish_initial_context( + &self, + step_context: &StepContext, + ) -> CodexResult>> { + let world_state = self + .build_world_state_for_step(step_context, /*new_window*/ true) + .await?; + let (updates, snapshot) = self + .build_initial_context_with_world_state(step_context, &world_state) + .await; + let _permit = self + .initial_context_publication + .acquire() + .await + .map_err(|err| CodexErr::Fatal(format!("cannot publish initial context: {err}")))?; + if self.reference_context_item().await.is_some() { + return Ok(None); + } + let items = crate::context_manager::updates::merge_world_state_updates(updates); + self.state + .lock() + .await + .history + .set_world_state_baseline(snapshot.clone()); + self.publish_step_context( + step_context, + items, + Some(WorldStateItem::full(snapshot.into_object())), + /*previous*/ None, + ) + .await; + Ok(Some(Arc::new(world_state))) + } + + async fn publish_step_context( + &self, + step_context: &StepContext, + mut context_items: Vec, + world_state_item: Option, + previous: Option<&TurnContextItem>, + ) { + let turn_context = step_context.turn.as_ref(); + let turn_context_item = step_context.to_turn_context_item(); + let turn_context_changed = previous != Some(&turn_context_item); + let should_inject_full_context = previous.is_none(); if !should_inject_full_context && turn_context_changed { context_items.extend( self.build_turn_context_contribution_items(step_context) @@ -5631,7 +5665,7 @@ impl Session { // A snapshot can change without producing model-visible or TurnContext updates. let only_world_state_changed = !turn_context_changed && context_items.is_empty(); if only_world_state_changed && world_state_item.is_none() { - return Ok(Arc::new(world_state)); + return; } if !context_items.is_empty() { self.record_conversation_items( @@ -5648,7 +5682,7 @@ impl Session { } // A snapshot-only change does not require a duplicate TurnContext record. if only_world_state_changed { - return Ok(Arc::new(world_state)); + return; } // Persist one `TurnContextItem` per real user turn so resume/lazy replay can recover the // latest durable baseline even when this turn emitted no model-visible context diffs. @@ -5672,7 +5706,6 @@ impl Session { // context items. let mut state = self.state.lock().await; state.set_reference_context_item(Some(turn_context_item)); - Ok(Arc::new(world_state)) } /// Record reported spending without changing the context baseline or estimating usage. diff --git a/codex-rs/core/src/session/session.rs b/codex-rs/core/src/session/session.rs index c8d6395de3..06406661c2 100644 --- a/codex-rs/core/src/session/session.rs +++ b/codex-rs/core/src/session/session.rs @@ -82,6 +82,8 @@ pub(crate) struct Session { pub(crate) parent_observer_event_tx: OnceLock>, pub(super) agent_status: watch::Sender, pub(super) state: Mutex, + /// Serializes initial prefix publication until its reference context is installed. + pub(super) initial_context_publication: Semaphore, /// Orders accepted settings commits and their persisted events with compaction checkpoints. /// Keep this separate from `state` so storage I/O does not block runtime state access. pub(super) thread_settings_persistence: Semaphore, @@ -2001,6 +2003,7 @@ impl Session { parent_observer_event_tx: OnceLock::new(), agent_status, state: Mutex::new(state), + initial_context_publication: Semaphore::new(/*permits*/ 1), thread_settings_persistence: Semaphore::new(/*permits*/ 1), code_mode_message_tasks: CodeModeMessageTasks::default(), managed_network_proxy_refresh_lock: Semaphore::new(/*permits*/ 1), diff --git a/codex-rs/core/src/session/tests.rs b/codex-rs/core/src/session/tests.rs index 14a1905d1c..157433aec6 100644 --- a/codex-rs/core/src/session/tests.rs +++ b/codex-rs/core/src/session/tests.rs @@ -7068,6 +7068,7 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) { parent_observer_event_tx: OnceLock::new(), agent_status: agent_status_tx, state: Mutex::new(state), + initial_context_publication: Semaphore::new(/*permits*/ 1), thread_settings_persistence: Semaphore::new(/*permits*/ 1), code_mode_message_tasks: Default::default(), managed_network_proxy_refresh_lock: Semaphore::new(/*permits*/ 1), @@ -9424,6 +9425,7 @@ where parent_observer_event_tx: OnceLock::new(), agent_status: agent_status_tx, state: Mutex::new(state), + initial_context_publication: Semaphore::new(/*permits*/ 1), thread_settings_persistence: Semaphore::new(/*permits*/ 1), code_mode_message_tasks: Default::default(), managed_network_proxy_refresh_lock: Semaphore::new(/*permits*/ 1), diff --git a/codex-rs/core/tests/suite/scenarios_preparation.rs b/codex-rs/core/tests/suite/scenarios_preparation.rs index cf7f7294a3..c18db95dcf 100644 --- a/codex-rs/core/tests/suite/scenarios_preparation.rs +++ b/codex-rs/core/tests/suite/scenarios_preparation.rs @@ -134,3 +134,152 @@ async fn prepared_context_reaches_first_request_and_survives_follow_up( test.codex.shutdown_and_wait().await?; Ok(()) } + +#[test_case(true; "turn_prepares_first")] +#[test_case(false; "injection_prepares_first")] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn first_turn_and_raw_injection_publish_one_initial_prefix( + turn_prepares_first: bool, +) -> Result<()> { + use codex_extension_api::ContextContributor; + use codex_extension_api::ExtensionFuture; + use codex_extension_api::PreviousWorldStateSection; + use codex_extension_api::RenderedWorldStateFragment; + use codex_extension_api::WorldStateContributionInput; + use codex_extension_api::WorldStateSectionContribution; + use codex_extension_api::input_text; + use core_test_support::responses; + use futures::FutureExt; + use std::sync::OnceLock; + use tokio::sync::Notify; + + const INITIAL: &str = "Instructions for the new window."; + const INJECTED: &str = "Caller-provided context."; + skip_if_no_network!(Ok(())); + + struct OverlappingInitializers { + paused_turn_id: OnceLock, + entered: Notify, + resume: Notify, + } + impl ContextContributor for OverlappingInitializers { + fn contribute_world_state<'a>( + &'a self, + input: WorldStateContributionInput<'a>, + ) -> ExtensionFuture<'a, Vec> { + Box::pin(async move { + // Hold the first caller after it observes the missing reference context. + if self.paused_turn_id.set(input.turn_id.to_owned()).is_ok() { + self.entered.notify_one(); + self.resume.notified().await; + } + let current = + if self.paused_turn_id.get().map(String::as_str) == Some(input.turn_id) { + "paused" + } else { + "other" + }; + vec![WorldStateSectionContribution::new( + "race_state", + move |previous| { + let previous = match previous { + PreviousWorldStateSection::Known(value) => { + value.as_str().unwrap_or("invalid") + } + PreviousWorldStateSection::Absent => "absent", + PreviousWorldStateSection::Unknown => "unknown", + }; + if previous == current { + return (None, None); + } + ( + Some(json!(current)), + Some(RenderedWorldStateFragment::new( + "developer", + ("", ""), + input_text(format!("Race state: {previous} -> {current}")), + )), + ) + }, + )] + }) + } + } + + let server = start_mock_server().await; + let mut extensions = ExtensionRegistryBuilder::::new(); + let overlap = Arc::new(OverlappingInitializers { + paused_turn_id: OnceLock::new(), + entered: Notify::new(), + resume: Notify::new(), + }); + extensions.prompt_contributor(overlap.clone()); + let test = test_codex() + .with_extensions(Arc::new(extensions.build())) + .with_config(|config| config.developer_instructions = Some(INITIAL.to_string())) + .build_with_auto_env(&server) + .await?; + let mock = responses::mount_response_handler(&server, |_| { + responses::sse_response(sse(vec![ev_completed("done")])) + }) + .await; + let item = serde_json::from_value(json!({ + "type": "message", "role": "developer", + "content": [{"type": "input_text", "text": INJECTED}] + }))?; + let turn = test.submit_text_turn("First turn.").boxed(); + let injection = async { Ok(test.codex.inject_response_items(vec![item]).await?) }.boxed(); + let (first, second) = if turn_prepares_first { + (turn, injection) + } else { + (injection, turn) + }; + tokio::try_join!(first, async { + overlap.entered.notified().await; + let result = second.await; + overlap.resume.notify_one(); + result + })?; + let transitions: Vec<_> = mock.requests()[0] + .message_input_texts("developer") + .into_iter() + .filter(|text| text.starts_with("Race state:")) + .collect(); + let mut expected = vec!["Race state: absent -> other"]; + if turn_prepares_first { + // Injection installed its snapshot. The real turn must publish its diff before sampling. + expected.push("Race state: other -> paused"); + } + assert_eq!(transitions, expected); + test.submit_text_turn("Follow up.").await?; + let texts = mock + .last_request() + .expect("follow-up request") + .message_input_texts("developer"); + for expected in [INITIAL, INJECTED] { + assert_eq!( + texts.iter().filter(|text| text.contains(expected)).count(), + 1, + "{expected}" + ); + } + if !turn_prepares_first { + // A losing injection appends only caller-provided items. + assert!( + !texts + .iter() + .any(|text| text == "Race state: other -> paused") + ); + } + let history = test + .codex + .load_history(/*include_archived*/ false) + .await? + .items; + let full_snapshots = history + .iter() + .filter(|item| matches!(item, RolloutItem::WorldState(state) if state.full)) + .count(); + assert_eq!(full_snapshots, 1); + Ok(()) +}