From 80d0a67185fade317799e70f7ab676d3aa6a1861 Mon Sep 17 00:00:00 2001 From: iceweasel-oai Date: Tue, 1 Dec 2026 19:54:08 +0000 Subject: [PATCH] Share runtime registration across app-server startup clients (#65106) ## Why Statsig starts before the model catalog. When runtime agent instances are enabled, separate auth managers can cause Statsig and model discovery to register the same app-server runtime twice. ## What changed Pass a shared runtime auth slot to Statsig and model discovery in both standalone and in-process app-server startup so they use the same runtime registration. ## Testing Extend the CLI startup integration test to require exactly one runtime registration and verify that both Statsig evaluations and model discovery send `AgentAssertion` authorization headers. GitOrigin-RevId: 132521b685c5c7c617e7067c648de65b9a4287b2 --- codex-rs/app-server/src/in_process.rs | 11 +++--- codex-rs/app-server/src/lib.rs | 1 + codex-rs/app-server/src/message_processor.rs | 7 ++-- codex-rs/cli/tests/statsig_startup.rs | 35 +++++++++++++------ codex-rs/core/src/lib.rs | 1 + codex-rs/core/src/statsig.rs | 15 +++++--- codex-rs/core/src/thread_manager.rs | 1 + .../core/src/thread_manager/model_catalog.rs | 18 +++++++++- 8 files changed, 64 insertions(+), 25 deletions(-) diff --git a/codex-rs/app-server/src/in_process.rs b/codex-rs/app-server/src/in_process.rs index fdd932d305..9837a6b8b8 100644 --- a/codex-rs/app-server/src/in_process.rs +++ b/codex-rs/app-server/src/in_process.rs @@ -529,6 +529,11 @@ async fn start_uninitialized(mut args: InProcessStartArgs) -> IoResult IoResult IoResult(channel_capacity); - let runtime_auth_slot = args - .config - .features - .enabled(codex_features::Feature::RuntimeAgentInstances) - .then(codex_runtime_agent_instance::RuntimeAuthSlot::default); let analytics_events_client = analytics_events_client_from_config( Arc::clone(&auth_manager), args.config.as_ref(), diff --git a/codex-rs/app-server/src/lib.rs b/codex-rs/app-server/src/lib.rs index fa4234fe17..4351663ca2 100644 --- a/codex-rs/app-server/src/lib.rs +++ b/codex-rs/app-server/src/lib.rs @@ -827,6 +827,7 @@ pub async fn run_main_with_transport_options( codex_core::statsig::StatsigSurface::AppServer, codex_core::statsig::HostIdentity::ServingAccount { auth: Arc::clone(&auth_manager), + runtime_auth_slot: runtime_auth_slot.clone(), default_analytics_enabled, }, ) diff --git a/codex-rs/app-server/src/message_processor.rs b/codex-rs/app-server/src/message_processor.rs index 8071e73bdb..53db6c4c8e 100644 --- a/codex-rs/app-server/src/message_processor.rs +++ b/codex-rs/app-server/src/message_processor.rs @@ -379,11 +379,12 @@ impl MessageProcessor { Arc::clone(&extension_event_sink), )) }); - let startup = codex_core::build_startup_models( + let startup = codex_core::build_startup_models_with_runtime_auth( config.as_ref(), auth_manager.clone(), installation_id, Arc::new(ModelCatalogSettings::disk(config.codex_home.to_path_buf())), + runtime_auth_slot.unwrap_or_default(), ); let manager = ThreadManager::new( config.as_ref(), @@ -437,10 +438,6 @@ impl MessageProcessor { } }); let runtime_auth = thread_manager.runtime_auth_manager(); - if let (Some(slot), Some(runtime)) = (runtime_auth_slot, &runtime_auth) { - // Analytics starts before the harness. Bind its requests to the harness registration. - slot.get_or_init(|| Arc::clone(runtime)); - } let model_catalog = Arc::new(crate::model_catalog::ModelCatalog::new( config_manager.clone(), startup_cwd, diff --git a/codex-rs/cli/tests/statsig_startup.rs b/codex-rs/cli/tests/statsig_startup.rs index ab07ee9628..6fb2b83bcc 100644 --- a/codex-rs/cli/tests/statsig_startup.rs +++ b/codex-rs/cli/tests/statsig_startup.rs @@ -33,14 +33,10 @@ async fn harness_startup_requests_statsig_evaluations() -> Result<()> { 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 registration = 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"}, + })); let registry = server.uri(); Mock::given(method("POST")) .and(path("/cloud/environment/fixture/register")) @@ -146,9 +142,24 @@ async fn harness_startup_requests_statsig_evaluations() -> Result<()> { let mut request = Mock::given(method("POST")) .and(header("host", host)) .and(path(endpoint)); - if mode == "app-server-runtime" { + let register = Mock::given(method("POST")) + .and(path("/agent/register")) + .respond_with(registration.clone()); + let (register, models) = if mode == "app-server-runtime" { request = request.and(header_regex("authorization", "^AgentAssertion ")); - } else if mode == "tui-daemon" { + // Bootstrap and model discovery must share one runtime registration. + let models = Mock::given(path("/backend-api/codex/models")) + .and(header_regex("authorization", "^AgentAssertion ")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({"models": []}))) + .expect(1..) + .mount_as_scoped(&server) + .await; + (register.expect(1), Some(models)) + } else { + (register, None) + }; + let _registration = register.mount_as_scoped(&server).await; + if mode == "tui-daemon" { request = request.and(body_partial_json(json!({"window_type": "tui"}))); } let request = request @@ -211,6 +222,10 @@ async fn harness_startup_requests_statsig_evaluations() -> Result<()> { "{mode} did not POST {endpoint}\n{}", String::from_utf8_lossy(&output), ); + if let Some(models) = models { + tokio::time::timeout(Duration::from_secs(15), models.wait_until_satisfied()).await + .expect("app-server did not request models with runtime auth"); + } drop(child.session); let _ = child.exit_rx.await; if let Some(daemon) = daemon { diff --git a/codex-rs/core/src/lib.rs b/codex-rs/core/src/lib.rs index f779002f38..fc2fd9ea52 100644 --- a/codex-rs/core/src/lib.rs +++ b/codex-rs/core/src/lib.rs @@ -216,6 +216,7 @@ pub use thread_manager::ThreadRegistrationReceipt; pub use thread_manager::ThreadShutdownReport; pub use thread_manager::build_models_manager; pub use thread_manager::build_startup_models; +pub use thread_manager::build_startup_models_with_runtime_auth; pub use thread_manager::local_agent_graph_store_from_state_db; pub use thread_manager::passthrough_image_store; pub use thread_store_builder::ThreadStoreBuilder; diff --git a/codex-rs/core/src/statsig.rs b/codex-rs/core/src/statsig.rs index 01db7e2e19..430c9382ed 100644 --- a/codex-rs/core/src/statsig.rs +++ b/codex-rs/core/src/statsig.rs @@ -14,6 +14,7 @@ use codex_statsig::StatsigIdentity; pub use codex_statsig::StatsigRuntime; use crate::config::Config; +use crate::runtime_auth::RuntimeAuthSlot; /// Harness launch surfaces sent to Statsig as targeting values. pub enum StatsigSurface { @@ -30,6 +31,7 @@ pub enum StatsigSurface { pub enum HostIdentity { ServingAccount { auth: Arc, + runtime_auth_slot: Option, default_analytics_enabled: bool, }, LocalAccount, @@ -64,9 +66,10 @@ impl Config { let stable_id = crate::resolve_installation_id(&self.codex_home) .await .ok()?; - let account_identity = |auth: Arc| { + let account_identity = |auth: Arc, slot: &RuntimeAuthSlot| { StatsigIdentity::Account(ProviderAuthContext { - runtime_auth: crate::build_runtime_auth_manager( + runtime_auth: crate::runtime_auth::runtime_for_config( + slot, self, Arc::clone(&auth), &stable_id, @@ -78,8 +81,12 @@ impl Config { let (identity, default_analytics_enabled) = match identity { HostIdentity::ServingAccount { auth, + runtime_auth_slot, default_analytics_enabled, - } => (account_identity(auth), Some(default_analytics_enabled)), + } => ( + account_identity(auth, &runtime_auth_slot.unwrap_or_default()), + Some(default_analytics_enabled), + ), HostIdentity::LocalAccount => { // Workload identity requires a token exchange, even when the auth modes are filtered. if !is_workload_identity_selected() @@ -96,7 +103,7 @@ impl Config { && auth.auth_cached().is_some() { ( - account_identity(auth), + account_identity(auth, &RuntimeAuthSlot::default()), (!matches!(surface, StatsigSurface::ExecServer)).then_some(true), ) } else { diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index f688002c4e..e313b621c6 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -645,6 +645,7 @@ pub(crate) struct ThreadManagerState { pub use model_catalog::StartupModels; pub use model_catalog::build_models_manager; pub use model_catalog::build_startup_models; +pub use model_catalog::build_startup_models_with_runtime_auth; /// Constructs the default image store that preserves inline images. pub fn passthrough_image_store() -> Arc { diff --git a/codex-rs/core/src/thread_manager/model_catalog.rs b/codex-rs/core/src/thread_manager/model_catalog.rs index 22ab881802..2c5b5956c5 100644 --- a/codex-rs/core/src/thread_manager/model_catalog.rs +++ b/codex-rs/core/src/thread_manager/model_catalog.rs @@ -39,7 +39,23 @@ pub fn build_startup_models( installation_id: String, factory: Arc, ) -> StartupModels { - let runtime_auth = RuntimeAuthSlot::default(); + build_startup_models_with_runtime_auth( + config, + credentials, + installation_id, + factory, + RuntimeAuthSlot::default(), + ) +} + +/// Share the host's runtime registration with clients that start before the model catalog. +pub fn build_startup_models_with_runtime_auth( + config: &Config, + credentials: Arc, + installation_id: String, + factory: Arc, + runtime_auth: RuntimeAuthSlot, +) -> StartupModels { let runtime = runtime_for_config( &runtime_auth, config,