mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
Inherit SessionStart env in spawned agents
This commit is contained in:
@@ -225,6 +225,9 @@ impl AgentControl {
|
||||
let inherited_exec_policy = self
|
||||
.inherited_exec_policy_for_source(&state, session_source.as_ref(), &config)
|
||||
.await;
|
||||
let inherited_session_start_env = self
|
||||
.inherited_session_start_env_for_source(&state, session_source.as_ref())
|
||||
.await;
|
||||
let (session_source, mut agent_metadata) = match session_source {
|
||||
Some(SessionSource::SubAgent(SubAgentSource::ThreadSpawn {
|
||||
parent_thread_id,
|
||||
@@ -258,6 +261,7 @@ impl AgentControl {
|
||||
&options,
|
||||
inherited_shell_snapshot,
|
||||
inherited_exec_policy,
|
||||
inherited_session_start_env,
|
||||
))
|
||||
.await?
|
||||
}
|
||||
@@ -273,6 +277,7 @@ impl AgentControl {
|
||||
/*metrics_service_name*/ None,
|
||||
inherited_shell_snapshot,
|
||||
inherited_exec_policy,
|
||||
inherited_session_start_env,
|
||||
options.environments.clone(),
|
||||
))
|
||||
.await?
|
||||
@@ -360,6 +365,7 @@ impl AgentControl {
|
||||
})
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn spawn_forked_thread(
|
||||
&self,
|
||||
state: &Arc<ThreadManagerState>,
|
||||
@@ -368,6 +374,7 @@ impl AgentControl {
|
||||
options: &SpawnAgentOptions,
|
||||
inherited_shell_snapshot: Option<Arc<ShellSnapshot>>,
|
||||
inherited_exec_policy: Option<Arc<crate::exec_policy::ExecPolicyManager>>,
|
||||
inherited_session_start_env: HashMap<String, String>,
|
||||
) -> CodexResult<crate::thread_manager::NewThread> {
|
||||
if options.fork_parent_spawn_call_id.is_none() {
|
||||
return Err(CodexErr::Fatal(
|
||||
@@ -494,6 +501,7 @@ impl AgentControl {
|
||||
/*persist_extended_history*/ false,
|
||||
inherited_shell_snapshot,
|
||||
inherited_exec_policy,
|
||||
inherited_session_start_env,
|
||||
options.environments.clone(),
|
||||
)
|
||||
.await
|
||||
@@ -627,6 +635,9 @@ impl AgentControl {
|
||||
let inherited_exec_policy = self
|
||||
.inherited_exec_policy_for_source(&state, Some(&session_source), &config)
|
||||
.await;
|
||||
let inherited_session_start_env = self
|
||||
.inherited_session_start_env_for_source(&state, Some(&session_source))
|
||||
.await;
|
||||
let stored_thread = state
|
||||
.read_stored_thread(ReadThreadParams {
|
||||
thread_id,
|
||||
@@ -651,6 +662,7 @@ impl AgentControl {
|
||||
session_source,
|
||||
inherited_shell_snapshot,
|
||||
inherited_exec_policy,
|
||||
inherited_session_start_env,
|
||||
})
|
||||
.await?;
|
||||
let mut agent_metadata = agent_metadata;
|
||||
@@ -1167,6 +1179,24 @@ impl AgentControl {
|
||||
))
|
||||
}
|
||||
|
||||
async fn inherited_session_start_env_for_source(
|
||||
&self,
|
||||
state: &Arc<ThreadManagerState>,
|
||||
session_source: Option<&SessionSource>,
|
||||
) -> HashMap<String, String> {
|
||||
let Some(SessionSource::SubAgent(SubAgentSource::ThreadSpawn {
|
||||
parent_thread_id, ..
|
||||
})) = session_source
|
||||
else {
|
||||
return HashMap::new();
|
||||
};
|
||||
|
||||
let Ok(parent_thread) = state.get_thread(*parent_thread_id).await else {
|
||||
return HashMap::new();
|
||||
};
|
||||
parent_thread.codex.session.session_start_env_snapshot()
|
||||
}
|
||||
|
||||
async fn open_thread_spawn_children(
|
||||
&self,
|
||||
parent_thread_id: ThreadId,
|
||||
|
||||
@@ -557,6 +557,53 @@ async fn spawn_agent_creates_thread_and_sends_prompt() {
|
||||
assert_eq!(captured, Some(expected));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn spawned_agent_inherits_session_start_env_snapshot() {
|
||||
let harness = AgentControlHarness::new().await;
|
||||
let (parent_thread_id, parent_thread) = harness.start_thread().await;
|
||||
parent_thread
|
||||
.codex
|
||||
.session
|
||||
.replace_session_start_env(HashMap::from([(
|
||||
"CODEX_TEST_PARENT_ENV".to_string(),
|
||||
"at-spawn".to_string(),
|
||||
)]));
|
||||
|
||||
let child_thread_id = harness
|
||||
.control
|
||||
.spawn_agent(
|
||||
harness.config.clone(),
|
||||
text_input("spawned"),
|
||||
Some(SessionSource::SubAgent(SubAgentSource::ThreadSpawn {
|
||||
parent_thread_id,
|
||||
depth: 1,
|
||||
agent_path: None,
|
||||
agent_nickname: None,
|
||||
agent_role: None,
|
||||
})),
|
||||
)
|
||||
.await
|
||||
.expect("child spawn should succeed");
|
||||
let child_thread = harness
|
||||
.manager
|
||||
.get_thread(child_thread_id)
|
||||
.await
|
||||
.expect("child thread should be registered");
|
||||
|
||||
parent_thread
|
||||
.codex
|
||||
.session
|
||||
.replace_session_start_env(HashMap::from([(
|
||||
"CODEX_TEST_PARENT_ENV".to_string(),
|
||||
"after-spawn".to_string(),
|
||||
)]));
|
||||
|
||||
assert_eq!(
|
||||
child_thread.codex.session.session_start_env_snapshot(),
|
||||
HashMap::from([("CODEX_TEST_PARENT_ENV".to_string(), "at-spawn".to_string(),)])
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn spawn_agent_can_fork_parent_thread_history_with_sanitized_items() {
|
||||
let harness = AgentControlHarness::new().await;
|
||||
|
||||
@@ -93,8 +93,9 @@ pub(crate) async fn run_codex_thread_interactive(
|
||||
persist_extended_history: false,
|
||||
metrics_service_name: None,
|
||||
inherited_shell_snapshot: None,
|
||||
user_shell_override: None,
|
||||
inherited_exec_policy: Some(Arc::clone(&parent_session.services.exec_policy)),
|
||||
inherited_session_start_env: parent_session.session_start_env_snapshot(),
|
||||
user_shell_override: None,
|
||||
parent_rollout_thread_trace: codex_rollout_trace::ThreadTraceContext::disabled(),
|
||||
parent_trace: None,
|
||||
environment_selections: parent_ctx.environments.clone(),
|
||||
|
||||
@@ -409,6 +409,7 @@ pub(crate) struct CodexSpawnArgs {
|
||||
pub(crate) metrics_service_name: Option<String>,
|
||||
pub(crate) inherited_shell_snapshot: Option<Arc<ShellSnapshot>>,
|
||||
pub(crate) inherited_exec_policy: Option<Arc<ExecPolicyManager>>,
|
||||
pub(crate) inherited_session_start_env: HashMap<String, String>,
|
||||
/// Parent rollout trace used only to derive fresh spawned child traces.
|
||||
///
|
||||
/// Root sessions and non-thread-spawn subagents pass a disabled context;
|
||||
@@ -473,8 +474,9 @@ impl Codex {
|
||||
persist_extended_history,
|
||||
metrics_service_name,
|
||||
inherited_shell_snapshot,
|
||||
user_shell_override,
|
||||
inherited_exec_policy,
|
||||
inherited_session_start_env,
|
||||
user_shell_override,
|
||||
parent_rollout_thread_trace,
|
||||
parent_trace: _,
|
||||
environment_selections,
|
||||
@@ -599,6 +601,7 @@ impl Codex {
|
||||
dynamic_tools,
|
||||
persist_extended_history,
|
||||
inherited_shell_snapshot,
|
||||
inherited_session_start_env,
|
||||
user_shell_override,
|
||||
};
|
||||
|
||||
@@ -3226,6 +3229,10 @@ impl Session {
|
||||
self.services.session_start_env.replace(env);
|
||||
}
|
||||
|
||||
pub(crate) fn session_start_env_snapshot(&self) -> HashMap<String, String> {
|
||||
self.services.session_start_env.snapshot()
|
||||
}
|
||||
|
||||
pub(crate) fn apply_session_start_env(&self, env: &mut HashMap<String, String>) {
|
||||
self.services.session_start_env.apply(env);
|
||||
}
|
||||
|
||||
@@ -102,6 +102,7 @@ pub(crate) struct SessionConfiguration {
|
||||
pub(super) dynamic_tools: Vec<DynamicToolSpec>,
|
||||
pub(super) persist_extended_history: bool,
|
||||
pub(super) inherited_shell_snapshot: Option<Arc<ShellSnapshot>>,
|
||||
pub(super) inherited_session_start_env: HashMap<String, String>,
|
||||
pub(super) user_shell_override: Option<shell::Shell>,
|
||||
}
|
||||
|
||||
@@ -998,7 +999,9 @@ impl Session {
|
||||
hooks: arc_swap::ArcSwap::from_pointee(hooks),
|
||||
rollout_thread_trace,
|
||||
user_shell: Arc::new(default_shell),
|
||||
session_start_env: Default::default(),
|
||||
session_start_env: crate::session_start_env::SessionStartEnvOverlay::new(
|
||||
session_configuration.inherited_session_start_env.clone(),
|
||||
),
|
||||
shell_snapshot_tx,
|
||||
show_raw_agent_reasoning: config.show_raw_agent_reasoning,
|
||||
exec_policy,
|
||||
|
||||
@@ -3100,6 +3100,7 @@ async fn set_rate_limits_retains_previous_credits() {
|
||||
dynamic_tools: Vec::new(),
|
||||
persist_extended_history: false,
|
||||
inherited_shell_snapshot: None,
|
||||
inherited_session_start_env: HashMap::new(),
|
||||
user_shell_override: None,
|
||||
};
|
||||
|
||||
@@ -3205,6 +3206,7 @@ async fn set_rate_limits_updates_plan_type_when_present() {
|
||||
dynamic_tools: Vec::new(),
|
||||
persist_extended_history: false,
|
||||
inherited_shell_snapshot: None,
|
||||
inherited_session_start_env: HashMap::new(),
|
||||
user_shell_override: None,
|
||||
};
|
||||
|
||||
@@ -3733,6 +3735,7 @@ pub(crate) async fn make_session_configuration_for_tests() -> SessionConfigurati
|
||||
dynamic_tools: Vec::new(),
|
||||
persist_extended_history: false,
|
||||
inherited_shell_snapshot: None,
|
||||
inherited_session_start_env: HashMap::new(),
|
||||
user_shell_override: None,
|
||||
}
|
||||
}
|
||||
@@ -4477,6 +4480,7 @@ async fn session_new_fails_when_zsh_fork_enabled_without_packaged_zsh() {
|
||||
dynamic_tools: Vec::new(),
|
||||
persist_extended_history: false,
|
||||
inherited_shell_snapshot: None,
|
||||
inherited_session_start_env: HashMap::new(),
|
||||
user_shell_override: None,
|
||||
};
|
||||
|
||||
@@ -4587,6 +4591,7 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) {
|
||||
dynamic_tools: Vec::new(),
|
||||
persist_extended_history: false,
|
||||
inherited_shell_snapshot: None,
|
||||
inherited_session_start_env: HashMap::new(),
|
||||
user_shell_override: None,
|
||||
};
|
||||
let per_turn_config =
|
||||
@@ -4823,6 +4828,7 @@ async fn make_session_with_config_and_rx(
|
||||
dynamic_tools: Vec::new(),
|
||||
persist_extended_history: false,
|
||||
inherited_shell_snapshot: None,
|
||||
inherited_session_start_env: HashMap::new(),
|
||||
user_shell_override: None,
|
||||
};
|
||||
|
||||
@@ -4927,6 +4933,7 @@ async fn make_session_with_history_source_and_agent_control_and_rx(
|
||||
dynamic_tools: Vec::new(),
|
||||
persist_extended_history: false,
|
||||
inherited_shell_snapshot: None,
|
||||
inherited_session_start_env: HashMap::new(),
|
||||
user_shell_override: None,
|
||||
};
|
||||
|
||||
@@ -6431,6 +6438,7 @@ where
|
||||
dynamic_tools,
|
||||
persist_extended_history: false,
|
||||
inherited_shell_snapshot: None,
|
||||
inherited_session_start_env: HashMap::new(),
|
||||
user_shell_override: None,
|
||||
};
|
||||
let per_turn_config =
|
||||
|
||||
@@ -700,6 +700,7 @@ async fn guardian_subagent_does_not_inherit_parent_exec_policy_rules() {
|
||||
metrics_service_name: None,
|
||||
inherited_shell_snapshot: None,
|
||||
inherited_exec_policy: Some(Arc::new(parent_exec_policy)),
|
||||
inherited_session_start_env: HashMap::new(),
|
||||
parent_rollout_thread_trace: codex_rollout_trace::ThreadTraceContext::disabled(),
|
||||
user_shell_override: None,
|
||||
parent_trace: None,
|
||||
|
||||
@@ -11,10 +11,23 @@ pub(crate) struct SessionStartEnvOverlay {
|
||||
}
|
||||
|
||||
impl SessionStartEnvOverlay {
|
||||
pub(crate) fn new(values: HashMap<String, String>) -> Self {
|
||||
Self {
|
||||
values: RwLock::new(values),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn replace(&self, values: HashMap<String, String>) {
|
||||
*self.values.write().unwrap_or_else(PoisonError::into_inner) = values;
|
||||
}
|
||||
|
||||
pub(crate) fn snapshot(&self) -> HashMap<String, String> {
|
||||
self.values
|
||||
.read()
|
||||
.unwrap_or_else(PoisonError::into_inner)
|
||||
.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn apply(&self, env: &mut HashMap<String, String>) {
|
||||
for (key, value) in self
|
||||
.values
|
||||
|
||||
@@ -189,6 +189,7 @@ pub(crate) struct ResumeThreadWithHistoryOptions {
|
||||
pub(crate) session_source: SessionSource,
|
||||
pub(crate) inherited_shell_snapshot: Option<Arc<ShellSnapshot>>,
|
||||
pub(crate) inherited_exec_policy: Option<Arc<crate::exec_policy::ExecPolicyManager>>,
|
||||
pub(crate) inherited_session_start_env: HashMap<String, String>,
|
||||
}
|
||||
|
||||
/// Shared, `Arc`-owned state for [`ThreadManager`]. This `Arc` is required to have a single
|
||||
@@ -608,6 +609,7 @@ impl ThreadManager {
|
||||
options.metrics_service_name,
|
||||
/*inherited_shell_snapshot*/ None,
|
||||
/*inherited_exec_policy*/ None,
|
||||
HashMap::new(),
|
||||
options.parent_trace,
|
||||
options.environments,
|
||||
/*user_shell_override*/ None,
|
||||
@@ -692,6 +694,7 @@ impl ThreadManager {
|
||||
/*metrics_service_name*/ None,
|
||||
/*inherited_shell_snapshot*/ None,
|
||||
/*inherited_exec_policy*/ None,
|
||||
HashMap::new(),
|
||||
parent_trace,
|
||||
environments,
|
||||
/*user_shell_override*/ None,
|
||||
@@ -753,6 +756,7 @@ impl ThreadManager {
|
||||
/*metrics_service_name*/ None,
|
||||
/*inherited_shell_snapshot*/ None,
|
||||
/*inherited_exec_policy*/ None,
|
||||
HashMap::new(),
|
||||
/*parent_trace*/ None,
|
||||
environments,
|
||||
/*user_shell_override*/ Some(user_shell_override),
|
||||
@@ -1045,6 +1049,7 @@ impl ThreadManagerState {
|
||||
/*metrics_service_name*/ None,
|
||||
/*inherited_shell_snapshot*/ None,
|
||||
/*inherited_exec_policy*/ None,
|
||||
HashMap::new(),
|
||||
/*environments*/ None,
|
||||
))
|
||||
.await
|
||||
@@ -1062,6 +1067,7 @@ impl ThreadManagerState {
|
||||
metrics_service_name: Option<String>,
|
||||
inherited_shell_snapshot: Option<Arc<ShellSnapshot>>,
|
||||
inherited_exec_policy: Option<Arc<crate::exec_policy::ExecPolicyManager>>,
|
||||
inherited_session_start_env: HashMap<String, String>,
|
||||
environments: Option<Vec<TurnEnvironmentSelection>>,
|
||||
) -> CodexResult<NewThread> {
|
||||
let environments = environments.unwrap_or_else(|| {
|
||||
@@ -1080,6 +1086,7 @@ impl ThreadManagerState {
|
||||
metrics_service_name,
|
||||
inherited_shell_snapshot,
|
||||
inherited_exec_policy,
|
||||
inherited_session_start_env,
|
||||
/*parent_trace*/ None,
|
||||
environments,
|
||||
/*user_shell_override*/ None,
|
||||
@@ -1098,6 +1105,7 @@ impl ThreadManagerState {
|
||||
session_source,
|
||||
inherited_shell_snapshot,
|
||||
inherited_exec_policy,
|
||||
inherited_session_start_env,
|
||||
} = options;
|
||||
let environments =
|
||||
default_thread_environment_selections(self.environment_manager.as_ref(), &config.cwd);
|
||||
@@ -1115,6 +1123,7 @@ impl ThreadManagerState {
|
||||
/*metrics_service_name*/ None,
|
||||
inherited_shell_snapshot,
|
||||
inherited_exec_policy,
|
||||
inherited_session_start_env,
|
||||
/*parent_trace*/ None,
|
||||
environments,
|
||||
/*user_shell_override*/ None,
|
||||
@@ -1134,6 +1143,7 @@ impl ThreadManagerState {
|
||||
persist_extended_history: bool,
|
||||
inherited_shell_snapshot: Option<Arc<ShellSnapshot>>,
|
||||
inherited_exec_policy: Option<Arc<crate::exec_policy::ExecPolicyManager>>,
|
||||
inherited_session_start_env: HashMap<String, String>,
|
||||
environments: Option<Vec<TurnEnvironmentSelection>>,
|
||||
) -> CodexResult<NewThread> {
|
||||
let environments = environments.unwrap_or_else(|| {
|
||||
@@ -1152,6 +1162,7 @@ impl ThreadManagerState {
|
||||
/*metrics_service_name*/ None,
|
||||
inherited_shell_snapshot,
|
||||
inherited_exec_policy,
|
||||
inherited_session_start_env,
|
||||
/*parent_trace*/ None,
|
||||
environments,
|
||||
/*user_shell_override*/ None,
|
||||
@@ -1189,6 +1200,7 @@ impl ThreadManagerState {
|
||||
metrics_service_name,
|
||||
/*inherited_shell_snapshot*/ None,
|
||||
/*inherited_exec_policy*/ None,
|
||||
HashMap::new(),
|
||||
parent_trace,
|
||||
environments,
|
||||
user_shell_override,
|
||||
@@ -1211,6 +1223,7 @@ impl ThreadManagerState {
|
||||
metrics_service_name: Option<String>,
|
||||
inherited_shell_snapshot: Option<Arc<ShellSnapshot>>,
|
||||
inherited_exec_policy: Option<Arc<crate::exec_policy::ExecPolicyManager>>,
|
||||
inherited_session_start_env: HashMap<String, String>,
|
||||
parent_trace: Option<W3cTraceContext>,
|
||||
environments: Vec<TurnEnvironmentSelection>,
|
||||
user_shell_override: Option<crate::shell::Shell>,
|
||||
@@ -1265,6 +1278,7 @@ impl ThreadManagerState {
|
||||
metrics_service_name,
|
||||
inherited_shell_snapshot,
|
||||
inherited_exec_policy,
|
||||
inherited_session_start_env,
|
||||
parent_rollout_thread_trace,
|
||||
user_shell_override,
|
||||
parent_trace,
|
||||
|
||||
Reference in New Issue
Block a user