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
@@ -7,6 +7,8 @@ use super::literals;
use super::posix_env_path_expansion_function;
use crate::shell_detect::ShellType;
use crate::shell_startup_script;
use codex_protocol::shell_environment::CODEX_THREAD_ID_ENV_VAR;
use codex_protocol::shell_environment::CODEX_TOOL_CALL_ID_ENV_VAR;
use std::borrow::Cow;
const SNAPSHOT_COMMAND_HELPER: &str = r#"__codex_snapshot_command() {
@@ -362,6 +364,13 @@ impl<'a> CapturedSnapshot<'a> {
});
}
}
// Each launch supplies execution IDs; omit their captured exports.
records.retain(|export| {
!matches!(
export.key,
CODEX_THREAD_ID_ENV_VAR | CODEX_TOOL_CALL_ID_ENV_VAR
)
});
return Some(Self {
startup_environment,
shell_type,
@@ -81,10 +81,12 @@ fn snapshot_capture_selects_declarations_and_environment() -> Result<()> {
)
.unwrap();
let output = Command::new(shell)
.args(["-c", &script])
.args(["-c", &format!("readonly CODEX_THREAD_ID; {script}")])
.env_clear()
.env("HOME", dir.path())
.env("PATH", "/usr/bin:/bin")
.env("CODEX_THREAD_ID", "capture-thread")
.env("CODEX_TOOL_CALL_ID", "profile-call")
.env("APP_SETTING", "value")
.output()?;
assert!(output.status.success(), "{shell}: {output:?}");
@@ -103,6 +105,55 @@ fn snapshot_capture_selects_declarations_and_environment() -> Result<()> {
(declarations, environment),
"{shell}"
);
if !declarations || !environment {
continue;
}
// Both replay paths must preserve current metadata from a full capture.
let replay_environment = HashMap::from([
("CODEX_THREAD_ID".to_string(), "capture-thread".to_string()),
("CODEX_TOOL_CALL_ID".to_string(), "profile-call".to_string()),
("APP_SETTING".to_string(), "value".to_string()),
]);
let empty = HashMap::new();
let brokered = prepare_snapshot_credentials(
&captured,
SnapshotCredentialEnvironment {
original: &replay_environment,
restored: &replay_environment,
allowed: &replay_environment,
..empty_snapshot_credentials(&empty, &|_| true)
},
|_| true,
)
.expect("metadata is not a credential");
for snapshot in [
captured.render_script(
&codex_protocol::config_types::ShellEnvironmentPolicy::default(),
),
brokered.script,
] {
assert!(!snapshot.contains("CODEX_THREAD_ID"), "{shell}: {snapshot}");
assert!(
!snapshot.contains("CODEX_TOOL_CALL_ID"),
"{shell}: {snapshot}"
);
let path = dir.path().join("snapshot.sh");
std::fs::write(&path, snapshot)?;
let replay = Command::new(shell)
.args(["-c", "set -e; . \"$1\"; printf '%s|%s|%s' \"$CODEX_THREAD_ID\" \"$CODEX_TOOL_CALL_ID\" \"$APP_SETTING\"", "replay"])
.arg(&path)
.env_clear()
.env("PATH", "/usr/bin:/bin")
.env("CODEX_THREAD_ID", "current-thread")
.env("CODEX_TOOL_CALL_ID", "current-call")
.output()?;
assert!(replay.status.success(), "{shell}: {replay:?}");
assert_eq!(
replay.stdout, b"current-thread|current-call|value",
"{shell}"
);
}
}
}
Ok(())