mirror of
https://github.com/openai/codex.git
synced 2026-11-26 23:40:43 +00:00
Tie executor attachments to environment selection lifetimes (#59043)
## Why Removing an executor must detach its selection even when older turn snapshots still reference it. Connection events queued for a removed attachment must not leak into a later selection of the same executor. ## What changed - Introduce `EnvironmentAttachment` with a detach signal shared by turn snapshots and a guard owned by the selection. - Preserve attachments across workspace and configuration changes, while giving reselections and child threads independent attachment lifetimes. - Stop forwarding connection events on detach and discard buffered events from detached attachments. - Activate initial attachments after `SessionConfigured`, using configured bindings for startup discovery. ## Testing Add coverage for discarding buffered connection events on reselection and extend snapshot tests to verify immediate detachment, fresh attachments on reselection, and independent parent and child lifetimes. GitOrigin-RevId: 6cf882e0d17a296030c13d80c6e2866c80e04638
This commit is contained in:
committed by
copyberry
parent
16ddd538bd
commit
7e8143a897
@@ -558,17 +558,17 @@ impl Session {
|
||||
.get_or_init(ExecutorCapabilityDiscoveryV2Cache::default);
|
||||
let remote_environment_count = environments
|
||||
.turn_environments()
|
||||
.filter(|selected| selected.environment.is_remote())
|
||||
.filter(|selected| selected.environment().is_remote())
|
||||
.count();
|
||||
cache.ensure_capacity(remote_environment_count);
|
||||
let mut snapshots = Vec::new();
|
||||
let mut skipped_incompatible_executor = false;
|
||||
for selected in environments.turn_environments() {
|
||||
if !selected.environment.is_remote() {
|
||||
if !selected.environment().is_remote() {
|
||||
continue;
|
||||
}
|
||||
let environment_id = &selected.selection.environment_id;
|
||||
let info = match selected.environment.info().await {
|
||||
let info = match selected.environment().info().await {
|
||||
Ok(info) => info,
|
||||
Err(_) => {
|
||||
tracing::warn!(
|
||||
@@ -587,7 +587,7 @@ impl Session {
|
||||
sandbox: Some(selected.sandbox_context(/*additional_permissions*/ None)),
|
||||
};
|
||||
let result = cache
|
||||
.discover(environment_id, &selected.environment, request)
|
||||
.discover(environment_id, selected.environment(), request)
|
||||
.await;
|
||||
snapshots.push(ExecutorCapabilityDiscoveryV2Snapshot {
|
||||
environment_id: environment_id.clone(),
|
||||
@@ -632,7 +632,7 @@ impl Session {
|
||||
environments.turn_environments().map(|environment| {
|
||||
environment
|
||||
.config_origin
|
||||
.selected_capability_roots(&environment.environment, environment.config())
|
||||
.selected_capability_roots(environment.environment(), environment.config())
|
||||
}),
|
||||
);
|
||||
for (index, root) in combined_roots.into_iter().enumerate() {
|
||||
|
||||
@@ -154,7 +154,7 @@ impl Session {
|
||||
let mut catalog = None;
|
||||
let mut registered = HashSet::new();
|
||||
for selected in environments.turn_environments() {
|
||||
let environment = &selected.environment;
|
||||
let environment = selected.environment();
|
||||
if !environment.is_remote() {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1535,9 +1535,11 @@ impl Session {
|
||||
config.features.enabled(Feature::DeferredExecutor),
|
||||
));
|
||||
turn_environments.update_configured_selections(environment_selections);
|
||||
turn_environments.update_selections(environment_selections);
|
||||
session_configuration.environments = turn_environments.selections();
|
||||
let resolved_environments = turn_environments.snapshot().await;
|
||||
let configured_environments = turn_environments.configured_snapshot_now();
|
||||
session_configuration.environments = configured_environments.all_selections();
|
||||
let resolved_environments = turn_environments
|
||||
.resolve_snapshot(configured_environments)
|
||||
.await;
|
||||
let agents_md_manager = Arc::new(AgentsMdManager::new(instructions));
|
||||
// Keep discovery's future out of the large embedded startup/resume frame.
|
||||
let plugin_skill_warmup = Box::pin(warm_plugins_and_skills_for_session_init(
|
||||
@@ -1976,7 +1978,7 @@ impl Session {
|
||||
let thread_config = sess
|
||||
.effective_thread_config_snapshot(
|
||||
&session_configuration,
|
||||
turn_environments.selections(),
|
||||
session_configuration.environments.clone(),
|
||||
)
|
||||
.await;
|
||||
let events = std::iter::once(Event {
|
||||
@@ -2009,7 +2011,7 @@ impl Session {
|
||||
for event in events {
|
||||
sess.send_event_raw(event).await;
|
||||
}
|
||||
turn_environments.start_connection_event_forwarding(tx_event.clone());
|
||||
turn_environments.activate_configured(tx_event.clone());
|
||||
|
||||
let startup_auth_changed = mcp_auth_changes.has_changed().unwrap_or(false);
|
||||
if startup_auth_changed {
|
||||
|
||||
@@ -21,6 +21,7 @@ use crate::config::RuntimeConfigRefresh;
|
||||
use crate::context::ContextualUserFragment;
|
||||
use crate::context::DeveloperInstructions;
|
||||
use crate::context::TurnAborted;
|
||||
use crate::environment_selection::EnvironmentAttachment;
|
||||
use crate::environment_selection::EnvironmentConfigOrigin;
|
||||
use crate::environment_selection::ThreadEnvironments;
|
||||
use crate::environment_selection::TurnEnvironmentState;
|
||||
@@ -7026,11 +7027,12 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) {
|
||||
resolved_environments,
|
||||
/*non_blocking_snapshots*/ false,
|
||||
));
|
||||
turn_environments.activate_configured(tx_event.clone());
|
||||
let environment = Arc::clone(
|
||||
&resolved_turn_environments
|
||||
resolved_turn_environments
|
||||
.primary()
|
||||
.expect("primary environment")
|
||||
.environment,
|
||||
.environment(),
|
||||
);
|
||||
let plugins_manager = Arc::new(plugins_manager_for_config(
|
||||
&config,
|
||||
@@ -7958,7 +7960,7 @@ async fn request_permissions_tool_resolves_legacy_paths_against_selected_environ
|
||||
config: EnvironmentConfigState::Ready(current_environment_config),
|
||||
},
|
||||
current_environment.config_origin,
|
||||
current_environment.environment,
|
||||
Arc::clone(current_environment.environment()),
|
||||
current_environment.shell,
|
||||
)
|
||||
};
|
||||
@@ -8466,11 +8468,11 @@ async fn turn_environments_set_primary_environment() {
|
||||
.primary()
|
||||
.expect("primary environment should be set");
|
||||
assert!(std::sync::Arc::ptr_eq(
|
||||
&turn_environment.environment,
|
||||
&turn_environments
|
||||
turn_environment.environment(),
|
||||
turn_environments
|
||||
.primary()
|
||||
.expect("primary environment")
|
||||
.environment
|
||||
.environment()
|
||||
));
|
||||
assert!(
|
||||
turn_context
|
||||
@@ -8495,17 +8497,17 @@ async fn turn_environments_set_primary_environment() {
|
||||
};
|
||||
assert!(Arc::ptr_eq(
|
||||
&stored_environment,
|
||||
&turn_environment.environment
|
||||
turn_environment.environment()
|
||||
));
|
||||
|
||||
let default_turn = session.new_default_turn().await;
|
||||
assert!(Arc::ptr_eq(
|
||||
&stored_environment,
|
||||
&default_turn
|
||||
default_turn
|
||||
.initial_environments
|
||||
.primary()
|
||||
.expect("default turn primary environment")
|
||||
.environment
|
||||
.environment()
|
||||
));
|
||||
}
|
||||
|
||||
@@ -8584,11 +8586,11 @@ async fn task_turn_does_not_overlay_legacy_fallback_cwd_onto_stored_thread_envir
|
||||
.primary()
|
||||
.expect("primary environment should be set");
|
||||
assert!(std::sync::Arc::ptr_eq(
|
||||
&turn_environment.environment,
|
||||
&turn_environments
|
||||
turn_environment.environment(),
|
||||
turn_environments
|
||||
.primary()
|
||||
.expect("primary environment")
|
||||
.environment
|
||||
.environment()
|
||||
));
|
||||
#[allow(deprecated)]
|
||||
let turn_cwd = turn_context.cwd.clone();
|
||||
@@ -8660,7 +8662,7 @@ async fn primary_environment_uses_first_turn_environment() {
|
||||
config: EnvironmentConfigState::Ready(first_environment_config),
|
||||
},
|
||||
first_environment.config_origin,
|
||||
Arc::clone(&first_environment.environment),
|
||||
Arc::clone(first_environment.environment()),
|
||||
/*shell*/ None,
|
||||
)));
|
||||
|
||||
@@ -9355,11 +9357,12 @@ where
|
||||
resolved_turn_environments.clone(),
|
||||
/*non_blocking_snapshots*/ false,
|
||||
));
|
||||
turn_environments.activate_configured(tx_event.clone());
|
||||
let environment = Arc::clone(
|
||||
&resolved_turn_environments
|
||||
resolved_turn_environments
|
||||
.primary()
|
||||
.expect("primary environment")
|
||||
.environment,
|
||||
.environment(),
|
||||
);
|
||||
let plugins_manager = Arc::new(plugins_manager_for_config(
|
||||
&config,
|
||||
@@ -10630,7 +10633,7 @@ async fn record_context_updates_emits_environment_item_for_cwd_changes() {
|
||||
.clone();
|
||||
let environment_config = environment.config().clone();
|
||||
current_context.initial_environments.environments[0] =
|
||||
TurnEnvironmentState::Ready(TurnEnvironment::new(
|
||||
TurnEnvironmentState::Ready(TurnEnvironment::from_attachment(
|
||||
TurnEnvironmentSelection {
|
||||
selected_capability_roots: Default::default(),
|
||||
environment_id: environment.selection.environment_id,
|
||||
@@ -10639,7 +10642,7 @@ async fn record_context_updates_emits_environment_item_for_cwd_changes() {
|
||||
config: EnvironmentConfigState::Ready(environment_config),
|
||||
},
|
||||
environment.config_origin,
|
||||
environment.environment,
|
||||
environment.attachment,
|
||||
environment.shell,
|
||||
));
|
||||
|
||||
@@ -10697,7 +10700,7 @@ async fn record_context_updates_use_environment_permission_profile_and_workspace
|
||||
environment_config.permission_profile =
|
||||
PermissionProfileSnapshot::legacy(PermissionProfile::workspace_write());
|
||||
current_context.initial_environments.environments[0] =
|
||||
TurnEnvironmentState::Ready(TurnEnvironment::new(
|
||||
TurnEnvironmentState::Ready(TurnEnvironment::from_attachment(
|
||||
TurnEnvironmentSelection {
|
||||
selected_capability_roots: Default::default(),
|
||||
environment_id: environment.selection.environment_id,
|
||||
@@ -10706,7 +10709,7 @@ async fn record_context_updates_use_environment_permission_profile_and_workspace
|
||||
config: EnvironmentConfigState::Ready(environment_config),
|
||||
},
|
||||
environment.config_origin,
|
||||
environment.environment,
|
||||
environment.attachment,
|
||||
environment.shell,
|
||||
));
|
||||
|
||||
@@ -10777,7 +10780,7 @@ async fn record_context_updates_omits_environment_item_when_disabled() {
|
||||
.clone();
|
||||
let environment_config = environment.config().clone();
|
||||
current_context.initial_environments.environments[0] =
|
||||
TurnEnvironmentState::Ready(TurnEnvironment::new(
|
||||
TurnEnvironmentState::Ready(TurnEnvironment::from_attachment(
|
||||
TurnEnvironmentSelection {
|
||||
selected_capability_roots: Default::default(),
|
||||
environment_id: environment.selection.environment_id,
|
||||
@@ -10786,7 +10789,7 @@ async fn record_context_updates_omits_environment_item_when_disabled() {
|
||||
config: EnvironmentConfigState::Ready(environment_config),
|
||||
},
|
||||
environment.config_origin,
|
||||
environment.environment,
|
||||
environment.attachment,
|
||||
environment.shell,
|
||||
));
|
||||
|
||||
@@ -11353,7 +11356,7 @@ async fn turn_context_item_stores_local_cwd() {
|
||||
config: EnvironmentConfigState::Ready(environment_config),
|
||||
},
|
||||
environment.config_origin,
|
||||
environment.environment,
|
||||
Arc::clone(environment.environment()),
|
||||
environment.shell,
|
||||
));
|
||||
|
||||
|
||||
@@ -947,7 +947,7 @@ async fn turn_diff_display_roots(step_context: &StepContext) -> Vec<(String, Pat
|
||||
// A turn cwd is expected to be a directory. If it is a file, the failed `<cwd>/.git` probe
|
||||
// is ignored and ancestor search continues from its parent.
|
||||
let root = find_nearest_ancestor_with_markers(
|
||||
turn_environment.environment.get_filesystem().as_ref(),
|
||||
turn_environment.environment().get_filesystem().as_ref(),
|
||||
cwd,
|
||||
vec![".git".to_string()],
|
||||
FindUpErrorPolicy::Ignore,
|
||||
|
||||
@@ -4,6 +4,7 @@ use super::token_budget::resolve_token_budget;
|
||||
use super::*;
|
||||
use crate::config::TokenBudgetConfig;
|
||||
use crate::cyber_access_program;
|
||||
use crate::environment_selection::EnvironmentAttachment;
|
||||
use crate::environment_selection::EnvironmentConfigOrigin;
|
||||
use crate::environment_selection::TurnEnvironmentSnapshot;
|
||||
use crate::exec_policy::AllowPrefixRules;
|
||||
@@ -69,7 +70,7 @@ pub(crate) struct ShellSnapshotCacheKey {
|
||||
pub(crate) struct TurnEnvironment {
|
||||
pub(crate) selection: TurnEnvironmentSelection,
|
||||
pub(crate) config_origin: EnvironmentConfigOrigin,
|
||||
pub(crate) environment: Arc<Environment>,
|
||||
pub(crate) attachment: Arc<EnvironmentAttachment>,
|
||||
/// Cached from the selected executor; `None` means it did not report one.
|
||||
pub(crate) user_home_dir: Option<PathUri>,
|
||||
/// Cached from the selected executor; `None` means it did not report them.
|
||||
@@ -84,17 +85,31 @@ pub(crate) struct TurnEnvironment {
|
||||
}
|
||||
|
||||
impl TurnEnvironment {
|
||||
#[cfg(test)]
|
||||
pub(crate) fn new(
|
||||
selection: TurnEnvironmentSelection,
|
||||
config_origin: EnvironmentConfigOrigin,
|
||||
environment: Arc<Environment>,
|
||||
shell: Option<shell::Shell>,
|
||||
) -> Self {
|
||||
let (attachment, guard, _) =
|
||||
EnvironmentAttachment::new(&selection.environment_id, environment);
|
||||
// Standalone test snapshots have no thread selection to signal detach.
|
||||
guard.disarm();
|
||||
Self::from_attachment(selection, config_origin, attachment, shell)
|
||||
}
|
||||
|
||||
pub(crate) fn from_attachment(
|
||||
selection: TurnEnvironmentSelection,
|
||||
config_origin: EnvironmentConfigOrigin,
|
||||
attachment: Arc<EnvironmentAttachment>,
|
||||
shell: Option<shell::Shell>,
|
||||
) -> Self {
|
||||
debug_assert!(matches!(selection.config, EnvironmentConfigState::Ready(_)));
|
||||
Self {
|
||||
selection,
|
||||
config_origin,
|
||||
environment,
|
||||
attachment,
|
||||
user_home_dir: None,
|
||||
temporary_directories: None,
|
||||
shell,
|
||||
@@ -106,6 +121,10 @@ impl TurnEnvironment {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn environment(&self) -> &Arc<Environment> {
|
||||
&self.attachment.environment
|
||||
}
|
||||
|
||||
pub(crate) fn config(&self) -> &EnvironmentConfig {
|
||||
let EnvironmentConfigState::Ready(config) = &self.selection.config else {
|
||||
unreachable!("ready turn environments always carry resolved configuration")
|
||||
@@ -182,7 +201,7 @@ impl TurnEnvironment {
|
||||
.as_ref()
|
||||
.clone()
|
||||
.build(
|
||||
Arc::clone(&self.environment),
|
||||
Arc::clone(self.environment()),
|
||||
PathUri::from_abs_path(cwd),
|
||||
Some(shell.clone()),
|
||||
use_login_shell,
|
||||
@@ -226,7 +245,7 @@ impl TurnEnvironment {
|
||||
additional_permissions: Option<AdditionalPermissionProfile>,
|
||||
) -> impl EnvironmentAccess + '_ {
|
||||
FileSystemEnvironmentAccessor::new(
|
||||
self.environment.filesystem_ref(),
|
||||
self.environment().filesystem_ref(),
|
||||
self.sandbox_context(additional_permissions),
|
||||
)
|
||||
}
|
||||
@@ -264,7 +283,7 @@ impl TurnEnvironment {
|
||||
&self,
|
||||
) -> codex_file_system::WindowsSandboxSelection {
|
||||
let config = self.config();
|
||||
if self.environment.is_remote() {
|
||||
if self.environment().is_remote() {
|
||||
executor_windows_sandbox_selection(
|
||||
config.windows_sandbox_type,
|
||||
config.windows_sandbox_level,
|
||||
@@ -299,7 +318,7 @@ impl std::fmt::Debug for TurnEnvironment {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("TurnEnvironment")
|
||||
.field("environment_id", &self.selection.environment_id)
|
||||
.field("environment", &self.environment)
|
||||
.field("environment", self.environment())
|
||||
.field("cwd", &self.selection.cwd)
|
||||
.field("workspace_roots", &self.config().workspace_roots)
|
||||
.field("user_home_dir", &self.user_home_dir)
|
||||
|
||||
@@ -238,7 +238,7 @@ impl Session {
|
||||
if turn_context.config.include_permissions_instructions {
|
||||
let environment = step_context.environments.primary();
|
||||
let sandbox = environment
|
||||
.filter(|environment| environment.environment.is_remote())
|
||||
.filter(|environment| environment.environment().is_remote())
|
||||
.map(|environment| {
|
||||
environment.sandbox_context(/*additional_permissions*/ None)
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user