From 7131f15461cac46fa1f49ba4ac9e44a34d6d7a01 Mon Sep 17 00:00:00 2001 From: Will Emmanuel Date: Tue, 1 Dec 2026 22:40:22 +0000 Subject: [PATCH] Include the host subagent roster in initial environment context (#65118) ## Why When a turn has no execution environments or other environment context to report, Codex can skip the environment update before reading the host roster. The model then misses any active subagents in that roster. ## What changed Keep the initial environment update until Codex has had a chance to attach the host roster. Emit `` when the roster is its only content, and drop the update if it is still empty. ## Testing Add integration coverage for roster-only context and empty context in both the default and per-environment formats. GitOrigin-RevId: 6ceffd9aa13fefa31387c4d598a81a4281554c1c --- .../src/context/world_state/environment.rs | 18 +++++- codex-rs/core/src/context/world_state/mod.rs | 9 ++- .../core/tests/suite/agent_control_tests.rs | 59 +++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/codex-rs/core/src/context/world_state/environment.rs b/codex-rs/core/src/context/world_state/environment.rs index afc7a543d7..6a263ee8bf 100644 --- a/codex-rs/core/src/context/world_state/environment.rs +++ b/codex-rs/core/src/context/world_state/environment.rs @@ -190,6 +190,8 @@ impl WorldStateSection for EnvironmentsState { subagents: self.subagents.clone(), }; let empty = EnvironmentsSnapshot::default(); + // The host roster is attached later and may be the only initial content. + let initial = !matches!(previous, PreviousSectionState::Known(_)); let previous = match previous { PreviousSectionState::Known(previous) => previous, PreviousSectionState::Absent | PreviousSectionState::Unknown => &empty, @@ -232,7 +234,7 @@ impl WorldStateSection for EnvironmentsState { && updates .values() .all(|update| matches!(update, EnvironmentUpdate::Current(_))); - let fragment = (!updates.is_empty() || turn_context_values_changed).then(|| { + let fragment = (initial || !updates.is_empty() || turn_context_values_changed).then(|| { let environment = Box::new(RenderedEnvironments { updates, legacy_single, @@ -299,6 +301,20 @@ pub(crate) struct RenderedEnvironments { pub(crate) subagents: Option, } +impl RenderedEnvironments { + pub(super) fn is_empty(&self) -> bool { + self.updates.is_empty() + && self.shell_version.is_none() + && !self.shell_version_removed + && self.current_date.is_none() + && !self.current_date_removed + && self.timezone.is_none() + && self.network.is_none() + && self.filesystem.is_none() + && self.subagents.is_none() + } +} + enum EnvironmentUpdate { Current(EnvironmentState), Unavailable, diff --git a/codex-rs/core/src/context/world_state/mod.rs b/codex-rs/core/src/context/world_state/mod.rs index f1922d045f..6a0f2a7746 100644 --- a/codex-rs/core/src/context/world_state/mod.rs +++ b/codex-rs/core/src/context/world_state/mod.rs @@ -592,9 +592,8 @@ enum RenderScope { } impl WorldState { - /// Read the roster only when rendering an environment fragment. If the read fails, - /// keep rendering the rest of the fragment. - pub(crate) async fn resolve_host_roster(&self, updates: &mut [WorldStateUpdate]) { + /// Attach the host roster before discarding empty environment updates. + pub(crate) async fn resolve_host_roster(&self, updates: &mut Vec) { if let Some(reader) = &self.host_roster_reader && let Some(environment) = updates @@ -614,6 +613,10 @@ impl WorldState { Err(_) => tracing::warn!("failed to read host agent roster for prompt context"), } } + updates.retain(|update| match &update.content { + WorldStateUpdateContent::Environment(environment) => !environment.is_empty(), + WorldStateUpdateContent::Fragment(_) | WorldStateUpdateContent::Item(_) => true, + }); } pub(crate) fn full_context_excluded_section_ids( diff --git a/codex-rs/core/tests/suite/agent_control_tests.rs b/codex-rs/core/tests/suite/agent_control_tests.rs index 5b30464217..edb8750766 100644 --- a/codex-rs/core/tests/suite/agent_control_tests.rs +++ b/codex-rs/core/tests/suite/agent_control_tests.rs @@ -324,6 +324,65 @@ impl AgentControl for TestAgentControl { } } +#[test_case(true, true; "per_environment_with_child")] +#[test_case(true, false; "per_environment_empty")] +#[test_case(false, true; "default_with_child")] +#[test_case(false, false; "default_empty")] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn host_roster_can_be_the_only_environment_context( + per_environment: bool, + has_child: bool, +) -> anyhow::Result<()> { + skip_if_no_network!(Ok(())); + let server = responses::start_mock_server().await; + let response = responses::mount_sse_once(&server, responses::sse_completed("roster")).await; + let mut extensions = ExtensionRegistryBuilder::new(); + extensions.thread_lifecycle_contributor(Arc::new(HostRuntime)); + let test = test_codex() + .with_extensions(Arc::new(extensions.build())) + .with_config(move |config| { + config.include_environment_context_time = false; + config + .features + .enable(Feature::MultiAgentV2) + .expect("enable multi-agent V2"); + config + .features + .set_enabled(Feature::PerEnvironmentContext, per_environment) + .expect("configure environment format"); + }) + .build_with_auto_env(&server) + .await?; + if has_child { + let reader = test + .codex + .thread_extension_data() + .get::() + .expect("host reader"); + *reader.children.lock().expect("host roster") = Some(vec![AgentRef { + thread_id: ThreadId::new(), + path: AgentPath::root().join("worker").expect("child path"), + }]); + } + test.submit_turn_with_environments("Check the roster.", Some(Vec::new())) + .await?; + let messages = response.single_request().message_input_texts("user"); + let hint = messages + .iter() + .find(|text| text.starts_with("")); + assert_eq!( + hint.map(String::as_str), + has_child.then_some( + r#" + + + +"# + ) + ); + Ok(()) +} + async fn test_with_host_control( server: &MockServer, extra_features: &[Feature],