mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
Initialize Statsig feature gates across Codex launch modes (#64001)
## Why Enabling `statsig_gates` can only load evaluations if the process starts the Statsig runtime. Several Codex entry points never started it. ## What changed Start Statsig in standalone and embedded app servers, the TUI when it connects to a separate app server, eligible `codex sandbox` launches, and local and remote exec servers. This also covers `codex exec` through its embedded app server. Use the serving or local account where applicable and installation identity for remote execution. Share the runtime with the embedded TUI and flush it on normal shutdown. ## Testing Add a CLI integration test that checks which evaluation endpoint each launch mode requests, including signed-in and signed-out sandbox launches. Supply a bundled fixture key in Bazel and nextest CI builds so the test runs. GitOrigin-RevId: 64df9933c1a9783a86be8712fa012b495f34b0f8
This commit is contained in:
@@ -8,6 +8,7 @@ use std::path::PathBuf;
|
||||
use std::process::Stdio;
|
||||
|
||||
use anyhow::Context as _;
|
||||
use codex_app_server::in_process::EmbeddedNetworkPolicy;
|
||||
use codex_config::CloudConfigBundleLoader;
|
||||
use codex_config::LoaderOverrides;
|
||||
use codex_core::config::Config;
|
||||
@@ -271,6 +272,7 @@ async fn run_command_under_sandbox(
|
||||
pub struct StartedSandboxChild {
|
||||
child: Child,
|
||||
_network_proxy: Option<codex_core::config::StartedNetworkProxy>,
|
||||
statsig: Option<std::sync::Arc<codex_core::statsig::StatsigRuntime>>,
|
||||
#[cfg(target_os = "macos")]
|
||||
denial_logger: Option<DenialLogger>,
|
||||
}
|
||||
@@ -296,6 +298,7 @@ impl StartedSandboxChild {
|
||||
}
|
||||
}
|
||||
}
|
||||
codex_core::statsig::StatsigRuntime::finish(self.statsig.take()).await;
|
||||
handle_exit_status(status);
|
||||
}
|
||||
}
|
||||
@@ -347,7 +350,13 @@ async fn start_command_under_sandbox(
|
||||
}
|
||||
None => codex_linux_sandbox_exe,
|
||||
};
|
||||
let config = load_debug_sandbox_config(
|
||||
let includes_managed_requirements = sandbox_state.is_none()
|
||||
&& !config_options.loader_overrides.ignore_managed_requirements
|
||||
&& matches!(
|
||||
config_options.managed_requirements_mode,
|
||||
ManagedRequirementsMode::Include
|
||||
);
|
||||
let mut config = load_debug_sandbox_config(
|
||||
config_overrides
|
||||
.parse_overrides()
|
||||
.map_err(anyhow::Error::msg)?,
|
||||
@@ -357,6 +366,23 @@ async fn start_command_under_sandbox(
|
||||
)
|
||||
.await?;
|
||||
|
||||
let statsig = if includes_managed_requirements
|
||||
&& config.statsig_enabled()
|
||||
&& config
|
||||
.config_layer_stack
|
||||
.ensure_config_server_applied()
|
||||
.is_ok()
|
||||
{
|
||||
EmbeddedNetworkPolicy::default().activate(&mut config);
|
||||
config
|
||||
.start_statsig(
|
||||
codex_core::statsig::StatsigSurface::Sandbox,
|
||||
codex_core::statsig::HostIdentity::LocalAccount,
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let cwd = &config.cwd;
|
||||
let env = create_env(
|
||||
&config.permissions.shell_environment_policy,
|
||||
@@ -445,7 +471,7 @@ async fn start_command_under_sandbox(
|
||||
.map(PathUri::to_abs_path)
|
||||
.collect::<std::io::Result<Vec<_>>>()
|
||||
.context("invalid Windows sandbox workspace roots")?;
|
||||
run_command_under_windows_session(
|
||||
let exit_code = run_command_under_windows_session(
|
||||
&config,
|
||||
&session_profile,
|
||||
command,
|
||||
@@ -454,6 +480,8 @@ async fn start_command_under_sandbox(
|
||||
env,
|
||||
)
|
||||
.await;
|
||||
codex_core::statsig::StatsigRuntime::finish(statsig).await;
|
||||
std::process::exit(exit_code);
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
SandboxType::Windows => anyhow::bail!("Windows sandbox is only available on Windows"),
|
||||
@@ -564,6 +592,7 @@ async fn start_command_under_sandbox(
|
||||
Ok(StartedSandboxChild {
|
||||
child,
|
||||
_network_proxy: network_proxy,
|
||||
statsig,
|
||||
#[cfg(target_os = "macos")]
|
||||
denial_logger,
|
||||
})
|
||||
@@ -577,7 +606,7 @@ async fn run_command_under_windows_session(
|
||||
cwd: AbsolutePathBuf,
|
||||
workspace_roots: Vec<AbsolutePathBuf>,
|
||||
env: std::collections::HashMap<String, String>,
|
||||
) -> ! {
|
||||
) -> i32 {
|
||||
use codex_core::windows_sandbox::WindowsSandboxLevelExt;
|
||||
use codex_protocol::config_types::WindowsSandboxLevel;
|
||||
use codex_windows_sandbox::WindowsSandboxProxySettingsMode;
|
||||
@@ -594,7 +623,7 @@ async fn run_command_under_windows_session(
|
||||
Ok(paths) => paths,
|
||||
Err(err) => {
|
||||
eprintln!("windows sandbox failed: {err}");
|
||||
std::process::exit(1);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -625,12 +654,11 @@ async fn run_command_under_windows_session(
|
||||
Ok(spawned) => spawned,
|
||||
Err(err) => {
|
||||
eprintln!("windows sandbox failed: {err}");
|
||||
std::process::exit(1);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
let exit_code = codex_windows_sandbox::forward_sandbox_session_stdio(spawned).await;
|
||||
std::process::exit(exit_code);
|
||||
codex_windows_sandbox::forward_sandbox_session_stdio(spawned).await
|
||||
}
|
||||
|
||||
async fn spawn_debug_sandbox_child(
|
||||
|
||||
@@ -0,0 +1,224 @@
|
||||
//! Checks which endpoint the shipped harness uses to initialize Statsig.
|
||||
|
||||
use anyhow::Result;
|
||||
use app_test_support::ChatGptAuthFixture;
|
||||
use app_test_support::mount_workspace_routing;
|
||||
use app_test_support::write_chatgpt_auth;
|
||||
use codex_config::types::AuthCredentialsStoreMode;
|
||||
use codex_utils_cargo_bin::cargo_bin;
|
||||
use codex_utils_pty::ChildFds;
|
||||
use codex_utils_pty::TerminalSize;
|
||||
use codex_utils_pty::spawn_pipe_process;
|
||||
use codex_utils_pty::spawn_pty_process;
|
||||
use core_test_support::runtime_auth::with_harness_proxy;
|
||||
use serde_json::json;
|
||||
use std::time::Duration;
|
||||
use wiremock::Mock;
|
||||
use wiremock::MockServer;
|
||||
use wiremock::ResponseTemplate;
|
||||
use wiremock::matchers::body_partial_json;
|
||||
use wiremock::matchers::header;
|
||||
use wiremock::matchers::header_regex;
|
||||
use wiremock::matchers::method;
|
||||
use wiremock::matchers::path;
|
||||
|
||||
#[tokio::test]
|
||||
async fn harness_startup_requests_statsig_evaluations() -> Result<()> {
|
||||
if option_env!("CODEX_BUNDLED_STATSIG_CLIENT_KEY").is_none_or(str::is_empty) {
|
||||
eprintln!(
|
||||
"Skipping Statsig launch coverage. Set CODEX_BUNDLED_STATSIG_CLIENT_KEY at build time."
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
let binary = cargo_bin("codex")?;
|
||||
let server = MockServer::start().await;
|
||||
mount_workspace_routing(&server).await;
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/agent/register"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
|
||||
"runtime_agent_instance_id": "fixture-runtime", "agent_host_id": "fixture-host",
|
||||
"actor": {"kind": "chatgpt", "user_id": "fixture-user", "account_id": "fixture-account"},
|
||||
})))
|
||||
.mount(&server)
|
||||
.await;
|
||||
let registry = server.uri();
|
||||
Mock::given(method("POST"))
|
||||
.and(path("/cloud/environment/fixture/register"))
|
||||
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
|
||||
"environment_id": "fixture",
|
||||
"url": format!("ws://{}/relay?role=environment", server.address()),
|
||||
"security_profile": "noise_hybrid_ik_v1",
|
||||
"executor_registration_id": "fixture",
|
||||
})))
|
||||
.mount(&server)
|
||||
.await;
|
||||
|
||||
with_harness_proxy(&server, /*realtime_target*/ None, async |mut command| {
|
||||
for key in [
|
||||
"CODEX_ACCESS_TOKEN",
|
||||
"CODEX_API_KEY",
|
||||
"CODEX_EXEC_SERVER_URL",
|
||||
"CODEX_SQLITE_HOME",
|
||||
"OPENAI_API_KEY",
|
||||
"OPENAI_FEDERATION_RULE_ID",
|
||||
"OPENAI_IDENTITY_TOKEN_FILE",
|
||||
"OPENAI_WORKLOAD_IDENTITY_CONTEXT",
|
||||
] {
|
||||
command.env_remove(key);
|
||||
}
|
||||
command.env("CODEX_INTERNAL_APP_SERVER_REMOTE_CONTROL_DISABLED", "1");
|
||||
for (mode, signed_in) in [
|
||||
("app-server", true),
|
||||
("app-server-runtime", true),
|
||||
#[cfg(unix)]
|
||||
("daemon", true),
|
||||
("exec", true),
|
||||
("tui", true),
|
||||
#[cfg(unix)]
|
||||
("tui-daemon", true),
|
||||
("sandbox", true),
|
||||
("sandbox", false),
|
||||
("exec-server", true),
|
||||
("exec-server", false),
|
||||
("remote", true),
|
||||
] {
|
||||
let tui = matches!(mode, "tui" | "tui-daemon");
|
||||
// Keep the managed daemon's Unix socket path short.
|
||||
#[cfg(unix)]
|
||||
let home = tempfile::tempdir_in("/tmp")?;
|
||||
#[cfg(not(unix))]
|
||||
let home = tempfile::tempdir()?;
|
||||
let cwd = home.path().canonicalize()?;
|
||||
let statsig_config = if mode == "app-server-runtime" {
|
||||
"features.runtime_agent_instances = { enabled = true, mode = 'assertion_only' }\n"
|
||||
} else if mode == "tui-daemon" {
|
||||
// CLI feature flags are checked against the daemon's settings.
|
||||
"features.statsig_gates = true\n"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
std::fs::write(
|
||||
cwd.join("config.toml"),
|
||||
format!(
|
||||
"{statsig_config}cli_auth_credentials_store = 'file'\ncheck_for_update_on_startup = false\n[analytics]\nenabled = false\n[projects.{}]\ntrust_level = 'trusted'\n",
|
||||
toml::Value::String(cwd.to_string_lossy().into_owned()),
|
||||
),
|
||||
)?;
|
||||
if signed_in {
|
||||
write_chatgpt_auth(
|
||||
&cwd,
|
||||
ChatGptAuthFixture::new("fixture-token")
|
||||
.account_id("fixture-account")
|
||||
.chatgpt_account_id("fixture-account")
|
||||
.chatgpt_user_id("fixture-user")
|
||||
.plan_type("plus"),
|
||||
AuthCredentialsStoreMode::File,
|
||||
)?;
|
||||
}
|
||||
let listen = format!("unix://{}/app-server.sock", cwd.display());
|
||||
let args = match mode {
|
||||
"app-server" | "app-server-runtime" => vec!["app-server"],
|
||||
"daemon" => vec!["app-server", "--managed-daemon", "--listen", &listen],
|
||||
"exec" => vec!["exec", "--skip-git-repo-check", "fixture prompt"],
|
||||
"tui" => vec!["--no-daemon", "--no-alt-screen"],
|
||||
"tui-daemon" => vec!["--remote", "unix://", "--no-alt-screen"],
|
||||
"sandbox" => {
|
||||
// Keep managed requirements enabled without depending on a host sandbox.
|
||||
let mut args = vec!["-c", "default_permissions=':danger-full-access'", "sandbox", "--"];
|
||||
if cfg!(windows) {
|
||||
args.extend(["powershell.exe", "-Command", "Start-Sleep -Seconds 20"]);
|
||||
} else {
|
||||
args.extend(["sleep", "20"]);
|
||||
}
|
||||
args
|
||||
}
|
||||
"exec-server" => vec!["exec-server", "--listen", "ws://127.0.0.1:0"],
|
||||
"remote" => vec!["exec-server", "--remote", ®istry, "--environment-id", "fixture"],
|
||||
_ => unreachable!("unknown launch mode {mode}"),
|
||||
};
|
||||
let flags: &[&str] = if mode == "tui-daemon" { &[] } else { &["--enable", "statsig_gates"] };
|
||||
let args: Vec<_> = flags.iter().copied().chain(args).map(str::to_owned).collect();
|
||||
let (host, endpoint) = if signed_in && mode != "remote" {
|
||||
("chatgpt.com", "/backend-api/wham/statsig/bootstrap")
|
||||
} else {
|
||||
("ab.chatgpt.com", "/v1/initialize")
|
||||
};
|
||||
let mut request = Mock::given(method("POST"))
|
||||
.and(header("host", host))
|
||||
.and(path(endpoint));
|
||||
if mode == "app-server-runtime" {
|
||||
request = request.and(header_regex("authorization", "^AgentAssertion "));
|
||||
} else if mode == "tui-daemon" {
|
||||
request = request.and(body_partial_json(json!({"window_type": "tui"})));
|
||||
}
|
||||
let request = request
|
||||
// The SDK test covers handling the response. Reject here to avoid a retry.
|
||||
.respond_with(ResponseTemplate::new(400))
|
||||
.expect(1)
|
||||
.named(format!("{mode} startup"))
|
||||
.mount_as_scoped(&server)
|
||||
.await;
|
||||
|
||||
command.env("CODEX_HOME", &cwd);
|
||||
if tui {
|
||||
command.env("TERM", "xterm-256color");
|
||||
}
|
||||
let env = command
|
||||
.as_std()
|
||||
.get_envs()
|
||||
.filter_map(|(key, value)| Some((key.to_str()?.to_owned(), value?.to_str()?.to_owned())))
|
||||
.collect();
|
||||
let daemon = if mode == "tui-daemon" {
|
||||
let daemon_args = ["--disable", "statsig_gates", "app-server", "--listen", "unix://"]
|
||||
.map(str::to_owned);
|
||||
let daemon = spawn_pipe_process(&binary, &daemon_args, &cwd, &env, /*arg0*/ &None, &[]).await?;
|
||||
let socket = codex_app_server::app_server_control_socket_path(&cwd)?;
|
||||
tokio::time::timeout(Duration::from_secs(30), async {
|
||||
while !socket.as_path().try_exists()? {
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
Ok::<_, anyhow::Error>(())
|
||||
}).await??;
|
||||
Some(daemon)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut child = if tui {
|
||||
spawn_pty_process(
|
||||
&binary, &args, &cwd, &env, /*arg0*/ &None,
|
||||
TerminalSize::default(), ChildFds::Inherited(&[]),
|
||||
).await?
|
||||
} else {
|
||||
spawn_pipe_process(&binary, &args, &cwd, &env, /*arg0*/ &None, &[]).await?
|
||||
};
|
||||
if mode == "exec" {
|
||||
child.session.close_stdin();
|
||||
}
|
||||
let mut output = Vec::new();
|
||||
let wait = async {
|
||||
loop {
|
||||
tokio::select! {
|
||||
biased;
|
||||
() = request.wait_until_satisfied() => break,
|
||||
status = &mut child.exit_rx => panic!("{mode} exited with {status:?} before POST {endpoint}\n{}", String::from_utf8_lossy(&output)),
|
||||
Some(chunk) = child.stdout_rx.recv() => output.extend(chunk),
|
||||
Some(chunk) = child.stderr_rx.recv() => output.extend(chunk),
|
||||
}
|
||||
}
|
||||
};
|
||||
assert!(
|
||||
tokio::time::timeout(Duration::from_secs(15), wait).await.is_ok(),
|
||||
"{mode} did not POST {endpoint}\n{}",
|
||||
String::from_utf8_lossy(&output),
|
||||
);
|
||||
child.session.terminate();
|
||||
let _ = child.exit_rx.await;
|
||||
if let Some(daemon) = daemon {
|
||||
daemon.session.signal(codex_utils_pty::ProcessSignal::Interrupt)?;
|
||||
tokio::time::timeout(Duration::from_secs(15), daemon.exit_rx).await??;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
.await
|
||||
}
|
||||
Reference in New Issue
Block a user