Files
codex/codex-rs/shell-command/src/shell_snapshot.rs
T
jif c98e263fb5 Replay exec-server shell snapshots through unnamed files (#48078)
## Why

Large shell snapshots need a replay path that does not add their state to the child process environment.

## What changed

- Keep captured state cached in memory and give each Bash or Zsh launch an independent, read-only file descriptor to source. Unlink the file before writing shell state, create it under the sandbox-protected daemon directory, and close the carrier descriptor before restoring functions.
- Probe descriptor-path access inside the capture sandbox. Retain environment replay for `sh` and when descriptor paths are unavailable; use normal shell startup if preparing the replay file fails.
- Group options and aliases during capture so sourcing preserves their parsing behavior.

## Testing

Add coverage for concurrent replays with 480 KiB payloads, pipe and TTY execution, stdin preservation, descriptor cleanup, sandbox protection, blocked descriptor-path fallback, and alias/option parsing equivalence.

GitOrigin-RevId: 8103d5c37ce4d1885628a4efba3551549f12bea8
2026-09-25 12:23:15 +00:00

114 lines
3.8 KiB
Rust

//! Capture, prepare credential-safe state, and render restorable shell snapshots.
#[path = "shell_snapshot_capture.rs"]
mod capture;
#[path = "shell_snapshot_credentials.rs"]
mod credentials;
#[path = "shell_snapshot_exports.rs"]
mod exports;
#[path = "shell_snapshot_literals.rs"]
mod literals;
#[path = "shell_snapshot_render.rs"]
mod render;
pub use capture::CapturedSnapshot;
pub use capture::CapturedStartupEnvironment;
pub use capture::SnapshotCaptureOptions;
pub use capture::snapshot_capture_script;
pub use capture::snapshot_source_capture_script;
pub use credentials::PreparedSnapshot;
pub use credentials::SnapshotCredentialEnvironment;
pub use credentials::prepare_snapshot_credentials;
use capture::BASH_SH_SNAPSHOT_HEADER;
#[cfg(all(test, unix))]
#[path = "shell_snapshot_tests.rs"]
mod tests;
/// Returns the POSIX shell helper used to resolve supported `ENV` startup paths.
///
/// The helper deliberately supports only non-evaluating path forms. Unsupported
/// shell expressions are returned unchanged rather than executed.
pub fn posix_env_path_expansion_function() -> &'static str {
r#"__codex_snapshot_expand_env() (
set +u
__codex_snapshot_getenv() {
# Preserve the value separately from lookup status and its formatting newline.
__codex_env_expanded=$(
if command -v printenv >/dev/null 2>&1; then
printenv "$1"
elif [ "$1" = PATH ]; then
[ "${PATH+x}" = x ] && printf '%s\n' "$PATH"
else
command -p printenv "$1"
fi && printf '.'
) || return 1
__codex_env_expanded=${__codex_env_expanded%?}
__codex_env_expanded=${__codex_env_expanded%?}
}
__codex_env_file=$1
case "$__codex_env_file" in
'~/'*) __codex_env_file="${HOME-}/${__codex_env_file#*/}" ;;
'${PATH%%:*}') __codex_env_file="${PATH%%:*}" ;;
'${PATH%%:*}/'*) __codex_env_file="${PATH%%:*}/${__codex_env_file#*/}" ;;
'${'*)
__codex_env_body=${__codex_env_file#\$\{}
case "$__codex_env_body" in
*\}*)
__codex_env_name=${__codex_env_body%%\}*}
__codex_env_suffix=${__codex_env_body#*\}}
case "$__codex_env_name" in
*:-*)
__codex_env_default=${__codex_env_name#*:-}
__codex_env_name=${__codex_env_name%%:-*}
__codex_env_has_default=1
;;
*) __codex_env_has_default= ;;
esac
case "$__codex_env_name" in
''|[0-9]*|*[!A-Za-z0-9_]*) ;;
*)
case "$__codex_env_suffix" in
''|/*)
if __codex_snapshot_getenv "$__codex_env_name" 2>/dev/null &&
{ [ -n "$__codex_env_expanded" ] || [ -z "$__codex_env_has_default" ]; }; then
__codex_env_file="$__codex_env_expanded$__codex_env_suffix"
elif [ -n "$__codex_env_has_default" ]; then
__codex_env_default=$(__codex_snapshot_expand_env "$__codex_env_default")
__codex_env_file="$__codex_env_default$__codex_env_suffix"
fi
;;
esac
;;
esac
;;
esac
;;
'$'*)
__codex_env_name=${__codex_env_file%%/*}
__codex_env_name=${__codex_env_name#\$}
case "$__codex_env_name" in
''|[0-9]*|*[!A-Za-z0-9_]*) ;;
*)
if __codex_snapshot_getenv "$__codex_env_name" 2>/dev/null; then
if [ "$__codex_env_file" = "\$$__codex_env_name" ]; then
__codex_env_file=$__codex_env_expanded
else
__codex_env_file="$__codex_env_expanded/${__codex_env_file#*/}"
fi
fi
;;
esac
;;
esac
printf '%s' "$__codex_env_file"
)"#
}
#[derive(Clone, Copy)]
pub enum SnapshotStartup {
Interactive,
NonInteractive,
}