From a5815ebe656246233ad9c3a82f41b288a9ad5ce2 Mon Sep 17 00:00:00 2001 From: iceweasel-oai Date: Tue, 24 Nov 2026 23:34:11 +0000 Subject: [PATCH] Add opt-in Statsig initialization to Codex core (#63780) ## What changed Add `Config::start_statsig` so launchers can initialize or reuse a process-wide Statsig runtime. Initialization requires `features.statsig_gates`, which defaults to off, and a nonempty `CODEX_BUNDLED_STATSIG_CLIENT_KEY` set at build time. Support serving-account, local-account, and installation identities. Include the installation ID, launch surface, Codex version, OS, and locale in the targeting context. Use the analytics setting and the selected identity's defaults to control exposure logging. GitOrigin-RevId: da1e04e1a617085cb810d81776d6b49fa62b589e --- codex-rs/Cargo.lock | 3 + codex-rs/Cargo.toml | 1 + codex-rs/core/Cargo.toml | 3 + codex-rs/core/config.schema.json | 6 ++ codex-rs/core/src/config/mod.rs | 2 + codex-rs/core/src/lib.rs | 1 + codex-rs/core/src/statsig.rs | 141 +++++++++++++++++++++++++++++++ codex-rs/features/src/lib.rs | 8 ++ 8 files changed, 165 insertions(+) create mode 100644 codex-rs/core/src/statsig.rs diff --git a/codex-rs/Cargo.lock b/codex-rs/Cargo.lock index ec89b87daf..4e805e785b 100644 --- a/codex-rs/Cargo.lock +++ b/codex-rs/Cargo.lock @@ -3107,6 +3107,7 @@ dependencies = [ "codex-skills", "codex-skills-extension", "codex-state", + "codex-statsig", "codex-terminal-detection", "codex-test-binary-support", "codex-thread-context-ids", @@ -3157,6 +3158,7 @@ dependencies = [ "openssl-sys", "opentelemetry", "opentelemetry_sdk", + "os_info", "predicates", "pretty_assertions", "rand 0.9.3", @@ -3169,6 +3171,7 @@ dependencies = [ "sha1 0.10.6", "shlex", "similar", + "sys-locale", "tempfile", "test-case", "test-log", diff --git a/codex-rs/Cargo.toml b/codex-rs/Cargo.toml index 2bd88a417b..b0e7ce617b 100644 --- a/codex-rs/Cargo.toml +++ b/codex-rs/Cargo.toml @@ -310,6 +310,7 @@ codex-shell-command = { path = "shell-command" } codex-skills-extension = { path = "ext/skills" } codex-skills = { path = "skills" } codex-state = { path = "state" } +codex-statsig = { path = "statsig" } codex-terminal-detection = { path = "terminal-detection" } codex-test-binary-support = { path = "test-binary-support" } codex-thread-context-ids = { path = "thread-context-ids" } diff --git a/codex-rs/core/Cargo.toml b/codex-rs/core/Cargo.toml index 224bf0b72b..bf29090695 100644 --- a/codex-rs/core/Cargo.toml +++ b/codex-rs/core/Cargo.toml @@ -71,6 +71,7 @@ codex-sandboxing = { workspace = true } codex-skills = { workspace = true } codex-skills-extension = { workspace = true } codex-state = { workspace = true } +codex-statsig = { workspace = true } codex-terminal-detection = { workspace = true } codex-thread-store = { workspace = true } codex-tool-call-id = { workspace = true } @@ -115,6 +116,8 @@ rmcp = { workspace = true, default-features = false, features = [ ] } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } +os_info = { workspace = true } +sys-locale = { workspace = true } sha1 = { workspace = true } shlex = { workspace = true } similar = { workspace = true } diff --git a/codex-rs/core/config.schema.json b/codex-rs/core/config.schema.json index 7283ca4deb..2acf872193 100644 --- a/codex-rs/core/config.schema.json +++ b/codex-rs/core/config.schema.json @@ -1423,6 +1423,9 @@ "standalone_web_search": { "type": "boolean" }, + "statsig_gates": { + "type": "boolean" + }, "steer": { "type": "boolean" }, @@ -8843,6 +8846,9 @@ "standalone_web_search": { "type": "boolean" }, + "statsig_gates": { + "type": "boolean" + }, "steer": { "type": "boolean" }, diff --git a/codex-rs/core/src/config/mod.rs b/codex-rs/core/src/config/mod.rs index e2da866540..ff21272645 100644 --- a/codex-rs/core/src/config/mod.rs +++ b/codex-rs/core/src/config/mod.rs @@ -204,6 +204,8 @@ pub(crate) use runtime_refresh::RuntimeConfigRefresh; mod schema; mod token_budget_startup; mod windows_sandbox_config; +pub use crate::statsig::HostIdentity as StatsigHostIdentity; +pub use crate::statsig::StatsigSurface; pub use auth_keyring::bootstrap_auth_config; pub use auth_keyring::resolve_bootstrap_auth_keyring_backend_kind; pub use codex_agent_roles::AgentRoleConfig; diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index 09a6b0cb38..cea2001ea8 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -274,6 +274,7 @@ pub use rollout::rollout_date_parts; mod feedback_config; mod function_tool; mod state; +pub mod statsig; mod tasks; mod user_shell_command; pub mod util; diff --git a/codex-rs/core/src/statsig.rs b/codex-rs/core/src/statsig.rs new file mode 100644 index 0000000000..01db7e2e19 --- /dev/null +++ b/codex-rs/core/src/statsig.rs @@ -0,0 +1,141 @@ +//! Adapts harness configuration and credentials to the process-local Statsig service. + +use std::sync::Arc; + +use codex_features::Feature; +use codex_login::AuthManager; +use codex_login::is_workload_identity_selected; +use codex_model_provider::ProviderAuthContext; +use codex_protocol::auth::AuthMode; +pub use codex_statsig::DynamicConfig; +pub use codex_statsig::Gate; +use codex_statsig::StatsigBootstrapContext; +use codex_statsig::StatsigIdentity; +pub use codex_statsig::StatsigRuntime; + +use crate::config::Config; + +/// Harness launch surfaces sent to Statsig as targeting values. +pub enum StatsigSurface { + Cli, + Exec, + AppServer, + Tui, + Sandbox, + ExecServer, + RemoteExecutor, +} + +/// The credential source selected by the launch path. +pub enum HostIdentity { + ServingAccount { + auth: Arc, + default_analytics_enabled: bool, + }, + LocalAccount, + Installation, +} + +impl Config { + /// Returns whether this launch opted in and has a client key. + pub fn statsig_enabled(&self) -> bool { + self.statsig_key().is_some() + } + + fn statsig_key(&self) -> Option { + if !self.features.enabled(Feature::StatsigGates) { + return None; + } + option_env!("CODEX_BUNDLED_STATSIG_CLIENT_KEY") + .filter(|key| !key.is_empty()) + .map(str::to_owned) + } + + /// Installs the flag store for the lifetime of the harness process. + pub async fn start_statsig( + &self, + surface: StatsigSurface, + identity: HostIdentity, + ) -> Option> { + let sdk_key = self.statsig_key()?; + if let Some(runtime) = StatsigRuntime::current() { + return Some(runtime); + } + let stable_id = crate::resolve_installation_id(&self.codex_home) + .await + .ok()?; + let account_identity = |auth: Arc| { + StatsigIdentity::Account(ProviderAuthContext { + runtime_auth: crate::build_runtime_auth_manager( + self, + Arc::clone(&auth), + &stable_id, + ) + .map(|runtime| (runtime, self.runtime_auth_mode)), + credentials: auth, + }) + }; + let (identity, default_analytics_enabled) = match identity { + HostIdentity::ServingAccount { + auth, + default_analytics_enabled, + } => (account_identity(auth), Some(default_analytics_enabled)), + HostIdentity::LocalAccount => { + // Workload identity requires a token exchange, even when the auth modes are filtered. + if !is_workload_identity_selected() + && let Ok(auth) = AuthManager::shared_from_config_with_auth_modes( + self, + /*enable_codex_api_key_env*/ false, + vec![ + AuthMode::Chatgpt, + AuthMode::ChatgptAuthTokens, + AuthMode::ApiKey, + ], + ) + .await + && auth.auth_cached().is_some() + { + ( + account_identity(auth), + (!matches!(surface, StatsigSurface::ExecServer)).then_some(true), + ) + } else { + ( + StatsigIdentity::Installation(self.http_client_factory()), + None, + ) + } + } + HostIdentity::Installation => ( + StatsigIdentity::Installation(self.http_client_factory()), + None, + ), + }; + let exposure_logging_enabled = default_analytics_enabled + .is_some_and(|default_enabled| self.analytics_enabled.unwrap_or(default_enabled)); + Some(StatsigRuntime::start_process( + identity, + self.chatgpt_base_url.clone(), + sdk_key, + StatsigBootstrapContext { + target_app: "PUBLIC-EVERYTHING-LEAKS-codex-cli", + stable_id, + app_version: env!("CARGO_PKG_VERSION").into(), + locale: sys_locale::get_locale(), + system_name: std::env::consts::OS.into(), + system_version: os_info::get().version().to_string(), + window_type: match surface { + StatsigSurface::Cli => "cli", + StatsigSurface::Exec => "exec", + StatsigSurface::AppServer => "app-server", + StatsigSurface::Tui => "tui", + StatsigSurface::Sandbox => "sandbox", + StatsigSurface::ExecServer => "exec-server", + StatsigSurface::RemoteExecutor => "remote-executor", + }, + brand_name: "codex", + }, + exposure_logging_enabled, + )) + } +} diff --git a/codex-rs/features/src/lib.rs b/codex-rs/features/src/lib.rs index c7dc71c4e7..32c056c571 100644 --- a/codex-rs/features/src/lib.rs +++ b/codex-rs/features/src/lib.rs @@ -113,6 +113,8 @@ pub enum Feature { DaybreakProgramPropagation, /// Restrict CLI resume of attributed local threads to their creator. CliResumeAccountCheck, + /// Fetch Statsig decisions for this harness. + StatsigGates, /// Removed no-op; use `tui.fullscreen_transcript` instead. TranscriptV2, // Stable. @@ -1213,6 +1215,12 @@ pub const FEATURES: &[FeatureSpec] = &[ stage: Stage::UnderDevelopment, default_enabled: false, }, + FeatureSpec { + id: Feature::StatsigGates, + key: "statsig_gates", + stage: Stage::UnderDevelopment, + default_enabled: false, + }, FeatureSpec { id: Feature::DaemonAutoStart, key: "daemon_auto_start",