mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Defer environment selection changes until the next turn (#46310)
## Why Updating environment selections while a turn is running must not redirect its tools or prevent its pending environment setup from completing. ## What changed - Store future environment selections separately from active environments, and activate them when preparing new work with no task running. - Keep turn contexts and MCP workspace roots tied to the active environment snapshot. Report saved selections through thread settings and `environment_selections()`. - Route environment configuration and failure callbacks to matching active or future selections so each can finish setup independently. - Stop the running task before manual compaction adopts the saved environments. ## Testing Add regression tests that preserve a running task's working directory and workspace roots, then adopt the saved selection for the next task. Add remote environment coverage showing that future setup can complete while the active turn waits, with each turn receiving its own capability roots. GitOrigin-RevId: 09a6b90131d2ab2441ce2fe9b0368a3668889f31
This commit is contained in:
1 parent
0c9be8a836
commit
c775dd3c33
15 files changed
+522
-135
No files matched your search
@@ -288,7 +288,12 @@ async fn executor_stop_hook_stops_after_disconnection() -> Result<()> {
|
||||
async fn executor_stop_hook_rejects_mismatched_environment() -> Result<()> {
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let fixture = executor_stop_hook_fixture().await?;
|
||||
let fixture = executor_hook_fixture(
|
||||
["first-turn", "switch-environments", "second-turn"]
|
||||
.map(completed_turn_response)
|
||||
.to_vec(),
|
||||
)
|
||||
.await?;
|
||||
let selection = fixture.attach().await?;
|
||||
fixture
|
||||
.test
|
||||
@@ -372,6 +377,11 @@ async fn executor_stop_hook_rejects_mismatched_environment() -> Result<()> {
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
fixture
|
||||
.test
|
||||
.submit_text_turn("apply the new executor environment")
|
||||
.await?;
|
||||
fixture.wait_for_hook_call().await?;
|
||||
|
||||
let mut mismatched_config = fixture.test.config.clone();
|
||||
let mut node_repl = mismatched_config
|
||||
@@ -391,7 +401,16 @@ async fn executor_stop_hook_rejects_mismatched_environment() -> Result<()> {
|
||||
.codex
|
||||
.refresh_mcp_config(mismatched_config)
|
||||
.await;
|
||||
wait_for_mcp_server(&fixture.test.codex, "node_repl").await?;
|
||||
fixture
|
||||
.test
|
||||
.codex
|
||||
.call_mcp_tool(
|
||||
"node_repl",
|
||||
"js",
|
||||
Some(json!({"code": "1 + 1"})),
|
||||
/*meta*/ None,
|
||||
)
|
||||
.await?;
|
||||
assert_eq!(
|
||||
fixture
|
||||
.test
|
||||
@@ -409,7 +428,15 @@ async fn executor_stop_hook_rejects_mismatched_environment() -> Result<()> {
|
||||
executor.abort();
|
||||
}
|
||||
|
||||
assert_eq!(fixture.calls().await?.len(), 1);
|
||||
assert_eq!(
|
||||
fixture
|
||||
.calls()
|
||||
.await?
|
||||
.iter()
|
||||
.filter(|call| call["params"]["name"] == "turn_ended")
|
||||
.count(),
|
||||
2
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ use codex_core::EnvironmentConfig;
|
||||
use codex_core::EnvironmentNetworkPolicy;
|
||||
use codex_core::StartThreadOptions;
|
||||
use codex_core::TurnInputRequest;
|
||||
use codex_core::TurnInputSubmission;
|
||||
use codex_core::WaitForEnvironmentToolConfig;
|
||||
use codex_core::compact::SUMMARIZATION_PROMPT;
|
||||
use codex_core::config::Config;
|
||||
@@ -1557,10 +1558,6 @@ async fn shared_executor_keeps_ready_capability_roots_scoped_to_each_attachment(
|
||||
)
|
||||
.await?;
|
||||
|
||||
assert_eq!(
|
||||
test.codex.inspect_selected_capability_roots().ready_roots,
|
||||
vec![root("first-root")]
|
||||
);
|
||||
assert_eq!(
|
||||
second
|
||||
.thread
|
||||
@@ -1629,6 +1626,10 @@ async fn shared_executor_keeps_ready_capability_roots_scoped_to_each_attachment(
|
||||
wait_for_event(thread, |event| matches!(event, EventMsg::TurnComplete(_))).await;
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
test.codex.inspect_selected_capability_roots().ready_roots,
|
||||
vec![root("first-updated-root")]
|
||||
);
|
||||
let requests = response_mock.requests();
|
||||
let root_fragments = requests
|
||||
.iter()
|
||||
@@ -1981,6 +1982,146 @@ async fn pending_attachment_installs_configuration_before_waiting_turn_resumes()
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn future_pending_environment_can_finish_without_retargeting_the_active_turn() -> Result<()> {
|
||||
let server = start_mock_server().await;
|
||||
let mut extensions = ExtensionRegistryBuilder::new();
|
||||
extensions.thread_lifecycle_contributor(Arc::new(WaitForEnvironmentTestExtension));
|
||||
extensions.prompt_contributor(Arc::new(ReadyCapabilityRootsTestExtension::default()));
|
||||
let mut builder = test_codex()
|
||||
.with_extensions(Arc::new(extensions.build()))
|
||||
.with_config(|config| {
|
||||
assert!(config.features.enable(Feature::DeferredExecutor).is_ok());
|
||||
});
|
||||
let test = builder.build_with_auto_env(&server).await?;
|
||||
let selection = test.codex.environment_selections().await.remove(0);
|
||||
let active = TurnEnvironmentSelection {
|
||||
config: EnvironmentConfigState::Pending,
|
||||
..selection
|
||||
};
|
||||
let future_cwd = active.cwd.join("future-environment")?;
|
||||
test.fs()
|
||||
.create_directory(
|
||||
&future_cwd,
|
||||
CreateDirectoryOptions {
|
||||
recursive: false,
|
||||
follow_symlinks: true,
|
||||
},
|
||||
/*sandbox*/ None,
|
||||
)
|
||||
.await?;
|
||||
let future = TurnEnvironmentSelection {
|
||||
cwd: future_cwd.clone(),
|
||||
workspace_roots: vec![future_cwd],
|
||||
..active.clone()
|
||||
};
|
||||
let owner_config = |selection: &TurnEnvironmentSelection, id: &str| EnvironmentConfig {
|
||||
allow_login_shell: test.config.permissions.allow_login_shell,
|
||||
workspace_roots: selection.workspace_roots.clone(),
|
||||
permission_profile: PermissionProfileSnapshot::legacy(PermissionProfile::read_only()),
|
||||
shell_environment_policy: Default::default(),
|
||||
windows_sandbox_level: WindowsSandboxLevel::from_config(&test.config),
|
||||
windows_sandbox_private_desktop: test.config.permissions.windows_sandbox_private_desktop,
|
||||
use_legacy_landlock: test.config.features.use_legacy_landlock(),
|
||||
exec_policy: None,
|
||||
mcp_policy: None,
|
||||
network_policy: None,
|
||||
selected_capability_roots: vec![SelectedCapabilityRoot {
|
||||
id: id.to_string(),
|
||||
location: CapabilityRootLocation::Environment {
|
||||
environment_id: selection.environment_id.clone(),
|
||||
path: selection.cwd.clone(),
|
||||
},
|
||||
}],
|
||||
};
|
||||
let thread = test
|
||||
.thread_manager
|
||||
.start_thread(StartThreadOptions {
|
||||
environments: Some(vec![active.clone()]),
|
||||
..StartThreadOptions::new(test.config.clone())
|
||||
})
|
||||
.await?
|
||||
.thread;
|
||||
let response_mock = mount_sse_sequence(
|
||||
&server,
|
||||
vec![
|
||||
sse(vec![
|
||||
ev_function_call(
|
||||
"wait-for-active-environment",
|
||||
"wait_for_environment",
|
||||
&json!({ "environment_id": active.environment_id }).to_string(),
|
||||
),
|
||||
ev_completed("waiting"),
|
||||
]),
|
||||
sse(vec![ev_completed("active-done")]),
|
||||
sse(vec![ev_completed("future-done")]),
|
||||
],
|
||||
)
|
||||
.await;
|
||||
let request = |text: &str| {
|
||||
TurnInputRequest::user_input(vec![UserInput::Text {
|
||||
text: text.to_string(),
|
||||
text_elements: Vec::new(),
|
||||
}])
|
||||
};
|
||||
thread.start_or_steer_turn(request("wait")).await?;
|
||||
wait_for_response_request_count(&response_mock, /*expected_count*/ 1).await;
|
||||
assert!(matches!(
|
||||
thread
|
||||
.start_or_steer_turn(
|
||||
request("use the other environment next turn").with_thread_settings(
|
||||
ThreadSettingsOverrides {
|
||||
environments: Some(TurnEnvironmentSelections::new(
|
||||
test.config.cwd.clone(),
|
||||
vec![future.clone()],
|
||||
)),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
)
|
||||
.await?,
|
||||
TurnInputSubmission::Steered { .. }
|
||||
));
|
||||
thread
|
||||
.environment_ready(&future, owner_config(&future, "future-root"))
|
||||
.await?;
|
||||
assert!(matches!(
|
||||
thread.environment_selections().await[0].config,
|
||||
EnvironmentConfigState::Ready(_)
|
||||
));
|
||||
thread
|
||||
.environment_ready(&active, owner_config(&active, "active-root"))
|
||||
.await?;
|
||||
wait_for_event(&thread, |event| matches!(event, EventMsg::TurnComplete(_))).await;
|
||||
thread.start_or_steer_turn(request("next turn")).await?;
|
||||
wait_for_event(&thread, |event| matches!(event, EventMsg::TurnComplete(_))).await;
|
||||
|
||||
let requests = response_mock.requests();
|
||||
let (output, _) = requests[1]
|
||||
.function_call_output_content_and_success("wait-for-active-environment")
|
||||
.context("active wait output should be model visible")?;
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(&output.context("active wait output should contain JSON")?)?,
|
||||
json!({ "environment_id": active.environment_id, "status": "ready" })
|
||||
);
|
||||
let ready_roots = requests[1..]
|
||||
.iter()
|
||||
.map(|request| {
|
||||
request
|
||||
.message_input_texts("user")
|
||||
.into_iter()
|
||||
.rfind(|text| text.contains("<ready_capability_roots>"))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
ready_roots,
|
||||
["active-root", "future-root"].map(|root| Some(format!(
|
||||
"<ready_capability_roots>{root}</ready_capability_roots>"
|
||||
)))
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test_case(true; "uses refreshed executor root")]
|
||||
#[test_case(false; "preserves persisted root when executor reports none")]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
|
||||
@@ -704,7 +704,7 @@ async fn environment_mcp_policy_filters_runtime_config_and_model_tools(
|
||||
skip_if_no_network!(Ok(()));
|
||||
|
||||
let server = responses::start_mock_server().await;
|
||||
let response = mount_sse_once(
|
||||
mount_sse_once(
|
||||
&server,
|
||||
responses::sse(vec![
|
||||
responses::ev_response_created("resp-1"),
|
||||
@@ -786,6 +786,9 @@ async fn environment_mcp_policy_filters_runtime_config_and_model_tools(
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
fixture
|
||||
.submit_text_turn("start with pending environment configuration")
|
||||
.await?;
|
||||
let (pending_config, _) = fixture.codex.current_mcp_config_and_runtime_context().await;
|
||||
let pending_servers = pending_config.mcp_server_catalog.configured_servers();
|
||||
assert!(!pending_servers["allowed"].enabled);
|
||||
@@ -856,6 +859,15 @@ async fn environment_mcp_policy_filters_runtime_config_and_model_tools(
|
||||
)
|
||||
.await?;
|
||||
|
||||
let response = mount_sse_once(
|
||||
&server,
|
||||
responses::sse(vec![
|
||||
responses::ev_response_created("resp-2"),
|
||||
responses::ev_assistant_message("msg-2", "done"),
|
||||
responses::ev_completed("resp-2"),
|
||||
]),
|
||||
)
|
||||
.await;
|
||||
fixture
|
||||
.submit_text_turn("show the available MCP tools")
|
||||
.await?;
|
||||
|
||||
Reference in new issue
Block a user