Add attributed exec process lifecycle telemetry (#42373)

## Why

Exec-server process and network-policy events need stable attribution to the
launching tool call and executor without recording process payloads.

## What changed

- Add optional `ExecMetadata` to `ExecParams` and propagate the thread and tool
  call IDs from unified exec.
- Emit bounded OpenTelemetry events for process start, spawn failure, sandbox
  denial, and exit. Correlate them with the launch trace and registry-issued
  executor identity while excluding arguments, paths, environment values,
  output, and error text.
- Preserve launch attribution across long-running processes and reconnects, and
  attach the same metadata to network-policy audit events.
- Keep the protocol backward compatible when metadata is omitted, and prevent
  invalid trace headers from inheriting an unrelated active span.

## Testing

Add coverage for metadata serialization and propagation, lifecycle event
fields, trace relationships, reconnect behavior, spawn failures, sandbox
denials, and network-policy attribution.

GitOrigin-RevId: 7aa480a7289c73cb95e2c124c35500bb6f0d5084
This commit is contained in:
viyatb-oai authored and copyberry committed 2026-09-02 21:58:58 +00:00
1 parent 5037919777
commit f53c91be2c
47 files changed
+1461 -178

No files matched your search

@@ -111,6 +111,7 @@ async fn exec_command_with_tty(
.open_session_with_prepared_exec_env(
process_id,
&request,
/*tool_ctx*/ None,
codex_sandboxing::WindowsSandboxProxySettingsMode::Reconcile,
/*network_policy_decider*/ None,
tty,
@@ -772,6 +773,7 @@ async fn completed_pipe_commands_preserve_exit_code() -> anyhow::Result<()> {
.open_session_with_prepared_exec_env(
/*process_id*/ 1234,
&request,
/*tool_ctx*/ None,
codex_sandboxing::WindowsSandboxProxySettingsMode::Reconcile,
/*network_policy_decider*/ None,
/*tty*/ false,
@@ -814,6 +816,7 @@ async fn unified_exec_uses_remote_exec_server_when_configured() -> anyhow::Resul
.open_session_with_prepared_exec_env(
/*process_id*/ 1234,
&request,
/*tool_ctx*/ None,
codex_sandboxing::WindowsSandboxProxySettingsMode::Reconcile,
/*network_policy_decider*/ None,
/*tty*/ true,
@@ -863,6 +866,7 @@ async fn remote_exec_server_rejects_inherited_fd_launches() -> anyhow::Result<()
.open_session_with_prepared_exec_env(
/*process_id*/ 1234,
&request,
/*tool_ctx*/ None,
codex_sandboxing::WindowsSandboxProxySettingsMode::Reconcile,
/*network_policy_decider*/ None,
/*tty*/ true,
@@ -213,6 +213,7 @@ fn exec_server_env_for_request(
fn exec_server_params_for_request(
process_id: i32,
request: &ExecRequest,
tool_ctx: Option<&ToolCtx>,
windows_sandbox_proxy_settings_mode: codex_sandboxing::WindowsSandboxProxySettingsMode,
tty: bool,
) -> codex_exec_server::ExecParams {
@@ -231,6 +232,10 @@ fn exec_server_params_for_request(
};
codex_exec_server::ExecParams {
process_id: exec_server_process_id.into(),
metadata: tool_ctx.map(|ctx| codex_exec_server::ExecMetadata {
thread_id: Some(ctx.session.thread_id()),
tool_call_id: Some(ctx.call_id.clone()),
}),
argv: request.command.clone(),
cwd: request.cwd.clone(),
env_policy,
@@ -1176,6 +1181,7 @@ impl UnifiedExecProcessManager {
pub(crate) async fn open_session_with_exec_env(
&self,
process_id: i32,
tool_ctx: &ToolCtx,
command: SandboxCommand,
options: ExecOptions,
attempt: &SandboxAttempt<'_>,
@@ -1208,6 +1214,7 @@ impl UnifiedExecProcessManager {
self.open_session_with_prepared_exec_env(
process_id,
&request,
Some(tool_ctx),
windows_sandbox_proxy_settings_mode,
network_policy_decider,
tty,
@@ -1231,6 +1238,7 @@ impl UnifiedExecProcessManager {
&self,
process_id: i32,
request: &ExecRequest,
tool_ctx: Option<&ToolCtx>,
windows_sandbox_proxy_settings_mode: codex_sandboxing::WindowsSandboxProxySettingsMode,
network_policy_decider: Option<Arc<dyn NetworkPolicyDecider>>,
tty: bool,
@@ -1250,6 +1258,7 @@ impl UnifiedExecProcessManager {
let params = exec_server_params_for_request(
process_id,
request,
tool_ctx,
windows_sandbox_proxy_settings_mode,
tty,
);
@@ -218,6 +218,7 @@ fn exec_server_params_use_path_uri_and_env_policy_overlay_contract() {
exec_server_params_for_request(
/*process_id*/ 123,
request,
/*tool_ctx*/ None,
proxy_settings_mode,
/*tty*/ true,
)
@@ -225,6 +226,7 @@ fn exec_server_params_use_path_uri_and_env_policy_overlay_contract() {
let params = params_for_request(&request);
assert_eq!(params.process_id.as_str(), "123");
assert_eq!(params.metadata, None);
assert_eq!(params.cwd, request.cwd);
assert!(params.enforce_managed_network);
assert_eq!(params.managed_network, Some(managed_network));
@@ -130,6 +130,7 @@ impl Session {
}
let params = ExecParams {
process_id: format!("shell-snapshot-warmup-{}", Uuid::new_v4()).into(),
metadata: None,
// This capture-only operation never starts the command or routes
// speculative work through tool approvals or exec-policy bypass.
argv: shell.derive_exec_args("", /*use_login_shell*/ true),