Expose originating tool call metadata in command environments (#52119)

## Why

Command processes need thread and tool call labels for correlation, without stale values from inherited environments or cached shell snapshots.

## What changed

- Export the originating `exec_command` call ID as `CODEX_TOOL_CALL_ID`; later `write_stdin` calls leave it unchanged.
- Apply executor metadata as `CODEX_THREAD_ID` and `CODEX_TOOL_CALL_ID` after environment policies, overlays, and snapshot replay. Clear omitted fields when metadata is present; without metadata, preserve thread context and remove the call ID.
- Exclude both IDs from newly captured snapshots and omit the call ID from capture and validation helpers.
- Preserve empty call IDs, omit NUL-containing IDs, and handle case-insensitive environment names on Windows. Document that these labels support correlation, not authorization.

## Testing

Add regression coverage for metadata precedence, missing and invalid IDs, snapshot capture and replay, local and remote execution with pipes and TTYs, sandboxed remote execution, and preservation of the originating call ID across later input.

GitOrigin-RevId: 1027212b5d495d8e7cfac0f498c3134e060aa897
This commit is contained in:
Leonardo Di Donato
2026-10-08 13:45:19 +00:00
committed by copyberry
parent 2fdf047c96
commit 9b738582b1
18 changed files with 610 additions and 34 deletions
+2
View File
@@ -10,6 +10,8 @@ use std::collections::HashMap;
pub use codex_protocol::shell_environment::CODEX_SESSION_ID_ENV_VAR;
pub use codex_protocol::shell_environment::CODEX_THREAD_ID_ENV_VAR;
pub use codex_protocol::shell_environment::CODEX_TOOL_CALL_ID_ENV_VAR;
pub(crate) use codex_protocol::shell_environment::set_tool_call_id_env_var;
pub(crate) const CODEX_VERSION_ENV_VAR: &str = "CODEX_VERSION";
+5 -1
View File
@@ -23,6 +23,7 @@ use codex_network_proxy::brokered_credential_value_env_keys;
use codex_otel::SessionTelemetry;
use codex_protocol::ThreadId;
use codex_protocol::config_types::ShellEnvironmentPolicy;
use codex_protocol::shell_environment::CODEX_TOOL_CALL_ID_ENV_VAR;
use codex_protocol::shell_environment::create_env_from_vars;
use codex_shell_command::shell_snapshot::CapturedSnapshot;
use codex_shell_command::shell_snapshot::PreparedSnapshot;
@@ -1053,11 +1054,13 @@ async fn run_script_with_timeout(
SnapshotShellMode::Validation(path) => Some(path),
SnapshotShellMode::Login | SnapshotShellMode::NonLogin => None,
};
let mut env = prepared_env.unwrap_or_else(|| std::env::vars().collect());
remove_env_value(&mut env, CODEX_TOOL_CALL_ID_ENV_VAR);
return sandbox
.run(
args,
cwd,
prepared_env.unwrap_or_else(|| std::env::vars().collect()),
env,
snapshot_timeout,
shell_name,
snapshot_read_path,
@@ -1081,6 +1084,7 @@ async fn run_script_with_timeout(
handler.env_clear();
handler.envs(env);
}
handler.env_remove(CODEX_TOOL_CALL_ID_ENV_VAR);
codex_protocol::shell_environment::scrub_non_inheritable_env_vars(handler.as_std_mut());
#[cfg(unix)]
let output = {
+74
View File
@@ -165,6 +165,80 @@ async fn get_snapshot(shell_type: ShellType) -> Result<String> {
Ok(content)
}
#[cfg(unix)]
#[tokio::test]
async fn snapshot_helpers_preserve_thread_without_caching_exec_metadata() -> Result<()> {
let output = Command::new(std::env::current_exe()?)
.arg("--exact")
.arg("shell_snapshot::tests::snapshot_helper_exec_metadata_child")
.arg("--nocapture")
.env("CODEX_TEST_SNAPSHOT_HELPER_CHILD", "1")
.env("CODEX_THREAD_ID", "helper-thread")
.env("CODEX_TOOL_CALL_ID", "stale-call")
.env("BASH_ENV", "/dev/null")
.output()?;
assert!(
output.status.success(),
"snapshot helper child failed:\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
Ok(())
}
#[cfg(unix)]
#[tokio::test]
async fn snapshot_helper_exec_metadata_child() -> Result<()> {
if std::env::var_os("CODEX_TEST_SNAPSHOT_HELPER_CHILD").is_none() {
return Ok(());
}
assert_eq!(
std::env::var("CODEX_THREAD_ID").as_deref(),
Ok("helper-thread")
);
assert_eq!(
std::env::var("CODEX_TOOL_CALL_ID").as_deref(),
Ok("stale-call")
);
let dir = tempdir()?;
let shell = crate::shell::get_shell(ShellType::Bash).context("No available Bash shell")?;
let helper_env = run_script_with_timeout(
&shell,
"printf '%s|%s' \"${CODEX_THREAD_ID-unset}\" \"${CODEX_TOOL_CALL_ID-unset}\"",
SNAPSHOT_TIMEOUT,
SnapshotShellMode::NonLogin,
&dir.path().abs(),
/*credential_broker*/ None,
/*sandbox*/ None,
)
.await?;
assert_eq!(helper_env, "helper-thread|unset");
let snapshot = get_snapshot(ShellType::Bash).await?;
assert!(!snapshot.contains("CODEX_THREAD_ID"));
assert!(!snapshot.contains("CODEX_TOOL_CALL_ID"));
assert!(!snapshot.contains("helper-thread"));
assert!(!snapshot.contains("stale-call"));
let validation_path = dir.path().join("validation.sh").abs();
fs::write(
&validation_path,
"test \"${CODEX_THREAD_ID-missing}\" = helper-thread && \
test \"${CODEX_TOOL_CALL_ID-missing}\" = missing\n",
)
.await?;
validate_snapshot(
&shell,
&validation_path,
&dir.path().abs(),
/*credential_broker*/ None,
/*sandbox*/ None,
)
.await?;
Ok(())
}
#[test]
fn snapshot_file_name_parser_supports_legacy_and_suffixed_names() {
let session_id = "019cf82b-6a62-7700-bbbd-46909794ef89";
+3
View File
@@ -7,6 +7,7 @@ small and focused and reuses the orchestrator for approvals + sandbox + retry.
use crate::exec_env::CODEX_PERMISSION_PROFILE_ENV_VAR;
use crate::exec_env::CODEX_SESSION_ID_ENV_VAR;
use crate::exec_env::CODEX_THREAD_ID_ENV_VAR;
use crate::exec_env::CODEX_TOOL_CALL_ID_ENV_VAR;
use crate::exec_env::CODEX_VERSION_ENV_VAR;
use crate::sandboxing::SandboxPermissions;
use crate::shell::Shell;
@@ -352,6 +353,7 @@ pub(crate) fn maybe_wrap_shell_lc_with_snapshot(
for key in [
CODEX_SESSION_ID_ENV_VAR,
CODEX_THREAD_ID_ENV_VAR,
CODEX_TOOL_CALL_ID_ENV_VAR,
CODEX_VERSION_ENV_VAR,
CODEX_PERMISSION_PROFILE_ENV_VAR,
CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR,
@@ -368,6 +370,7 @@ pub(crate) fn maybe_wrap_shell_lc_with_snapshot(
CODEX_PERMISSION_PROFILE_ENV_VAR,
CODEX_APPLY_PATCH_PRESERVE_LINE_ENDINGS_ENV_VAR,
PLUGIN_METRICS_OUTPUT_ENV_VAR,
CODEX_TOOL_CALL_ID_ENV_VAR,
],
);
let (proxy_captures, proxy_exports) = build_proxy_env_exports(env);
+15 -13
View File
@@ -552,7 +552,7 @@ fn maybe_wrap_shell_lc_with_snapshot_restores_codex_metadata_from_env() {
let snapshot_path = dir.path().join("snapshot.sh");
std::fs::write(
&snapshot_path,
"# Snapshot file\nexport CODEX_THREAD_ID='parent-thread'\nexport CODEX_VERSION='old-version'\n",
"# Snapshot file\nexport CODEX_THREAD_ID='parent-thread'\nexport CODEX_TOOL_CALL_ID='parent-call'\nexport CODEX_VERSION='old-version'\n",
)
.expect("write snapshot");
let (session_shell, shell_snapshot) =
@@ -560,10 +560,12 @@ fn maybe_wrap_shell_lc_with_snapshot_restores_codex_metadata_from_env() {
let command = vec![
"/bin/bash".to_string(),
"-lc".to_string(),
"printf '%s|%s' \"$CODEX_THREAD_ID\" \"$CODEX_VERSION\"".to_string(),
"printf '%s|%s|%s' \"$CODEX_THREAD_ID\" \"$CODEX_TOOL_CALL_ID\" \"$CODEX_VERSION\""
.to_string(),
];
let env = HashMap::from([
("CODEX_THREAD_ID".to_string(), "nested-thread".to_string()),
("CODEX_TOOL_CALL_ID".to_string(), "nested-call".to_string()),
(
"CODEX_VERSION".to_string(),
env!("CARGO_PKG_VERSION").to_string(),
@@ -586,7 +588,7 @@ fn maybe_wrap_shell_lc_with_snapshot_restores_codex_metadata_from_env() {
assert!(output.status.success(), "command failed: {output:?}");
assert_eq!(
String::from_utf8_lossy(&output.stdout),
concat!("nested-thread|", env!("CARGO_PKG_VERSION")),
concat!("nested-thread|nested-call|", env!("CARGO_PKG_VERSION")),
);
}
@@ -628,21 +630,21 @@ fn maybe_wrap_shell_lc_with_snapshot_restores_permission_profile_from_env() {
assert_eq!(String::from_utf8_lossy(&output.stdout), "current-profile\n");
}
#[test]
fn maybe_wrap_shell_lc_with_snapshot_unsets_absent_permission_profile() {
let dir = tempdir().expect("create temp dir");
#[test_case::test_case(CODEX_PERMISSION_PROFILE_ENV_VAR; "permission profile")]
#[test_case::test_case(CODEX_TOOL_CALL_ID_ENV_VAR; "tool call ID")]
fn maybe_wrap_shell_lc_with_snapshot_unsets_absent_metadata(key: &str) -> anyhow::Result<()> {
let dir = tempdir()?;
let snapshot_path = dir.path().join("snapshot.sh");
std::fs::write(
&snapshot_path,
"# Snapshot file\nexport CODEX_PERMISSION_PROFILE='stale-profile'\n",
)
.expect("write snapshot");
format!("# Snapshot file\nexport {key}='stale-value'\n"),
)?;
let (session_shell, shell_snapshot) =
shell_with_snapshot(ShellType::Bash, "/bin/bash", snapshot_path.abs());
let command = vec![
"/bin/bash".to_string(),
"-lc".to_string(),
"printenv CODEX_PERMISSION_PROFILE".to_string(),
format!("printenv {key}"),
];
let rewritten = maybe_wrap_shell_lc_with_snapshot(
&command,
@@ -654,12 +656,12 @@ fn maybe_wrap_shell_lc_with_snapshot_unsets_absent_permission_profile() {
);
let output = Command::new(&rewritten[0])
.args(&rewritten[1..])
.env_remove(CODEX_PERMISSION_PROFILE_ENV_VAR)
.output()
.expect("run rewritten command");
.env_remove(key)
.output()?;
assert_eq!(output.status.code(), Some(1));
assert_eq!(output.stdout, b"");
Ok(())
}
#[test]
@@ -22,6 +22,7 @@ use crate::exec_env::create_env;
use crate::exec_env::inject_apply_patch_env;
use crate::exec_env::inject_permission_profile_env;
use crate::exec_env::inject_session_env;
use crate::exec_env::set_tool_call_id_env_var;
use crate::exec_policy::ExecApprovalRequest;
use crate::guardian::GuardianReviewContext;
use crate::plugins::metrics::finish_and_track_measurements;
@@ -1445,10 +1446,13 @@ impl UnifiedExecProcessManager {
let shell_environment_policy = request.turn_environment.shell_environment_policy();
let local_policy_env = create_env(shell_environment_policy, /*thread_id*/ None);
let mut env = local_policy_env.clone();
#[cfg(windows)]
env.retain(|name, _| !name.eq_ignore_ascii_case(CODEX_THREAD_ID_ENV_VAR));
env.insert(
CODEX_THREAD_ID_ENV_VAR.to_string(),
context.session.thread_id.to_string(),
);
set_tool_call_id_env_var(&mut env, Some(&context.call_id));
inject_session_env(&mut env, context.session.session_id());
inject_apply_patch_env(&mut env);
let active_permission_profile = request.turn_environment.active_permission_profile();