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 `<environment_context>` 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
This commit is contained in:
Will Emmanuel
2026-12-02 00:28:07 +00:00
committed by copyberry
parent c4fcff5e97
commit 7131f15461
3 changed files with 82 additions and 4 deletions
@@ -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<String>,
}
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,
+6 -3
View File
@@ -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<WorldStateUpdate>) {
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(
@@ -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::<HostSessionReader>()
.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("<environment_context>"));
assert_eq!(
hint.map(String::as_str),
has_child.then_some(
r#"<environment_context>
<subagents>
<agent name="/root/worker" />
</subagents>
</environment_context>"#
)
);
Ok(())
}
async fn test_with_host_control(
server: &MockServer,
extra_features: &[Feature],