[codex] Bind shell snapshots to retained thread environments (#28421)

## Why

Shell snapshots are currently session-scoped even though shell and cwd
are properties of a selected turn environment. That makes snapshot
refresh depend on separate session-cwd plumbing, prevents retained
environments from retaining their snapshot work, and can make snapshot
construction use a different shell than command execution.

This follows #27955 by making the retained thread-environment service
own environment snapshot lifecycles. Session configuration remains the
requested selection state, while `ThreadEnvironments` remains the source
of successfully resolved environments.

## What changed

- Configure the shell-snapshot builder before initial environment
resolution.
- Start each local environment snapshot task when its `TurnEnvironment`
is built and retain that shared task while environment ID and cwd still
match.
- Inherit retained environment snapshots into spawned child threads.
- Carry the selected `TurnEnvironment` through shell runtimes so
snapshot construction and command execution use the same
environment-specific shell and cwd.
- Load project instructions and warm plugins/skills after initial
environment resolution.
- Continue decoding invalid UTF-8 instruction files lossily without
emitting a startup warning.
- Keep requested selections in `SessionConfiguration`; failed or
duplicate resolutions only affect the resolved environment snapshot.

## Validation

- `cargo check -p codex-core --tests`
- `just test -p codex-home instructions` (6 passed)
- Focused environment, instruction, shell-snapshot, and user-shell tests
(84 passed)
- Focused shell-snapshot, user-shell, and unified-exec tests (126
passed; two event-timing tests passed on retry)
This commit is contained in:
pakrym-oai authored and GitHub committed 2026-06-15 20:10:53 -07:00
1 parent ecfe174d5f
commit 022f1221e8
28 files changed
+453 -617

No files matched your search

+12 -12
View File
@@ -4,7 +4,7 @@ use super::*;
const AGENT_NAMES: &str = include_str!("../agent_names.txt");
struct SpawnAgentThreadInheritance {
shell_snapshot: Option<Arc<ShellSnapshot>>,
environments: Option<TurnEnvironmentSnapshot>,
exec_policy: Option<Arc<crate::exec_policy::ExecPolicyManager>>,
}
@@ -156,8 +156,8 @@ impl AgentControl {
let parent_thread_id = initial_history
.get_resumed_parent_thread_id()
.or(stored_parent_thread_id);
let inherited_shell_snapshot = self
.inherited_shell_snapshot_for_source(&state, Some(&session_source))
let inherited_environments = self
.inherited_environments_for_source(&state, Some(&session_source))
.await;
let inherited_exec_policy = self
.inherited_exec_policy_for_source(&state, Some(&session_source), &config)
@@ -170,7 +170,7 @@ impl AgentControl {
agent_control: self.clone(),
session_source,
parent_thread_id,
inherited_shell_snapshot,
inherited_environments,
inherited_exec_policy,
})
.await
@@ -231,8 +231,8 @@ impl AgentControl {
};
let mut reservation = self.state.reserve_spawn_slot(reservation_max_threads)?;
let inheritance = SpawnAgentThreadInheritance {
shell_snapshot: self
.inherited_shell_snapshot_for_source(&state, session_source.as_ref())
environments: self
.inherited_environments_for_source(&state, session_source.as_ref())
.await,
exec_policy: self
.inherited_exec_policy_for_source(&state, session_source.as_ref(), &config)
@@ -283,7 +283,7 @@ impl AgentControl {
/*forked_from_thread_id*/ None,
/*thread_source*/ Some(ThreadSource::Subagent),
/*metrics_service_name*/ None,
inheritance.shell_snapshot,
inheritance.environments,
inheritance.exec_policy,
options.environments.clone(),
))
@@ -386,7 +386,7 @@ impl AgentControl {
multi_agent_version: MultiAgentVersion,
) -> CodexResult<crate::thread_manager::NewThread> {
let SpawnAgentThreadInheritance {
shell_snapshot: inherited_shell_snapshot,
environments: inherited_environments,
exec_policy: inherited_exec_policy,
} = inheritance;
if options.fork_parent_spawn_call_id.is_none() {
@@ -513,7 +513,7 @@ impl AgentControl {
/*thread_source*/ Some(ThreadSource::Subagent),
/*parent_thread_id*/ Some(parent_thread_id),
/*forked_from_thread_id*/ Some(parent_thread_id),
inherited_shell_snapshot,
inherited_environments,
inherited_exec_policy,
options.environments.clone(),
)
@@ -665,8 +665,8 @@ impl AgentControl {
other => (other, AgentMetadata::default()),
};
let notification_source = session_source.clone();
let inherited_shell_snapshot = self
.inherited_shell_snapshot_for_source(&state, Some(&session_source))
let inherited_environments = self
.inherited_environments_for_source(&state, Some(&session_source))
.await;
let inherited_exec_policy = self
.inherited_exec_policy_for_source(&state, Some(&session_source), &config)
@@ -679,7 +679,7 @@ impl AgentControl {
agent_control: self.clone(),
session_source,
parent_thread_id,
inherited_shell_snapshot,
inherited_environments,
inherited_exec_policy,
})
.await?;