diff --git a/codex-rs/core/src/exec_env.rs b/codex-rs/core/src/exec_env.rs index 91bf97ef0c..4b4fee7bd2 100644 --- a/codex-rs/core/src/exec_env.rs +++ b/codex-rs/core/src/exec_env.rs @@ -4,6 +4,8 @@ use crate::config::types::ShellEnvironmentPolicyInherit; use std::collections::HashMap; use std::collections::HashSet; +pub const CODEX_SESSION_ID_ENV_VAR: &str = "CODEX_SESSION_ID"; + /// Construct an environment map based on the rules in the specified policy. The /// resulting map can be passed directly to `Command::envs()` after calling /// `env_clear()` to ensure no unintended variables are leaked to the spawned diff --git a/codex-rs/core/src/tools/handlers/shell.rs b/codex-rs/core/src/tools/handlers/shell.rs index b59c0140d1..f6e315282e 100644 --- a/codex-rs/core/src/tools/handlers/shell.rs +++ b/codex-rs/core/src/tools/handlers/shell.rs @@ -5,6 +5,7 @@ use std::sync::Arc; use crate::codex::TurnContext; use crate::exec::ExecParams; +use crate::exec_env::CODEX_SESSION_ID_ENV_VAR; use crate::exec_env::create_env; use crate::exec_policy::ExecApprovalRequest; use crate::function_tool::FunctionCallError; @@ -238,6 +239,10 @@ impl ShellHandler { if !dependency_env.is_empty() { exec_params.env.extend(dependency_env); } + exec_params + .env + .entry(CODEX_SESSION_ID_ENV_VAR.to_string()) + .or_insert_with(|| session.conversation_id.to_string()); // Approval policy guard for explicit escalation in non-OnRequest modes. if exec_params