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
This commit is contained in:
iceweasel-oai
2026-12-01 23:12:29 +00:00
committed by copyberry
parent 47e1cf4fe4
commit 80d0a67185
8 changed files with 64 additions and 25 deletions
+6 -5
View File
@@ -529,6 +529,11 @@ async fn start_uninitialized(mut args: InProcessStartArgs) -> IoResult<InProcess
.await?;
let channel_capacity = args.channel_capacity.max(1);
let installation_id = resolve_installation_id(&args.config.codex_home).await?;
let runtime_auth_slot = args
.config
.features
.enabled(codex_features::Feature::RuntimeAgentInstances)
.then(codex_runtime_agent_instance::RuntimeAuthSlot::default);
let statsig = args
.config
.start_statsig(
@@ -539,6 +544,7 @@ async fn start_uninitialized(mut args: InProcessStartArgs) -> IoResult<InProcess
},
codex_core::statsig::HostIdentity::ServingAccount {
auth: Arc::clone(&auth_manager),
runtime_auth_slot: runtime_auth_slot.clone(),
default_analytics_enabled: true,
},
)
@@ -550,11 +556,6 @@ async fn start_uninitialized(mut args: InProcessStartArgs) -> IoResult<InProcess
let runtime_handle = tokio::spawn(async move {
let (outgoing_tx, outgoing_rx) = mpsc::channel::<OutgoingEnvelope>(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(),
+1
View File
@@ -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,
},
)
+2 -5
View File
@@ -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,
+25 -10
View File
@@ -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 {
+1
View File
@@ -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;
+11 -4
View File
@@ -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<AuthManager>,
runtime_auth_slot: Option<RuntimeAuthSlot>,
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<AuthManager>| {
let account_identity = |auth: Arc<AuthManager>, 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 {
+1
View File
@@ -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<dyn AttachmentStore> {
@@ -39,7 +39,23 @@ pub fn build_startup_models(
installation_id: String,
factory: Arc<dyn ModelCatalogFactory>,
) -> 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<AuthManager>,
installation_id: String,
factory: Arc<dyn ModelCatalogFactory>,
runtime_auth: RuntimeAuthSlot,
) -> StartupModels {
let runtime = runtime_for_config(
&runtime_auth,
config,