mirror of
https://github.com/openai/codex.git
synced 2026-11-15 21:50:46 +00:00
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:
committed by
copyberry
parent
2fdf047c96
commit
9b738582b1
@@ -5,6 +5,7 @@ use std::collections::HashMap;
|
||||
|
||||
pub const CODEX_SESSION_ID_ENV_VAR: &str = "CODEX_SESSION_ID";
|
||||
pub const CODEX_THREAD_ID_ENV_VAR: &str = "CODEX_THREAD_ID";
|
||||
pub const CODEX_TOOL_CALL_ID_ENV_VAR: &str = "CODEX_TOOL_CALL_ID";
|
||||
pub const CODEX_EXEC_SERVER_NOISE_AUTH_TOKEN_ENV_VAR: &str = "CODEX_EXEC_SERVER_NOISE_AUTH_TOKEN";
|
||||
pub const OPENAI_FEDERATION_RULE_ID_ENV_VAR: &str = "OPENAI_FEDERATION_RULE_ID";
|
||||
pub const OPENAI_IDENTITY_TOKEN_FILE_ENV_VAR: &str = "OPENAI_IDENTITY_TOKEN_FILE";
|
||||
@@ -26,6 +27,19 @@ pub fn is_non_inheritable_env_var(name: &str) -> bool {
|
||||
.any(|restricted| restricted.eq_ignore_ascii_case(name))
|
||||
}
|
||||
|
||||
pub fn set_tool_call_id_env_var(env: &mut HashMap<String, String>, tool_call_id: Option<&str>) {
|
||||
#[cfg(windows)]
|
||||
env.retain(|name, _| !name.eq_ignore_ascii_case(CODEX_TOOL_CALL_ID_ENV_VAR));
|
||||
#[cfg(not(windows))]
|
||||
env.remove(CODEX_TOOL_CALL_ID_ENV_VAR);
|
||||
if let Some(tool_call_id) = tool_call_id.filter(|tool_call_id| !tool_call_id.contains('\0')) {
|
||||
env.insert(
|
||||
CODEX_TOOL_CALL_ID_ENV_VAR.to_string(),
|
||||
tool_call_id.to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Configures a child command to omit non-inheritable variables from the
|
||||
/// process environment and explicit command overrides.
|
||||
///
|
||||
|
||||
@@ -9,6 +9,50 @@ const CHILD_MODE_ENV_VAR: &str = "CODEX_SHELL_ENVIRONMENT_SCRUBBER_TEST_MODE";
|
||||
const TEST_NAME: &str =
|
||||
"shell_environment::tests::command_scrubber_removes_names_from_real_child_environment";
|
||||
|
||||
#[test]
|
||||
fn tool_call_id_replaces_platform_equivalent_names() {
|
||||
let representable = format!("exec-雪-{}", "x".repeat(512));
|
||||
for (call_id, expected_call) in [
|
||||
(None, None),
|
||||
(Some(""), Some("")),
|
||||
(Some("invalid\0call"), None),
|
||||
(Some("current-call"), Some("current-call")),
|
||||
(Some(representable.as_str()), Some(representable.as_str())),
|
||||
] {
|
||||
let mut env = HashMap::from([
|
||||
("CODEX_TOOL_CALL_ID".to_string(), "stale-call".to_string()),
|
||||
(
|
||||
"Codex_Tool_Call_Id".to_string(),
|
||||
"mixed-case-call".to_string(),
|
||||
),
|
||||
(
|
||||
"codex_tool_call_id".to_string(),
|
||||
"lowercase-call".to_string(),
|
||||
),
|
||||
("OTHER".to_string(), "unchanged".to_string()),
|
||||
]);
|
||||
|
||||
set_tool_call_id_env_var(&mut env, call_id);
|
||||
|
||||
let mut expected = HashMap::from([("OTHER".to_string(), "unchanged".to_string())]);
|
||||
#[cfg(not(windows))]
|
||||
expected.extend([
|
||||
(
|
||||
"Codex_Tool_Call_Id".to_string(),
|
||||
"mixed-case-call".to_string(),
|
||||
),
|
||||
(
|
||||
"codex_tool_call_id".to_string(),
|
||||
"lowercase-call".to_string(),
|
||||
),
|
||||
]);
|
||||
if let Some(expected_call) = expected_call {
|
||||
expected.insert("CODEX_TOOL_CALL_ID".to_string(), expected_call.to_string());
|
||||
}
|
||||
assert_eq!(env, expected, "call_id: {call_id:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_inheritable_environment_is_removed_after_policy_overrides() {
|
||||
let vars = [
|
||||
|
||||
Reference in New Issue
Block a user