mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
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
This commit is contained in:
Generated
+3
@@ -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",
|
||||
|
||||
@@ -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" }
|
||||
|
||||
@@ -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 }
|
||||
|
||||
@@ -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"
|
||||
},
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<AuthManager>,
|
||||
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<String> {
|
||||
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<Arc<StatsigRuntime>> {
|
||||
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<AuthManager>| {
|
||||
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,
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user